From ebce14b36eac91cbf889e13c7e9f80e5ce1db570 Mon Sep 17 00:00:00 2001 From: Xianglin Gao Date: Thu, 2 Aug 2018 20:59:43 +0800 Subject: [PATCH] Use the Join of Dir and File name instead of DefaultKubeConfig Signed-off-by: Xianglin Gao --- cmd/kubeadm/app/cmd/config.go | 3 ++- cmd/kubeadm/app/cmd/phases/addons.go | 3 ++- cmd/kubeadm/app/cmd/phases/bootstraptoken.go | 3 ++- cmd/kubeadm/app/cmd/phases/markmaster.go | 4 +++- cmd/kubeadm/app/cmd/phases/selfhosting.go | 3 ++- cmd/kubeadm/app/cmd/phases/uploadconfig.go | 3 ++- cmd/kubeadm/app/cmd/token.go | 23 +++++--------------- cmd/kubeadm/app/cmd/upgrade/upgrade.go | 3 ++- cmd/kubeadm/app/cmd/util/BUILD | 1 + cmd/kubeadm/app/cmd/util/cmdutil.go | 17 +++++++++++++++ cmd/kubeadm/app/constants/constants.go | 3 --- cmd/kubeadm/app/constants/constants_test.go | 3 ++- 12 files changed, 40 insertions(+), 29 deletions(-) diff --git a/cmd/kubeadm/app/cmd/config.go b/cmd/kubeadm/app/cmd/config.go index f6e0328e9d4..b515a690421 100644 --- a/cmd/kubeadm/app/cmd/config.go +++ b/cmd/kubeadm/app/cmd/config.go @@ -21,6 +21,7 @@ import ( "fmt" "io" "io/ioutil" + "path/filepath" "strings" "github.com/golang/glog" @@ -80,7 +81,7 @@ func NewCmdConfig(out io.Writer) *cobra.Command { RunE: cmdutil.SubCmdRunE("config"), } - cmd.PersistentFlags().StringVar(&kubeConfigFile, "kubeconfig", kubeadmconstants.DefaultKubeConfig, "The KubeConfig file to use when talking to the cluster.") + cmd.PersistentFlags().StringVar(&kubeConfigFile, "kubeconfig", filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName), "The KubeConfig file to use when talking to the cluster.") cmd.AddCommand(NewCmdConfigPrintDefault(out)) cmd.AddCommand(NewCmdConfigMigrate(out)) diff --git a/cmd/kubeadm/app/cmd/phases/addons.go b/cmd/kubeadm/app/cmd/phases/addons.go index 840ee3dad09..252538e6318 100644 --- a/cmd/kubeadm/app/cmd/phases/addons.go +++ b/cmd/kubeadm/app/cmd/phases/addons.go @@ -17,6 +17,7 @@ limitations under the License. package phases import ( + "path/filepath" "strings" "github.com/golang/glog" @@ -141,7 +142,7 @@ func getAddonsSubCommands() []*cobra.Command { } // Add flags to the command - cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", kubeadmconstants.DefaultKubeConfig, "The KubeConfig file to use when talking to the cluster") + cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName), "The KubeConfig file to use when talking to the cluster") cmd.Flags().StringVar(&cfgPath, "config", cfgPath, "Path to a kubeadm config file. WARNING: Usage of a configuration file is experimental") cmd.Flags().StringVar(&cfg.KubernetesVersion, "kubernetes-version", cfg.KubernetesVersion, `Choose a specific Kubernetes version for the control plane`) cmd.Flags().StringVar(&cfg.ImageRepository, "image-repository", cfg.ImageRepository, `Choose a container registry to pull control plane images from`) diff --git a/cmd/kubeadm/app/cmd/phases/bootstraptoken.go b/cmd/kubeadm/app/cmd/phases/bootstraptoken.go index e3eebd05394..23293095db2 100644 --- a/cmd/kubeadm/app/cmd/phases/bootstraptoken.go +++ b/cmd/kubeadm/app/cmd/phases/bootstraptoken.go @@ -18,6 +18,7 @@ package phases import ( "fmt" + "path/filepath" "github.com/golang/glog" "github.com/spf13/cobra" @@ -95,7 +96,7 @@ func NewCmdBootstrapToken() *cobra.Command { Aliases: []string{"bootstraptoken"}, } - cmd.PersistentFlags().StringVar(&kubeConfigFile, "kubeconfig", kubeadmconstants.DefaultKubeConfig, "The KubeConfig file to use when talking to the cluster") + cmd.PersistentFlags().StringVar(&kubeConfigFile, "kubeconfig", filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName), "The KubeConfig file to use when talking to the cluster") // Add subcommands cmd.AddCommand(NewSubCmdBootstrapTokenAll(&kubeConfigFile)) diff --git a/cmd/kubeadm/app/cmd/phases/markmaster.go b/cmd/kubeadm/app/cmd/phases/markmaster.go index 804496fe649..a1dec564d9b 100644 --- a/cmd/kubeadm/app/cmd/phases/markmaster.go +++ b/cmd/kubeadm/app/cmd/phases/markmaster.go @@ -17,6 +17,8 @@ limitations under the License. package phases import ( + "path/filepath" + "github.com/spf13/cobra" kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme" @@ -81,7 +83,7 @@ func NewCmdMarkMaster() *cobra.Command { }, } - cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", kubeadmconstants.DefaultKubeConfig, "The KubeConfig file to use when talking to the cluster") + cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName), "The KubeConfig file to use when talking to the cluster") cmd.Flags().StringVar(&cfgPath, "config", cfgPath, "Path to kubeadm config file. WARNING: Usage of a configuration file is experimental") cmd.Flags().StringVar(&cfg.NodeRegistration.Name, "node-name", cfg.NodeRegistration.Name, `The node name to which label and taints should apply`) diff --git a/cmd/kubeadm/app/cmd/phases/selfhosting.go b/cmd/kubeadm/app/cmd/phases/selfhosting.go index 40943aaf570..0a0d9b38eca 100644 --- a/cmd/kubeadm/app/cmd/phases/selfhosting.go +++ b/cmd/kubeadm/app/cmd/phases/selfhosting.go @@ -18,6 +18,7 @@ package phases import ( "os" + "path/filepath" "strings" "time" @@ -123,7 +124,7 @@ func getSelfhostingSubCommand() *cobra.Command { // flags that are not bound to the configuration object // Note: All flags that are not bound to the cfg object should be whitelisted in cmd/kubeadm/app/apis/kubeadm/validation/validation.go - cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", kubeadmconstants.DefaultKubeConfig, "The KubeConfig file to use when talking to the cluster") + cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName), "The KubeConfig file to use when talking to the cluster") return cmd } diff --git a/cmd/kubeadm/app/cmd/phases/uploadconfig.go b/cmd/kubeadm/app/cmd/phases/uploadconfig.go index 5d473de3b19..4ff0bf2b4a2 100644 --- a/cmd/kubeadm/app/cmd/phases/uploadconfig.go +++ b/cmd/kubeadm/app/cmd/phases/uploadconfig.go @@ -18,6 +18,7 @@ package phases import ( "fmt" + "path/filepath" "github.com/spf13/cobra" @@ -77,7 +78,7 @@ func NewCmdUploadConfig() *cobra.Command { }, } - cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", kubeadmconstants.DefaultKubeConfig, "The KubeConfig file to use when talking to the cluster") + cmd.Flags().StringVar(&kubeConfigFile, "kubeconfig", filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName), "The KubeConfig file to use when talking to the cluster") cmd.Flags().StringVar(&cfgPath, "config", "", "Path to a kubeadm config file. WARNING: Usage of a configuration file is experimental") return cmd diff --git a/cmd/kubeadm/app/cmd/token.go b/cmd/kubeadm/app/cmd/token.go index be111df0359..b35c5cb17e8 100644 --- a/cmd/kubeadm/app/cmd/token.go +++ b/cmd/kubeadm/app/cmd/token.go @@ -20,6 +20,7 @@ import ( "fmt" "io" "os" + "path/filepath" "strings" "text/tabwriter" "time" @@ -34,7 +35,6 @@ import ( clientset "k8s.io/client-go/kubernetes" bootstrapapi "k8s.io/client-go/tools/bootstrap/token/api" bootstraputil "k8s.io/client-go/tools/bootstrap/token/util" - "k8s.io/client-go/tools/clientcmd" kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme" kubeadmapiv1alpha3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha3" @@ -85,7 +85,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command { } tokenCmd.PersistentFlags().StringVar(&kubeConfigFile, - "kubeconfig", kubeadmconstants.DefaultKubeConfig, "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") + "kubeconfig", filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName), "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") tokenCmd.PersistentFlags().BoolVar(&dryRun, "dry-run", dryRun, "Whether to enable dry-run mode or not") @@ -122,7 +122,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command { kubeadmutil.CheckErr(err) glog.V(1).Infoln("[token] getting Clientsets from KubeConfig file") - kubeConfigFile = findExistingKubeConfig(kubeConfigFile) + kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile) client, err := getClientset(kubeConfigFile, dryRun) kubeadmutil.CheckErr(err) @@ -149,7 +149,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command { This command will list all bootstrap tokens for you. `), Run: func(tokenCmd *cobra.Command, args []string) { - kubeConfigFile = findExistingKubeConfig(kubeConfigFile) + kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile) client, err := getClientset(kubeConfigFile, dryRun) kubeadmutil.CheckErr(err) @@ -173,7 +173,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command { if len(args) < 1 { kubeadmutil.CheckErr(fmt.Errorf("missing subcommand; 'token delete' is missing token of form %q", bootstrapapi.BootstrapTokenIDPattern)) } - kubeConfigFile = findExistingKubeConfig(kubeConfigFile) + kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile) client, err := getClientset(kubeConfigFile, dryRun) kubeadmutil.CheckErr(err) @@ -356,16 +356,3 @@ func getClientset(file string, dryRun bool) (clientset.Interface, error) { } return kubeconfigutil.ClientSetFromFile(file) } - -func findExistingKubeConfig(file string) string { - // The user did provide a --kubeconfig flag. Respect that and threat it as an - // explicit path without building a DefaultClientConfigLoadingRules object. - if file != kubeadmconstants.DefaultKubeConfig { - return file - } - // The user did not provide a --kubeconfig flag. Find a config in the standard - // locations using DefaultClientConfigLoadingRules, but also consider `defaultKubeConfig`. - rules := clientcmd.NewDefaultClientConfigLoadingRules() - rules.Precedence = append(rules.Precedence, kubeadmconstants.DefaultKubeConfig) - return rules.GetDefaultFilename() -} diff --git a/cmd/kubeadm/app/cmd/upgrade/upgrade.go b/cmd/kubeadm/app/cmd/upgrade/upgrade.go index 812e5acdec6..31b85e7315a 100644 --- a/cmd/kubeadm/app/cmd/upgrade/upgrade.go +++ b/cmd/kubeadm/app/cmd/upgrade/upgrade.go @@ -18,6 +18,7 @@ package upgrade import ( "io" + "path/filepath" "strings" "github.com/spf13/cobra" @@ -46,7 +47,7 @@ type applyPlanFlags struct { // NewCmdUpgrade returns the cobra command for `kubeadm upgrade` func NewCmdUpgrade(out io.Writer) *cobra.Command { flags := &applyPlanFlags{ - kubeConfigPath: kubeadmconstants.DefaultKubeConfig, + kubeConfigPath: filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName), cfgPath: "", featureGatesString: "", allowExperimentalUpgrades: false, diff --git a/cmd/kubeadm/app/cmd/util/BUILD b/cmd/kubeadm/app/cmd/util/BUILD index db04f0dfda9..63418fadf02 100644 --- a/cmd/kubeadm/app/cmd/util/BUILD +++ b/cmd/kubeadm/app/cmd/util/BUILD @@ -10,6 +10,7 @@ go_library( importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util", visibility = ["//visibility:public"], deps = [ + "//cmd/kubeadm/app/constants:go_default_library", "//cmd/kubeadm/app/util/kubeconfig:go_default_library", "//cmd/kubeadm/app/util/pubkeypin:go_default_library", "//pkg/util/normalizer:go_default_library", diff --git a/cmd/kubeadm/app/cmd/util/cmdutil.go b/cmd/kubeadm/app/cmd/util/cmdutil.go index 87dcdff67f0..cf9042f4ae8 100644 --- a/cmd/kubeadm/app/cmd/util/cmdutil.go +++ b/cmd/kubeadm/app/cmd/util/cmdutil.go @@ -18,8 +18,11 @@ package util import ( "fmt" + "path/filepath" "github.com/spf13/cobra" + "k8s.io/client-go/tools/clientcmd" + kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants" ) // SubCmdRunE returns a function that handles a case where a subcommand must be specified @@ -57,3 +60,17 @@ func ValidateExactArgNumber(args []string, supportedArgs []string) error { } return nil } + +// FindExistingKubeConfig returns the localtion of kubeconfig +func FindExistingKubeConfig(file string) string { + // The user did provide a --kubeconfig flag. Respect that and threat it as an + // explicit path without building a DefaultClientConfigLoadingRules object. + if file != filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName) { + return file + } + // The user did not provide a --kubeconfig flag. Find a config in the standard + // locations using DefaultClientConfigLoadingRules, but also consider `defaultKubeConfig`. + rules := clientcmd.NewDefaultClientConfigLoadingRules() + rules.Precedence = append(rules.Precedence, filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName)) + return rules.GetDefaultFilename() +} diff --git a/cmd/kubeadm/app/constants/constants.go b/cmd/kubeadm/app/constants/constants.go index d22dd35c878..b48cd4effd9 100644 --- a/cmd/kubeadm/app/constants/constants.go +++ b/cmd/kubeadm/app/constants/constants.go @@ -302,9 +302,6 @@ const ( // YAMLDocumentSeparator is the separator for YAML documents // TODO: Find a better place for this constant YAMLDocumentSeparator = "---\n" - - // DefaultKubeConfig is the default localtion of KubeConfig - DefaultKubeConfig = "/etc/kubernetes/admin.conf" ) var ( diff --git a/cmd/kubeadm/app/constants/constants_test.go b/cmd/kubeadm/app/constants/constants_test.go index 09bf2b1897f..f2bdd5ae9a9 100644 --- a/cmd/kubeadm/app/constants/constants_test.go +++ b/cmd/kubeadm/app/constants/constants_test.go @@ -18,6 +18,7 @@ package constants import ( "fmt" + "path/filepath" "strings" "testing" @@ -38,7 +39,7 @@ func TestGetStaticPodDirectory(t *testing.T) { } func TestGetAdminKubeConfigPath(t *testing.T) { - expected := DefaultKubeConfig + expected := filepath.Join(KubernetesDir, AdminKubeConfigFileName) actual := GetAdminKubeConfigPath() if actual != expected {