From 1d61cdf4e1d092d6ebf85a4e8eaa671ccac57c36 Mon Sep 17 00:00:00 2001 From: JB Pinalie <2850825+jybp@users.noreply.github.com> Date: Fri, 11 Apr 2025 11:13:43 +0200 Subject: [PATCH] Add FLUX_SYSTEM_NAMESPACE_FROM_CONTEXT to inherit kubens context Signed-off-by: JB Pinalie <2850825+jybp@users.noreply.github.com> --- cmd/flux/main.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/flux/main.go b/cmd/flux/main.go index 1226a347..3958f7ee 100644 --- a/cmd/flux/main.go +++ b/cmd/flux/main.go @@ -205,6 +205,20 @@ func main() { func configureDefaultNamespace() { *kubeconfigArgs.Namespace = rootArgs.defaults.Namespace + + // First check if we should use the namespace from the current context + if os.Getenv("FLUX_SYSTEM_NAMESPACE_FROM_CONTEXT") == "true" { + // Get the current context's namespace from kubeconfig + config, err := kubeconfigArgs.ToRawKubeConfigLoader().RawConfig() + if err == nil { + if ctx, exists := config.Contexts[config.CurrentContext]; exists && ctx.Namespace != "" { + kubeconfigArgs.Namespace = &ctx.Namespace + return + } + } + } + + // Fall back to environment variable if set fromEnv := os.Getenv("FLUX_SYSTEM_NAMESPACE") if fromEnv != "" { // namespace must be a valid DNS label. Assess against validation