From 5b637126d7ef21acb7bbbf8b19b878657bde51d8 Mon Sep 17 00:00:00 2001 From: iwilltry42 Date: Fri, 24 Apr 2020 14:02:38 +0200 Subject: [PATCH] createCluster: add --update-kubeconfig flag - enables wait-for-master - calls GetAndWriteKubeConfig after successful cluster creation to update the default kubeconfig with the new cluster's details - does NOT automatically switch the current-context - outputs a different line saying, that you can switch context now --- cmd/create/createCluster.go | 30 ++++++++++++++++++++++++------ pkg/cluster/kubeconfig.go | 4 ++-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/cmd/create/createCluster.go b/cmd/create/createCluster.go index 2f047c01..8abd7303 100644 --- a/cmd/create/createCluster.go +++ b/cmd/create/createCluster.go @@ -51,6 +51,7 @@ Every cluster will consist of at least 2 containers: func NewCmdCreateCluster() *cobra.Command { createClusterOpts := &k3d.CreateClusterOpts{} + var updateKubeconfig bool // create new command cmd := &cobra.Command{ @@ -68,6 +69,10 @@ func NewCmdCreateCluster() *cobra.Command { } // create cluster + if updateKubeconfig { + log.Debugln("'--update-kubeconfig set: enabling wait-for-master") + cluster.CreateClusterOpts.WaitForMaster = true + } if err := k3dCluster.CreateCluster(cmd.Context(), cluster, runtimes.SelectedRuntime); err != nil { // rollback if creation failed log.Errorln(err) @@ -78,16 +83,28 @@ func NewCmdCreateCluster() *cobra.Command { } log.Fatalln("Cluster creation FAILED, all changes have been rolled back!") } + log.Infof("Cluster '%s' created successfully!", cluster.Name) + + if updateKubeconfig { + log.Debugf("Updating default kubeconfig with a new context for cluster %s", cluster.Name) + if err := k3dCluster.GetAndWriteKubeConfig(runtimes.SelectedRuntime, cluster, "", &k3dCluster.WriteKubeConfigOptions{UpdateExisting: true, OverwriteExisting: false, UpdateCurrentContext: false}); err != nil { + log.Fatalln(err) + } + } // print information on how to use the cluster with kubectl - log.Infof("Cluster '%s' created successfully. You can now use it like this:", cluster.Name) - if runtime.GOOS == "windows" { - log.Debugf("GOOS is %s", runtime.GOOS) - fmt.Printf("$env:KUBECONFIG=(%s get kubeconfig %s)\n", os.Args[0], cluster.Name) + log.Infoln("You can now use it like this:") + if updateKubeconfig { + fmt.Printf("kubectl config use-context %s\n", fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, cluster.Name)) } else { - fmt.Printf("export KUBECONFIG=$(%s get kubeconfig %s)\n", os.Args[0], cluster.Name) + if runtime.GOOS == "windows" { + log.Debugf("GOOS is %s", runtime.GOOS) + fmt.Printf("$env:KUBECONFIG=(%s get kubeconfig %s)\n", os.Args[0], cluster.Name) + } else { + fmt.Printf("export KUBECONFIG=$(%s get kubeconfig %s)\n", os.Args[0], cluster.Name) + } + fmt.Println("kubectl cluster-info") } - fmt.Println("kubectl cluster-info") }, } @@ -104,6 +121,7 @@ func NewCmdCreateCluster() *cobra.Command { cmd.Flags().StringArrayP("port", "p", nil, "Map ports from the node containers to the host (Format: `[HOST:][HOSTPORT:]CONTAINERPORT[/PROTOCOL][@NODEFILTER]`)\n - Example: `k3d create -w 2 -p 8080:80@worker[0] -p 8081@worker[1]`") cmd.Flags().BoolVar(&createClusterOpts.WaitForMaster, "wait", false, "Wait for for the master(s) to be ready before returning. Use `--timeout DURATION` to not wait forever.") cmd.Flags().DurationVar(&createClusterOpts.Timeout, "timeout", 0*time.Second, "Rollback changes if cluster couldn't be created in specified duration.") + cmd.Flags().BoolVar(&updateKubeconfig, "update-kubeconfig", false, "Directly update the default kubeconfig with the new cluster's context") /* Image Importing */ cmd.Flags().BoolVar(&createClusterOpts.DisableImageVolume, "no-image-volume", false, "Disable the creation of a volume for importing images") diff --git a/pkg/cluster/kubeconfig.go b/pkg/cluster/kubeconfig.go index 29c61711..00699201 100644 --- a/pkg/cluster/kubeconfig.go +++ b/pkg/cluster/kubeconfig.go @@ -168,8 +168,8 @@ func GetKubeconfig(runtime runtimes.Runtime, cluster *k3d.Cluster) (*clientcmdap kc.Clusters[newClusterName] = kc.Clusters["default"] delete(kc.Clusters, "default") - // rename context from default to admin@clustername - newContextName := fmt.Sprintf("admin@%s-%s", k3d.DefaultObjectNamePrefix, cluster.Name) + // rename context from default to clustername + newContextName := fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, cluster.Name) kc.Contexts[newContextName] = kc.Contexts["default"] delete(kc.Contexts, "default")