Skip to content

Commit 786b5d0

Browse files
authored
Merge pull request stakater#933 from yo-ga/fix/specific-namespace
Ignore namespaceSelector when not watch globally
2 parents 35cc982 + 34f2ec8 commit 786b5d0

5 files changed

Lines changed: 23 additions & 7 deletions

File tree

deployments/kubernetes/chart/reloader/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ helm uninstall {{RELEASE_NAME}} -n {{NAMESPACE}}
5252
| `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` |
5353
| `reloader.reloadStrategy` | Strategy to trigger resource restart, set to either `default`, `env-vars` or `annotations` | enumeration | `default` |
5454
| `reloader.ignoreNamespaces` | List of comma separated namespaces to ignore, if multiple are provided, they are combined with the AND operator | string | `""` |
55-
| `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 | `""` |
55+
| `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 | `""` |
5656
| `reloader.resourceLabelSelector` | List of comma separated label selectors, if multiple are provided they are combined with the AND operator | string | `""` |
5757
| `reloader.logFormat` | Set type of log format. Value could be either `json` or `""` | string | `""` |
5858
| `reloader.watchGlobally` | Allow Reloader to watch in all namespaces (`true`) or just in a single namespace (`false`) | boolean | `true` |

deployments/kubernetes/chart/reloader/templates/_helpers.tpl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,12 @@ Create the annotations to support helm3
7070
meta.helm.sh/release-namespace: {{ .Release.Namespace | quote }}
7171
meta.helm.sh/release-name: {{ .Release.Name | quote }}
7272
{{- end -}}
73+
74+
{{/*
75+
Create the namespace selector if it does not watch globally
76+
*/}}
77+
{{- define "reloader-namespaceSelector" -}}
78+
{{- if and .Values.reloader.watchGlobally .Values.reloader.namespaceSelector -}}
79+
{{ .Values.reloader.namespaceSelector }}
80+
{{- end -}}
81+
{{- end -}}

deployments/kubernetes/chart/reloader/templates/clusterrole.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ rules:
3131
- list
3232
- get
3333
- watch
34-
{{- if .Values.reloader.namespaceSelector }}
34+
{{- if (include "reloader-namespaceSelector" .) }}
3535
- apiGroups:
3636
- ""
3737
resources:

deployments/kubernetes/chart/reloader/templates/deployment.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ spec:
198198
{{- . | toYaml | nindent 10 }}
199199
{{- end }}
200200
{{- end }}
201-
{{- 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)}}
201+
{{- 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)}}
202202
args:
203203
{{- if .Values.reloader.logFormat }}
204204
- "--log-format={{ .Values.reloader.logFormat }}"
@@ -215,8 +215,8 @@ spec:
215215
{{- if .Values.reloader.ignoreNamespaces }}
216216
- "--namespaces-to-ignore={{ .Values.reloader.ignoreNamespaces }}"
217217
{{- end }}
218-
{{- if .Values.reloader.namespaceSelector }}
219-
- "--namespace-selector={{ .Values.reloader.namespaceSelector }}"
218+
{{- if (include "reloader-namespaceSelector" .) }}
219+
- "--namespace-selector=\"{{ include "reloader-namespaceSelector" . }}\""
220220
{{- end }}
221221
{{- if .Values.reloader.resourceLabelSelector }}
222222
- "--resource-label-selector={{ .Values.reloader.resourceLabelSelector }}"

internal/pkg/cmd/reloader.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,9 +128,11 @@ func startReloader(cmd *cobra.Command, args []string) {
128128
}
129129

130130
logrus.Info("Starting Reloader")
131+
isGlobal := false
131132
currentNamespace := os.Getenv("KUBERNETES_NAMESPACE")
132133
if len(currentNamespace) == 0 {
133134
currentNamespace = v1.NamespaceAll
135+
isGlobal = true
134136
logrus.Warnf("KUBERNETES_NAMESPACE is unset, will detect changes in all namespaces.")
135137
}
136138

@@ -150,7 +152,7 @@ func startReloader(cmd *cobra.Command, args []string) {
150152
logrus.Fatal(err)
151153
}
152154

153-
namespaceLabelSelector, err := getNamespaceLabelSelector(cmd)
155+
namespaceLabelSelector, err := getNamespaceLabelSelector(cmd, isGlobal)
154156
if err != nil {
155157
logrus.Fatal(err)
156158
}
@@ -215,7 +217,7 @@ func getIgnoredNamespacesList(cmd *cobra.Command) (util.List, error) {
215217
return getStringSliceFromFlags(cmd, "namespaces-to-ignore")
216218
}
217219

218-
func getNamespaceLabelSelector(cmd *cobra.Command) (string, error) {
220+
func getNamespaceLabelSelector(cmd *cobra.Command, isGlobal bool) (string, error) {
219221
slice, err := getStringSliceFromFlags(cmd, "namespace-selector")
220222
if err != nil {
221223
logrus.Fatal(err)
@@ -246,6 +248,11 @@ func getNamespaceLabelSelector(cmd *cobra.Command) (string, error) {
246248
logrus.Fatal(err)
247249
}
248250

251+
if !isGlobal && len(namespaceLabelSelector) > 0 {
252+
logrus.Warnf("KUBERNETES_NAMESPACE is set but also namespace-selector is set, will ignore the filter and detect changes in the specific namespace.")
253+
return "", nil
254+
}
255+
249256
return namespaceLabelSelector, nil
250257
}
251258

0 commit comments

Comments
 (0)