Skip to content

Commit 60e247f

Browse files
authored
Merge pull request #3 from flyhope/2-supoort-context-param
supoort --context param
2 parents 3f2ada7 + 4cd0f17 commit 60e247f

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

k8s/config.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ var ShowKubeConfigPath = sync.OnceValue(func() string {
2828
// ShowKubeConfig 获取kube的配置
2929
var ShowKubeConfig = sync.OnceValue(func() *restclient.Config {
3030

31-
// 加载 kubeconfig 文件
32-
config, err := clientcmd.BuildConfigFromFlags("", ShowKubeConfigPath())
31+
configOverrides := &clientcmd.ConfigOverrides{
32+
CurrentContext: ShowContext(),
33+
}
34+
config, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
35+
&clientcmd.ClientConfigLoadingRules{ExplicitPath: ShowKubeConfigPath()},
36+
configOverrides,
37+
).ClientConfig()
3338
if err != nil {
34-
logrus.Fatalf("Error loading kubeconfig: %v", err)
39+
logrus.Fatalf("Error loading kubeconfig: %v\n", err)
3540
}
3641

3742
return config
@@ -57,7 +62,7 @@ func ShowNamespace() string {
5762
}
5863

5964
config := ShowApiConfig()
60-
currentContext := config.CurrentContext
65+
currentContext := ShowContext()
6166
contextDetails := config.Contexts[currentContext]
6267
if contextDetails == nil {
6368
logrus.Fatal("\"current context not found in kubeconfig")

0 commit comments

Comments
 (0)