Skip to content

Commit

Permalink
fix(clustertool): fix kubectl logging error, add kubeconfig command a…
Browse files Browse the repository at this point in the history
…nd fix some command docs
  • Loading branch information
PrivatePuffin committed Nov 7, 2024
1 parent 8abe0ab commit df20030
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 5 deletions.
20 changes: 19 additions & 1 deletion clustertool/cmd/adv_testcmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package cmd

import (
"context"
"os"
"path/filepath"
"strings"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/truecharts/public/clustertool/pkg/helper"
"github.com/truecharts/public/clustertool/pkg/initfiles"
"github.com/truecharts/public/clustertool/pkg/kubectlcmds"
"github.com/truecharts/public/clustertool/pkg/talassist"
)

Expand All @@ -17,13 +23,25 @@ var testcmd = &cobra.Command{
Short: "tests specific code for developer usages",
Long: advTestCmdlongHelp,
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
initfiles.LoadTalEnv(false)
talassist.LoadTalConfig()
// err := fluxhandler.ProcessJSONFiles("./testdata/truenas_exports")
// if err != nil {
// log.Info().Msg("Error:", err)
// }
RunApply(false, "", []string{})
var manifestPaths = []string{
filepath.Join(helper.KubernetesPath, "flux-system", "flux", "sopssecret.secret.yaml"),
filepath.Join(helper.KubernetesPath, "flux-system", "flux", "deploykey.secret.yaml"),
filepath.Join(helper.KubernetesPath, "flux-system", "flux", "clustersettings.secret.yaml"),
}
for _, filePath := range manifestPaths {
log.Info().Msgf("Bootstrap: Loading Manifest: %v", filePath)
if err := kubectlcmds.KubectlApply(ctx, filePath); err != nil {
log.Info().Msgf("Error applying manifest for %s: %v\n", filepath.Base(filePath), err)
os.Exit(1)
}
}
},
}

Expand Down
2 changes: 1 addition & 1 deletion clustertool/cmd/talos_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var advBootstrapLongHelp = strings.TrimSpace(`
var bootstrap = &cobra.Command{
Use: "bootstrap",
Short: "bootstrap first Talos Node",
Example: "clustertool adv bootstrap",
Example: "clustertool talos bootstrap",
Long: advBootstrapLongHelp,
Run: bootstrapfunc,
}
Expand Down
6 changes: 5 additions & 1 deletion clustertool/cmd/talos_health.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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/sops"
"github.com/truecharts/public/clustertool/pkg/talassist"
)

var advHealthLongHelp = strings.TrimSpace(`
Expand All @@ -17,12 +19,14 @@ var advHealthLongHelp = strings.TrimSpace(`
var health = &cobra.Command{
Use: "health",
Short: "Check Talos Cluster Health",
Example: "clustertool adv health",
Example: "clustertool talos health",
Long: advHealthLongHelp,
Run: func(cmd *cobra.Command, args []string) {
if err := sops.DecryptFiles(); err != nil {
log.Info().Msgf("Error decrypting files: %v\n", err)
}
initfiles.LoadTalEnv(false)
talassist.LoadTalConfig()
log.Info().Msg("Running Cluster HealthCheck")
healthcmd := gencmd.GenPlain("health", helper.TalEnv["VIP_IP"], []string{})
gencmd.ExecCmd(healthcmd[0])
Expand Down
52 changes: 52 additions & 0 deletions clustertool/cmd/talos_kubeconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cmd

import (
"strings"

"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/truecharts/public/clustertool/pkg/gencmd"
"github.com/truecharts/public/clustertool/pkg/initfiles"
"github.com/truecharts/public/clustertool/pkg/sops"
"github.com/truecharts/public/clustertool/pkg/talassist"
)

var advKubeconfigLongHelp = strings.TrimSpace(`
`)

var kubeconfig = &cobra.Command{
Use: "kubeconfig",
Short: "kubeconfig for Talos Cluster",
Example: "clustertool talos kubeconfig <NodeIP>",
Long: advResetLongHelp,
Run: func(cmd *cobra.Command, args []string) {
var extraArgs []string
node := ""

if len(args) > 1 {
extraArgs = args[1:]
}
if len(args) >= 1 {
node = args[0]
if args[0] == "all" {
node = ""
}
}

if err := sops.DecryptFiles(); err != nil {
log.Info().Msgf("Error decrypting files: %v\n", err)
}
initfiles.LoadTalEnv(false)
talassist.LoadTalConfig()
log.Info().Msg("Running Cluster kubeconfig")

taloscmds := gencmd.GenPlain("kubeconfig", node, extraArgs)
gencmd.ExecCmds(taloscmds, true)

},
}

func init() {
talosCmd.AddCommand(kubeconfig)
}
6 changes: 5 additions & 1 deletion clustertool/cmd/talos_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
"github.com/truecharts/public/clustertool/pkg/gencmd"
"github.com/truecharts/public/clustertool/pkg/initfiles"
"github.com/truecharts/public/clustertool/pkg/sops"
"github.com/truecharts/public/clustertool/pkg/talassist"
)

var advResetLongHelp = strings.TrimSpace(`
Expand All @@ -16,7 +18,7 @@ var advResetLongHelp = strings.TrimSpace(`
var reset = &cobra.Command{
Use: "reset",
Short: "Reset Talos Nodes and Kubernetes",
Example: "clustertool adv reset <NodeIP>",
Example: "clustertool talos reset <NodeIP>",
Long: advResetLongHelp,
Run: func(cmd *cobra.Command, args []string) {
var extraArgs []string
Expand All @@ -35,6 +37,8 @@ var reset = &cobra.Command{
if err := sops.DecryptFiles(); err != nil {
log.Info().Msgf("Error decrypting files: %v\n", err)
}
initfiles.LoadTalEnv(false)
talassist.LoadTalConfig()

log.Info().Msg("Running Cluster node Reset")

Expand Down
6 changes: 5 additions & 1 deletion clustertool/cmd/talos_upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,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/sops"
"github.com/truecharts/public/clustertool/pkg/talassist"
)

var upgradeLongHelp = strings.TrimSpace(`
Expand All @@ -21,7 +23,7 @@ On top of this, after upgrading Talos on all nodes, it also executes kubernetes-
var upgrade = &cobra.Command{
Use: "upgrade",
Short: "Upgrade Talos Nodes and Kubernetes",
Example: "clustertool upgrade <NodeIP>",
Example: "clustertool talos upgrade <NodeIP>",
Long: upgradeLongHelp,
Run: func(cmd *cobra.Command, args []string) {
var extraArgs []string
Expand All @@ -40,6 +42,8 @@ var upgrade = &cobra.Command{
if err := sops.DecryptFiles(); err != nil {
log.Info().Msgf("Error decrypting files: %v\n", err)
}
initfiles.LoadTalEnv(false)
talassist.LoadTalConfig()

log.Info().Msg("Running Cluster Upgrade")

Expand Down
1 change: 1 addition & 0 deletions clustertool/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/budimanjojo/talhelper/v3 v3.0.8
github.com/getsops/sops/v3 v3.9.1
github.com/go-git/go-git/v5 v5.12.0
github.com/go-logr/zerologr v1.2.3
github.com/go-playground/validator/v10 v10.22.1
github.com/invopop/jsonschema v0.12.0
github.com/joho/godotenv v1.5.1
Expand Down
2 changes: 2 additions & 0 deletions clustertool/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg=
github.com/go-logr/zerologr v1.2.3 h1:up5N9vcH9Xck3jJkXzgyOxozT14R47IyDODz8LM1KSs=
github.com/go-logr/zerologr v1.2.3/go.mod h1:BxwGo7y5zgSHYR1BjbnHPyF/5ZjVKfKxAZANVu6E8Ho=
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ=
Expand Down
11 changes: 11 additions & 0 deletions clustertool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import (
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"

"github.com/go-logr/zerologr"
"github.com/truecharts/public/clustertool/cmd"
"github.com/truecharts/public/clustertool/embed"
"github.com/truecharts/public/clustertool/pkg/helper"
k8slog "sigs.k8s.io/controller-runtime/pkg/log"
)

var Version = "dev"
Expand Down Expand Up @@ -54,6 +56,15 @@ func main() {
NoColor: noColor, // Set to true if you prefer no color
})

// Initialize zerolog with console output
zlogger := zerolog.New(os.Stderr).With().Timestamp().Logger()

// Wrap zerolog with zerologr to create a logr.Logger
logger := zerologr.New(&zlogger)

// Set this logger for dependencies expecting log.SetLogger
k8slog.SetLogger(logger)

fmt.Printf("\n%s\n", helper.Logo)
fmt.Printf("---\nClustertool Version: %s\n---\n", Version)

Expand Down

0 comments on commit df20030

Please sign in to comment.