Skip to content

Commit da618f5

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 subcommands. This allows for the removal of CallPersistentPreRun, since the commands no longer rely on proper chaining of the PersistentPreRun hooks. Signed-off-by: Tom Wieczorek <[email protected]>
1 parent cb15775 commit da618f5

26 files changed

+327
-210
lines changed

cmd/airgap/listimages.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,25 @@ package airgap
1919
import (
2020
"fmt"
2121

22+
"github.com/k0sproject/k0s/cmd/internal"
2223
"github.com/k0sproject/k0s/pkg/airgap"
2324
"github.com/k0sproject/k0s/pkg/config"
2425

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

2829
func NewAirgapListImagesCmd() *cobra.Command {
29-
var all bool
30+
var (
31+
debugFlags internal.DebugFlags
32+
all bool
33+
)
3034

3135
cmd := &cobra.Command{
32-
Use: "list-images",
33-
Short: "List image names and version needed for air-gap install",
34-
Example: `k0s airgap list-images`,
35-
Args: cobra.NoArgs,
36+
Use: "list-images",
37+
Short: "List image names and version needed for air-gap install",
38+
Example: `k0s airgap list-images`,
39+
Args: cobra.NoArgs,
40+
PersistentPreRun: debugFlags.Run,
3641
RunE: func(cmd *cobra.Command, _ []string) error {
3742
opts, err := config.GetCmdOpts(cmd)
3843
if err != nil {
@@ -54,6 +59,8 @@ func NewAirgapListImagesCmd() *cobra.Command {
5459
},
5560
}
5661

62+
debugFlags.AddToFlagSet(cmd.PersistentFlags())
63+
5764
flags := cmd.Flags()
5865
flags.AddFlagSet(config.GetPersistentFlagSet())
5966
flags.AddFlagSet(config.FileInputFlag())

cmd/api/api.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"strings"
3333
"time"
3434

35-
internallog "github.com/k0sproject/k0s/internal/pkg/log"
35+
"github.com/k0sproject/k0s/cmd/internal"
3636
mw "github.com/k0sproject/k0s/internal/pkg/middleware"
3737
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
3838
"github.com/k0sproject/k0s/pkg/config"
@@ -52,17 +52,15 @@ import (
5252
)
5353

5454
func NewAPICmd() *cobra.Command {
55+
var debugFlags internal.DebugFlags
56+
5557
cmd := &cobra.Command{
5658
Use: "api",
5759
Short: "Run the controller API",
5860
Long: `Run the controller API.
5961
Reads the runtime configuration from standard input.`,
60-
Args: cobra.NoArgs,
61-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
62-
logrus.SetOutput(cmd.OutOrStdout())
63-
internallog.SetInfoLevel()
64-
return config.CallParentPersistentPreRun(cmd, args)
65-
},
62+
Args: cobra.NoArgs,
63+
PersistentPreRun: debugFlags.Run,
6664
RunE: func(cmd *cobra.Command, _ []string) error {
6765
var run func() error
6866

@@ -76,6 +74,8 @@ Reads the runtime configuration from standard input.`,
7674
},
7775
}
7876

77+
debugFlags.LongRunning().AddToFlagSet(cmd.PersistentFlags())
78+
7979
flags := cmd.Flags()
8080
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
8181
switch f.Name {

cmd/backup/backup_unix.go

+11-4
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
RunE: func(cmd *cobra.Command, _ []string) error {
4752
opts, err := config.GetCmdOpts(cmd)
4853
if err != nil {
@@ -60,6 +65,8 @@ func NewBackupCmd() *cobra.Command {
6065
},
6166
}
6267

68+
debugFlags.AddToFlagSet(cmd.PersistentFlags())
69+
6370
flags := cmd.Flags()
6471
flags.AddFlagSet(config.GetPersistentFlagSet())
6572
flags.StringVar(&savePath, "save-path", "", "destination directory path for backup assets, use '-' for stdout")

cmd/config/config.go

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

1919
import (
20+
"github.com/k0sproject/k0s/cmd/internal"
21+
2022
"github.com/spf13/cobra"
2123
"github.com/spf13/pflag"
2224
)
2325

2426
func NewConfigCmd() *cobra.Command {
27+
var debugFlags internal.DebugFlags
28+
2529
cmd := &cobra.Command{
26-
Use: "config",
27-
Short: "Configuration related sub-commands",
28-
Args: cobra.NoArgs,
29-
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
30+
Use: "config",
31+
Short: "Configuration related sub-commands",
32+
Args: cobra.NoArgs,
33+
PersistentPreRun: debugFlags.Run,
34+
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
3035
}
36+
37+
debugFlags.AddToFlagSet(cmd.PersistentFlags())
38+
3139
cmd.AddCommand(NewCreateCmd())
3240
cmd.AddCommand(NewEditCmd())
3341
cmd.AddCommand(NewStatusCmd())
3442
cmd.AddCommand(NewValidateCmd())
43+
3544
return cmd
3645
}
3746

cmd/config/create.go

+11-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 {
@@ -57,7 +59,11 @@ func NewCreateCmd() *cobra.Command {
5759
}
5860

5961
flags := cmd.Flags()
60-
flags.AddFlagSet(config.GetPersistentFlagSet())
62+
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
63+
f.Hidden = true
64+
f.Deprecated = "it has no effect and will be removed in a future release"
65+
cmd.PersistentFlags().AddFlag(f)
66+
})
6167
flags.BoolVar(&includeImages, "include-images", false, "include the default images in the output")
6268

6369
return cmd

cmd/config/validate.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/k0sproject/k0s/pkg/config"
2727

2828
"github.com/spf13/cobra"
29+
"github.com/spf13/pflag"
2930
)
3031

3132
func NewValidateCmd() *cobra.Command {
@@ -63,7 +64,11 @@ func NewValidateCmd() *cobra.Command {
6364
}
6465

6566
flags := cmd.Flags()
66-
flags.AddFlagSet(config.GetPersistentFlagSet())
67+
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
68+
f.Hidden = true
69+
f.Deprecated = "it has no effect and will be removed in a future release"
70+
cmd.PersistentFlags().AddFlag(f)
71+
})
6772
flags.AddFlagSet(config.FileInputFlag())
6873
_ = cmd.MarkFlagRequired("config")
6974

cmd/controller/controller.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ import (
3333
"time"
3434

3535
"github.com/avast/retry-go"
36+
"github.com/k0sproject/k0s/cmd/internal"
3637
workercmd "github.com/k0sproject/k0s/cmd/worker"
3738
"github.com/k0sproject/k0s/internal/pkg/dir"
3839
"github.com/k0sproject/k0s/internal/pkg/file"
39-
internallog "github.com/k0sproject/k0s/internal/pkg/log"
4040
"github.com/k0sproject/k0s/internal/pkg/sysinfo"
4141
"github.com/k0sproject/k0s/internal/sync/value"
4242
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
@@ -70,7 +70,10 @@ import (
7070
type command config.CLIOptions
7171

7272
func NewControllerCmd() *cobra.Command {
73-
var ignorePreFlightChecks bool
73+
var (
74+
debugFlags internal.DebugFlags
75+
ignorePreFlightChecks bool
76+
)
7477

7578
cmd := &cobra.Command{
7679
Use: "controller [join-token]",
@@ -83,12 +86,8 @@ func NewControllerCmd() *cobra.Command {
8386
or CLI flag:
8487
$ k0s controller --token-file [path_to_file]
8588
Note: Token can be passed either as a CLI argument or as a flag`,
86-
Args: cobra.MaximumNArgs(1),
87-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
88-
logrus.SetOutput(cmd.OutOrStdout())
89-
internallog.SetInfoLevel()
90-
return config.CallParentPersistentPreRun(cmd, args)
91-
},
89+
Args: cobra.MaximumNArgs(1),
90+
PersistentPreRun: debugFlags.Run,
9291
RunE: func(cmd *cobra.Command, args []string) error {
9392
opts, err := config.GetCmdOpts(cmd)
9493
if err != nil {
@@ -117,10 +116,12 @@ func NewControllerCmd() *cobra.Command {
117116

118117
ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
119118
defer cancel()
120-
return c.start(ctx)
119+
return c.start(ctx, debugFlags.IsDebug())
121120
},
122121
}
123122

123+
debugFlags.LongRunning().AddToFlagSet(cmd.PersistentFlags())
124+
124125
flags := cmd.Flags()
125126
flags.AddFlagSet(config.GetPersistentFlagSet())
126127
flags.AddFlagSet(config.GetControllerFlags())
@@ -130,7 +131,7 @@ func NewControllerCmd() *cobra.Command {
130131
return cmd
131132
}
132133

133-
func (c *command) start(ctx context.Context) error {
134+
func (c *command) start(ctx context.Context, debug bool) error {
134135
perfTimer := performance.NewTimer("controller-start").Buffer().Start()
135136

136137
nodeConfig, err := c.K0sVars.NodeConfig()
@@ -264,8 +265,8 @@ func (c *command) start(ctx context.Context) error {
264265
nodeComponents.Add(ctx, &cplb.Keepalived{
265266
K0sVars: c.K0sVars,
266267
Config: cplbCfg.Keepalived,
267-
DetailedLogging: c.Debug,
268-
LogConfig: c.Debug,
268+
DetailedLogging: debug,
269+
LogConfig: debug,
269270
KubeConfigPath: c.K0sVars.AdminKubeConfigPath,
270271
APIPort: nodeConfig.Spec.API.Port,
271272
})

cmd/controller/controller_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Flags:
6464
-c, --config string config file, use '-' to read the config from stdin (default `+defaultConfigPath+`)
6565
--cri-socket string container runtime socket to use, default to internal containerd. Format: [remote|docker]:[path-to-socket]
6666
--data-dir string Data Directory for k0s. DO NOT CHANGE for an existing setup, things will break! (default `+defaultDataDir+`)
67-
-d, --debug Debug logging (default: false)
67+
-d, --debug Debug logging (implies verbose logging)
6868
--debugListenOn string Http listenOn for Debug pprof handler (default ":6060")
6969
--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)
7070
--enable-cloud-provider Whether or not to enable cloud provider support in kubelet
@@ -88,6 +88,6 @@ Flags:
8888
--status-socket string Full file path to the socket file. (default: <rundir>/status.sock)
8989
--taints strings Node taints, list of key=value:effect strings
9090
--token-file string Path to the file containing join-token.
91-
-v, --verbose Verbose logging (default: false)
91+
-v, --verbose Verbose logging (default true)
9292
`, out.String())
9393
}

cmd/etcd/etcd.go

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ 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

@@ -28,14 +29,14 @@ import (
2829
)
2930

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

4041
opts, err := config.GetCmdOpts(cmd)
4142
if err != nil {
@@ -56,7 +57,9 @@ func NewEtcdCmd() *cobra.Command {
5657
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
5758
}
5859

59-
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
60+
pflags := cmd.PersistentFlags()
61+
debugFlags.AddToFlagSet(pflags)
62+
pflags.AddFlagSet(config.GetPersistentFlagSet())
6063

6164
cmd.AddCommand(etcdLeaveCmd())
6265
cmd.AddCommand(etcdListCmd())

cmd/install/controller_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ Flags:
6060
-c, --config string config file, use '-' to read the config from stdin (default `+defaultConfigPath+`)
6161
--cri-socket string container runtime socket to use, default to internal containerd. Format: [remote|docker]:[path-to-socket]
6262
--data-dir string Data Directory for k0s. DO NOT CHANGE for an existing setup, things will break! (default `+defaultDataDir+`)
63-
-d, --debug Debug logging (default: false)
64-
--debugListenOn string Http listenOn for Debug pprof handler (default ":6060")
6563
--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)
6664
--enable-cloud-provider Whether or not to enable cloud provider support in kubelet
6765
--enable-dynamic-config enable cluster-wide dynamic config based on custom resource
@@ -83,10 +81,12 @@ Flags:
8381
--status-socket string Full file path to the socket file. (default: <rundir>/status.sock)
8482
--taints strings Node taints, list of key=value:effect strings
8583
--token-file string Path to the file containing join-token.
86-
-v, --verbose Verbose logging (default: false)
8784
8885
Global Flags:
89-
-e, --env stringArray set environment variable
90-
--force force init script creation
86+
-d, --debug Debug logging (implies verbose logging)
87+
--debugListenOn string Http listenOn for Debug pprof handler (default ":6060")
88+
-e, --env stringArray set environment variable
89+
--force force init script creation
90+
-v, --verbose Verbose logging
9191
`, out.String())
9292
}

cmd/install/install.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package install
1818

1919
import (
20+
"github.com/k0sproject/k0s/cmd/internal"
2021
"github.com/k0sproject/k0s/pkg/config"
2122
"github.com/spf13/cobra"
2223
"github.com/spf13/pflag"
@@ -28,16 +29,21 @@ type installFlags struct {
2829
}
2930

3031
func NewInstallCmd() *cobra.Command {
31-
var installFlags installFlags
32+
var (
33+
debugFlags internal.DebugFlags
34+
installFlags installFlags
35+
)
3236

3337
cmd := &cobra.Command{
34-
Use: "install",
35-
Short: "Install k0s on a brand-new system. Must be run as root (or with sudo)",
36-
Args: cobra.NoArgs,
37-
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
38+
Use: "install",
39+
Short: "Install k0s on a brand-new system. Must be run as root (or with sudo)",
40+
Args: cobra.NoArgs,
41+
PersistentPreRun: debugFlags.Run,
42+
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
3843
}
3944

4045
pflags := cmd.PersistentFlags()
46+
debugFlags.AddToFlagSet(pflags)
4147
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
4248
f.Hidden = true
4349
f.Deprecated = "it has no effect and will be removed in a future release"

0 commit comments

Comments
 (0)