-
Notifications
You must be signed in to change notification settings - Fork 321
fix: support multi-file KUBECONFIG with three-way merge #970
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,8 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| package cmd | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| "testing" | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -63,3 +65,119 @@ func TestIsRemoteAccessAllowed(t *testing.T) { | |||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestPrepareEnvSettings_MultiFileKubeconfig(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| original := os.Getenv("KUBECONFIG") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer os.Setenv("KUBECONFIG", original) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| cases := []struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeconfig string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeContext string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeConfig string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeContext string | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "single file kubeconfig is preserved", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeconfig: "/path/to/config", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeContext: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeConfig: "/path/to/config", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeContext: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "multi-file kubeconfig is cleared", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeconfig: "/path/to/file1" + string(filepath.ListSeparator) + "/path/to/file2", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeContext: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeConfig: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeContext: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "multi-file kubeconfig with three files is cleared", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeconfig: "/a" + string(filepath.ListSeparator) + "/b" + string(filepath.ListSeparator) + "/c", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeContext: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeConfig: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeContext: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "empty kubeconfig is preserved", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeconfig: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeContext: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeConfig: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeContext: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "kube-context override is applied", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeconfig: "/path/to/config", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeContext: "my-context", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeConfig: "/path/to/config", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeContext: "my-context", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| name: "multi-file kubeconfig with kube-context override", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeconfig: "/path/to/file1" + string(filepath.ListSeparator) + "/path/to/file2", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| kubeContext: "my-context", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeConfig: "", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| wantKubeContext: "my-context", | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| for _, tc := range cases { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Run(tc.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.Setenv("KUBECONFIG", tc.kubeconfig) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| env := prepareEnvSettings(tc.kubeContext) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if env.KubeConfig != tc.wantKubeConfig { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("KubeConfig: got %q, want %q", env.KubeConfig, tc.wantKubeConfig) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if env.KubeContext != tc.wantKubeContext { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("KubeContext: got %q, want %q", env.KubeContext, tc.wantKubeContext) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| func TestPrepareEnvSettings_ConfigFlagsPointToCorrectFields(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| original := os.Getenv("KUBECONFIG") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| defer os.Setenv("KUBECONFIG", original) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Run("config flags reflect kube-context override", func(t *testing.T) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| os.Setenv("KUBECONFIG", "/some/config") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| env := prepareEnvSettings("my-override-context") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if env.KubeContext != "my-override-context" { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| t.Errorf("env.KubeContext = %q, want %q", env.KubeContext, "my-override-context") | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | |
| } | |
| if env.ConfigFlags == nil { | |
| t.Fatal("env.ConfigFlags is nil") | |
| } | |
| if env.ConfigFlags.Context == nil { | |
| t.Fatal("env.ConfigFlags.Context is nil") | |
| } | |
| if got := *env.ConfigFlags.Context; got != "my-override-context" { | |
| t.Errorf("env.ConfigFlags.Context = %q, want %q", got, "my-override-context") | |
| } | |
| getter := env.RESTClientGetter() | |
| rawLoader := getter.ToRawKubeConfigLoader() | |
| rawCfg, err := rawLoader.RawConfig() | |
| if err != nil { | |
| t.Fatalf("RawConfig() returned error: %v", err) | |
| } | |
| if rawCfg.CurrentContext != "my-override-context" { | |
| t.Errorf("raw kubeconfig CurrentContext = %q, want %q", rawCfg.CurrentContext, "my-override-context") | |
| } |
Copilot
AI
Apr 10, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This subtest says it preserves ExplicitPath, but it only checks env.KubeConfig. Consider also asserting that env.RESTClientGetter().ToRawKubeConfigLoader().ConfigAccess().GetExplicitFile() equals the single kubeconfig path so the test actually validates ExplicitPath behavior.
| } | |
| } | |
| getter := env.RESTClientGetter() | |
| rawConfig := getter.ToRawKubeConfigLoader() | |
| loadingRules := rawConfig.ConfigAccess() | |
| if loadingRules == nil { | |
| t.Fatal("ConfigAccess() = nil, want non-nil for single-file KUBECONFIG") | |
| } | |
| if explicitPath := loadingRules.GetExplicitFile(); explicitPath != "/tmp/single-config" { | |
| t.Errorf("ExplicitPath = %q, want %q", explicitPath, "/tmp/single-config") | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test restores KUBECONFIG via
original := os.Getenv(...)+defer os.Setenv(...), which can leave KUBECONFIG set to an empty string even if it was originally unset. Prefert.Setenv(per subtest) or useos.LookupEnvandos.Unsetenvto restore the pre-test state accurately (and avoid ignoringos.Setenverrors).