diff --git a/cmd/create/createCluster.go b/cmd/create/createCluster.go index 444f7910..a0a62904 100644 --- a/cmd/create/createCluster.go +++ b/cmd/create/createCluster.go @@ -51,7 +51,7 @@ Every cluster will consist of at least 2 containers: func NewCmdCreateCluster() *cobra.Command { createClusterOpts := &k3d.CreateClusterOpts{} - var updateKubeconfig bool + var updateKubeconfig, updateCurrentContext bool // create new command cmd := &cobra.Command{ @@ -69,7 +69,7 @@ func NewCmdCreateCluster() *cobra.Command { } // create cluster - if updateKubeconfig { + if updateKubeconfig || updateCurrentContext { log.Debugln("'--update-kubeconfig set: enabling wait-for-master") cluster.CreateClusterOpts.WaitForMaster = true } @@ -85,25 +85,25 @@ func NewCmdCreateCluster() *cobra.Command { } log.Infof("Cluster '%s' created successfully!", cluster.Name) - if updateKubeconfig { + if updateKubeconfig || updateCurrentContext { log.Debugf("Updating default kubeconfig with a new context for cluster %s", cluster.Name) - if _, err := k3dCluster.GetAndWriteKubeConfig(cmd.Context(), runtimes.SelectedRuntime, cluster, "", &k3dCluster.WriteKubeConfigOptions{UpdateExisting: true, OverwriteExisting: false, UpdateCurrentContext: false}); err != nil { + if _, err := k3dCluster.GetAndWriteKubeConfig(cmd.Context(), runtimes.SelectedRuntime, cluster, "", &k3dCluster.WriteKubeConfigOptions{UpdateExisting: true, OverwriteExisting: false, UpdateCurrentContext: updateCurrentContext}); err != nil { log.Fatalln(err) } } // print information on how to use the cluster with kubectl log.Infoln("You can now use it like this:") - if updateKubeconfig { + if updateKubeconfig && !updateCurrentContext { fmt.Printf("kubectl config use-context %s\n", fmt.Sprintf("%s-%s", k3d.DefaultObjectNamePrefix, cluster.Name)) - } else { + } else if !updateCurrentContext { if runtime.GOOS == "windows" { 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") }, } @@ -121,6 +121,7 @@ func NewCmdCreateCluster() *cobra.Command { cmd.Flags().BoolVar(&createClusterOpts.WaitForMaster, "wait", false, "Wait 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") + cmd.Flags().BoolVar(&updateCurrentContext, "switch", false, "Directly switch the default kubeconfig's current-context to the new cluster's context (implies --update-kubeconfig)") cmd.Flags().BoolVar(&createClusterOpts.DisableLoadBalancer, "no-lb", false, "Disable the creation of a LoadBalancer in front of the master nodes") /* Image Importing */ diff --git a/docs/usage/commands.md b/docs/usage/commands.md index 3fc79054..12e8e430 100644 --- a/docs/usage/commands.md +++ b/docs/usage/commands.md @@ -17,6 +17,7 @@ k3d --secret # specify a cluster secret (default: auto-generated) --timeout # specify a timeout, after which the cluster creation will be interrupted and changes rolled back --update-kubeconfig # enable the automated update of the default kubeconfig with the details of the newly created cluster (also sets '--wait=true') + --switch # (implies --update-kubeconfig) automatically sets the current-context of your default kubeconfig to the new cluster's context -v, --volume # specify additional bind-mounts --wait # enable waiting for all master nodes to be ready before returning -w, --workers # specify how many worker nodes you want to create