Skip to content

Commit

Permalink
feat(clustertool): move all talhelper references to talassist
Browse files Browse the repository at this point in the history
  • Loading branch information
PrivatePuffin committed Nov 6, 2024
1 parent 66e079f commit 16c0e0c
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 267 deletions.
11 changes: 7 additions & 4 deletions clustertool/cmd/talos_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand Down Expand Up @@ -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"})
Expand Down Expand Up @@ -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]: ") {
Expand Down
12 changes: 11 additions & 1 deletion clustertool/cmd/talos_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
Expand All @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions clustertool/cmd/talos_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
},
}

Expand Down
2 changes: 1 addition & 1 deletion clustertool/cmd/talos_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

},
Expand Down
4 changes: 2 additions & 2 deletions clustertool/cmd/talos_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])

},
}
Expand Down
49 changes: 15 additions & 34 deletions clustertool/pkg/gencmd/apply.go
Original file line number Diff line number Diff line change
@@ -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
}
22 changes: 7 additions & 15 deletions clustertool/pkg/gencmd/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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"})

Expand All @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 2 additions & 2 deletions clustertool/pkg/gencmd/execcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
107 changes: 5 additions & 102 deletions clustertool/pkg/gencmd/genconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
}
12 changes: 0 additions & 12 deletions clustertool/pkg/gencmd/health.go

This file was deleted.

Loading

0 comments on commit 16c0e0c

Please sign in to comment.