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

Only use persistent flags if appropriate #5423

Merged
merged 2 commits into from
Jan 14, 2025
Merged
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
12 changes: 10 additions & 2 deletions cmd/airgap/airgap.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ func NewAirgapCmd() *cobra.Command {
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
}

var deprecatedFlags pflag.FlagSet
deprecatedFlags.AddFlagSet(config.GetPersistentFlagSet())
deprecatedFlags.AddFlagSet(config.FileInputFlag())
deprecatedFlags.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)
})

cmd.AddCommand(NewAirgapListImagesCmd())
cmd.PersistentFlags().AddFlagSet(config.FileInputFlag())
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

return cmd
}
9 changes: 6 additions & 3 deletions cmd/airgap/listimages.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ func NewAirgapListImagesCmd() *cobra.Command {
return nil
},
}
cmd.Flags().AddFlagSet(config.FileInputFlag())
cmd.Flags().BoolVar(&all, "all", false, "include all images, even if they are not used in the current configuration")
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.FileInputFlag())
flags.BoolVar(&all, "all", false, "include all images, even if they are not used in the current configuration")

return cmd
}
4 changes: 3 additions & 1 deletion cmd/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ func NewAPICmd() *cobra.Command {
return (&command{CLIOptions: opts}).start()
},
}
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

cmd.Flags().AddFlagSet(config.GetPersistentFlagSet())

return cmd
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/backup/backup_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,11 @@ func NewBackupCmd() *cobra.Command {
return c.backup(savePath, cmd.OutOrStdout())
},
}
cmd.Flags().StringVar(&savePath, "save-path", "", "destination directory path for backup assets, use '-' for stdout")
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.StringVar(&savePath, "save-path", "", "destination directory path for backup assets, use '-' for stdout")

return cmd
}

Expand Down
7 changes: 5 additions & 2 deletions cmd/config/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ func NewCreateCmd() *cobra.Command {
return err
},
}
cmd.Flags().BoolVar(&includeImages, "include-images", false, "include the default images in the output")
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.BoolVar(&includeImages, "include-images", false, "include the default images in the output")

return cmd
}
4 changes: 3 additions & 1 deletion cmd/config/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func NewEditCmd() *cobra.Command {
return reExecKubectl(cmd, "-n", "kube-system", "edit", "clusterconfig", "k0s")
},
}
cmd.PersistentFlags().AddFlagSet(config.GetKubeCtlFlagSet())

cmd.Flags().AddFlagSet(config.GetKubeCtlFlagSet())

return cmd
}
7 changes: 5 additions & 2 deletions cmd/config/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ func NewStatusCmd() *cobra.Command {
return reExecKubectl(cmd, args...)
},
}
cmd.PersistentFlags().AddFlagSet(config.GetKubeCtlFlagSet())
cmd.Flags().StringVarP(&outputFormat, "output", "o", "", "Output format. Must be one of yaml|json")

flags := cmd.Flags()
flags.AddFlagSet(config.GetKubeCtlFlagSet())
flags.StringVarP(&outputFormat, "output", "o", "", "Output format. Must be one of yaml|json")

return cmd
}
6 changes: 4 additions & 2 deletions cmd/config/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ func NewValidateCmd() *cobra.Command {
},
}

cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
cmd.Flags().AddFlagSet(config.FileInputFlag())
flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.FileInputFlag())
_ = cmd.MarkFlagRequired("config")

return cmd
}
11 changes: 6 additions & 5 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,12 @@ func NewControllerCmd() *cobra.Command {
},
}

// append flags
cmd.Flags().BoolVar(&ignorePreFlightChecks, "ignore-pre-flight-checks", false, "continue even if pre-flight checks fail")
cmd.Flags().AddFlagSet(config.GetPersistentFlagSet())
cmd.PersistentFlags().AddFlagSet(config.GetControllerFlags())
cmd.PersistentFlags().AddFlagSet(config.GetWorkerFlags())
flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.GetControllerFlags())
flags.AddFlagSet(config.GetWorkerFlags())
flags.BoolVar(&ignorePreFlightChecks, "ignore-pre-flight-checks", false, "continue even if pre-flight checks fail")

return cmd
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/etcd/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,11 @@ func NewEtcdCmd() *cobra.Command {
},
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
}

cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

cmd.AddCommand(etcdLeaveCmd())
cmd.AddCommand(etcdListCmd())
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

return cmd
}
5 changes: 3 additions & 2 deletions cmd/etcd/leave.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,14 @@ func etcdLeaveCmd() *cobra.Command {
},
}

cmd.Flags().AddFlag(&pflag.Flag{
flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlag(&pflag.Flag{
Name: "peer-address",
Usage: "etcd peer address to remove (default <this node's peer address>)",
Value: (*ipOrDNSName)(&peerAddressArg),
})

cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
return cmd
}

Expand Down
4 changes: 3 additions & 1 deletion cmd/etcd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func etcdListCmd() *cobra.Command {
return json.NewEncoder(cmd.OutOrStdout()).Encode(map[string]interface{}{"members": members})
},
}
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

cmd.Flags().AddFlagSet(config.GetPersistentFlagSet())

return cmd
}
10 changes: 6 additions & 4 deletions cmd/install/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ With the controller subcommand you can setup a single node cluster by running:
return nil
},
}
// append flags
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
cmd.Flags().AddFlagSet(config.GetControllerFlags())
cmd.Flags().AddFlagSet(config.GetWorkerFlags())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.GetControllerFlags())
flags.AddFlagSet(config.GetWorkerFlags())

return cmd
}
13 changes: 9 additions & 4 deletions cmd/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package install

import (
"github.com/k0sproject/k0s/pkg/config"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
)
Expand All @@ -38,11 +37,17 @@ func NewInstallCmd() *cobra.Command {
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
}

pflags := cmd.PersistentFlags()
config.GetPersistentFlagSet().VisitAll(func(f *pflag.Flag) {
f.Hidden = true
f.Deprecated = "it has no effect and will be removed in a future release"
pflags.AddFlag(f)
})
pflags.BoolVar(&installFlags.force, "force", false, "force init script creation")
pflags.StringArrayVarP(&installFlags.envVars, "env", "e", nil, "set environment variable")

cmd.AddCommand(installWorkerCmd(&installFlags))
addPlatformSpecificCommands(cmd, &installFlags)

cmd.PersistentFlags().BoolVar(&installFlags.force, "force", false, "force init script creation")
cmd.PersistentFlags().StringArrayVarP(&installFlags.envVars, "env", "e", nil, "set environment variable")
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
return cmd
}
7 changes: 4 additions & 3 deletions cmd/install/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ Windows flags like "--api-server", "--cidr-range" and "--cluster-dns" will be ig
return nil
},
}
// append flags
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
cmd.PersistentFlags().AddFlagSet(config.GetWorkerFlags())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.GetWorkerFlags())

return cmd
}
4 changes: 3 additions & 1 deletion cmd/kubeconfig/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func kubeConfigAdminCmd() *cobra.Command {
return err
},
}
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

cmd.Flags().AddFlagSet(config.GetPersistentFlagSet())

return cmd
}
8 changes: 5 additions & 3 deletions cmd/kubeconfig/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@ Note: A certificate once signed cannot be revoked for a particular user`,
},
}

cmd.Flags().AddFlagSet(config.FileInputFlag())
cmd.Flags().StringVar(&groups, "groups", "", "Specify groups")
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.FileInputFlag())
flags.StringVar(&groups, "groups", "", "Specify groups")

return cmd
}

Expand Down
9 changes: 8 additions & 1 deletion cmd/kubeconfig/kubeconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,15 @@ func NewKubeConfigCmd() *cobra.Command {
Args: cobra.NoArgs,
RunE: func(*cobra.Command, []string) error { return pflag.ErrHelp }, // Enforce arg validation
}

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)
})

cmd.AddCommand(kubeconfigCreateCmd())
cmd.AddCommand(kubeConfigAdminCmd())
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

return cmd
}
2 changes: 1 addition & 1 deletion cmd/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func NewResetCmd() *cobra.Command {
return c.reset()
},
}
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.AddFlagSet(config.GetCriSocketFlag())
flags.AddFlagSet(config.FileInputFlag())
flags.String("kubelet-root-dir", "", "Kubelet root directory for k0s")
Expand Down
19 changes: 4 additions & 15 deletions cmd/restore/restore_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -62,14 +61,10 @@ func NewRestoreCmd() *cobra.Command {
},
}

cwd, err := os.Getwd()
if err != nil {
logrus.Fatal("failed to get local path")
}
flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.StringVar(&restoredConfigPath, "config-out", "", "Specify desired name and full path for the restored k0s.yaml file (default: k0s_<archive timestamp>.yaml")

restoredConfigPathDescription := fmt.Sprintf("Specify desired name and full path for the restored k0s.yaml file (default: %s/k0s_<archive timestamp>.yaml", cwd)
cmd.Flags().StringVar(&restoredConfigPath, "config-out", "", restoredConfigPathDescription)
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
return cmd
}

Expand Down Expand Up @@ -113,11 +108,5 @@ func defaultConfigFileOutputPath(archivePath string) string {
f := filepath.Base(archivePath)
nameWithoutExt := strings.Split(f, ".")[0]
fName := strings.TrimPrefix(nameWithoutExt, "k0s_backup_")
restoredFileName := fmt.Sprintf("k0s_%s.yaml", fName)

cwd, err := os.Getwd()
if err != nil {
return ""
}
return path.Join(cwd, restoredFileName)
return fmt.Sprintf("k0s_%s.yaml", fName)
}
17 changes: 14 additions & 3 deletions cmd/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

func NewStatusCmd() *cobra.Command {
var output string

cmd := &cobra.Command{
Use: "status",
Short: "Get k0s instance status information",
Expand All @@ -56,14 +57,18 @@ func NewStatusCmd() *cobra.Command {
},
}

cmd.PersistentFlags().StringVarP(&output, "out", "o", "", "sets type of output to json or yaml")
cmd.PersistentFlags().String("status-socket", "", "Full file path to the socket file. (default: <rundir>/status.sock)")

cmd.AddCommand(NewStatusSubCmdComponents())

cmd.Flags().StringVarP(&output, "out", "o", "", "sets type of output to json or yaml")

return cmd
}

func NewStatusSubCmdComponents() *cobra.Command {
var maxCount int

cmd := &cobra.Command{
Use: "components",
Short: "Get k0s instance component status information",
Expand All @@ -87,9 +92,15 @@ func NewStatusSubCmdComponents() *cobra.Command {
return nil
},
}
cmd.Flags().IntVar(&maxCount, "max-count", 1, "how many latest probes to show")
return cmd

flags := cmd.Flags()
flags.IntVar(&maxCount, "max-count", 1, "how many latest probes to show")
flags.StringP("out", "o", "", "")
outFlag := flags.Lookup("out")
outFlag.Hidden = true
outFlag.Deprecated = "it has no effect and will be removed in a future release"

return cmd
}

func printStatus(w io.Writer, status *status.K0sStatus, output string) {
Expand Down
11 changes: 6 additions & 5 deletions cmd/token/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,12 @@ k0s token create --role worker --expiry 10m //sets expiration time to 10 minute
return nil
},
}
// append flags
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())
cmd.Flags().StringVar(&tokenExpiry, "expiry", "0s", "Expiration time of the token. Format 1.5h, 2h45m or 300ms.")
cmd.Flags().StringVar(&createTokenRole, "role", "worker", "Either worker or controller")
cmd.Flags().BoolVar(&waitCreate, "wait", false, "wait forever (default false)")

flags := cmd.Flags()
flags.AddFlagSet(config.GetPersistentFlagSet())
flags.StringVar(&tokenExpiry, "expiry", "0s", "Expiration time of the token. Format 1.5h, 2h45m or 300ms.")
flags.StringVar(&createTokenRole, "role", "worker", "Either worker or controller")
flags.BoolVar(&waitCreate, "wait", false, "wait forever (default false)")

return cmd
}
Expand Down
4 changes: 3 additions & 1 deletion cmd/token/invalidate.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func tokenInvalidateCmd() *cobra.Command {
return nil
},
}
cmd.PersistentFlags().AddFlagSet(config.GetPersistentFlagSet())

cmd.Flags().AddFlagSet(config.GetPersistentFlagSet())

return cmd
}
Loading
Loading