Merge pull request #933 from yo-ga/fix/specific-namespace

Ignore namespaceSelector when not watch globally
This commit is contained in:
Muhammad Safwan Karim
2025-07-11 17:14:19 +05:00
committed by GitHub
5 changed files with 23 additions and 7 deletions

View File

@@ -52,7 +52,7 @@ helm uninstall {{RELEASE_NAME}} -n {{NAMESPACE}}
| `reloader.syncAfterRestart` | Enable sync after Reloader restarts for **Add** events, works only when reloadOnCreate is `true`. Valid value are either `true` or `false` | boolean | `false` | | `reloader.syncAfterRestart` | Enable sync after Reloader restarts for **Add** events, works only when reloadOnCreate is `true`. Valid value are either `true` or `false` | boolean | `false` |
| `reloader.reloadStrategy` | Strategy to trigger resource restart, set to either `default`, `env-vars` or `annotations` | enumeration | `default` | | `reloader.reloadStrategy` | Strategy to trigger resource restart, set to either `default`, `env-vars` or `annotations` | enumeration | `default` |
| `reloader.ignoreNamespaces` | List of comma separated namespaces to ignore, if multiple are provided, they are combined with the AND operator | string | `""` | | `reloader.ignoreNamespaces` | List of comma separated namespaces to ignore, if multiple are provided, they are combined with the AND operator | string | `""` |
| `reloader.namespaceSelector` | List of comma separated k8s label selectors for namespaces selection. See [LIST and WATCH filtering](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#list-and-watch-filtering) for more details on label-selector | string | `""` | | `reloader.namespaceSelector` | List of comma separated k8s label selectors for namespaces selection. The parameter only used when `reloader.watchGlobally` is `true`. See [LIST and WATCH filtering](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#list-and-watch-filtering) for more details on label-selector | string | `""` |
| `reloader.resourceLabelSelector` | List of comma separated label selectors, if multiple are provided they are combined with the AND operator | string | `""` | | `reloader.resourceLabelSelector` | List of comma separated label selectors, if multiple are provided they are combined with the AND operator | string | `""` |
| `reloader.logFormat` | Set type of log format. Value could be either `json` or `""` | string | `""` | | `reloader.logFormat` | Set type of log format. Value could be either `json` or `""` | string | `""` |
| `reloader.watchGlobally` | Allow Reloader to watch in all namespaces (`true`) or just in a single namespace (`false`) | boolean | `true` | | `reloader.watchGlobally` | Allow Reloader to watch in all namespaces (`true`) or just in a single namespace (`false`) | boolean | `true` |

View File

@@ -70,3 +70,12 @@ Create the annotations to support helm3
meta.helm.sh/release-namespace: {{ .Release.Namespace | quote }} meta.helm.sh/release-namespace: {{ .Release.Namespace | quote }}
meta.helm.sh/release-name: {{ .Release.Name | quote }} meta.helm.sh/release-name: {{ .Release.Name | quote }}
{{- end -}} {{- end -}}
{{/*
Create the namespace selector if it does not watch globally
*/}}
{{- define "reloader-namespaceSelector" -}}
{{- if and .Values.reloader.watchGlobally .Values.reloader.namespaceSelector -}}
{{ .Values.reloader.namespaceSelector }}
{{- end -}}
{{- end -}}

View File

@@ -31,7 +31,7 @@ rules:
- list - list
- get - get
- watch - watch
{{- if .Values.reloader.namespaceSelector }} {{- if (include "reloader-namespaceSelector" .) }}
- apiGroups: - apiGroups:
- "" - ""
resources: resources:

View File

@@ -198,7 +198,7 @@ spec:
{{- . | toYaml | nindent 10 }} {{- . | toYaml | nindent 10 }}
{{- end }} {{- end }}
{{- end }} {{- end }}
{{- if or (.Values.reloader.logFormat) (.Values.reloader.logLevel) (.Values.reloader.ignoreSecrets) (.Values.reloader.ignoreNamespaces) (.Values.reloader.namespaceSelector) (.Values.reloader.resourceLabelSelector) (.Values.reloader.ignoreConfigMaps) (.Values.reloader.custom_annotations) (eq .Values.reloader.isArgoRollouts true) (eq .Values.reloader.reloadOnCreate true) (eq .Values.reloader.reloadOnDelete true) (ne .Values.reloader.reloadStrategy "default") (.Values.reloader.enableHA) (.Values.reloader.autoReloadAll)}} {{- if or (.Values.reloader.logFormat) (.Values.reloader.logLevel) (.Values.reloader.ignoreSecrets) (.Values.reloader.ignoreNamespaces) (include "reloader-namespaceSelector" .) (.Values.reloader.resourceLabelSelector) (.Values.reloader.ignoreConfigMaps) (.Values.reloader.custom_annotations) (eq .Values.reloader.isArgoRollouts true) (eq .Values.reloader.reloadOnCreate true) (eq .Values.reloader.reloadOnDelete true) (ne .Values.reloader.reloadStrategy "default") (.Values.reloader.enableHA) (.Values.reloader.autoReloadAll)}}
args: args:
{{- if .Values.reloader.logFormat }} {{- if .Values.reloader.logFormat }}
- "--log-format={{ .Values.reloader.logFormat }}" - "--log-format={{ .Values.reloader.logFormat }}"
@@ -215,8 +215,8 @@ spec:
{{- if .Values.reloader.ignoreNamespaces }} {{- if .Values.reloader.ignoreNamespaces }}
- "--namespaces-to-ignore={{ .Values.reloader.ignoreNamespaces }}" - "--namespaces-to-ignore={{ .Values.reloader.ignoreNamespaces }}"
{{- end }} {{- end }}
{{- if .Values.reloader.namespaceSelector }} {{- if (include "reloader-namespaceSelector" .) }}
- "--namespace-selector={{ .Values.reloader.namespaceSelector }}" - "--namespace-selector=\"{{ include "reloader-namespaceSelector" . }}\""
{{- end }} {{- end }}
{{- if .Values.reloader.resourceLabelSelector }} {{- if .Values.reloader.resourceLabelSelector }}
- "--resource-label-selector={{ .Values.reloader.resourceLabelSelector }}" - "--resource-label-selector={{ .Values.reloader.resourceLabelSelector }}"

View File

@@ -128,9 +128,11 @@ func startReloader(cmd *cobra.Command, args []string) {
} }
logrus.Info("Starting Reloader") logrus.Info("Starting Reloader")
isGlobal := false
currentNamespace := os.Getenv("KUBERNETES_NAMESPACE") currentNamespace := os.Getenv("KUBERNETES_NAMESPACE")
if len(currentNamespace) == 0 { if len(currentNamespace) == 0 {
currentNamespace = v1.NamespaceAll currentNamespace = v1.NamespaceAll
isGlobal = true
logrus.Warnf("KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces.") logrus.Warnf("KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces.")
} }
@@ -150,7 +152,7 @@ func startReloader(cmd *cobra.Command, args []string) {
logrus.Fatal(err) logrus.Fatal(err)
} }
namespaceLabelSelector, err := getNamespaceLabelSelector(cmd) namespaceLabelSelector, err := getNamespaceLabelSelector(cmd, isGlobal)
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
} }
@@ -215,7 +217,7 @@ func getIgnoredNamespacesList(cmd *cobra.Command) (util.List, error) {
return getStringSliceFromFlags(cmd, "namespaces-to-ignore") return getStringSliceFromFlags(cmd, "namespaces-to-ignore")
} }
func getNamespaceLabelSelector(cmd *cobra.Command) (string, error) { func getNamespaceLabelSelector(cmd *cobra.Command, isGlobal bool) (string, error) {
slice, err := getStringSliceFromFlags(cmd, "namespace-selector") slice, err := getStringSliceFromFlags(cmd, "namespace-selector")
if err != nil { if err != nil {
logrus.Fatal(err) logrus.Fatal(err)
@@ -246,6 +248,11 @@ func getNamespaceLabelSelector(cmd *cobra.Command) (string, error) {
logrus.Fatal(err) logrus.Fatal(err)
} }
if !isGlobal && len(namespaceLabelSelector) > 0 {
logrus.Warnf("KUBERNETES_NAMESPACE is set but also namespace-selector is set, will ignore the filter and detect changes in the specific namespace.")
return "", nil
}
return namespaceLabelSelector, nil return namespaceLabelSelector, nil
} }