Skip to content
Closed
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
7 changes: 5 additions & 2 deletions cluster/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions cluster/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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")
Expand Down