Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use existing k8s cluster instead of minikube #442

Merged
merged 7 commits into from
Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package externalclusterprovisioner

import (
"io/ioutil"
"os"
"fmt"
)

// Represents an actual external cluster being used, should not be able to
// actually delete or create, but can point to actual kubeconfig file.
type externalCluster struct {
kubeconfigPath string
kubeconfigFile string
}


func NewExternalCluster(kubeconfigPath string) (*externalCluster, error) {
if _, err := os.Stat(kubeconfigPath); os.IsNotExist(err) {
return nil, fmt.Errorf("file at %s does not exist", kubeconfigPath)
}

return &externalCluster{kubeconfigPath:kubeconfigPath}, nil
}


func (e *externalCluster) Create() error {
// noop
return nil
}

func (e *externalCluster) Delete() error {
// noop
return nil
}

func (e *externalCluster) GetKubeconfig() (string, error) {

if e.kubeconfigFile == "" {
b, err := ioutil.ReadFile(e.kubeconfigPath)
if err != nil {
return "", err
}

e.kubeconfigFile = string(b)
}


return e.kubeconfigFile, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package externalclusterprovisioner

import (
"testing"
"os"
"io/ioutil"
)

func TestGetKubeconfig(t *testing.T) {
const contents = "dfserfafaew"
f, err := createTempFile(contents)
if err != nil {
t.Fatal("Unable to create test file.")
}
defer os.Remove(f)

t.Run("invalid path given", func(t *testing.T){
_, err = NewExternalCluster("")
if err == nil {
t.Fatal("Should not be able create External Cluster Provisioner.")
}
})

t.Run("file exists", func(t *testing.T) {

ec, err := NewExternalCluster(f)
if err != nil {
t.Fatal("Should be able create External Cluster Provisioner.")
}

c, err := ec.GetKubeconfig()
if err != nil {
t.Fatalf("Unexpected err. Got: %v", err)
return
}
if c != contents {
t.Fatalf("Unexpected contents. Got: %v, Want: %v", c, contents)
}
})
}

func createTempFile(contents string) (string, error) {
f, err := ioutil.TempFile("", "")
if err != nil {
return "", err
}
defer f.Close()
f.WriteString(contents)
return f.Name(), nil
}
33 changes: 23 additions & 10 deletions clusterctl/cmd/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/cluster-api/clusterctl/clusterdeployer"
"sigs.k8s.io/cluster-api/clusterctl/clusterdeployer/minikube"
"sigs.k8s.io/cluster-api/clusterctl/clusterdeployer/externalclusterprovisioner"
clustercommon "sigs.k8s.io/cluster-api/pkg/apis/cluster/common"
clusterv1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1"
"sigs.k8s.io/cluster-api/pkg/util"
)

type CreateOptions struct {
Cluster string
Machine string
ProviderComponents string
AddonComponents string
CleanupExternalCluster bool
VmDriver string
Provider string
KubeconfigOutput string
Cluster string
Machine string
ProviderComponents string
AddonComponents string
CleanupExternalCluster bool
VmDriver string
Provider string
KubeconfigOutput string
ExistingClusterKubeconfigPath string
}

var co = &CreateOptions{}
Expand Down Expand Up @@ -73,7 +75,17 @@ func RunCreate(co *CreateOptions) error {
return err
}

mini := minikube.New(co.VmDriver)
var exernalProvider clusterdeployer.ClusterProvisioner
if co.ExistingClusterKubeconfigPath != "" {
exernalProvider, err = externalclusterprovisioner.NewExternalCluster(co.ExistingClusterKubeconfigPath)
if err != nil {
return err
}
} else {
exernalProvider = minikube.New(co.VmDriver)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo, should be externalProvider


}

pd, err := getProvider(co.Provider)
if err != nil {
return err
Expand All @@ -91,7 +103,7 @@ func RunCreate(co *CreateOptions) error {
}
pcsFactory := clusterdeployer.NewProviderComponentsStoreFactory()
d := clusterdeployer.New(
mini,
exernalProvider,
clusterdeployer.NewClientFactory(),
string(pc),
string(ac),
Expand All @@ -112,6 +124,7 @@ func init() {
createClusterCmd.Flags().BoolVarP(&co.CleanupExternalCluster, "cleanup-external-cluster", "", true, "Whether to cleanup the external cluster after bootstrap")
createClusterCmd.Flags().StringVarP(&co.VmDriver, "vm-driver", "", "", "Which vm driver to use for minikube")
createClusterCmd.Flags().StringVarP(&co.KubeconfigOutput, "kubeconfig-out", "", "kubeconfig", "Where to output the kubeconfig for the provisioned cluster")
createClusterCmd.Flags().StringVarP(&co.ExistingClusterKubeconfigPath, "existing-cluster-kubeconfig", "k", "", "path to an existing cluster's kubeconfig (intead of using minikube)")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can you use uppercase for "path" to match the other flags values?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit of a confusing concept for the external user, but it's unclear from the flag name what the existing cluster will be used for. Maybe it could be called "existing-bootstrap-cluster-kubeconfig" or change the explanation text to "path to an existing cluster's kubeconfig for bootstrapping (intead of using minikube)"


createCmd.AddCommand(createClusterCmd)
}
Expand Down
19 changes: 10 additions & 9 deletions clusterctl/testdata/create-cluster-no-args-invalid-flag.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ Usage:
clusterctl create cluster [flags]

Flags:
-a, --addon-components string A yaml file containing cluster addons to apply to the internal cluster
--cleanup-external-cluster Whether to cleanup the external cluster after bootstrap (default true)
-c, --cluster string A yaml file containing cluster object definition
-h, --help help for cluster
--kubeconfig-out string Where to output the kubeconfig for the provisioned cluster (default "kubeconfig")
-m, --machines string A yaml file containing machine object definition(s)
--provider string Which provider deployment logic to use (google/vsphere/azure)
-p, --provider-components string A yaml file containing cluster api provider controllers and supporting objects
--vm-driver string Which vm driver to use for minikube
-a, --addon-components string A yaml file containing cluster addons to apply to the internal cluster
--cleanup-external-cluster Whether to cleanup the external cluster after bootstrap (default true)
-c, --cluster string A yaml file containing cluster object definition
-k, --existing-cluster-kubeconfig string path to an existing cluster's kubeconfig (intead of using minikube)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to use the -k? Since it's not a required flag, it seems like we could force people to use the full "existing-cluster-kubeconfig" string

-h, --help help for cluster
--kubeconfig-out string Where to output the kubeconfig for the provisioned cluster (default "kubeconfig")
-m, --machines string A yaml file containing machine object definition(s)
--provider string Which provider deployment logic to use (google/vsphere/azure)
-p, --provider-components string A yaml file containing cluster api provider controllers and supporting objects
--vm-driver string Which vm driver to use for minikube

Global Flags:
--alsologtostderr log to standard error as well as files
Expand Down
19 changes: 10 additions & 9 deletions clusterctl/testdata/create-cluster-no-args.golden
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@ Usage:
clusterctl create cluster [flags]

Flags:
-a, --addon-components string A yaml file containing cluster addons to apply to the internal cluster
--cleanup-external-cluster Whether to cleanup the external cluster after bootstrap (default true)
-c, --cluster string A yaml file containing cluster object definition
-h, --help help for cluster
--kubeconfig-out string Where to output the kubeconfig for the provisioned cluster (default "kubeconfig")
-m, --machines string A yaml file containing machine object definition(s)
--provider string Which provider deployment logic to use (google/vsphere/azure)
-p, --provider-components string A yaml file containing cluster api provider controllers and supporting objects
--vm-driver string Which vm driver to use for minikube
-a, --addon-components string A yaml file containing cluster addons to apply to the internal cluster
--cleanup-external-cluster Whether to cleanup the external cluster after bootstrap (default true)
-c, --cluster string A yaml file containing cluster object definition
-k, --existing-cluster-kubeconfig string path to an existing cluster's kubeconfig (intead of using minikube)
-h, --help help for cluster
--kubeconfig-out string Where to output the kubeconfig for the provisioned cluster (default "kubeconfig")
-m, --machines string A yaml file containing machine object definition(s)
--provider string Which provider deployment logic to use (google/vsphere/azure)
-p, --provider-components string A yaml file containing cluster api provider controllers and supporting objects
--vm-driver string Which vm driver to use for minikube

Global Flags:
--alsologtostderr log to standard error as well as files
Expand Down