Use the Join of Dir and File name instead of DefaultKubeConfig

Signed-off-by: Xianglin Gao <xianglin.gxl@alibaba-inc.com>
This commit is contained in:
Xianglin Gao 2018-08-02 20:59:43 +08:00
parent 866631fab1
commit ebce14b36e
12 changed files with 40 additions and 29 deletions

View File

@ -21,6 +21,7 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"path/filepath"
"strings" "strings"
"github.com/golang/glog" "github.com/golang/glog"
@ -80,7 +81,7 @@ func NewCmdConfig(out io.Writer) *cobra.Command {
RunE: cmdutil.SubCmdRunE("config"), 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(NewCmdConfigPrintDefault(out))
cmd.AddCommand(NewCmdConfigMigrate(out)) cmd.AddCommand(NewCmdConfigMigrate(out))

View File

@ -17,6 +17,7 @@ limitations under the License.
package phases package phases
import ( import (
"path/filepath"
"strings" "strings"
"github.com/golang/glog" "github.com/golang/glog"
@ -141,7 +142,7 @@ func getAddonsSubCommands() []*cobra.Command {
} }
// Add flags to the 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(&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.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`) cmd.Flags().StringVar(&cfg.ImageRepository, "image-repository", cfg.ImageRepository, `Choose a container registry to pull control plane images from`)

View File

@ -18,6 +18,7 @@ package phases
import ( import (
"fmt" "fmt"
"path/filepath"
"github.com/golang/glog" "github.com/golang/glog"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -95,7 +96,7 @@ func NewCmdBootstrapToken() *cobra.Command {
Aliases: []string{"bootstraptoken"}, 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 // Add subcommands
cmd.AddCommand(NewSubCmdBootstrapTokenAll(&kubeConfigFile)) cmd.AddCommand(NewSubCmdBootstrapTokenAll(&kubeConfigFile))

View File

@ -17,6 +17,8 @@ limitations under the License.
package phases package phases
import ( import (
"path/filepath"
"github.com/spf13/cobra" "github.com/spf13/cobra"
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme" 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(&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`) cmd.Flags().StringVar(&cfg.NodeRegistration.Name, "node-name", cfg.NodeRegistration.Name, `The node name to which label and taints should apply`)

View File

@ -18,6 +18,7 @@ package phases
import ( import (
"os" "os"
"path/filepath"
"strings" "strings"
"time" "time"
@ -123,7 +124,7 @@ func getSelfhostingSubCommand() *cobra.Command {
// flags that are not bound to the configuration object // 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 // 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 return cmd
} }

View File

@ -18,6 +18,7 @@ package phases
import ( import (
"fmt" "fmt"
"path/filepath"
"github.com/spf13/cobra" "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") cmd.Flags().StringVar(&cfgPath, "config", "", "Path to a kubeadm config file. WARNING: Usage of a configuration file is experimental")
return cmd return cmd

View File

@ -20,6 +20,7 @@ import (
"fmt" "fmt"
"io" "io"
"os" "os"
"path/filepath"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
"time" "time"
@ -34,7 +35,6 @@ import (
clientset "k8s.io/client-go/kubernetes" clientset "k8s.io/client-go/kubernetes"
bootstrapapi "k8s.io/client-go/tools/bootstrap/token/api" bootstrapapi "k8s.io/client-go/tools/bootstrap/token/api"
bootstraputil "k8s.io/client-go/tools/bootstrap/token/util" bootstraputil "k8s.io/client-go/tools/bootstrap/token/util"
"k8s.io/client-go/tools/clientcmd"
kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm" kubeadmapi "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm"
kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme" kubeadmscheme "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/scheme"
kubeadmapiv1alpha3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1alpha3" 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, 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, tokenCmd.PersistentFlags().BoolVar(&dryRun,
"dry-run", dryRun, "Whether to enable dry-run mode or not") "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) kubeadmutil.CheckErr(err)
glog.V(1).Infoln("[token] getting Clientsets from KubeConfig file") glog.V(1).Infoln("[token] getting Clientsets from KubeConfig file")
kubeConfigFile = findExistingKubeConfig(kubeConfigFile) kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
client, err := getClientset(kubeConfigFile, dryRun) client, err := getClientset(kubeConfigFile, dryRun)
kubeadmutil.CheckErr(err) 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. This command will list all bootstrap tokens for you.
`), `),
Run: func(tokenCmd *cobra.Command, args []string) { Run: func(tokenCmd *cobra.Command, args []string) {
kubeConfigFile = findExistingKubeConfig(kubeConfigFile) kubeConfigFile = cmdutil.FindExistingKubeConfig(kubeConfigFile)
client, err := getClientset(kubeConfigFile, dryRun) client, err := getClientset(kubeConfigFile, dryRun)
kubeadmutil.CheckErr(err) kubeadmutil.CheckErr(err)
@ -173,7 +173,7 @@ func NewCmdToken(out io.Writer, errW io.Writer) *cobra.Command {
if len(args) < 1 { if len(args) < 1 {
kubeadmutil.CheckErr(fmt.Errorf("missing subcommand; 'token delete' is missing token of form %q", bootstrapapi.BootstrapTokenIDPattern)) 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) client, err := getClientset(kubeConfigFile, dryRun)
kubeadmutil.CheckErr(err) kubeadmutil.CheckErr(err)
@ -356,16 +356,3 @@ func getClientset(file string, dryRun bool) (clientset.Interface, error) {
} }
return kubeconfigutil.ClientSetFromFile(file) 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()
}

View File

@ -18,6 +18,7 @@ package upgrade
import ( import (
"io" "io"
"path/filepath"
"strings" "strings"
"github.com/spf13/cobra" "github.com/spf13/cobra"
@ -46,7 +47,7 @@ type applyPlanFlags struct {
// NewCmdUpgrade returns the cobra command for `kubeadm upgrade` // NewCmdUpgrade returns the cobra command for `kubeadm upgrade`
func NewCmdUpgrade(out io.Writer) *cobra.Command { func NewCmdUpgrade(out io.Writer) *cobra.Command {
flags := &applyPlanFlags{ flags := &applyPlanFlags{
kubeConfigPath: kubeadmconstants.DefaultKubeConfig, kubeConfigPath: filepath.Join(kubeadmconstants.KubernetesDir, kubeadmconstants.AdminKubeConfigFileName),
cfgPath: "", cfgPath: "",
featureGatesString: "", featureGatesString: "",
allowExperimentalUpgrades: false, allowExperimentalUpgrades: false,

View File

@ -10,6 +10,7 @@ go_library(
importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util", importpath = "k8s.io/kubernetes/cmd/kubeadm/app/cmd/util",
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
deps = [ deps = [
"//cmd/kubeadm/app/constants:go_default_library",
"//cmd/kubeadm/app/util/kubeconfig:go_default_library", "//cmd/kubeadm/app/util/kubeconfig:go_default_library",
"//cmd/kubeadm/app/util/pubkeypin:go_default_library", "//cmd/kubeadm/app/util/pubkeypin:go_default_library",
"//pkg/util/normalizer:go_default_library", "//pkg/util/normalizer:go_default_library",

View File

@ -18,8 +18,11 @@ package util
import ( import (
"fmt" "fmt"
"path/filepath"
"github.com/spf13/cobra" "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 // 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 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()
}

View File

@ -302,9 +302,6 @@ const (
// YAMLDocumentSeparator is the separator for YAML documents // YAMLDocumentSeparator is the separator for YAML documents
// TODO: Find a better place for this constant // TODO: Find a better place for this constant
YAMLDocumentSeparator = "---\n" YAMLDocumentSeparator = "---\n"
// DefaultKubeConfig is the default localtion of KubeConfig
DefaultKubeConfig = "/etc/kubernetes/admin.conf"
) )
var ( var (

View File

@ -18,6 +18,7 @@ package constants
import ( import (
"fmt" "fmt"
"path/filepath"
"strings" "strings"
"testing" "testing"
@ -38,7 +39,7 @@ func TestGetStaticPodDirectory(t *testing.T) {
} }
func TestGetAdminKubeConfigPath(t *testing.T) { func TestGetAdminKubeConfigPath(t *testing.T) {
expected := DefaultKubeConfig expected := filepath.Join(KubernetesDir, AdminKubeConfigFileName)
actual := GetAdminKubeConfigPath() actual := GetAdminKubeConfigPath()
if actual != expected { if actual != expected {