From 8e306762d97c430383a4698d23e2f9a7a4e1c745 Mon Sep 17 00:00:00 2001 From: chenk Date: Thu, 13 Apr 2023 13:57:11 +0300 Subject: [PATCH] fix: current context title Signed-off-by: chenk --- pkg/k8s/k8s.go | 12 ++++++++---- pkg/k8s/k8s_test.go | 4 ++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/k8s/k8s.go b/pkg/k8s/k8s.go index 003135c..bd32790 100644 --- a/pkg/k8s/k8s.go +++ b/pkg/k8s/k8s.go @@ -103,10 +103,10 @@ func GetCluster(opts ...ClusterOption) (Cluster, error) { return nil, err } - return getCluster(clientConfig, restMapper) + return getCluster(clientConfig, restMapper, *cf.Context) } -func getCluster(clientConfig clientcmd.ClientConfig, restMapper meta.RESTMapper) (*cluster, error) { +func getCluster(clientConfig clientcmd.ClientConfig, restMapper meta.RESTMapper, currentContext string) (*cluster, error) { kubeConfig, err := clientConfig.ClientConfig() if err != nil { return nil, err @@ -129,7 +129,11 @@ func getCluster(clientConfig clientcmd.ClientConfig, restMapper meta.RESTMapper) } var namespace string - if context, ok := rawCfg.Contexts[rawCfg.CurrentContext]; ok { + + if len(currentContext) == 0 { + currentContext = rawCfg.CurrentContext + } + if context, ok := rawCfg.Contexts[currentContext]; ok { namespace = context.Namespace } @@ -138,7 +142,7 @@ func getCluster(clientConfig clientcmd.ClientConfig, restMapper meta.RESTMapper) } return &cluster{ - currentContext: rawCfg.CurrentContext, + currentContext: currentContext, currentNamespace: namespace, dynamicClient: k8sDynamicClient, restMapper: restMapper, diff --git a/pkg/k8s/k8s_test.go b/pkg/k8s/k8s_test.go index e6b0cb9..0610a4e 100644 --- a/pkg/k8s/k8s_test.go +++ b/pkg/k8s/k8s_test.go @@ -30,7 +30,7 @@ func TestGetCurrentNamespace(t *testing.T) { for _, test := range tests { t.Run(test.Name, func(t *testing.T) { fakeConfig := createValidTestConfig(test.Namespace) - cluster, err := getCluster(fakeConfig, nil) + cluster, err := getCluster(fakeConfig, nil, "") assert.NoError(t, err) assert.Equal(t, test.ExpectedNamespace, cluster.GetCurrentNamespace()) }) @@ -62,7 +62,7 @@ func TestGetGVR(t *testing.T) { fakeConfig := createValidTestConfig("") - cluster, err := getCluster(fakeConfig, mapper) + cluster, err := getCluster(fakeConfig, mapper, "") assert.NoError(t, err) gvr, err := cluster.GetGVR(test.Resource)