Skip to content

Commit df20030

Browse files
committed
fix(clustertool): fix kubectl logging error, add kubeconfig command and fix some command docs
1 parent 8abe0ab commit df20030

File tree

9 files changed

+101
-5
lines changed

9 files changed

+101
-5
lines changed

clustertool/cmd/adv_testcmd.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package cmd
22

33
import (
4+
"context"
5+
"os"
6+
"path/filepath"
47
"strings"
58

9+
"github.com/rs/zerolog/log"
610
"github.com/spf13/cobra"
11+
"github.com/truecharts/public/clustertool/pkg/helper"
712
"github.com/truecharts/public/clustertool/pkg/initfiles"
13+
"github.com/truecharts/public/clustertool/pkg/kubectlcmds"
814
"github.com/truecharts/public/clustertool/pkg/talassist"
915
)
1016

@@ -17,13 +23,25 @@ var testcmd = &cobra.Command{
1723
Short: "tests specific code for developer usages",
1824
Long: advTestCmdlongHelp,
1925
Run: func(cmd *cobra.Command, args []string) {
26+
ctx := context.Background()
2027
initfiles.LoadTalEnv(false)
2128
talassist.LoadTalConfig()
2229
// err := fluxhandler.ProcessJSONFiles("./testdata/truenas_exports")
2330
// if err != nil {
2431
// log.Info().Msg("Error:", err)
2532
// }
26-
RunApply(false, "", []string{})
33+
var manifestPaths = []string{
34+
filepath.Join(helper.KubernetesPath, "flux-system", "flux", "sopssecret.secret.yaml"),
35+
filepath.Join(helper.KubernetesPath, "flux-system", "flux", "deploykey.secret.yaml"),
36+
filepath.Join(helper.KubernetesPath, "flux-system", "flux", "clustersettings.secret.yaml"),
37+
}
38+
for _, filePath := range manifestPaths {
39+
log.Info().Msgf("Bootstrap: Loading Manifest: %v", filePath)
40+
if err := kubectlcmds.KubectlApply(ctx, filePath); err != nil {
41+
log.Info().Msgf("Error applying manifest for %s: %v\n", filepath.Base(filePath), err)
42+
os.Exit(1)
43+
}
44+
}
2745
},
2846
}
2947

clustertool/cmd/talos_bootstrap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var advBootstrapLongHelp = strings.TrimSpace(`
1717
var bootstrap = &cobra.Command{
1818
Use: "bootstrap",
1919
Short: "bootstrap first Talos Node",
20-
Example: "clustertool adv bootstrap",
20+
Example: "clustertool talos bootstrap",
2121
Long: advBootstrapLongHelp,
2222
Run: bootstrapfunc,
2323
}

clustertool/cmd/talos_health.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"github.com/spf13/cobra"
88
"github.com/truecharts/public/clustertool/pkg/gencmd"
99
"github.com/truecharts/public/clustertool/pkg/helper"
10+
"github.com/truecharts/public/clustertool/pkg/initfiles"
1011
"github.com/truecharts/public/clustertool/pkg/sops"
12+
"github.com/truecharts/public/clustertool/pkg/talassist"
1113
)
1214

1315
var advHealthLongHelp = strings.TrimSpace(`
@@ -17,12 +19,14 @@ var advHealthLongHelp = strings.TrimSpace(`
1719
var health = &cobra.Command{
1820
Use: "health",
1921
Short: "Check Talos Cluster Health",
20-
Example: "clustertool adv health",
22+
Example: "clustertool talos health",
2123
Long: advHealthLongHelp,
2224
Run: func(cmd *cobra.Command, args []string) {
2325
if err := sops.DecryptFiles(); err != nil {
2426
log.Info().Msgf("Error decrypting files: %v\n", err)
2527
}
28+
initfiles.LoadTalEnv(false)
29+
talassist.LoadTalConfig()
2630
log.Info().Msg("Running Cluster HealthCheck")
2731
healthcmd := gencmd.GenPlain("health", helper.TalEnv["VIP_IP"], []string{})
2832
gencmd.ExecCmd(healthcmd[0])
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package cmd
2+
3+
import (
4+
"strings"
5+
6+
"github.com/rs/zerolog/log"
7+
"github.com/spf13/cobra"
8+
"github.com/truecharts/public/clustertool/pkg/gencmd"
9+
"github.com/truecharts/public/clustertool/pkg/initfiles"
10+
"github.com/truecharts/public/clustertool/pkg/sops"
11+
"github.com/truecharts/public/clustertool/pkg/talassist"
12+
)
13+
14+
var advKubeconfigLongHelp = strings.TrimSpace(`
15+
16+
`)
17+
18+
var kubeconfig = &cobra.Command{
19+
Use: "kubeconfig",
20+
Short: "kubeconfig for Talos Cluster",
21+
Example: "clustertool talos kubeconfig <NodeIP>",
22+
Long: advResetLongHelp,
23+
Run: func(cmd *cobra.Command, args []string) {
24+
var extraArgs []string
25+
node := ""
26+
27+
if len(args) > 1 {
28+
extraArgs = args[1:]
29+
}
30+
if len(args) >= 1 {
31+
node = args[0]
32+
if args[0] == "all" {
33+
node = ""
34+
}
35+
}
36+
37+
if err := sops.DecryptFiles(); err != nil {
38+
log.Info().Msgf("Error decrypting files: %v\n", err)
39+
}
40+
initfiles.LoadTalEnv(false)
41+
talassist.LoadTalConfig()
42+
log.Info().Msg("Running Cluster kubeconfig")
43+
44+
taloscmds := gencmd.GenPlain("kubeconfig", node, extraArgs)
45+
gencmd.ExecCmds(taloscmds, true)
46+
47+
},
48+
}
49+
50+
func init() {
51+
talosCmd.AddCommand(kubeconfig)
52+
}

clustertool/cmd/talos_reset.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ import (
66
"github.com/rs/zerolog/log"
77
"github.com/spf13/cobra"
88
"github.com/truecharts/public/clustertool/pkg/gencmd"
9+
"github.com/truecharts/public/clustertool/pkg/initfiles"
910
"github.com/truecharts/public/clustertool/pkg/sops"
11+
"github.com/truecharts/public/clustertool/pkg/talassist"
1012
)
1113

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

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

clustertool/cmd/talos_upgrade.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"github.com/spf13/cobra"
88
"github.com/truecharts/public/clustertool/pkg/gencmd"
99
"github.com/truecharts/public/clustertool/pkg/helper"
10+
"github.com/truecharts/public/clustertool/pkg/initfiles"
1011
"github.com/truecharts/public/clustertool/pkg/sops"
12+
"github.com/truecharts/public/clustertool/pkg/talassist"
1113
)
1214

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

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

clustertool/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/budimanjojo/talhelper/v3 v3.0.8
1010
github.com/getsops/sops/v3 v3.9.1
1111
github.com/go-git/go-git/v5 v5.12.0
12+
github.com/go-logr/zerologr v1.2.3
1213
github.com/go-playground/validator/v10 v10.22.1
1314
github.com/invopop/jsonschema v0.12.0
1415
github.com/joho/godotenv v1.5.1

clustertool/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
277277
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
278278
github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ=
279279
github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg=
280+
github.com/go-logr/zerologr v1.2.3 h1:up5N9vcH9Xck3jJkXzgyOxozT14R47IyDODz8LM1KSs=
281+
github.com/go-logr/zerologr v1.2.3/go.mod h1:BxwGo7y5zgSHYR1BjbnHPyF/5ZjVKfKxAZANVu6E8Ho=
280282
github.com/go-openapi/jsonpointer v0.21.0 h1:YgdVicSA9vH5RiHs9TZW5oyafXZFc6+2Vc1rr/O9oNQ=
281283
github.com/go-openapi/jsonpointer v0.21.0/go.mod h1:IUyH9l/+uyhIYQ/PXVA41Rexl+kOkAPDdXEYns6fzUY=
282284
github.com/go-openapi/jsonreference v0.21.0 h1:Rs+Y7hSXT83Jacb7kFyjn4ijOuVGSvOdF2+tg1TRrwQ=

clustertool/main.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ import (
88
"github.com/rs/zerolog"
99
"github.com/rs/zerolog/log"
1010

11+
"github.com/go-logr/zerologr"
1112
"github.com/truecharts/public/clustertool/cmd"
1213
"github.com/truecharts/public/clustertool/embed"
1314
"github.com/truecharts/public/clustertool/pkg/helper"
15+
k8slog "sigs.k8s.io/controller-runtime/pkg/log"
1416
)
1517

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

59+
// Initialize zerolog with console output
60+
zlogger := zerolog.New(os.Stderr).With().Timestamp().Logger()
61+
62+
// Wrap zerolog with zerologr to create a logr.Logger
63+
logger := zerologr.New(&zlogger)
64+
65+
// Set this logger for dependencies expecting log.SetLogger
66+
k8slog.SetLogger(logger)
67+
5768
fmt.Printf("\n%s\n", helper.Logo)
5869
fmt.Printf("---\nClustertool Version: %s\n---\n", Version)
5970

0 commit comments

Comments
 (0)