From 638b21f4a59194af5b4dcf93a33d038d0310ce3c Mon Sep 17 00:00:00 2001 From: Kjeld Schouten Date: Mon, 4 Nov 2024 15:34:01 +0100 Subject: [PATCH] feat(clustertool): move some functions away from talhelper --- clustertool/cmd/apply.go | 2 +- clustertool/cmd/fluxbootstrap.go | 7 ---- clustertool/cmd/upgrade.go | 2 +- clustertool/pkg/gencmd/apply.go | 51 +++++++++------------------- clustertool/pkg/gencmd/bootstrap.go | 30 ++-------------- clustertool/pkg/gencmd/kubeconfig.go | 9 +++-- clustertool/pkg/gencmd/reset.go | 49 +++++++++----------------- clustertool/pkg/gencmd/upgrade.go | 1 + clustertool/pkg/talassist/ips.go | 2 +- clustertool/pkg/talassist/vars.go | 2 +- 10 files changed, 46 insertions(+), 109 deletions(-) diff --git a/clustertool/cmd/apply.go b/clustertool/cmd/apply.go index 108b2166fb1..8e0584e214f 100644 --- a/clustertool/cmd/apply.go +++ b/clustertool/cmd/apply.go @@ -105,7 +105,7 @@ func RunApply(kubeconfig bool, node string, extraArgs []string) { gencmd.ExecCmds(taloscmds, true) if kubeconfig { - kubeconfigcmds := gencmd.GenKubeConfig(helper.TalEnv["VIP_IP"]) + kubeconfigcmds := gencmd.GenKubeConfig(helper.TalEnv["VIP_IP"], extraArgs) gencmd.ExecCmd(kubeconfigcmds) } diff --git a/clustertool/cmd/fluxbootstrap.go b/clustertool/cmd/fluxbootstrap.go index 7dcc54aae9a..259b7d1b83a 100644 --- a/clustertool/cmd/fluxbootstrap.go +++ b/clustertool/cmd/fluxbootstrap.go @@ -4,11 +4,9 @@ import ( "context" "strings" - talhelperCfg "github.com/budimanjojo/talhelper/v3/pkg/config" "github.com/rs/zerolog/log" "github.com/spf13/cobra" "github.com/truecharts/public/clustertool/pkg/fluxhandler" - "github.com/truecharts/public/clustertool/pkg/helper" "github.com/truecharts/public/clustertool/pkg/initfiles" "github.com/truecharts/public/clustertool/pkg/sops" ) @@ -29,11 +27,6 @@ var fluxbootstrap = &cobra.Command{ log.Info().Msgf("Error decrypting files: %v\n", err) } initfiles.LoadTalEnv(false) - _, err := talhelperCfg.LoadAndValidateFromFile(helper.TalConfigFile, []string{helper.ClusterEnvFile}, false) - if err != nil { - log.Fatal().Err(err).Msg("failed to parse talconfig or talenv file: %s") - } - fluxhandler.FluxBootstrap(ctx) }, } diff --git a/clustertool/cmd/upgrade.go b/clustertool/cmd/upgrade.go index cf899cd477d..a8798022a28 100644 --- a/clustertool/cmd/upgrade.go +++ b/clustertool/cmd/upgrade.go @@ -51,7 +51,7 @@ var upgrade = &cobra.Command{ gencmd.ExecCmd(kubeUpgradeCmd) log.Info().Msg("(re)Loading KubeConfig)") - kubeconfigcmds := gencmd.GenKubeConfig(helper.TalEnv["VIP_IP"]) + kubeconfigcmds := gencmd.GenKubeConfig(helper.TalEnv["VIP_IP"], extraArgs) gencmd.ExecCmd(kubeconfigcmds) }, diff --git a/clustertool/pkg/gencmd/apply.go b/clustertool/pkg/gencmd/apply.go index 88e766c744e..36b68d74b20 100644 --- a/clustertool/pkg/gencmd/apply.go +++ b/clustertool/pkg/gencmd/apply.go @@ -1,49 +1,30 @@ package gencmd import ( - "io" - "os" - "strings" - - "github.com/rs/zerolog/log" - - talhelperCfg "github.com/budimanjojo/talhelper/v3/pkg/config" - "github.com/budimanjojo/talhelper/v3/pkg/generate" "github.com/truecharts/public/clustertool/embed" "github.com/truecharts/public/clustertool/pkg/helper" - "github.com/truecharts/public/clustertool/pkg/initfiles" + "github.com/truecharts/public/clustertool/pkg/talassist" ) func GenApply(node string, extraFlags []string) []string { - initfiles.LoadTalEnv(false) - cfg, err := talhelperCfg.LoadAndValidateFromFile(helper.TalConfigFile, []string{helper.ClusterEnvFile}, false) - if err != nil { - log.Fatal().Err(err).Msg("failed to parse talconfig or talenv file: %s") - } - applyStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w + commands := []string{} + //extraFlags = append(extraFlags, "--preserve") - err = generate.GenerateApplyCommand(cfg, helper.TalosGenerated, node, extraFlags) - - w.Close() - out, _ := io.ReadAll(r) - os.Stdout = applyStdout - - sliceOut := strings.Split(string(out), ";\n") talosPath := embed.GetTalosExec() - var slice []string - for _, str := range sliceOut { - if str != "" { - str = strings.ReplaceAll(str, "talosctl", talosPath) - slice = append(slice, str) + if node == "" { + for _, nodeRef := range talassist.IpAddresses { + talassist.LoadNodeIPs() + // TODO add extraFlags + + // TODO: add images Refs + // TODO: add schematic + cmd := talosPath + " apply --talosconfig " + helper.TalosConfigFile + " -n " + nodeRef + " " + commands = append(commands, cmd) } - + } else { + cmd := talosPath + " apply --talosconfig " + helper.TalosConfigFile + " -n " + node + " " + commands = append(commands, cmd) } - - if err != nil { - log.Fatal().Err(err).Msg("failed to generate talosctl apply command: %s") - } - return slice + return commands } diff --git a/clustertool/pkg/gencmd/bootstrap.go b/clustertool/pkg/gencmd/bootstrap.go index f9cc3d2ade4..a13fe0478ca 100644 --- a/clustertool/pkg/gencmd/bootstrap.go +++ b/clustertool/pkg/gencmd/bootstrap.go @@ -2,21 +2,18 @@ package gencmd import ( "context" - "io" "os" "path/filepath" - "strings" "github.com/rs/zerolog/log" - talhelperCfg "github.com/budimanjojo/talhelper/v3/pkg/config" - "github.com/budimanjojo/talhelper/v3/pkg/generate" "github.com/truecharts/public/clustertool/embed" "github.com/truecharts/public/clustertool/pkg/fluxhandler" "github.com/truecharts/public/clustertool/pkg/helper" "github.com/truecharts/public/clustertool/pkg/kubectlcmds" "github.com/truecharts/public/clustertool/pkg/nodestatus" "github.com/truecharts/public/clustertool/pkg/sops" + "github.com/truecharts/public/clustertool/pkg/talassist" ) var HelmRepos map[string]*fluxhandler.HelmRepo @@ -28,29 +25,8 @@ var manifestPaths = []string{ } func GenBootstrap(node string, extraFlags []string) string { - cfg, err := talhelperCfg.LoadAndValidateFromFile(helper.TalConfigFile, []string{helper.ClusterEnvFile}, false) - if err != nil { - log.Fatal().Err(err).Msg("failed to parse talconfig or talenv file: %s") - } - - applyStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - - err = generate.GenerateBootstrapCommand(cfg, helper.TalosGenerated, node, extraFlags) - - w.Close() - out, _ := io.ReadAll(r) - os.Stdout = applyStdout - talosPath := embed.GetTalosExec() - strout := strings.ReplaceAll(string(out), "talosctl", talosPath) - strout = strings.ReplaceAll(strout, "\n", "") - strout = strings.ReplaceAll(strout, ";", "") - - if err != nil { - log.Fatal().Err(err).Msg("failed to generate talosctl bootstrap command: %s") - } + strout := talosPath + "bootstrap --talosconfig " + helper.TalosConfigFile + " -n " + talassist.IpAddresses[0] return strout } @@ -83,7 +59,7 @@ func RunBootstrap(args []string) { log.Info().Msgf("Bootstrap: Configuring kubectl for VIP: %v", helper.TalEnv["VIP_IP"]) // Ensure kubeconfig is loaded - kubeconfigcmds := GenKubeConfig(helper.TalEnv["VIP_IP"]) + kubeconfigcmds := GenKubeConfig(helper.TalEnv["VIP_IP"], extraArgs) ExecCmd(kubeconfigcmds) // Desired pod names diff --git a/clustertool/pkg/gencmd/kubeconfig.go b/clustertool/pkg/gencmd/kubeconfig.go index 9c48b11e75a..862f60a3ea1 100644 --- a/clustertool/pkg/gencmd/kubeconfig.go +++ b/clustertool/pkg/gencmd/kubeconfig.go @@ -5,8 +5,11 @@ import ( "github.com/truecharts/public/clustertool/pkg/helper" ) -func GenKubeConfig(node string) string { +func GenKubeConfig(node string, extraFlags []string) string { + + //extraFlags = append(extraFlags, "--preserve") talosPath := embed.GetTalosExec() - strout := talosPath + " kubeconfig --talosconfig " + helper.TalosConfigFile + " -n " + node + " --force" - return strout + cmd := talosPath + " kubeconfig --talosconfig " + helper.TalosConfigFile + " -n " + node + " " + + return cmd } diff --git a/clustertool/pkg/gencmd/reset.go b/clustertool/pkg/gencmd/reset.go index b17cca40c52..49fce79839f 100644 --- a/clustertool/pkg/gencmd/reset.go +++ b/clustertool/pkg/gencmd/reset.go @@ -1,47 +1,30 @@ package gencmd import ( - "io" - "os" - "strings" - - "github.com/rs/zerolog/log" - - talhelperCfg "github.com/budimanjojo/talhelper/v3/pkg/config" - "github.com/budimanjojo/talhelper/v3/pkg/generate" "github.com/truecharts/public/clustertool/embed" "github.com/truecharts/public/clustertool/pkg/helper" + "github.com/truecharts/public/clustertool/pkg/talassist" ) func GenReset(node string, extraFlags []string) []string { - cfg, err := talhelperCfg.LoadAndValidateFromFile(helper.TalConfigFile, []string{helper.ClusterEnvFile}, false) - if err != nil { - log.Fatal().Err(err).Msgf("failed to parse talconfig or talenv file: %s", err) - } - resetStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w + commands := []string{} + //extraFlags = append(extraFlags, "--preserve") - err = generate.GenerateResetCommand(cfg, helper.TalosGenerated, node, extraFlags) - - w.Close() - out, _ := io.ReadAll(r) - os.Stdout = resetStdout - - sliceOut := strings.Split(string(out), ";\n") talosPath := embed.GetTalosExec() - var slice []string - for _, str := range sliceOut { - if str != "" { - str = strings.ReplaceAll(str, "talosctl", talosPath) - slice = append(slice, str) + if node == "" { + for _, nodeRef := range talassist.IpAddresses { + talassist.LoadNodeIPs() + // TODO add extraFlags + + // TODO: add images Refs + // TODO: add schematic + cmd := talosPath + " reset --talosconfig " + helper.TalosConfigFile + " -n " + nodeRef + " " + commands = append(commands, cmd) } - + } else { + cmd := talosPath + " reset --talosconfig " + helper.TalosConfigFile + " -n " + node + " " + commands = append(commands, cmd) } - - if err != nil { - log.Fatal().Err(err).Msgf("failed to generate talosctl reset command: %s", err) - } - return slice + return commands } diff --git a/clustertool/pkg/gencmd/upgrade.go b/clustertool/pkg/gencmd/upgrade.go index bd8b631b228..5891c82e0d9 100644 --- a/clustertool/pkg/gencmd/upgrade.go +++ b/clustertool/pkg/gencmd/upgrade.go @@ -13,6 +13,7 @@ import ( "github.com/truecharts/public/clustertool/pkg/helper" ) +// TODO: remove talhelper dependency for cmd creation func GenUpgrade(node string, extraFlags []string) []string { cfg, err := talhelperCfg.LoadAndValidateFromFile(helper.TalConfigFile, []string{helper.ClusterEnvFile}, false) if err != nil { diff --git a/clustertool/pkg/talassist/ips.go b/clustertool/pkg/talassist/ips.go index 019dfd531d8..0d4827c86e6 100644 --- a/clustertool/pkg/talassist/ips.go +++ b/clustertool/pkg/talassist/ips.go @@ -30,7 +30,7 @@ func LoadNodeIPs() error { // Extract the IP addresses into a list for _, node := range config.Nodes { - ipAddresses = append(ipAddresses, node.IPAddress) + IpAddresses = append(IpAddresses, node.IPAddress) } return nil diff --git a/clustertool/pkg/talassist/vars.go b/clustertool/pkg/talassist/vars.go index f277e3fd3fb..ead49deef54 100644 --- a/clustertool/pkg/talassist/vars.go +++ b/clustertool/pkg/talassist/vars.go @@ -1,5 +1,5 @@ package talassist var ( - ipAddresses = []string{} + IpAddresses = []string{} )