Skip to content

Commit 363c778

Browse files
committed
kubeadm: use DefValue for the --kubeconfig flag
- Rename FindExistingKubeConfig to GetKubeConfigPath - Cobra supports a DefValue option which can be used to differentiate between the cases where the user set a flag and when a flag was unset, while still adding a default value. Use this in options/generic.go for the kubeconfig flag. - Remove the GetKubeConfigPath() logic from `reset` and `upgrade` as these are node level kubeadm commands. - Default kubeconfig values to "" everywhere where GetKubeConfigPath is used. This allows to search for existing kubeconfig locations.
1 parent a22763b commit 363c778

File tree

10 files changed

+32
-28
lines changed

10 files changed

+32
-28
lines changed

cmd/kubeadm/app/cmd/alpha/certs.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
kubeadmapiv1beta1 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta1"
2525
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
2626
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
27-
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
2827
certsphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/certs"
2928
"k8s.io/kubernetes/cmd/kubeadm/app/phases/certs/renewal"
3029
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
@@ -81,9 +80,7 @@ type renewConfig struct {
8180
}
8281

8382
func getRenewSubCommands() []*cobra.Command {
84-
cfg := &renewConfig{
85-
kubeconfigPath: constants.GetAdminKubeConfigPath(),
86-
}
83+
cfg := &renewConfig{}
8784
// Default values for the cobra help text
8885
kubeadmscheme.Scheme.Default(&cfg.cfg)
8986

@@ -168,7 +165,7 @@ func generateRenewalCommand(cert *certsphase.KubeadmCert, cfg *renewConfig) *cob
168165

169166
func getRenewer(cfg *renewConfig, caCertBaseName string) (renewal.Interface, error) {
170167
if cfg.useAPI {
171-
kubeConfigPath := cmdutil.FindExistingKubeConfig(cfg.kubeconfigPath)
168+
kubeConfigPath := cmdutil.GetKubeConfigPath(cfg.kubeconfigPath)
172169
client, err := kubeconfigutil.ClientSetFromFile(kubeConfigPath)
173170
if err != nil {
174171
return nil, err

cmd/kubeadm/app/cmd/alpha/kubelet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func getKubeletVersion(kubeletVersionStr string) (*version.Version, error) {
130130
// This feature is still in alpha and an experimental state
131131
func newCmdKubeletConfigEnableDynamic() *cobra.Command {
132132
var nodeName, kubeletVersionStr string
133-
kubeConfigFile := constants.GetAdminKubeConfigPath()
133+
var kubeConfigFile string
134134

135135
cmd := &cobra.Command{
136136
Use: "enable-dynamic",
@@ -148,7 +148,7 @@ func newCmdKubeletConfigEnableDynamic() *cobra.Command {
148148
kubeletVersion, err := version.ParseSemantic(kubeletVersionStr)
149149
kubeadmutil.CheckErr(err)
150150

151-
kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
151+
kubeConfigFile = cmdutil.GetKubeConfigPath(kubeConfigFile)
152152
client, err := kubeconfigutil.ClientSetFromFile(kubeConfigFile)
153153
kubeadmutil.CheckErr(err)
154154

cmd/kubeadm/app/cmd/alpha/selfhosting.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,8 @@ func getSelfhostingSubCommand(in io.Reader) *cobra.Command {
7979
// Default values for the cobra help text
8080
kubeadmscheme.Scheme.Default(cfg)
8181

82-
var cfgPath, featureGatesString string
82+
var cfgPath, featureGatesString, kubeConfigFile string
8383
forcePivot, certsInSecrets := false, false
84-
kubeConfigFile := constants.GetAdminKubeConfigPath()
8584

8685
// Creates the UX Command
8786
cmd := &cobra.Command{
@@ -119,7 +118,7 @@ func getSelfhostingSubCommand(in io.Reader) *cobra.Command {
119118
}
120119

121120
// Gets the Kubernetes client
122-
kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
121+
kubeConfigFile = cmdutil.GetKubeConfigPath(kubeConfigFile)
123122
client, err := kubeconfigutil.ClientSetFromFile(kubeConfigFile)
124123
kubeadmutil.CheckErr(err)
125124

cmd/kubeadm/app/cmd/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var (
6262

6363
// NewCmdConfig returns cobra.Command for "kubeadm config" command
6464
func NewCmdConfig(out io.Writer) *cobra.Command {
65-
kubeConfigFile := constants.GetAdminKubeConfigPath()
65+
var kubeConfigFile string
6666

6767
cmd := &cobra.Command{
6868
Use: "config",
@@ -83,7 +83,7 @@ func NewCmdConfig(out io.Writer) *cobra.Command {
8383

8484
options.AddKubeConfigFlag(cmd.PersistentFlags(), &kubeConfigFile)
8585

86-
kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
86+
kubeConfigFile = cmdutil.GetKubeConfigPath(kubeConfigFile)
8787
cmd.AddCommand(NewCmdConfigPrint(out))
8888
cmd.AddCommand(NewCmdConfigMigrate(out))
8989
cmd.AddCommand(NewCmdConfigUpload(out, &kubeConfigFile))

cmd/kubeadm/app/cmd/options/generic.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@ package options
1919
import (
2020
"github.com/spf13/pflag"
2121
cliflag "k8s.io/component-base/cli/flag"
22+
"k8s.io/kubernetes/cmd/kubeadm/app/constants"
2223
)
2324

2425
// AddKubeConfigFlag adds the --kubeconfig flag to the given flagset
2526
func AddKubeConfigFlag(fs *pflag.FlagSet, kubeConfigFile *string) {
26-
fs.StringVar(kubeConfigFile, KubeconfigPath, *kubeConfigFile, "The kubeconfig file to use when talking to the cluster. If the flag is not set, a set of standard locations are searched for an existing KubeConfig file.")
27+
fs.StringVar(kubeConfigFile, KubeconfigPath, *kubeConfigFile, "The kubeconfig file to use when talking to the cluster. If the flag is not set, a set of standard locations can be searched for an existing kubeconfig file.")
28+
// Note that DefValue is the text shown in the terminal and not the default value assigned to the flag
29+
fs.Lookup(KubeconfigPath).DefValue = constants.GetAdminKubeConfigPath()
2730
}
2831

2932
// AddKubeConfigDirFlag adds the --kubeconfig-dir flag to the given flagset

cmd/kubeadm/app/cmd/reset.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ func NewCmdReset(in io.Reader, out io.Writer) *cobra.Command {
6161
ignorePreflightErrorsSet, err := validation.ValidateIgnorePreflightErrors(ignorePreflightErrors)
6262
kubeadmutil.CheckErr(err)
6363

64-
kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
6564
if _, err := os.Stat(kubeConfigFile); !os.IsNotExist(err) {
6665
client, err = getClientset(kubeConfigFile, false)
6766
kubeadmutil.CheckErr(err)
67+
klog.V(1).Infof("[reset] loaded client set from kubeconfig file: %s", kubeConfigFile)
68+
} else {
69+
klog.V(1).Infof("[reset] could not get client set from missing kubeconfig file: %s", kubeConfigFile)
6870
}
6971

7072
if criSocketPath == "" {

cmd/kubeadm/app/cmd/token.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ import (
4242
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
4343
phaseutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/phases"
4444
cmdutil "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util"
45-
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
4645
tokenphase "k8s.io/kubernetes/cmd/kubeadm/app/phases/bootstraptoken/node"
4746
kubeadmutil "k8s.io/kubernetes/cmd/kubeadm/app/util"
4847
"k8s.io/kubernetes/cmd/kubeadm/app/util/apiclient"
@@ -52,7 +51,7 @@ import (
5251

5352
// NewCmdToken returns cobra.Command for token management
5453
func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
55-
kubeConfigFile := kubeadmconstants.GetAdminKubeConfigPath()
54+
var kubeConfigFile string
5655
var dryRun bool
5756
tokenCmd := &cobra.Command{
5857
Use: "token",
@@ -121,7 +120,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
121120
kubeadmutil.CheckErr(err)
122121

123122
klog.V(1).Infoln("[token] getting Clientsets from kubeconfig file")
124-
kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
123+
kubeConfigFile = cmdutil.GetKubeConfigPath(kubeConfigFile)
125124
client, err := getClientset(kubeConfigFile, dryRun)
126125
kubeadmutil.CheckErr(err)
127126

@@ -148,7 +147,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
148147
This command will list all bootstrap tokens for you.
149148
`),
150149
Run: func(tokenCmd *cobra.Command, args []string) {
151-
kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
150+
kubeConfigFile = cmdutil.GetKubeConfigPath(kubeConfigFile)
152151
client, err := getClientset(kubeConfigFile, dryRun)
153152
kubeadmutil.CheckErr(err)
154153

@@ -172,7 +171,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
172171
if len(args) < 1 {
173172
kubeadmutil.CheckErr(errors.Errorf("missing subcommand; 'token delete' is missing token of form %q", bootstrapapi.BootstrapTokenIDPattern))
174173
}
175-
kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
174+
kubeConfigFile = cmdutil.GetKubeConfigPath(kubeConfigFile)
176175
client, err := getClientset(kubeConfigFile, dryRun)
177176
kubeadmutil.CheckErr(err)
178177

cmd/kubeadm/app/cmd/upgrade/upgrade.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func NewCmdUpgrade(out io.Writer) *cobra.Command {
6262
RunE: cmdutil.SubCmdRunE("upgrade"),
6363
}
6464

65-
flags.kubeConfigPath = cmdutil.FindExistingKubeConfig(flags.kubeConfigPath)
6665
cmd.AddCommand(NewCmdApply(flags))
6766
cmd.AddCommand(NewCmdPlan(flags))
6867
cmd.AddCommand(NewCmdDiff(out))

cmd/kubeadm/app/cmd/util/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ go_library(
2020
"//vendor/github.com/pkg/errors:go_default_library",
2121
"//vendor/github.com/spf13/cobra:go_default_library",
2222
"//vendor/github.com/spf13/pflag:go_default_library",
23+
"//vendor/k8s.io/klog:go_default_library",
2324
],
2425
)
2526

cmd/kubeadm/app/cmd/util/cmdutil.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/spf13/pflag"
2323

2424
"k8s.io/client-go/tools/clientcmd"
25+
"k8s.io/klog"
2526
"k8s.io/kubernetes/cmd/kubeadm/app/cmd/options"
2627
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
2728
)
@@ -62,18 +63,21 @@ func ValidateExactArgNumber(args []string, supportedArgs []string) error {
6263
return nil
6364
}
6465

65-
// FindExistingKubeConfig returns the localtion of kubeconfig
66-
func FindExistingKubeConfig(file string) string {
67-
// The user did provide a --kubeconfig flag. Respect that and threat it as an
68-
// explicit path without building a DefaultClientConfigLoadingRules object.
69-
if file != kubeadmconstants.GetAdminKubeConfigPath() {
66+
// GetKubeConfigPath can be used to search for a kubeconfig in standard locations
67+
// if and empty string is passed to the function. If a non-empty string is passed
68+
// the function returns the same string.
69+
func GetKubeConfigPath(file string) string {
70+
// If a value is provided respect that.
71+
if file != "" {
7072
return file
7173
}
72-
// The user did not provide a --kubeconfig flag. Find a config in the standard
73-
// locations using DefaultClientConfigLoadingRules, but also consider the default config path.
74+
// Find a config in the standard locations using DefaultClientConfigLoadingRules,
75+
// but also consider the default config path.
7476
rules := clientcmd.NewDefaultClientConfigLoadingRules()
7577
rules.Precedence = append(rules.Precedence, kubeadmconstants.GetAdminKubeConfigPath())
76-
return rules.GetDefaultFilename()
78+
file = rules.GetDefaultFilename()
79+
klog.V(1).Infof("Using kubeconfig file: %s", file)
80+
return file
7781
}
7882

7983
// AddCRISocketFlag adds the cri-socket flag to the supplied flagSet

0 commit comments

Comments
 (0)