Skip to content

Commit

Permalink
Merge pull request #1437 from lonelyCZ/init-kubeconfig
Browse files Browse the repository at this point in the history
Fix `karmadactl init` can not read KUBECONFIG environment variable issue
  • Loading branch information
karmada-bot committed Mar 14, 2022
2 parents 7e6d4f6 + d3ac601 commit 0961767
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 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
}
24 changes: 20 additions & 4 deletions pkg/karmadactl/cmdinit/kubernetes/deploy.go
Expand Up @@ -8,6 +8,7 @@ import (
"net"
"os"
"path"
"path/filepath"
"strings"
"time"

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

var defaultKubeConfig = filepath.Join(homeDir(), ".kube", "config")

// CommandInitOption holds all flags options for init.
type CommandInitOption struct {
EtcdImage string
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 @@ -437,3 +446,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 0961767

Please sign in to comment.