Visitar URL original
fix: allow ambient KUBECONFIG in render mode (#4406) · pulumi/pulumi-kubernetes@db24ea9 · GitHub
Skip to content

Commit db24ea9

Browse files
immanuwellguineveresaengerclaude
authored
fix: allow ambient KUBECONFIG in render mode (#4406)
### Proposed changes Allow render mode to accept `kubeconfig` when it is only the ambient `KUBECONFIG` value injected by the SDK. This path is pretty normal in CI. `google-github-actions/get-gke-credentials` exports `KUBECONFIG` out of the box. Before this PR, render mode tripped on that and failed with `"kubeconfig" arg is not compatible with "renderYamlToDirectory" arg`. After this PR, that ambient path is allowed, while explicit `kubeconfig` is still blocked. Added a regression test too. Repro: 1. Set `KUBECONFIG` to a valid kubeconfig file. 2. Create a provider with `renderYamlToDirectory` and do not pass `kubeconfig` yourself. 3. The SDK fills `kubeconfig` from `KUBECONFIG`, then `CheckConfig` fails. 4. With this PR, render mode works as expected. ### Related issues (optional) Fixes #4399 Related #3498 --------- Co-authored-by: guineveresaenger <guinevere@pulumi.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 0833fab commit db24ea9

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

‎provider/pkg/provider/provider.go‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,9 @@ func (k *kubeProvider) CheckConfig(
415415
Reason: fmt.Sprintf(errTemplate, "context"),
416416
})
417417
}
418-
if truthyValue("kubeconfig", news) {
418+
// SDKs default kubeconfig from $KUBECONFIG. In render mode we still want ambient
419+
// kubeconfig loading to work, so don't reject that specific defaulted value.
420+
if truthyValue("kubeconfig", news) && !matchesAmbientKubeconfigEnv(news["kubeconfig"]) {
419421
failures = append(failures, &pulumirpc.CheckFailure{
420422
Property: "kubeconfig",
421423
Reason: fmt.Sprintf(errTemplate, "kubeconfig"),
@@ -440,6 +442,14 @@ func (k *kubeProvider) CheckConfig(
440442
return &pulumirpc.CheckResponse{Inputs: req.GetNews()}, nil
441443
}
442444

445+
func matchesAmbientKubeconfigEnv(v resource.PropertyValue) bool {
446+
if !v.IsString() {
447+
return false
448+
}
449+
kubeconfig, ok := os.LookupEnv("KUBECONFIG")
450+
return ok && kubeconfig != "" && v.StringValue() == kubeconfig
451+
}
452+
443453
// DiffConfig diffs the configuration for this provider.
444454
func (k *kubeProvider) DiffConfig(
445455
_ context.Context,

‎provider/pkg/provider/provider_checkconfig_test.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ package provider
1616

1717
import (
1818
"context"
19+
"os"
1920

2021
_ "embed"
2122

@@ -134,6 +135,43 @@ var _ = gk.Describe("RPC:CheckConfig", func() {
134135
})
135136
})
136137

138+
gk.Context("when kubeconfig matches ambient KUBECONFIG", func() {
139+
gk.BeforeEach(func() {
140+
path := WriteKubeconfigToFile(config)
141+
gm.Expect(os.Setenv("KUBECONFIG", path)).To(gm.Succeed())
142+
gk.DeferCleanup(func() {
143+
os.Unsetenv("KUBECONFIG")
144+
})
145+
news["kubeconfig"] = resource.NewStringProperty(path)
146+
})
147+
gk.It("should succeed", func() {
148+
resp, err := k.CheckConfig(context.Background(), req)
149+
gm.Expect(err).ToNot(gm.HaveOccurred())
150+
gm.Expect(resp.Failures).To(gm.BeEmpty())
151+
})
152+
})
153+
154+
gk.Context("when kubeconfig does not match ambient KUBECONFIG", func() {
155+
gk.BeforeEach(func() {
156+
path := WriteKubeconfigToFile(config)
157+
gm.Expect(os.Setenv("KUBECONFIG", path)).To(gm.Succeed())
158+
gk.DeferCleanup(func() {
159+
os.Unsetenv("KUBECONFIG")
160+
})
161+
news["kubeconfig"] = resource.NewStringProperty(WriteKubeconfigToString(config))
162+
})
163+
gk.It("should fail because an explicit kubeconfig is disallowed", func() {
164+
resp, err := k.CheckConfig(context.Background(), req)
165+
gm.Expect(err).ToNot(gm.HaveOccurred())
166+
gm.Expect(resp.Failures).To(gm.HaveExactElements(
167+
CheckFailure(
168+
"kubeconfig",
169+
gm.Equal(`"kubeconfig" arg is not compatible with "renderYamlToDirectory" arg`),
170+
),
171+
))
172+
})
173+
})
174+
137175
gk.Context("when context is specified", func() {
138176
gk.BeforeEach(func() {
139177
news["context"] = resource.NewStringProperty(config.CurrentContext)

0 commit comments

Comments
 (0)