Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Encapsulate debug flag handling #5390

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions cmd/airgap/listimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,25 @@ package airgap
import (
"fmt"

"github.com/k0sproject/k0s/cmd/internal"
"github.com/k0sproject/k0s/pkg/airgap"
"github.com/k0sproject/k0s/pkg/config"

"github.com/spf13/cobra"
)

func NewAirgapListImagesCmd() *cobra.Command {
var all bool
var (
debugFlags internal.DebugFlags
all bool
)

cmd := &cobra.Command{
Use: "list-images",
Short: "List image names and version needed for air-gap install",
Example: `k0s airgap list-images`,
Args: cobra.NoArgs,
Use: "list-images",
Short: "List image names and version needed for air-gap install",
Example: `k0s airgap list-images`,
Args: cobra.NoArgs,
PersistentPreRun: debugFlags.Run,
RunE: func(cmd *cobra.Command, _ []string) error {
opts, err := config.GetCmdOpts(cmd)
if err != nil {
Expand All @@ -54,6 +59,8 @@ func NewAirgapListImagesCmd() *cobra.Command {
},
}

debugFlags.AddToFlagSet(cmd.PersistentFlags())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.FileInputFlag())
Expand Down
14 changes: 7 additions & 7 deletions cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (
"strings"
"time"

internallog "github.com/k0sproject/k0s/internal/pkg/log"
"github.com/k0sproject/k0s/cmd/internal"
mw "github.com/k0sproject/k0s/internal/pkg/middleware"
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
"github.com/k0sproject/k0s/pkg/config"
Expand All @@ -52,17 +52,15 @@ import (
)

func NewAPICmd() *cobra.Command {
var debugFlags internal.DebugFlags

cmd := &cobra.Command{
Use: "api",
Short: "Run the controller API",
Long: `Run the controller API.
Reads the runtime configuration from standard input.`,
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
logrus.SetOutput(cmd.OutOrStdout())
internallog.SetInfoLevel()
return config.CallParentPersistentPreRun(cmd, args)
},
Args: cobra.NoArgs,
PersistentPreRun: debugFlags.Run,
RunE: func(cmd *cobra.Command, _ []string) error {
var run func() error

Expand All @@ -76,6 +74,8 @@ Reads the runtime configuration from standard input.`,
},
}

debugFlags.LongRunning().AddToFlagSet(cmd.PersistentFlags())

flags := cmd.Flags()
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
switch f.Name {
Expand Down
15 changes: 11 additions & 4 deletions cmd/backup/backup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"strings"

"github.com/k0sproject/k0s/cmd/internal"
"github.com/k0sproject/k0s/internal/pkg/dir"
k0sv1beta1 "github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
"github.com/k0sproject/k0s/pkg/backup"
Expand All @@ -38,12 +39,16 @@ import (
type command config.CLIOptions

func NewBackupCmd() *cobra.Command {
var savePath string
var (
debugFlags internal.DebugFlags
savePath string
)

cmd := &cobra.Command{
Use: "backup",
Short: "Back-Up k0s configuration. Must be run as root (or with sudo)",
Args: cobra.NoArgs,
Use: "backup",
Short: "Back-Up k0s configuration. Must be run as root (or with sudo)",
Args: cobra.NoArgs,
PersistentPreRun: debugFlags.Run,
RunE: func(cmd *cobra.Command, _ []string) error {
opts, err := config.GetCmdOpts(cmd)
if err != nil {
Expand All @@ -61,6 +66,8 @@ func NewBackupCmd() *cobra.Command {
},
}

debugFlags.AddToFlagSet(cmd.PersistentFlags())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.StringVar(&savePath, "save-path", "", "destination directory path for backup assets, use '-' for stdout")
Expand Down
17 changes: 13 additions & 4 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@ limitations under the License.
package config

import (
"github.com/k0sproject/k0s/cmd/internal"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func NewConfigCmd() *cobra.Command {
var debugFlags internal.DebugFlags

cmd := &cobra.Command{
Use: "config",
Short: "Configuration related sub-commands",
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
Use: "config",
Short: "Configuration related sub-commands",
Args: cobra.NoArgs,
PersistentPreRun: debugFlags.Run,
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
}

debugFlags.AddToFlagSet(cmd.PersistentFlags())

cmd.AddCommand(NewCreateCmd())
cmd.AddCommand(NewEditCmd())
cmd.AddCommand(NewStatusCmd())
cmd.AddCommand(NewValidateCmd())

return cmd
}

Expand Down
16 changes: 11 additions & 5 deletions cmd/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.
package config

import (
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"

"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
k0sscheme "github.com/k0sproject/k0s/pkg/client/clientset/scheme"
"github.com/k0sproject/k0s/pkg/config"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"sigs.k8s.io/yaml"
)

func NewCreateCmd() *cobra.Command {
Expand Down Expand Up @@ -57,7 +59,11 @@ func NewCreateCmd() *cobra.Command {
}

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

return cmd
Expand Down
7 changes: 6 additions & 1 deletion cmd/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/k0sproject/k0s/pkg/config"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)

func NewValidateCmd() *cobra.Command {
Expand Down Expand Up @@ -63,7 +64,11 @@ func NewValidateCmd() *cobra.Command {
}

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
f.Hidden = true
f.Deprecated = "it has no effect and will be removed in a future release"
cmd.PersistentFlags().AddFlag(f)
})
flags.AddFlagSet(config.FileInputFlag())
_ = cmd.MarkFlagRequired("config")

Expand Down
25 changes: 13 additions & 12 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ import (
"time"

"github.com/avast/retry-go"
"github.com/k0sproject/k0s/cmd/internal"
workercmd "github.com/k0sproject/k0s/cmd/worker"
"github.com/k0sproject/k0s/internal/pkg/dir"
"github.com/k0sproject/k0s/internal/pkg/file"
internallog "github.com/k0sproject/k0s/internal/pkg/log"
"github.com/k0sproject/k0s/internal/pkg/sysinfo"
"github.com/k0sproject/k0s/internal/sync/value"
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
Expand Down Expand Up @@ -70,7 +70,10 @@ import (
type command config.CLIOptions

func NewControllerCmd() *cobra.Command {
var ignorePreFlightChecks bool
var (
debugFlags internal.DebugFlags
ignorePreFlightChecks bool
)

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

ctx, cancel := signal.NotifyContext(cmd.Context(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
return c.start(ctx)
return c.start(ctx, debugFlags.IsDebug())
},
}

debugFlags.LongRunning().AddToFlagSet(cmd.PersistentFlags())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.GetControllerFlags())
Expand All @@ -130,7 +131,7 @@ func NewControllerCmd() *cobra.Command {
return cmd
}

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

nodeConfig, err := c.K0sVars.NodeConfig()
Expand Down Expand Up @@ -264,8 +265,8 @@ func (c *command) start(ctx context.Context) error {
nodeComponents.Add(ctx, &cplb.Keepalived{
K0sVars: c.K0sVars,
Config: cplbCfg.Keepalived,
DetailedLogging: c.Debug,
LogConfig: c.Debug,
DetailedLogging: debug,
LogConfig: debug,
KubeConfigPath: c.K0sVars.AdminKubeConfigPath,
APIPort: nodeConfig.Spec.API.Port,
})
Expand Down
4 changes: 2 additions & 2 deletions cmd/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Flags:
-c, --config string config file, use '-' to read the config from stdin (default `+defaultConfigPath+`)
--cri-socket string container runtime socket to use, default to internal containerd. Format: [remote|docker]:[path-to-socket]
--data-dir string Data Directory for k0s. DO NOT CHANGE for an existing setup, things will break! (default `+defaultDataDir+`)
-d, --debug Debug logging (default: false)
-d, --debug Debug logging (implies verbose logging)
--debugListenOn string Http listenOn for Debug pprof handler (default ":6060")
--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)
--enable-cloud-provider Whether or not to enable cloud provider support in kubelet
Expand All @@ -88,6 +88,6 @@ Flags:
--status-socket string Full file path to the socket file. (default: <rundir>/status.sock)
--taints strings Node taints, list of key=value:effect strings
--token-file string Path to the file containing join-token.
-v, --verbose Verbose logging (default: false)
-v, --verbose Verbose logging (default true)
`, out.String())
}
11 changes: 7 additions & 4 deletions cmd/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"errors"
"fmt"

"github.com/k0sproject/k0s/cmd/internal"
"github.com/k0sproject/k0s/pkg/apis/k0s/v1beta1"
"github.com/k0sproject/k0s/pkg/config"

Expand All @@ -28,14 +29,14 @@ import (
)

func NewEtcdCmd() *cobra.Command {
var debugFlags internal.DebugFlags

cmd := &cobra.Command{
Use: "etcd",
Short: "Manage etcd cluster",
Args: cobra.NoArgs,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if err := config.CallParentPersistentPreRun(cmd, args); err != nil {
return err
}
debugFlags.Run(cmd, args)

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

cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
pflags := cmd.PersistentFlags()
debugFlags.AddToFlagSet(pflags)
pflags.AddFlagSet(config.GetPersistentFlagSet())

cmd.AddCommand(etcdLeaveCmd())
cmd.AddCommand(etcdListCmd())
Expand Down
10 changes: 5 additions & 5 deletions cmd/install/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,6 @@ Flags:
-c, --config string config file, use '-' to read the config from stdin (default `+defaultConfigPath+`)
--cri-socket string container runtime socket to use, default to internal containerd. Format: [remote|docker]:[path-to-socket]
--data-dir string Data Directory for k0s. DO NOT CHANGE for an existing setup, things will break! (default `+defaultDataDir+`)
-d, --debug Debug logging (default: false)
--debugListenOn string Http listenOn for Debug pprof handler (default ":6060")
--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)
--enable-cloud-provider Whether or not to enable cloud provider support in kubelet
--enable-dynamic-config enable cluster-wide dynamic config based on custom resource
Expand All @@ -83,10 +81,12 @@ Flags:
--status-socket string Full file path to the socket file. (default: <rundir>/status.sock)
--taints strings Node taints, list of key=value:effect strings
--token-file string Path to the file containing join-token.
-v, --verbose Verbose logging (default: false)

Global Flags:
-e, --env stringArray set environment variable
--force force init script creation
-d, --debug Debug logging (implies verbose logging)
--debugListenOn string Http listenOn for Debug pprof handler (default ":6060")
-e, --env stringArray set environment variable
--force force init script creation
-v, --verbose Verbose logging
`, out.String())
}
16 changes: 11 additions & 5 deletions cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package install

import (
"github.com/k0sproject/k0s/cmd/internal"
"github.com/k0sproject/k0s/pkg/config"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand All @@ -28,16 +29,21 @@ type installFlags struct {
}

func NewInstallCmd() *cobra.Command {
var installFlags installFlags
var (
debugFlags internal.DebugFlags
installFlags installFlags
)

cmd := &cobra.Command{
Use: "install",
Short: "Install k0s on a brand-new system. Must be run as root (or with sudo)",
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
Use: "install",
Short: "Install k0s on a brand-new system. Must be run as root (or with sudo)",
Args: cobra.NoArgs,
PersistentPreRun: debugFlags.Run,
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
}

pflags := cmd.PersistentFlags()
debugFlags.AddToFlagSet(pflags)
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
f.Hidden = true
f.Deprecated = "it has no effect and will be removed in a future release"
Expand Down
Loading
Loading