Skip to content
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
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 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
76 changes: 76 additions & 0 deletions cluster/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestExtendedClusterManager(t *testing.T) {

func TestInClusterConfig(t *testing.T) {
t.Run("LoadInClusterConfig", testLoadInClusterConfig)
t.Run("SetCurrentContextWithInClusterConfig", testSetCurrentContextWithInClusterConfig)
}

func testLoadInClusterConfig(t *testing.T) {
Expand Down Expand Up @@ -86,6 +87,81 @@ func testLoadInClusterConfig(t *testing.T) {
})
}

func testSetCurrentContextWithInClusterConfig(t *testing.T) {
t.Run("SetCurrentContextWithEmptyConfigPath", func(t *testing.T) {
// Test that SetCurrentContext doesn't fail when ConfigPath is empty (in-cluster config)
cm := New()

// Create a fake in-cluster context with empty ConfigPath
fakeClient := fake.NewSimpleClientset()
inClusterContext := &kai.ContextInfo{
Name: "in-cluster",
Cluster: "in-cluster",
User: "service-account",
Namespace: "default",
ServerURL: "https://kubernetes.default.svc",
ConfigPath: "", // Empty ConfigPath simulates in-cluster config
IsActive: false,
}
cm.clients["in-cluster"] = fakeClient
cm.contexts["in-cluster"] = inClusterContext

// This should not fail even though ConfigPath is empty
err := cm.SetCurrentContext("in-cluster")
assert.NoError(t, err)
assert.Equal(t, "in-cluster", cm.GetCurrentContext())
assert.True(t, inClusterContext.IsActive)
})

t.Run("SetCurrentContextWithValidConfigPath", func(t *testing.T) {
// Test that SetCurrentContext still calls updateKubeconfigCurrentContext with a valid ConfigPath
cm := New()

// Create two fake contexts, one with ConfigPath and one without
fakeClient := fake.NewSimpleClientset()

inClusterContext := &kai.ContextInfo{
Name: "in-cluster",
Cluster: "in-cluster",
User: "service-account",
Namespace: "default",
ServerURL: "https://kubernetes.default.svc",
ConfigPath: "", // Empty ConfigPath
IsActive: true,
}

fileBasedContext := &kai.ContextInfo{
Name: "file-based",
Cluster: "cluster1",
User: "user1",
Namespace: "default",
ServerURL: "https://example.com",
ConfigPath: "/path/to/kubeconfig", // Non-empty ConfigPath
IsActive: false,
}

cm.clients["in-cluster"] = fakeClient
cm.contexts["in-cluster"] = inClusterContext
cm.clients["file-based"] = fakeClient
cm.contexts["file-based"] = fileBasedContext
cm.currentContext = "in-cluster"

// Switch to file-based context - this would normally update the kubeconfig file
// but will fail here because the file doesn't exist - that's expected
err := cm.SetCurrentContext("file-based")
// We expect an error because the kubeconfig file doesn't exist
assert.Error(t, err)
assert.Contains(t, err.Error(), "failed to update kubeconfig file")

// 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.GetCurrentContext())
assert.True(t, inClusterContext.IsActive)
assert.False(t, fileBasedContext.IsActive)
})
}

func testNewClusterManager(t *testing.T) {
cm := New()
assert.NotNil(t, cm)
Expand Down