Skip to content

Commit

Permalink
adding flag option
Browse files Browse the repository at this point in the history
  • Loading branch information
bbensky committed Jul 22, 2024
1 parent ec80be4 commit bced211
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
9 changes: 4 additions & 5 deletions pkg/kube/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
package kube

import (
"os"
"flag"
"sync"

"k8s.io/client-go/kubernetes"
Expand Down Expand Up @@ -81,10 +81,9 @@ func GetConfig(kubeContext string, kubeConfigPath string) (*rest.Config, error)
klog.V(3).Infof("using kube context: %s", kubeContext)
}

if kubeConfigPath != "" {
klog.V(3).Infof("using kubeconfig at path: %s", kubeConfigPath)
os.Setenv("KUBECONFIG", kubeConfigPath)
}
fs := flag.NewFlagSet("fs", flag.ContinueOnError)
fs.String("kubeconfig", kubeConfigPath, "")
config.RegisterFlags(fs)

kubeConfig, err := config.GetConfigWithContext(kubeContext)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/kube/kube_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ func Test_getKubeClient(t *testing.T) {
kubeConfigPath: "",
wantErr: true,
},
{
name: "valid kubeconfig path",
kubeContext: "kind-kind",
kubeConfig: "testdata/kubeconfig_invalid",
kubeConfigPath: "testdata/kubeconfig",
wantErr: false,
},
{
name: "invalid kubeconfig path",
kubeContext: "kind-kind",
kubeConfig: "testdata/kubeconfig",
kubeConfigPath: "testdata/kubeconfig_invalid",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit bced211

Please sign in to comment.