Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var RootCmd = &cobra.Command{ Use: "kw_kind [<name>]", DisableAutoGenTag: true, Args: cobra.RangeArgs(0, 1), Short: "Switch to a local kind cluster", Long: `Switch to a local kind cluster. This command sets the kubeconfig to the output of 'kind get kubeconfg --name <name>'. No-op if the current kubeconfig is already set to the kubeconfig of the specified kind cluster. The '--reload' flag causes the kubeconfig to be loaded even if it is the currently targeted cluster. If the flag is set, the cluster name can be omitted to reload the current cluster's kubeconfig. This results in an error if the currently targeted kubeconfig was not set via this command. If neither a cluster name, nor the '--reload' flag is set, the user will be prompted to select one of the known kind clusters via a fuzzy selector.`, Run: func(cmd *cobra.Command, args []string) { clusterName := "" if len(args) > 0 { clusterName = args[0] } debug.Debug("Loading kubeswitcher context from environment") con, err := libcontext.NewContextFromEnv() if err != nil { libutils.Fatal(1, "error creating kubeswitcher context from environment (this is a plugin, did you run it as standalone?): %w\n", err) } debug.Debug("Kubeswitcher context loaded:\n%s", con.String()) debug.Debug("Loading plugin configuration") cfg, err := config.LoadFromBytes([]byte(con.PluginConfig)) if err != nil { libutils.Fatal(1, "error loading plugin configuration: %w\n", err) } debug.Debug("Plugin configuration loaded:\n%s", cfg.String()) kState := &state.KindState{} ok, err := kState.Load(con) if err != nil { libutils.Fatal(1, "error loading plugin state: %w\n", err) } if ok { debug.Debug("Plugin state loaded:\n%s", kState.String()) if clusterName == "" && reload { clusterName = kState.ClusterName } else if clusterName == kState.ClusterName && !reload { debug.Debug("Cluster name matches current cluster name and --reload flag is not set. Writing notification and exiting.") if err := con.WriteNotificationMessage(kState.Notification()); err != nil { libutils.Fatal(1, "%w\n", err) } return } } else { debug.Debug("Unable to load plugin state from kubeswitcher (either not found or current state is from a different plugin)") if clusterName == "" && reload { libutils.Fatal(1, "Unable to reload kind cluster kubeconfig, because the current kubeconfig was not set via this subcommand.\nEither provide a kind cluster name or switch to a kind cluster first.\n") } } if clusterName == "" && !reload { kindArgs := []string{"get", "clusters"} bin := exec.Command(cfg.Binary, kindArgs...) if bin.Env == nil { bin.Env = []string{} } bin.Env = append(bin.Env, os.Environ()...) errBuffer := libutils.NewWriteBuffer() outBuffer := libutils.NewWriteBuffer() bin.Stderr = errBuffer bin.Stdout = outBuffer bin.Stdin = cmd.InOrStdin() debug.Debug("starting kind execution for prompt data") if err := bin.Run(); err != nil { _ = outBuffer.Flush(cmd.OutOrStdout()) _ = errBuffer.Flush(cmd.ErrOrStderr()) libutils.Fatal(1, "error running kind: %w\n", err) } debug.Debug("finished kind execution for prompt data") kindClusters := strings.Split(outBuffer.FlushToString(), "\n") _, clusterName, _ = selector.New[string](). WithPrompt("Select kind cluster: "). WithFatalOnAbort("No cluster selected."). WithFatalOnError("error selecting cluster: %w"). WithSortFunc(selector.Invert(strings.Compare)). From(kindClusters, selector.Identity). Select() } kindArgs := []string{"get", "kubeconfig", "--name", clusterName} bin := exec.Command(cfg.Binary, kindArgs...) if bin.Env == nil { bin.Env = []string{} } bin.Env = append(bin.Env, os.Environ()...) errBuffer := libutils.NewWriteBuffer() outBuffer := libutils.NewWriteBuffer() bin.Stderr = errBuffer bin.Stdout = outBuffer bin.Stdin = cmd.InOrStdin() debug.Debug("starting kind execution") if err := bin.Run(); err != nil { _ = outBuffer.Flush(cmd.OutOrStdout()) _ = errBuffer.Flush(cmd.ErrOrStderr()) libutils.Fatal(1, "error running kind: %w\n", err) } debug.Debug("finished kind execution") kcfgData := outBuffer.Data() kState.ClusterName = clusterName if err := con.WriteKubeconfig(kcfgData, kState.Notification()); err != nil { libutils.Fatal(1, "%w\n", err) } if err := con.WriteId(kState.Id(con.CurrentPluginName)); err != nil { libutils.Fatal(1, "%w\n", err) } if err := con.WritePluginState(kState); err != nil { libutils.Fatal(1, "%w\n", err) } }, }
Functions ¶
This section is empty.
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.