Skip to content

Commit

Permalink
Merge pull request #1482 from lonelyCZ/automated-cherry-pick-of-#1437…
Browse files Browse the repository at this point in the history
…-upstream-release-1.0

Automated cherry pick of #1437: fix `karmadactl init` can not read KUBECONFIG environment variable issue
  • Loading branch information
karmada-bot committed Mar 15, 2022
2 parents 2eb9285 + 469b32e commit f5c0940
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
11 changes: 1 addition & 10 deletions pkg/karmadactl/cmdinit/cmdinit.go
Expand Up @@ -3,8 +3,6 @@ package cmdinit
import (
"fmt"
"io"
"os"
"path/filepath"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -46,7 +44,7 @@ func NewCmdInit(cmdOut io.Writer, parentCommand string) *cobra.Command {
// Kubernetes
flags.StringVarP(&opts.Namespace, "namespace", "n", "karmada-system", "Kubernetes namespace")
flags.StringVar(&opts.StorageClassesName, "storage-classes-name", "", "Kubernetes StorageClasses Name")
flags.StringVar(&opts.KubeConfig, "kubeconfig", filepath.Join(homeDir(), ".kube", "config"), "absolute path to the kubeconfig file")
flags.StringVar(&opts.KubeConfig, "kubeconfig", "", "absolute path to the kubeconfig file")
// etcd
flags.StringVarP(&opts.EtcdStorageMode, "etcd-storage-mode", "", "emptyDir",
"etcd data storage mode(emptyDir,hostPath,PVC). value is PVC, specify --storage-classes-name")
Expand Down Expand Up @@ -109,10 +107,3 @@ func getInitExample(parentCommand string) string {
`
return initExample
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}
26 changes: 21 additions & 5 deletions pkg/karmadactl/cmdinit/kubernetes/deploy.go
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"os"
"path"
"path/filepath"
"strings"
"time"

Expand All @@ -34,7 +35,9 @@ var certList = []string{
options.FrontProxyClientCertAndKeyName,
}

//CommandInitOption holds all flags options for init.
var defaultKubeConfig = filepath.Join(homeDir(), ".kube", "config")

// CommandInitOption holds all flags options for init.
type CommandInitOption struct {
EtcdImage string
EtcdReplicas int32
Expand Down Expand Up @@ -71,10 +74,6 @@ type CommandInitOption struct {

// Validate Check that there are enough flags to run the command.
func (i *CommandInitOption) Validate(parentCommand string) error {
if !utils.IsExist(i.KubeConfig) {
return fmt.Errorf("kubeconfig file does not exist.absolute path to the kubeconfig file")
}

if i.EtcdStorageMode == "hostPath" && i.EtcdHostDataPath == "" {
return fmt.Errorf("when etcd storage mode is hostPath, dataPath is not empty. See '%s init --help'", parentCommand)
}
Expand All @@ -96,6 +95,16 @@ func (i *CommandInitOption) Validate(parentCommand string) error {

// Complete Initialize k8s client
func (i *CommandInitOption) Complete() error {
// check config path of host kubernetes cluster
if i.KubeConfig == "" {
env := os.Getenv("KUBECONFIG")
if env != "" {
i.KubeConfig = env
} else {
i.KubeConfig = defaultKubeConfig
}
}

restConfig, err := utils.RestConfig(i.KubeConfig)
if err != nil {
return err
Expand Down Expand Up @@ -436,3 +445,10 @@ func (i *CommandInitOption) RunInit(_ io.Writer, parentCommand string) error {
utils.GenExamples(i.KarmadaDataPath, parentCommand)
return nil
}

func homeDir() string {
if h := os.Getenv("HOME"); h != "" {
return h
}
return os.Getenv("USERPROFILE") // windows
}

0 comments on commit f5c0940

Please sign in to comment.