From 16c0e0c387a3b603cb25026ee28e6cb08e3394e9 Mon Sep 17 00:00:00 2001 From: Kjeld Schouten Date: Wed, 6 Nov 2024 01:17:12 +0100 Subject: [PATCH] feat(clustertool): move all talhelper references to talassist --- clustertool/cmd/talos_apply.go | 11 +- clustertool/cmd/talos_bootstrap.go | 12 +- clustertool/cmd/talos_health.go | 4 +- clustertool/cmd/talos_reset.go | 2 +- clustertool/cmd/talos_upgrade.go | 4 +- clustertool/pkg/gencmd/apply.go | 49 +++----- clustertool/pkg/gencmd/bootstrap.go | 22 ++-- clustertool/pkg/gencmd/execcmd.go | 4 +- clustertool/pkg/gencmd/genconfig.go | 107 +----------------- clustertool/pkg/gencmd/health.go | 12 -- clustertool/pkg/gencmd/kubeconfig.go | 15 --- clustertool/pkg/gencmd/{reset.go => plain.go} | 13 +-- clustertool/pkg/gencmd/upgrade.go | 8 +- clustertool/pkg/helper/envsubst.go | 1 - clustertool/pkg/initfiles/initfiles.go | 22 +--- clustertool/pkg/talassist/ips.go | 37 ------ clustertool/pkg/talassist/load.go | 76 +++++++++++++ clustertool/pkg/talassist/vars.go | 5 - 18 files changed, 137 insertions(+), 267 deletions(-) delete mode 100644 clustertool/pkg/gencmd/health.go delete mode 100644 clustertool/pkg/gencmd/kubeconfig.go rename clustertool/pkg/gencmd/{reset.go => plain.go} (54%) delete mode 100644 clustertool/pkg/talassist/ips.go create mode 100644 clustertool/pkg/talassist/load.go delete mode 100644 clustertool/pkg/talassist/vars.go diff --git a/clustertool/cmd/talos_apply.go b/clustertool/cmd/talos_apply.go index d84421fe0bfc..c511c47c3688 100644 --- a/clustertool/cmd/talos_apply.go +++ b/clustertool/cmd/talos_apply.go @@ -7,8 +7,10 @@ import ( "github.com/spf13/cobra" "github.com/truecharts/public/clustertool/pkg/gencmd" "github.com/truecharts/public/clustertool/pkg/helper" + "github.com/truecharts/public/clustertool/pkg/initfiles" "github.com/truecharts/public/clustertool/pkg/nodestatus" "github.com/truecharts/public/clustertool/pkg/sops" + "github.com/truecharts/public/clustertool/pkg/talassist" ) var applyLongHelp = strings.TrimSpace(` @@ -65,8 +67,9 @@ var apply = &cobra.Command{ log.Info().Msgf("Error decrypting files: %v\n", err) } - bootstrapcmds := gencmd.GenBootstrap("", extraArgs) - bootstrapNode := helper.ExtractNode(bootstrapcmds) + initfiles.LoadTalEnv(false) + talassist.LoadTalConfig() + bootstrapNode := talassist.TalConfig.Nodes[0].IPAddress log.Info().Msgf("Checking if first node is ready to recieve anything... %s", bootstrapNode) status, err := nodestatus.WaitForHealth(bootstrapNode, []string{"running", "maintenance"}) @@ -105,8 +108,8 @@ func RunApply(kubeconfig bool, node string, extraArgs []string) { gencmd.ExecCmds(taloscmds, true) if kubeconfig { - kubeconfigcmds := gencmd.GenKubeConfig(helper.TalEnv["VIP_IP"], extraArgs) - gencmd.ExecCmd(kubeconfigcmds) + kubeconfigcmds := gencmd.GenPlain("kubeconfig", helper.TalEnv["VIP_IP"], extraArgs) + gencmd.ExecCmd(kubeconfigcmds[0]) } //if helper.GetYesOrNo("Do you want to (re)load ssh, Sops and ClusterEnv onto the cluster? (yes/no) [y/n]: ") { diff --git a/clustertool/cmd/talos_bootstrap.go b/clustertool/cmd/talos_bootstrap.go index 60bf99b9ba79..ef1d82d90548 100644 --- a/clustertool/cmd/talos_bootstrap.go +++ b/clustertool/cmd/talos_bootstrap.go @@ -5,6 +5,9 @@ import ( "github.com/spf13/cobra" "github.com/truecharts/public/clustertool/pkg/gencmd" + "github.com/truecharts/public/clustertool/pkg/helper" + "github.com/truecharts/public/clustertool/pkg/initfiles" + "github.com/truecharts/public/clustertool/pkg/talassist" ) var advBootstrapLongHelp = strings.TrimSpace(` @@ -20,7 +23,14 @@ var bootstrap = &cobra.Command{ } func bootstrapfunc(cmd *cobra.Command, args []string) { - gencmd.RunBootstrap(args) + if helper.GetYesOrNo("Do you want to also run the complete ClusterTool Bootstrap, besides just talos? (yes/no) [y/n]: ") { + initfiles.LoadTalEnv(false) + talassist.LoadTalConfig() + gencmd.RunBootstrap(args) + } else { + bootstrapcmds := gencmd.GenPlain("bootstrap", talassist.TalConfig.Nodes[0].IPAddress, []string{}) + gencmd.ExecCmd(bootstrapcmds[0]) + } } func init() { diff --git a/clustertool/cmd/talos_health.go b/clustertool/cmd/talos_health.go index 3af6b2abeecc..f2f3ee330ddc 100644 --- a/clustertool/cmd/talos_health.go +++ b/clustertool/cmd/talos_health.go @@ -24,8 +24,8 @@ var health = &cobra.Command{ log.Info().Msgf("Error decrypting files: %v\n", err) } log.Info().Msg("Running Cluster HealthCheck") - healthcmd := gencmd.GenHealth(helper.TalEnv["VIP_IP"]) - gencmd.ExecCmd(healthcmd) + healthcmd := gencmd.GenPlain("health", helper.TalEnv["VIP_IP"], []string{}) + gencmd.ExecCmd(healthcmd[0]) }, } diff --git a/clustertool/cmd/talos_reset.go b/clustertool/cmd/talos_reset.go index bbd9a05e0bdf..bcf9c6eae0dd 100644 --- a/clustertool/cmd/talos_reset.go +++ b/clustertool/cmd/talos_reset.go @@ -38,7 +38,7 @@ var reset = &cobra.Command{ log.Info().Msg("Running Cluster node Reset") - taloscmds := gencmd.GenReset(node, extraArgs) + taloscmds := gencmd.GenPlain("reset", node, extraArgs) gencmd.ExecCmds(taloscmds, true) }, diff --git a/clustertool/cmd/talos_upgrade.go b/clustertool/cmd/talos_upgrade.go index 94360d1a5f8f..86f11fb251d8 100644 --- a/clustertool/cmd/talos_upgrade.go +++ b/clustertool/cmd/talos_upgrade.go @@ -51,8 +51,8 @@ var upgrade = &cobra.Command{ gencmd.ExecCmd(kubeUpgradeCmd) log.Info().Msg("(re)Loading KubeConfig)") - kubeconfigcmds := gencmd.GenKubeConfig(helper.TalEnv["VIP_IP"], extraArgs) - gencmd.ExecCmd(kubeconfigcmds) + kubeconfigcmds := gencmd.GenPlain("health", helper.TalEnv["VIP_IP"], extraArgs) + gencmd.ExecCmd(kubeconfigcmds[0]) }, } diff --git a/clustertool/pkg/gencmd/apply.go b/clustertool/pkg/gencmd/apply.go index f31beb111778..68c9fd6daa2b 100644 --- a/clustertool/pkg/gencmd/apply.go +++ b/clustertool/pkg/gencmd/apply.go @@ -1,49 +1,30 @@ package gencmd import ( - "io" - "os" - "strings" + "path/filepath" - "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 - // replace this with self-made stuff, be aware we also need to use the configfile - err = generate.GenerateApplyCommand(cfg, helper.TalosGenerated, node, extraFlags) + commands := []string{} + //extraFlags = append(extraFlags, "--preserve") - 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.TalConfig.Nodes { + // TODO add extraFlags + filename := talassist.TalConfig.ClusterName + "-" + noderef.Hostname + ".yaml" + cmd := talosPath + " " + "apply" + " --talosconfig " + helper.TalosConfigFile + " -n " + noderef.IPAddress + " " + "--file=" + filepath.Join(helper.TalosGenerated, filename) + 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 d83fdbffb70e..5f3fb79cb95a 100644 --- a/clustertool/pkg/gencmd/bootstrap.go +++ b/clustertool/pkg/gencmd/bootstrap.go @@ -7,7 +7,6 @@ import ( "github.com/rs/zerolog/log" - "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" @@ -24,12 +23,6 @@ var manifestPaths = []string{ filepath.Join(helper.KubernetesPath, "flux-system", "flux", "clustersettings.secret.yaml"), } -func GenBootstrap(node string, extraFlags []string) string { - talosPath := embed.GetTalosExec() - strout := talosPath + "bootstrap --talosconfig " + helper.TalosConfigFile + " -n " + talassist.IpAddresses[0] - return strout -} - func RunBootstrap(args []string) { var extraArgs []string if len(args) > 1 { @@ -38,9 +31,8 @@ func RunBootstrap(args []string) { if err := sops.DecryptFiles(); err != nil { log.Info().Msgf("Error decrypting files: %v\n", err) } - - bootstrapcmds := GenBootstrap("", extraArgs) - bootstrapNode := helper.ExtractNode(bootstrapcmds) + bootstrapNode := talassist.TalConfig.Nodes[0].IPAddress + bootstrapcmds := GenPlain("bootstrap", bootstrapNode, extraArgs) nodestatus.WaitForHealth(bootstrapNode, []string{"maintenance"}) @@ -52,15 +44,15 @@ func RunBootstrap(args []string) { log.Info().Msgf("Bootstrap: At this point your system is installed to disk, please make sure not to reboot into the installer ISO/USB %s", bootstrapNode) log.Info().Msgf("Bootstrap: running bootstrap on node: %s", bootstrapNode) - ExecCmd(bootstrapcmds) + ExecCmd(bootstrapcmds[0]) log.Info().Msgf("Bootstrap: waiting for VIP %v to come online...", helper.TalEnv["VIP_IP"]) nodestatus.WaitForHealth(helper.TalEnv["VIP_IP"], []string{"running"}) log.Info().Msgf("Bootstrap: Configuring kubectl for VIP: %v", helper.TalEnv["VIP_IP"]) // Ensure kubeconfig is loaded - kubeconfigcmds := GenKubeConfig(helper.TalEnv["VIP_IP"], extraArgs) - ExecCmd(kubeconfigcmds) + kubeconfigcmds := GenPlain("health", helper.TalEnv["VIP_IP"], extraArgs) + ExecCmd(kubeconfigcmds[0]) // Desired pod names requiredPods := []string{ @@ -150,8 +142,8 @@ func RunBootstrap(args []string) { log.Info().Msg("Bootstrap: Base Cluster Configuration Completed, continuing setup...") log.Info().Msg("Bootstrap: Confirming cluster health...") - healthcmd := GenHealth(helper.TalEnv["VIP_IP"]) - ExecCmd(healthcmd) + healthcmd := GenPlain("health", helper.TalEnv["VIP_IP"], []string{}) + ExecCmd(healthcmd[0]) close(stopCh) prioCharts := []fluxhandler.HelmChart{ diff --git a/clustertool/pkg/gencmd/execcmd.go b/clustertool/pkg/gencmd/execcmd.go index 6b5ea9f3f917..d152c172f77e 100644 --- a/clustertool/pkg/gencmd/execcmd.go +++ b/clustertool/pkg/gencmd/execcmd.go @@ -76,8 +76,8 @@ func ExecCmds(taloscmds []string, healthcheck bool) error { } else { if helper.GetYesOrNo("Do you want to check the health of the cluster? (yes/no) [y/n]: ") { log.Info().Msg("Checking if cluster is healthy...") - healthcmd := GenHealth(helper.TalEnv["VIP_IP"]) - ExecCmd(healthcmd) + healthcmd := GenPlain("health", helper.TalEnv["VIP_IP"], []string{}) + ExecCmd(healthcmd[0]) } else { skipped = true } diff --git a/clustertool/pkg/gencmd/genconfig.go b/clustertool/pkg/gencmd/genconfig.go index 6d42aedf90a6..2254e2827d58 100644 --- a/clustertool/pkg/gencmd/genconfig.go +++ b/clustertool/pkg/gencmd/genconfig.go @@ -3,30 +3,23 @@ package gencmd import ( "bytes" "errors" - "fmt" "os" "path" "github.com/rs/zerolog/log" - talhelperCfg "github.com/budimanjojo/talhelper/v3/pkg/config" - "github.com/budimanjojo/talhelper/v3/pkg/generate" - "github.com/budimanjojo/talhelper/v3/pkg/substitute" - "github.com/budimanjojo/talhelper/v3/pkg/talos" - sideroConfig "github.com/siderolabs/talos/pkg/machinery/config" - "github.com/siderolabs/talos/pkg/machinery/config/generate/secrets" "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/talassist" ) func GenConfig(args []string) error { - initfiles.GenSchema() + talassist.GenSchema() initfiles.GenTalEnvConfigMap() initfiles.CheckEnvVariables() genTalSecret() - validateTalConfig(args) - talhelperGenConfig() + talassist.TalhelperGenConfig() initfiles.UpdateGitRepo() if err := fluxhandler.ProcessDirectory(path.Join(helper.ClusterPath, "kubernetes")); err != nil { @@ -56,18 +49,13 @@ func genTalSecret() error { } defer outfile.Close() - var s *secrets.Bundle - version, _ := sideroConfig.ParseContractFromVersion(talhelperCfg.LatestTalosVersion) - s, err = talos.NewSecretBundle(secrets.NewClock(), *version) - if err != nil { - return err - } + secretbundle := talassist.NewSecretBundle() buf := new(bytes.Buffer) encoder := helper.YamlNewEncoder(buf) encoder.SetIndent(2) - err = encoder.Encode(s) + err = encoder.Encode(secretbundle) if err != nil { return err @@ -86,88 +74,3 @@ func genTalSecret() error { } return nil } - -func talhelperGenConfig() error { - genconfigTalosMode := "metal" - genconfigNoGitignore := false - genconfigDryRun := false - genconfigOfflineMode := false - - 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) - } - - err = generate.GenerateConfig(cfg, genconfigDryRun, helper.TalosGenerated, helper.TalSecretFile, genconfigTalosMode, genconfigOfflineMode) - if err != nil { - log.Fatal().Err(err).Msgf("failed to generate talos config: %s", err) - } - - if !genconfigNoGitignore && !genconfigDryRun { - err = cfg.GenerateGitignore(helper.TalosGenerated) - if err != nil { - log.Fatal().Err(err).Msgf("failed to generate gitignore file: %s", err) - } - } - return nil -} - -func validateTalConfig(argsInt []string) error { - cfg := helper.TalConfigFile - - log.Info().Msgf("start loading and validating Talconfig file for cluster %s", helper.ClusterName) - log.Debug().Msg(fmt.Sprintf("reading %s", cfg)) - cfgByte, err := os.ReadFile(cfg) - if err != nil { - log.Fatal().Err(err).Msgf("failed to read Talconfig file %s: %s", helper.TalConfigFile, err) - } - - if err := substitute.LoadEnvFromFiles([]string{helper.ClusterEnvFile}); err != nil { - log.Fatal().Err(err).Msg("failed to load env file: %s") - } - cfgByte, err = substitute.SubstituteEnvFromByte(cfgByte) - if err != nil { - log.Fatal().Err(err).Msg("failed trying to substitute env: %s") - } - - log.Debug().Msg("Checking configfile after substitution...") - - errs, warns, err := talhelperCfg.ValidateFromByte(cfgByte) - if err != nil { - log.Fatal().Err(err).Msgf("failed to validate talhelper config file: %s", err) - } - - if len(errs) > 0 { - log.Trace().Msg("running talconfig validation errs...") - log.Error().Msg("There are issues with your talhelper config file:") - groupedErr := make(map[string][]string) - for _, v := range errs { - groupedErr[v.Field] = append(groupedErr[v.Field], v.Message.Error()) - } - for field, list := range groupedErr { - log.Error().Msgf("field: %q\n", field) - for _, l := range list { - log.Error().Msgf(l + "\n") - } - } - os.Exit(1) - } else if len(warns) > 0 { - log.Trace().Msg("running talconfig validation warns...") - log.Warn().Msg("There might be some issues with your talhelper config file:") - groupedWarn := make(map[string][]string) - for _, v := range warns { - groupedWarn[v.Field] = append(groupedWarn[v.Field], v.Message) - - } - for field, list := range groupedWarn { - log.Warn().Msgf("field: %q\n", field) - for _, l := range list { - log.Warn().Msgf(l + "\n") - } - } - } else { - log.Info().Msg("Your talhelper config file is looking great!") - } - log.Info().Msg("Finished validating talconfig") - return nil -} diff --git a/clustertool/pkg/gencmd/health.go b/clustertool/pkg/gencmd/health.go deleted file mode 100644 index ee399ee722d9..000000000000 --- a/clustertool/pkg/gencmd/health.go +++ /dev/null @@ -1,12 +0,0 @@ -package gencmd - -import ( - "github.com/truecharts/public/clustertool/embed" - "github.com/truecharts/public/clustertool/pkg/helper" -) - -func GenHealth(node string) string { - talosPath := embed.GetTalosExec() - strout := talosPath + " health --talosconfig " + helper.TalosConfigFile + " -n " + node - return strout -} diff --git a/clustertool/pkg/gencmd/kubeconfig.go b/clustertool/pkg/gencmd/kubeconfig.go deleted file mode 100644 index 862f60a3ea1b..000000000000 --- a/clustertool/pkg/gencmd/kubeconfig.go +++ /dev/null @@ -1,15 +0,0 @@ -package gencmd - -import ( - "github.com/truecharts/public/clustertool/embed" - "github.com/truecharts/public/clustertool/pkg/helper" -) - -func GenKubeConfig(node string, extraFlags []string) string { - - //extraFlags = append(extraFlags, "--preserve") - talosPath := embed.GetTalosExec() - cmd := talosPath + " kubeconfig --talosconfig " + helper.TalosConfigFile + " -n " + node + " " - - return cmd -} diff --git a/clustertool/pkg/gencmd/reset.go b/clustertool/pkg/gencmd/plain.go similarity index 54% rename from clustertool/pkg/gencmd/reset.go rename to clustertool/pkg/gencmd/plain.go index 49fce79839fc..ff17742b0796 100644 --- a/clustertool/pkg/gencmd/reset.go +++ b/clustertool/pkg/gencmd/plain.go @@ -6,24 +6,21 @@ import ( "github.com/truecharts/public/clustertool/pkg/talassist" ) -func GenReset(node string, extraFlags []string) []string { +func GenPlain(command string, node string, extraFlags []string) []string { commands := []string{} //extraFlags = append(extraFlags, "--preserve") talosPath := embed.GetTalosExec() 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 + " " + for _, noderef := range talassist.TalConfig.Nodes { + // TODO add extraFlags + cmd := talosPath + " " + command + " --talosconfig " + helper.TalosConfigFile + " -n " + noderef.IPAddress + " " commands = append(commands, cmd) } } else { - cmd := talosPath + " reset --talosconfig " + helper.TalosConfigFile + " -n " + node + " " + cmd := talosPath + " " + command + " --talosconfig " + helper.TalosConfigFile + " -n " + node + " " commands = append(commands, cmd) } return commands diff --git a/clustertool/pkg/gencmd/upgrade.go b/clustertool/pkg/gencmd/upgrade.go index 2794998e507e..36fd6f4d889c 100644 --- a/clustertool/pkg/gencmd/upgrade.go +++ b/clustertool/pkg/gencmd/upgrade.go @@ -7,25 +7,21 @@ import ( "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" ) // TODO: remove talhelper dependency for cmd creation func GenUpgrade(node string, extraFlags []string) []string { // TODO: get rid of this, due to double uncontrollable log output - 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) - } upgradeStdout := os.Stdout r, w, _ := os.Pipe() os.Stdout = w extraFlags = append(extraFlags, "--preserve") - err = generate.GenerateUpgradeCommand(cfg, helper.TalosGenerated, node, extraFlags) + err := generate.GenerateUpgradeCommand(talassist.TalConfig, helper.TalosGenerated, node, extraFlags) w.Close() out, _ := io.ReadAll(r) diff --git a/clustertool/pkg/helper/envsubst.go b/clustertool/pkg/helper/envsubst.go index 71d20df67d32..65ec08cfac64 100644 --- a/clustertool/pkg/helper/envsubst.go +++ b/clustertool/pkg/helper/envsubst.go @@ -28,7 +28,6 @@ func LoadEnvFromFile(file string, output map[string]string) error { // Strip comments from YAML content before processing content = StripYamlComment(content) - // See: https://github.com/budimanjojo/talhelper/issues/220 content = StripYAMLDocDelimiter(content) if err := LoadEnv(content, output); err != nil { return fmt.Errorf("trying to load env from %s: %s", file, err) diff --git a/clustertool/pkg/initfiles/initfiles.go b/clustertool/pkg/initfiles/initfiles.go index bbafcffcf963..937fbcb30a4c 100644 --- a/clustertool/pkg/initfiles/initfiles.go +++ b/clustertool/pkg/initfiles/initfiles.go @@ -2,7 +2,6 @@ package initfiles import ( "bufio" - "encoding/json" "errors" "fmt" "io/ioutil" @@ -17,10 +16,9 @@ import ( "sigs.k8s.io/yaml" age "filippo.io/age" - talhelperCfg "github.com/budimanjojo/talhelper/v3/pkg/config" - "github.com/invopop/jsonschema" "github.com/truecharts/public/clustertool/pkg/fluxhandler" "github.com/truecharts/public/clustertool/pkg/helper" + "github.com/truecharts/public/clustertool/pkg/talassist" corev1 "k8s.io/api/core/v1" ) @@ -30,7 +28,7 @@ func InitFiles() error { genBaseFiles() UpdateRootFiles() UpdateBaseFiles() - GenSchema() + talassist.GenSchema() GenPatches() genKubernetes() GenTalEnvConfigMap() @@ -484,19 +482,3 @@ func GenSopsSecret() error { log.Info().Msgf("SOPS secret YAML saved to: %s\n", secretPath) return nil } - -func GenSchema() error { - cfg := talhelperCfg.TalhelperConfig{} - r := new(jsonschema.Reflector) - r.FieldNameTag = "yaml" - r.RequiredFromJSONSchemaTags = true - os.MkdirAll(helper.ClusterPath+"/talos", os.ModePerm) - var genschemaFile = path.Join(helper.ClusterPath, "/talos/talconfig.json") - - schema := r.Reflect(&cfg) - data, _ := json.MarshalIndent(schema, "", " ") - if err := os.WriteFile(genschemaFile, data, os.FileMode(0o644)); err != nil { - log.Fatal().Err(err).Msg("failed to write file to %s: %v") - } - return nil -} diff --git a/clustertool/pkg/talassist/ips.go b/clustertool/pkg/talassist/ips.go deleted file mode 100644 index 6f6f49b691bd..000000000000 --- a/clustertool/pkg/talassist/ips.go +++ /dev/null @@ -1,37 +0,0 @@ -package talassist - -import ( - "io/ioutil" - - "github.com/truecharts/public/clustertool/pkg/helper" - "sigs.k8s.io/yaml" -) - -// NodeIPConfig represents a simplified YAML structure to only parse node IP addresses -type NodeIPConfig struct { - Nodes []struct { - IPAddress string `yaml:"ipAddress"` - } `yaml:"nodes"` -} - -// LoadNodeIPs loads the list of IP addresses in nodes[].ipAddress from talconfig.yaml -func LoadNodeIPs() error { - // Read the YAML file - data, err := ioutil.ReadFile(helper.TalConfigFile) - if err != nil { - return err - } - - // Unmarshal only the IP addresses from the YAML - var config NodeIPConfig - if err := yaml.Unmarshal(data, &config); err != nil { - return err - } - - // Extract the IP addresses into a list - for _, node := range config.Nodes { - IpAddresses = append(IpAddresses, node.IPAddress) - } - - return nil -} diff --git a/clustertool/pkg/talassist/load.go b/clustertool/pkg/talassist/load.go new file mode 100644 index 000000000000..9e4498f4e00c --- /dev/null +++ b/clustertool/pkg/talassist/load.go @@ -0,0 +1,76 @@ +package talassist + +import ( + "encoding/json" + "os" + "path" + + talhelperCfg "github.com/budimanjojo/talhelper/v3/pkg/config" + "github.com/budimanjojo/talhelper/v3/pkg/generate" + talhelperTalos "github.com/budimanjojo/talhelper/v3/pkg/talos" + "github.com/invopop/jsonschema" + "github.com/rs/zerolog/log" + sideroConfig "github.com/siderolabs/talos/pkg/machinery/config" + "github.com/siderolabs/talos/pkg/machinery/config/generate/secrets" + "github.com/truecharts/public/clustertool/pkg/helper" +) + +var ( + TalConfig *talhelperCfg.TalhelperConfig + LatestTalosVersion string +) + +func LoadTalConfig() { + 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") + } + TalConfig = cfg + LatestTalosVersion = talhelperCfg.LatestTalosVersion + return +} + +func GenSchema() error { + cfg := talhelperCfg.TalhelperConfig{} + r := new(jsonschema.Reflector) + r.FieldNameTag = "yaml" + r.RequiredFromJSONSchemaTags = true + os.MkdirAll(helper.ClusterPath+"/talos", os.ModePerm) + var genschemaFile = path.Join(helper.ClusterPath, "/talos/talconfig.json") + + schema := r.Reflect(&cfg) + data, _ := json.MarshalIndent(schema, "", " ") + if err := os.WriteFile(genschemaFile, data, os.FileMode(0o644)); err != nil { + log.Fatal().Err(err).Msg("failed to write file to %s: %v") + } + return nil +} + +func NewSecretBundle() *secrets.Bundle { + version, _ := sideroConfig.ParseContractFromVersion(LatestTalosVersion) + s, err := talhelperTalos.NewSecretBundle(secrets.NewClock(), *version) + if err != nil { + log.Error().Msgf("Error loading secret bundle %s", err) + } + return s +} + +func TalhelperGenConfig() error { + genconfigTalosMode := "metal" + genconfigNoGitignore := false + genconfigDryRun := false + genconfigOfflineMode := false + + err := generate.GenerateConfig(TalConfig, genconfigDryRun, helper.TalosGenerated, helper.TalSecretFile, genconfigTalosMode, genconfigOfflineMode) + if err != nil { + log.Fatal().Err(err).Msgf("failed to generate talos config: %s", err) + } + + if !genconfigNoGitignore && !genconfigDryRun { + err = TalConfig.GenerateGitignore(helper.TalosGenerated) + if err != nil { + log.Fatal().Err(err).Msgf("failed to generate gitignore file: %s", err) + } + } + return nil +} diff --git a/clustertool/pkg/talassist/vars.go b/clustertool/pkg/talassist/vars.go deleted file mode 100644 index ead49deef549..000000000000 --- a/clustertool/pkg/talassist/vars.go +++ /dev/null @@ -1,5 +0,0 @@ -package talassist - -var ( - IpAddresses = []string{} -)