Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: introduce default kubeconfig path #78

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading