From 7862774d71c1afa8bd72a2707e80e6eaae99b932 Mon Sep 17 00:00:00 2001 From: budimanjojo Date: Mon, 12 Feb 2024 23:15:05 +0700 Subject: [PATCH] feat(gencommand): add kubeconfig command Signed-off-by: budimanjojo --- cmd/gencommand_kubeconfig.go | 31 ++++++++++++++++++++++++++++++ pkg/generate/command.go | 37 ++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 cmd/gencommand_kubeconfig.go diff --git a/cmd/gencommand_kubeconfig.go b/cmd/gencommand_kubeconfig.go new file mode 100644 index 00000000..04c2064e --- /dev/null +++ b/cmd/gencommand_kubeconfig.go @@ -0,0 +1,31 @@ +package cmd + +import ( + "log" + + "github.com/spf13/cobra" + + "github.com/budimanjojo/talhelper/pkg/config" + "github.com/budimanjojo/talhelper/pkg/generate" +) + +var gencommandKubeconfigCmd = &cobra.Command{ + Use: "kubeconfig", + Short: "Generate talosctl kubeconfig commands.", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + cfg, err := config.LoadAndValidateFromFile(gencommandCfgFile, gencommandEnvFile) + if err != nil { + log.Fatalf("failed to parse config file: %s", err) + } + + err = generate.GenerateKubeconfigCommand(cfg, gencommandOutDir, gencommandNode, gencommandExtraFlags) + if err != nil { + log.Fatalf("failed to generate talosctl kubeconfig command: %s", err) + } + }, +} + +func init() { + gencommandCmd.AddCommand(gencommandKubeconfigCmd) +} diff --git a/pkg/generate/command.go b/pkg/generate/command.go index 4257ddd0..cab68912 100644 --- a/pkg/generate/command.go +++ b/pkg/generate/command.go @@ -160,6 +160,43 @@ func GenerateBootstrapCommand(cfg *config.TalhelperConfig, outDir string, node s } } +// GenerateKubeconfigCommand prints out `talosctl kubeconfig` command for selected node. +// `outDir` is directory where talosconfig is located. +// If `node` is empty string, it prints command for the first controlplane node found +// in `cfg.Nodes`. It returns error if `node` is not found or is not controlplane. +func GenerateKubeconfigCommand(cfg *config.TalhelperConfig, outDir string, node string, extraFlags []string) error { + var result string + for _, n := range cfg.Nodes { + isSelectedNode := ((node != "") && (node == n.IPAddress)) || ((node != "") && (node == n.Hostname)) + noNodeSelected := (node == "") + kubeconfigFlags := []string{ + "--talosconfig=" + outDir + "/talosconfig", + } + if noNodeSelected && n.ControlPlane { + kubeconfigFlags = append(kubeconfigFlags, extraFlags...) + kubeconfigFlags = append(kubeconfigFlags, "--nodes="+n.IPAddress) + result = fmt.Sprintf("talosctl kubeconfig %s;", strings.Join(kubeconfigFlags, " ")) + break + } + if isSelectedNode { + if !n.ControlPlane { + return fmt.Errorf("node with IP %s is not a controlplane node", n.IPAddress) + } + kubeconfigFlags = append(kubeconfigFlags, extraFlags...) + kubeconfigFlags = append(kubeconfigFlags, "--nodes="+n.IPAddress) + result = fmt.Sprintf("talosctl kubeconfig %s;", strings.Join(kubeconfigFlags, " ")) + break + } + } + + if result != "" { + fmt.Printf("%s\n", result) + return nil + } else { + return fmt.Errorf("node with IP or hostname %s not found", node) + } +} + // GenarateResetCommand prints out `talosctl reset` command for selected node. // `outDir` is directory where generated talosconfig and node manifest files are located. // If `node` is empty string, it prints commands for all nodes in `cfg.Nodes`.