Skip to content

Commit f7d165f

Browse files
committed
Encapsulate debug flag handling
Introduce internal.DebugFlags to replace debug and verbose logging flag handling. Move the flag definitions from the "PersistentFlagSet" to a struct method. Remove the debug flag handling from the root command and move it to the sub-commands. This allows for the removal of CallPersistentPreRun, since the commands no longer rely on proper chaining of the PersistentPreRun hooks. Remove some redundant calls to the "PersistentFlagSet" in some sub-commands. Signed-off-by: Tom Wieczorek <[email protected]>
1 parent 0e0d033 commit f7d165f

38 files changed

+410
-226
lines changed

cmd/airgap/airgap.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@ package airgap
1919
import (
2020
"github.com/spf13/cobra"
2121

22-
"github.com/k0sproject/k0s/pkg/config"
22+
"github.com/k0sproject/k0s/cmd/internal"
2323
)
2424

2525
func NewAirgapCmd() *cobra.Command {
26+
var debugFlags internal.DebugFlags
27+
2628
cmd := &cobra.Command{
27-
Use: "airgap",
28-
Short: "Manage airgap setup",
29-
Args: cobra.NoArgs,
30-
Run: func(*cobra.Command, []string) { /* Enforce arg validation. */ },
29+
Use: "airgap",
30+
Short: "Manage airgap setup",
31+
Args: cobra.NoArgs,
32+
PersistentPreRun: debugFlags.Run,
33+
Run: func(*cobra.Command, []string) { /* Enforce arg validation. */ },
3134
}
3235

36+
debugFlags.AddToFlagSet(cmd.PersistentFlags())
37+
3338
cmd.AddCommand(NewAirgapListImagesCmd())
34-
cmd.PersistentFlags().AddFlagSet(config.FileInputFlag())
35-
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
39+
3640
return cmd
3741
}

cmd/airgap/listimages.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,11 @@ func NewAirgapListImagesCmd() *cobra.Command {
5353
return nil
5454
},
5555
}
56+
57+
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
58+
5659
cmd.Flags().AddFlagSet(config.FileInputFlag())
5760
cmd.Flags().BoolVar(&all, "all", false, "include all images, even if they are not used in the current configuration")
58-
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
61+
5962
return cmd
6063
}

cmd/api/api.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"strings"
3030
"time"
3131

32-
k0slog "github.com/k0sproject/k0s/internal/pkg/log"
32+
"github.com/k0sproject/k0s/cmd/internal"
3333
mw "github.com/k0sproject/k0s/internal/pkg/middleware"
3434
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
3535
"github.com/k0sproject/k0s/pkg/config"
@@ -50,15 +50,13 @@ type command struct {
5050
}
5151

5252
func NewAPICmd() *cobra.Command {
53+
var debugFlags internal.DebugFlags
54+
5355
cmd := &cobra.Command{
54-
Use: "api",
55-
Short: "Run the controller API",
56-
Args: cobra.NoArgs,
57-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
58-
logrus.SetOutput(cmd.OutOrStdout())
59-
k0slog.SetInfoLevel()
60-
return config.CallParentPersistentPreRun(cmd, args)
61-
},
56+
Use: "api",
57+
Short: "Run the controller API",
58+
Args: cobra.NoArgs,
59+
PersistentPreRun: debugFlags.Run,
6260
RunE: func(cmd *cobra.Command, _ []string) error {
6361
opts, err := config.GetCmdOpts(cmd)
6462
if err != nil {
@@ -67,7 +65,11 @@ func NewAPICmd() *cobra.Command {
6765
return (&command{CLIOptions: opts}).start()
6866
},
6967
}
70-
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
68+
69+
pflags := cmd.PersistentFlags()
70+
debugFlags.LongRunning().AddToFlagSet(pflags)
71+
pflags.AddFlagSet(config.GetPersistentFlagSet())
72+
7173
return cmd
7274
}
7375

cmd/backup/backup_unix.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"strings"
2727

28+
"github.com/k0sproject/k0s/cmd/internal"
2829
"github.com/k0sproject/k0s/internal/pkg/dir"
2930
"github.com/k0sproject/k0s/pkg/backup"
3031
"github.com/k0sproject/k0s/pkg/component/status"
@@ -37,12 +38,16 @@ import (
3738
type command config.CLIOptions
3839

3940
func NewBackupCmd() *cobra.Command {
40-
var savePath string
41+
var (
42+
debugFlags internal.DebugFlags
43+
savePath string
44+
)
4145

4246
cmd := &cobra.Command{
43-
Use: "backup",
44-
Short: "Back-Up k0s configuration. Must be run as root (or with sudo)",
45-
Args: cobra.NoArgs,
47+
Use: "backup",
48+
Short: "Back-Up k0s configuration. Must be run as root (or with sudo)",
49+
Args: cobra.NoArgs,
50+
PersistentPreRun: debugFlags.Run,
4651
PreRun: func(cmd *cobra.Command, args []string) {
4752
// ensure logs don't mess up output
4853
logrus.SetOutput(cmd.ErrOrStderr())
@@ -63,8 +68,13 @@ func NewBackupCmd() *cobra.Command {
6368
return c.backup(savePath, cmd.OutOrStdout())
6469
},
6570
}
71+
72+
pflags := cmd.PersistentFlags()
73+
debugFlags.AddToFlagSet(pflags)
74+
pflags.AddFlagSet(config.GetPersistentFlagSet())
75+
6676
cmd.Flags().StringVar(&savePath, "save-path", "", "destination directory path for backup assets, use '-' for stdout")
67-
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
77+
6878
return cmd
6979
}
7080

cmd/config/config.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,27 @@ limitations under the License.
1717
package config
1818

1919
import (
20+
"github.com/k0sproject/k0s/cmd/internal"
2021
"github.com/spf13/cobra"
2122
)
2223

2324
func NewConfigCmd() *cobra.Command {
25+
var debugFlags internal.DebugFlags
26+
2427
cmd := &cobra.Command{
25-
Use: "config",
26-
Short: "Configuration related sub-commands",
27-
Args: cobra.NoArgs,
28-
Run: func(*cobra.Command, []string) { /* Enforce arg validation. */ },
28+
Use: "config",
29+
Short: "Configuration related sub-commands",
30+
Args: cobra.NoArgs,
31+
PersistentPreRun: debugFlags.Run,
32+
Run: func(*cobra.Command, []string) { /* Enforce arg validation. */ },
2933
}
3034
cmd.AddCommand(NewCreateCmd())
3135
cmd.AddCommand(NewEditCmd())
3236
cmd.AddCommand(NewStatusCmd())
3337
cmd.AddCommand(NewValidateCmd())
38+
39+
debugFlags.AddToFlagSet(cmd.PersistentFlags())
40+
3441
return cmd
3542
}
3643

cmd/config/create.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ limitations under the License.
1717
package config
1818

1919
import (
20-
"github.com/spf13/cobra"
21-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
22-
"sigs.k8s.io/yaml"
23-
2420
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
2521
k0sscheme "github.com/k0sproject/k0s/pkg/client/clientset/scheme"
2622
"github.com/k0sproject/k0s/pkg/config"
23+
24+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
25+
26+
"github.com/spf13/cobra"
27+
"github.com/spf13/pflag"
28+
"sigs.k8s.io/yaml"
2729
)
2830

2931
func NewCreateCmd() *cobra.Command {
@@ -55,7 +57,15 @@ func NewCreateCmd() *cobra.Command {
5557
return err
5658
},
5759
}
60+
61+
pflags := cmd.PersistentFlags()
62+
// Add unused persistent flags for backwards compatibility.
63+
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
64+
f.Hidden = true
65+
pflags.AddFlag(f)
66+
})
67+
5868
cmd.Flags().BoolVar(&includeImages, "include-images", false, "include the default images in the output")
59-
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
69+
6070
return cmd
6171
}

cmd/config/edit.go

+2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func NewEditCmd() *cobra.Command {
3131
return reExecKubectl(cmd, "-n", "kube-system", "edit", "clusterconfig", "k0s")
3232
},
3333
}
34+
3435
cmd.PersistentFlags().AddFlagSet(config.GetKubeCtlFlagSet())
36+
3537
return cmd
3638
}

cmd/config/status.go

+3
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ func NewStatusCmd() *cobra.Command {
3838
return reExecKubectl(cmd, args...)
3939
},
4040
}
41+
4142
cmd.PersistentFlags().AddFlagSet(config.GetKubeCtlFlagSet())
43+
4244
cmd.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format. Must be one of yaml|json")
45+
4346
return cmd
4447
}

cmd/config/validate.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ import (
2525
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
2626
"github.com/k0sproject/k0s/pkg/config"
2727

28+
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
29+
2830
"github.com/spf13/cobra"
31+
"github.com/spf13/pflag"
2932
)
3033

3134
func NewValidateCmd() *cobra.Command {
@@ -62,8 +65,15 @@ func NewValidateCmd() *cobra.Command {
6265
},
6366
}
6467

65-
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
68+
pflags := cmd.PersistentFlags()
69+
// Add unused persistent flags for backwards compatibility.
70+
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
71+
f.Hidden = true
72+
pflags.AddFlag(f)
73+
})
74+
6675
cmd.Flags().AddFlagSet(config.FileInputFlag())
67-
_ = cmd.MarkFlagRequired("config")
76+
utilruntime.Must(cmd.MarkFlagRequired("config"))
77+
6878
return cmd
6979
}

cmd/controller/controller.go

+18-15
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ import (
3131
"time"
3232

3333
"github.com/avast/retry-go"
34+
"github.com/k0sproject/k0s/cmd/internal"
3435
workercmd "github.com/k0sproject/k0s/cmd/worker"
3536
"github.com/k0sproject/k0s/internal/pkg/dir"
3637
"github.com/k0sproject/k0s/internal/pkg/file"
37-
k0slog "github.com/k0sproject/k0s/internal/pkg/log"
3838
"github.com/k0sproject/k0s/internal/pkg/sysinfo"
3939
"github.com/k0sproject/k0s/internal/sync/value"
4040
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
@@ -67,7 +67,10 @@ import (
6767
type command config.CLIOptions
6868

6969
func NewControllerCmd() *cobra.Command {
70-
var ignorePreFlightChecks bool
70+
var (
71+
debugFlags internal.DebugFlags
72+
ignorePreFlightChecks bool
73+
)
7174

7275
cmd := &cobra.Command{
7376
Use: "controller [join-token]",
@@ -80,12 +83,8 @@ func NewControllerCmd() *cobra.Command {
8083
or CLI flag:
8184
$ k0s controller --token-file [path_to_file]
8285
Note: Token can be passed either as a CLI argument or as a flag`,
83-
Args: cobra.MaximumNArgs(1),
84-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
85-
logrus.SetOutput(cmd.OutOrStdout())
86-
k0slog.SetInfoLevel()
87-
return config.CallParentPersistentPreRun(cmd, args)
88-
},
86+
Args: cobra.MaximumNArgs(1),
87+
PersistentPreRun: debugFlags.Run,
8988
RunE: func(cmd *cobra.Command, args []string) error {
9089
opts, err := config.GetCmdOpts(cmd)
9190
if err != nil {
@@ -114,19 +113,23 @@ func NewControllerCmd() *cobra.Command {
114113

115114
ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
116115
defer cancel()
117-
return c.start(ctx)
116+
return c.start(ctx, debugFlags.IsDebug())
118117
},
119118
}
120119

121120
// append flags
121+
pflags := cmd.PersistentFlags()
122+
debugFlags.LongRunning().AddToFlagSet(pflags)
123+
pflags.AddFlagSet(config.GetPersistentFlagSet())
124+
pflags.AddFlagSet(config.GetControllerFlags())
125+
pflags.AddFlagSet(config.GetWorkerFlags())
126+
122127
cmd.Flags().BoolVar(&ignorePreFlightChecks, "ignore-pre-flight-checks", false, "continue even if pre-flight checks fail")
123-
cmd.Flags().AddFlagSet(config.GetPersistentFlagSet())
124-
cmd.PersistentFlags().AddFlagSet(config.GetControllerFlags())
125-
cmd.PersistentFlags().AddFlagSet(config.GetWorkerFlags())
128+
126129
return cmd
127130
}
128131

129-
func (c *command) start(ctx context.Context) error {
132+
func (c *command) start(ctx context.Context, debug bool) error {
130133
perfTimer := performance.NewTimer("controller-start").Buffer().Start()
131134

132135
nodeConfig, err := c.K0sVars.NodeConfig()
@@ -247,8 +250,8 @@ func (c *command) start(ctx context.Context) error {
247250
nodeComponents.Add(ctx, &cplb.Keepalived{
248251
K0sVars: c.K0sVars,
249252
Config: cplbCfg.Keepalived,
250-
DetailedLogging: c.Debug,
251-
LogConfig: c.Debug,
253+
DetailedLogging: debug,
254+
LogConfig: debug,
252255
KubeConfigPath: c.K0sVars.AdminKubeConfigPath,
253256
APIPort: nodeConfig.Spec.API.Port,
254257
})

cmd/controller/controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Flags:
5959
-c, --config string config file, use '-' to read the config from stdin (default `+defaultConfigPath+`)
6060
--cri-socket string container runtime socket to use, default to internal containerd. Format: [remote|docker]:[path-to-socket]
6161
--data-dir string Data Directory for k0s. DO NOT CHANGE for an existing setup, things will break! (default `+defaultDataDir+`)
62-
-d, --debug Debug logging (default: false)
62+
-d, --debug Debug logging (implies verbose logging)
6363
--debugListenOn string Http listenOn for Debug pprof handler (default ":6060")
6464
--disable-components strings disable components (valid items: applier-manager,autopilot,control-api,coredns,csr-approver,endpoint-reconciler,helm,konnectivity-server,kube-controller-manager,kube-proxy,kube-scheduler,metrics-server,network-provider,node-role,system-rbac,windows-node,worker-config)
6565
--enable-cloud-provider Whether or not to enable cloud provider support in kubelet
@@ -82,6 +82,6 @@ Flags:
8282
--status-socket string Full file path to the socket file. (default: <rundir>/status.sock)
8383
--taints strings Node taints, list of key=value:effect strings
8484
--token-file string Path to the file containing join-token.
85-
-v, --verbose Verbose logging (default: false)
85+
-v, --verbose Verbose logging (default true)
8686
`, out.String())
8787
}

cmd/etcd/etcd.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,22 @@ import (
2020
"errors"
2121
"fmt"
2222

23+
"github.com/k0sproject/k0s/cmd/internal"
2324
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
2425
"github.com/k0sproject/k0s/pkg/config"
2526

2627
"github.com/spf13/cobra"
2728
)
2829

2930
func NewEtcdCmd() *cobra.Command {
31+
var debugFlags internal.DebugFlags
32+
3033
cmd := &cobra.Command{
3134
Use: "etcd",
3235
Short: "Manage etcd cluster",
3336
Args: cobra.NoArgs,
3437
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
35-
if err := config.CallParentPersistentPreRun(cmd, args); err != nil {
36-
return err
37-
}
38+
debugFlags.Run(cmd, args)
3839

3940
opts, err := config.GetCmdOpts(cmd)
4041
if err != nil {
@@ -54,8 +55,11 @@ func NewEtcdCmd() *cobra.Command {
5455
},
5556
Run: func(*cobra.Command, []string) { /* Enforce arg validation. */ },
5657
}
58+
59+
debugFlags.AddToFlagSet(cmd.PersistentFlags())
60+
5761
cmd.AddCommand(etcdLeaveCmd())
5862
cmd.AddCommand(etcdListCmd())
59-
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
63+
6064
return cmd
6165
}

0 commit comments

Comments
 (0)