diff --git a/cmd/kperf/commands/runner/runner.go b/cmd/kperf/commands/runner/runner.go index 5f8eae6..5b4e73e 100644 --- a/cmd/kperf/commands/runner/runner.go +++ b/cmd/kperf/commands/runner/runner.go @@ -9,6 +9,7 @@ import ( "path/filepath" "github.com/Azure/kperf/api/types" + "github.com/Azure/kperf/cmd/kperf/commands/utils" "github.com/Azure/kperf/metrics" "github.com/Azure/kperf/request" @@ -32,6 +33,7 @@ var runCommand = cli.Command{ cli.StringFlag{ Name: "kubeconfig", Usage: "Path to the kubeconfig file", + Value: utils.DefaultKubeConfigPath, }, cli.IntFlag{ Name: "client", diff --git a/cmd/kperf/commands/runnergroup/root.go b/cmd/kperf/commands/runnergroup/root.go index 05f2f68..ec164b4 100644 --- a/cmd/kperf/commands/runnergroup/root.go +++ b/cmd/kperf/commands/runnergroup/root.go @@ -1,6 +1,8 @@ package runnergroup import ( + "github.com/Azure/kperf/cmd/kperf/commands/utils" + "github.com/urfave/cli" ) @@ -13,6 +15,7 @@ var Command = cli.Command{ cli.StringFlag{ Name: "kubeconfig", Usage: "Path to the kubeconfig file", + Value: utils.DefaultKubeConfigPath, }, }, Subcommands: []cli.Command{ diff --git a/cmd/kperf/commands/utils/helper.go b/cmd/kperf/commands/utils/helper.go index 66b950e..6e46c2e 100644 --- a/cmd/kperf/commands/utils/helper.go +++ b/cmd/kperf/commands/utils/helper.go @@ -2,9 +2,24 @@ package utils import ( "fmt" + "os" + "path/filepath" "strings" + + "k8s.io/client-go/util/homedir" ) +// DefaultKubeConfigPath is default kubeconfig path if there is home dir. +var DefaultKubeConfigPath string + +func init() { + if !inCluster() { + if home := homedir.HomeDir(); home != "" { + DefaultKubeConfigPath = filepath.Join(home, ".kube", "config") + } + } +} + // KeyValuesMap converts key=value[,value] into map[string][]string. func KeyValuesMap(strs []string) (map[string][]string, error) { res := make(map[string][]string, len(strs)) @@ -18,3 +33,14 @@ func KeyValuesMap(strs []string) (map[string][]string, error) { } return res, nil } + +// inCluster is to check if current process is in pod. +func inCluster() bool { + f, err := os.Stat("/var/run/secrets/kubernetes.io/serviceaccount/token") + if err != nil || f.IsDir() { + return false + } + + return os.Getenv("KUBERNETES_SERVICE_HOST") != "" && + os.Getenv("KUBERNETES_SERVICE_PORT") != "" +} diff --git a/cmd/kperf/commands/virtualcluster/nodepool.go b/cmd/kperf/commands/virtualcluster/nodepool.go index f42f01e..3eef985 100644 --- a/cmd/kperf/commands/virtualcluster/nodepool.go +++ b/cmd/kperf/commands/virtualcluster/nodepool.go @@ -18,6 +18,7 @@ var nodepoolCommand = cli.Command{ cli.StringFlag{ Name: "kubeconfig", Usage: "Path to the kubeconfig file", + Value: utils.DefaultKubeConfigPath, }, }, Subcommands: []cli.Command{