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

fix: current context title #140

Merged
merged 1 commit into from
Apr 13, 2023
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
12 changes: 8 additions & 4 deletions pkg/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pkg/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down Expand Up @@ -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)
Expand Down