-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Changes from 4 commits
c0e37ec
7939f1a
c9bf258
e2fca82
5f39296
8fab2a4
a8a0f86
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{} | ||
|
@@ -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) | ||
|
||
} | ||
|
||
pd, err := getProvider(co.Provider) | ||
if err != nil { | ||
return err | ||
|
@@ -91,7 +103,7 @@ func RunCreate(co *CreateOptions) error { | |
} | ||
pcsFactory := clusterdeployer.NewProviderComponentsStoreFactory() | ||
d := clusterdeployer.New( | ||
mini, | ||
exernalProvider, | ||
clusterdeployer.NewClientFactory(), | ||
string(pc), | ||
string(ac), | ||
|
@@ -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)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo, should be externalProvider