Skip to content

Commit

Permalink
cnivpctl: fix kubeconfig path
Browse files Browse the repository at this point in the history
  • Loading branch information
fioncat committed Dec 11, 2024
1 parent 70fe9b0 commit 1ee2a3f
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions cmd/cnivpctl/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"fmt"
"os"
"path"
"strconv"
"sync"
"time"
Expand All @@ -34,17 +33,22 @@ import (
"github.com/ucloud/uk8s-cni-vpc/rpc"
)

var preferredConfigPaths = []string{
"$HOME/.kube/config",
"/etc/kubernetes/cnivpc.kubeconfig",
"/etc/kubernetes/kubelet.kubeconfig",
}

func kubeConfig() (*rest.Config, error) {
configPath := os.Getenv("KUBECONFIG")
if configPath == "" {
homeDir, err := os.UserHomeDir()
if err != nil {
return nil, fmt.Errorf("Get home dir error: %v", err)
}
configPath = path.Join(homeDir, ".kube", "config")
_, err = os.Stat(configPath)
if err != nil {
configPath = "/etc/kubernetes/kubelet.kubeconfig"
for _, path := range preferredConfigPaths {
path = os.ExpandEnv(path)
_, err := os.Stat(path)
if err == nil {
configPath = path
break
}
}
}

Expand Down

0 comments on commit 1ee2a3f

Please sign in to comment.