mirror of
https://github.com/helmfile/helmfile.git
synced 2026-10-19 05:45:56 +02:00
feat: support HELMFILE_KUBE_CONTEXT env var for default kube context (#2593)
* feat: support HELMFILE_KUBE_CONTEXT env var for default kube context Mirrors the existing HELMFILE_ENVIRONMENT pattern: the --kube-context CLI flag takes precedence, falling back to HELMFILE_KUBE_CONTEXT when unset. Refs #1213. Signed-off-by: Dominik Schmidt <dev@dominik-schmidt.de> * docs: mention HELMFILE_KUBE_CONTEXT in cli.md and templating.md Signed-off-by: Dominik Schmidt <dev@dominik-schmidt.de> --------- Signed-off-by: Dominik Schmidt <dev@dominik-schmidt.de>
This commit is contained in:
+11
-1
@@ -124,7 +124,17 @@ func (g *GlobalImpl) Kubeconfig() string {
|
||||
|
||||
// KubeContext returns the name of the kubectl context to use.
|
||||
func (g *GlobalImpl) KubeContext() string {
|
||||
return g.GlobalOptions.KubeContext
|
||||
var kubeContext string
|
||||
|
||||
switch {
|
||||
case g.GlobalOptions.KubeContext != "":
|
||||
kubeContext = g.GlobalOptions.KubeContext
|
||||
case os.Getenv("HELMFILE_KUBE_CONTEXT") != "":
|
||||
kubeContext = os.Getenv("HELMFILE_KUBE_CONTEXT")
|
||||
default:
|
||||
kubeContext = ""
|
||||
}
|
||||
return kubeContext
|
||||
}
|
||||
|
||||
// Namespace returns the namespace to use.
|
||||
|
||||
@@ -45,3 +45,40 @@ func TestFileOrDir(t *testing.T) {
|
||||
}
|
||||
os.Unsetenv(envvar.FilePath)
|
||||
}
|
||||
|
||||
// TestKubeContext tests the kube-context flag and HELMFILE_KUBE_CONTEXT env var fallback
|
||||
func TestKubeContext(t *testing.T) {
|
||||
tests := []struct {
|
||||
opts GlobalOptions
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
opts: GlobalOptions{},
|
||||
env: "",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
opts: GlobalOptions{},
|
||||
env: "envset",
|
||||
expected: "envset",
|
||||
},
|
||||
{
|
||||
opts: GlobalOptions{KubeContext: "flagset"},
|
||||
env: "",
|
||||
expected: "flagset",
|
||||
},
|
||||
{
|
||||
opts: GlobalOptions{KubeContext: "flagset"},
|
||||
env: "envset",
|
||||
expected: "flagset",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
os.Setenv(envvar.KubeContext, test.env)
|
||||
received := NewGlobalImpl(&test.opts).KubeContext()
|
||||
require.Equalf(t, test.expected, received, "KubeContext expected %s, received %s", test.expected, received)
|
||||
}
|
||||
os.Unsetenv(envvar.KubeContext)
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ const (
|
||||
DisableRunnerUniqueID = "HELMFILE_DISABLE_RUNNER_UNIQUE_ID"
|
||||
Experimental = "HELMFILE_EXPERIMENTAL" // environment variable for experimental features, expecting "true" lower case
|
||||
Environment = "HELMFILE_ENVIRONMENT"
|
||||
KubeContext = "HELMFILE_KUBE_CONTEXT"
|
||||
FilePath = "HELMFILE_FILE_PATH"
|
||||
TempDir = "HELMFILE_TEMPDIR"
|
||||
UpgradeNoticeDisabled = "HELMFILE_UPGRADE_NOTICE_DISABLED"
|
||||
|
||||
Reference in New Issue
Block a user