From c6f24358da84079224a0184c11212fc49842e138 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 12:27:35 +0000 Subject: [PATCH 1/3] Initial plan From fdb0e010957b9f1f08cee826ba9960786a94a90b Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 12:32:10 +0000 Subject: [PATCH 2/3] fix: skip kubeconfig update for in-cluster contexts Add check in SetCurrentContext to skip kubeconfig file update when ConfigPath is empty (in-cluster configurations). This prevents errors when switching to in-cluster contexts that don't have an associated kubeconfig file. Co-authored-by: basebandit <8973567+basebandit@users.noreply.github.com> --- cluster/manager.go | 7 ++-- cluster/manager_test.go | 76 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 2 deletions(-) diff --git a/cluster/manager.go b/cluster/manager.go index 3f0a572..056afc1 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 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..bdac8db 100644 --- a/cluster/manager_test.go +++ b/cluster/manager_test.go @@ -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) { @@ -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) From 9df4a36826351222501ca3feb35aea083aa195e9 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sun, 1 Feb 2026 14:27:56 +0000 Subject: [PATCH 3/3] fix: remove trailing whitespace in manager_test.go Fix gofmt linting error by removing trailing whitespace on lines 122 and 132 in cluster/manager_test.go. Co-authored-by: basebandit <8973567+basebandit@users.noreply.github.com> --- cluster/manager_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cluster/manager_test.go b/cluster/manager_test.go index bdac8db..3a002e4 100644 --- a/cluster/manager_test.go +++ b/cluster/manager_test.go @@ -119,7 +119,7 @@ func testSetCurrentContextWithInClusterConfig(t *testing.T) { // Create two fake contexts, one with ConfigPath and one without fakeClient := fake.NewSimpleClientset() - + inClusterContext := &kai.ContextInfo{ Name: "in-cluster", Cluster: "in-cluster", @@ -129,7 +129,7 @@ func testSetCurrentContextWithInClusterConfig(t *testing.T) { ConfigPath: "", // Empty ConfigPath IsActive: true, } - + fileBasedContext := &kai.ContextInfo{ Name: "file-based", Cluster: "cluster1",