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) + } + }) + } +}