Skip to content

Commit

Permalink
cmd: introduce default kubeconfig path
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Fu <[email protected]>
  • Loading branch information
fuweid committed Jan 30, 2024
1 parent d5b7088 commit bc59090
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/kperf/commands/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions cmd/kperf/commands/runnergroup/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package runnergroup

import (
"github.com/Azure/kperf/cmd/kperf/commands/utils"

"github.com/urfave/cli"
)

Expand All @@ -13,6 +15,7 @@ var Command = cli.Command{
cli.StringFlag{
Name: "kubeconfig",
Usage: "Path to the kubeconfig file",
Value: utils.DefaultKubeConfigPath,
},
},
Subcommands: []cli.Command{
Expand Down
26 changes: 26 additions & 0 deletions cmd/kperf/commands/utils/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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") != ""
}
1 change: 1 addition & 0 deletions cmd/kperf/commands/virtualcluster/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var nodepoolCommand = cli.Command{
cli.StringFlag{
Name: "kubeconfig",
Usage: "Path to the kubeconfig file",
Value: utils.DefaultKubeConfigPath,
},
},
Subcommands: []cli.Command{
Expand Down

0 comments on commit bc59090

Please sign in to comment.