diff --git a/cluster/manager.go b/cluster/manager.go index 3f0a572..1b6ca9a 100644 --- a/cluster/manager.go +++ b/cluster/manager.go @@ -349,8 +349,11 @@ func (cm *Manager) SetCurrentContext(contextName string) error { contextInfo.IsActive = true // Update the kubeconfig file to reflect the context switch - if err := cm.updateKubeconfigCurrentContext(contextName, contextInfo.ConfigPath); err != nil { - return fmt.Errorf("failed to update kubeconfig file: %w", err) + // Skip the update for in-cluster configurations (empty ConfigPath) + if contextInfo.ConfigPath != "" { + if err := cm.updateKubeconfigCurrentContext(contextName, contextInfo.ConfigPath); err != nil { + return fmt.Errorf("failed to update kubeconfig file: %w", err) + } } } diff --git a/cluster/manager_test.go b/cluster/manager_test.go index f98daeb..64eb407 100644 --- a/cluster/manager_test.go +++ b/cluster/manager_test.go @@ -45,6 +45,7 @@ func TestExtendedClusterManager(t *testing.T) { t.Run("ListContexts", testListContexts) t.Run("LoadKubeConfigDuplicateName", testLoadKubeConfigDuplicateName) t.Run("SetCurrentContextUpdatesActiveStatus", testSetCurrentContextUpdatesActiveStatus) + t.Run("SetCurrentContextWithEmptyConfigPath", testSetCurrentContextWithEmptyConfigPath) t.Run("UpdateKubeconfigCurrentContext", testUpdateKubeconfigCurrentContext) } @@ -601,6 +602,41 @@ users: assert.Equal(t, testContext2, config.CurrentContext) } +func testSetCurrentContextWithEmptyConfigPath(t *testing.T) { + cm := New() + + fakeClient1 := fake.NewSimpleClientset() + fakeClient2 := fake.NewSimpleClientset() + + // Create a regular context with a config path + contextInfo1 := &kai.ContextInfo{ + Name: testContext1, + ConfigPath: "/some/path/to/config", + IsActive: true, + } + + // Create an in-cluster context with empty ConfigPath (simulating in-cluster config) + contextInfo2 := &kai.ContextInfo{ + Name: "in-cluster", + ConfigPath: "", // Empty config path for in-cluster + IsActive: false, + } + + cm.clients[testContext1] = fakeClient1 + cm.clients["in-cluster"] = fakeClient2 + cm.contexts[testContext1] = contextInfo1 + cm.contexts["in-cluster"] = contextInfo2 + cm.currentContext = testContext1 + + // Switch to in-cluster context - should succeed without trying to update kubeconfig + err := cm.SetCurrentContext("in-cluster") + assert.NoError(t, err) + + assert.Equal(t, "in-cluster", cm.currentContext) + assert.False(t, cm.contexts[testContext1].IsActive) + assert.True(t, cm.contexts["in-cluster"].IsActive) +} + func testUpdateKubeconfigCurrentContext(t *testing.T) { tempDir := t.TempDir() kubeconfigPath := filepath.Join(tempDir, "config")