mirror of
https://github.com/helmfile/helmfile.git
synced 2026-09-30 06:26:30 +02:00
feat: support HELMFILE_NAMESPACE env var for default namespace (#2592)
* feat: support HELMFILE_NAMESPACE env var for default namespace Mirrors the existing HELMFILE_ENVIRONMENT pattern: the --namespace CLI flag takes precedence, falling back to HELMFILE_NAMESPACE when unset. Signed-off-by: Dominik Schmidt <dev@dominik-schmidt.de> * docs: mention HELMFILE_NAMESPACE 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
@@ -139,7 +139,17 @@ func (g *GlobalImpl) KubeContext() string {
|
||||
|
||||
// Namespace returns the namespace to use.
|
||||
func (g *GlobalImpl) Namespace() string {
|
||||
return g.GlobalOptions.Namespace
|
||||
var namespace string
|
||||
|
||||
switch {
|
||||
case g.GlobalOptions.Namespace != "":
|
||||
namespace = g.GlobalOptions.Namespace
|
||||
case os.Getenv("HELMFILE_NAMESPACE") != "":
|
||||
namespace = os.Getenv("HELMFILE_NAMESPACE")
|
||||
default:
|
||||
namespace = ""
|
||||
}
|
||||
return namespace
|
||||
}
|
||||
|
||||
// Chart returns the chart to use.
|
||||
|
||||
@@ -82,3 +82,40 @@ func TestKubeContext(t *testing.T) {
|
||||
}
|
||||
os.Unsetenv(envvar.KubeContext)
|
||||
}
|
||||
|
||||
// TestNamespace tests the namespace flag and HELMFILE_NAMESPACE env var fallback
|
||||
func TestNamespace(t *testing.T) {
|
||||
tests := []struct {
|
||||
opts GlobalOptions
|
||||
env string
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
opts: GlobalOptions{},
|
||||
env: "",
|
||||
expected: "",
|
||||
},
|
||||
{
|
||||
opts: GlobalOptions{},
|
||||
env: "envset",
|
||||
expected: "envset",
|
||||
},
|
||||
{
|
||||
opts: GlobalOptions{Namespace: "flagset"},
|
||||
env: "",
|
||||
expected: "flagset",
|
||||
},
|
||||
{
|
||||
opts: GlobalOptions{Namespace: "flagset"},
|
||||
env: "envset",
|
||||
expected: "flagset",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
os.Setenv(envvar.Namespace, test.env)
|
||||
received := NewGlobalImpl(&test.opts).Namespace()
|
||||
require.Equalf(t, test.expected, received, "Namespace expected %s, received %s", test.expected, received)
|
||||
}
|
||||
os.Unsetenv(envvar.Namespace)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user