Skip to content

Commit

Permalink
Merge pull request #340 from budimanjojo/339
Browse files Browse the repository at this point in the history
feat(gencommand): add kubeconfig command
  • Loading branch information
budimanjojo authored Feb 12, 2024
2 parents 605d39d + 7862774 commit 4b732e9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
31 changes: 31 additions & 0 deletions cmd/gencommand_kubeconfig.go
Original file line number Diff line number Diff line change
@@ -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)
}
37 changes: 37 additions & 0 deletions pkg/generate/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit 4b732e9

Please sign in to comment.