From f8c41e4bc80a6c0ebe39500ced0f128f29d99ba3 Mon Sep 17 00:00:00 2001 From: Nikolaus Schuetz Date: Mon, 29 Jun 2026 12:19:03 -0700 Subject: [PATCH] fix: reflect --namespaces-to-ignore in startup namespace-scope log When KUBERNETES_NAMESPACE is unset, startReloader logged "will detect changes in all namespaces." unconditionally, even when --namespaces-to-ignore was set, which is misleading. Extract the scope message into namespaceWatchScopeMessage(), move the log after ignoredNamespacesList is resolved, and include the excluded namespaces in the message when filtering is active. Closes #1131 Assisted-by: Claude Code (Anthropic, Opus 4.x) --- internal/pkg/cmd/reloader.go | 18 +++++++++++++- internal/pkg/cmd/reloader_test.go | 40 +++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 internal/pkg/cmd/reloader_test.go diff --git a/internal/pkg/cmd/reloader.go b/internal/pkg/cmd/reloader.go index 00463fa7..67f357fc 100644 --- a/internal/pkg/cmd/reloader.go +++ b/internal/pkg/cmd/reloader.go @@ -102,6 +102,20 @@ func getHAEnvs() (string, string) { return podName, podNamespace } +// namespaceWatchScopeMessage returns the startup log message describing the +// namespace scope Reloader will watch when KUBERNETES_NAMESPACE is unset +// (global mode). It reflects --namespaces-to-ignore so the log is not +// misleading when namespace filtering is configured. +func namespaceWatchScopeMessage(ignoredNamespaces []string) string { + if len(ignoredNamespaces) > 0 { + return fmt.Sprintf( + "KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces except: %s.", + strings.Join(ignoredNamespaces, ", "), + ) + } + return "KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces." +} + func startReloader(cmd *cobra.Command, args []string) { common.GetCommandLineOptions() err := configureLogging(options.LogFormat, options.LogLevel) @@ -115,7 +129,6 @@ func startReloader(cmd *cobra.Command, args []string) { if len(currentNamespace) == 0 { currentNamespace = v1.NamespaceAll isGlobal = true - logrus.Warnf("KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces.") } // create the clientset @@ -130,6 +143,9 @@ func startReloader(cmd *cobra.Command, args []string) { } ignoredNamespacesList := options.NamespacesToIgnore + if isGlobal { + logrus.Warn(namespaceWatchScopeMessage(ignoredNamespacesList)) + } namespaceLabelSelector := "" if isGlobal { diff --git a/internal/pkg/cmd/reloader_test.go b/internal/pkg/cmd/reloader_test.go new file mode 100644 index 00000000..a5a68e9a --- /dev/null +++ b/internal/pkg/cmd/reloader_test.go @@ -0,0 +1,40 @@ +package cmd + +import "testing" + +func TestNamespaceWatchScopeMessage(t *testing.T) { + tests := []struct { + name string + ignoredNamespaces []string + want string + }{ + { + name: "no ignored namespaces - all namespaces", + ignoredNamespaces: nil, + want: "KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces.", + }, + { + name: "empty ignored namespaces - all namespaces", + ignoredNamespaces: []string{}, + want: "KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces.", + }, + { + name: "single ignored namespace", + ignoredNamespaces: []string{"kube-system"}, + want: "KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces except: kube-system.", + }, + { + name: "multiple ignored namespaces", + ignoredNamespaces: []string{"kube-system", "kube-public"}, + want: "KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces except: kube-system, kube-public.", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := namespaceWatchScopeMessage(tt.ignoredNamespaces); got != tt.want { + t.Errorf("namespaceWatchScopeMessage(%v) = %q, want %q", tt.ignoredNamespaces, got, tt.want) + } + }) + } +}