Skip to content

Commit 4c42681

Browse files
authoredApr 8, 2024
Merge pull request #604 from kichel98/issue-582-auto-annotation-with-resource-type-squashed
[#582] Allow to use auto annotation with specific resource type (configmap or secret)
2 parents ae5bc2c + 1cae7a0 commit 4c42681

File tree

8 files changed

+410
-135
lines changed

8 files changed

+410
-135
lines changed
 

‎README.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ spec:
4949
5050
This will discover deploymentconfigs/deployments/daemonsets/statefulset/rollouts automatically where `foo-configmap` or `foo-secret` is being used either via environment variable or from volume mount. And it will perform rolling upgrade on related pods when `foo-configmap` or `foo-secret`are updated.
5151

52-
You can restrict this discovery to only `ConfigMap` or `Secret` objects that
52+
You can filter it by the type of monitored resource and use typed versions of `auto` annotation. If you want to discover changes only in mounted `Secret`s and ignore changes in `ConfigMap`s, add `secret.reloader.stakater.com/auto` annotation instead. Analogously, you can use `configmap.reloader.stakater.com/auto` annotation to look for changes in mounted `ConfigMap`, changes in any of mounted `Secret`s will not trigger a rolling upgrade on related pods.
53+
54+
You can also restrict this discovery to only `ConfigMap` or `Secret` objects that
5355
are tagged with a special annotation. To take advantage of that, annotate
5456
your deploymentconfigs/deployments/daemonsets/statefulset/rollouts like this:
5557

@@ -84,11 +86,13 @@ will always restart upon a change in configmaps or secrets it uses, regardless
8486
of whether they have the `reloader.stakater.com/match: "true"` annotation or
8587
not.
8688

89+
Similarly, `reloader.stakater.com/auto` and its typed version (`secret.reloader.stakater.com/auto` or `configmap.reloader.stakater.com/auto`) do not work together. If you have both annotations in your deployment, then only one of them needs to be true to trigger the restart. For example, having both `reloader.stakater.com/auto: "true"` and `secret.reloader.stakater.com/auto: "false"` or both `reloader.stakater.com/auto: "false"` and `secret.reloader.stakater.com/auto: "true"` will restart upon a change in a secret it uses.
90+
8791
We can also specify a specific configmap or secret which would trigger rolling upgrade only upon change in our specified configmap or secret, this way, it will not trigger rolling upgrade upon changes in all configmaps or secrets used in a `deploymentconfig`, `deployment`, `daemonset`, `statefulset` or `rollout`.
8892
To do this either set the auto annotation to `"false"` (`reloader.stakater.com/auto: "false"`) or remove it altogether, and use annotations for [Configmap](.#Configmap) or [Secret](.#Secret).
8993
9094
It's also possible to enable auto reloading for all resources, by setting the `--auto-reload-all` flag.
91-
In this case, all resources that do not have the auto annotation set to `"false"`, will be reloaded automatically when their ConfigMaps or Secrets are updated.
95+
In this case, all resources that do not have the auto annotation (or its typed version) set to `"false"`, will be reloaded automatically when their ConfigMaps or Secrets are updated.
9296
Notice that setting the auto annotation to an undefined value counts as false as-well.
9397

9498
### Configmap
@@ -154,6 +158,8 @@ spec:
154158
- `reloader.stakater.com/auto: "true"` will only reload the pod, if the configmap or secret is used (as a volume mount or as an env) in `DeploymentConfigs/Deployment/Daemonsets/Statefulsets`
155159
- `secret.reloader.stakater.com/reload` or `configmap.reloader.stakater.com/reload` annotation will reload the pod upon changes in specified configmap or secret, irrespective of the usage of configmap or secret.
156160
- you may override the auto annotation with the `--auto-annotation` flag
161+
- you may override the secret typed auto annotation with the `--secret-auto-annotation` flag
162+
- you may override the configmap typed auto annotation with the `--configmap-auto-annotation` flag
157163
- you may override the search annotation with the `--auto-search-annotation` flag
158164
and the match annotation with the `--search-match-annotation` flag
159165
- you may override the configmap annotation with the `--configmap-annotation` flag

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

+8
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ spec:
204204
- "--auto-annotation"
205205
- "{{ .Values.reloader.custom_annotations.auto }}"
206206
{{- end }}
207+
{{- if .Values.reloader.custom_annotations.secret_auto }}
208+
- "--secret-auto-annotation"
209+
- "{{ .Values.reloader.custom_annotations.secret_auto }}"
210+
{{- end }}
211+
{{- if .Values.reloader.custom_annotations.configmap_auto }}
212+
- "--configmap-auto-annotation"
213+
- "{{ .Values.reloader.custom_annotations.configmap_auto }}"
214+
{{- end }}
207215
{{- if .Values.reloader.custom_annotations.search }}
208216
- "--auto-search-annotation"
209217
- "{{ .Values.reloader.custom_annotations.search }}"

‎internal/pkg/cmd/reloader.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ func NewReloaderCommand() *cobra.Command {
3535
cmd.PersistentFlags().BoolVar(&options.AutoReloadAll, "auto-reload-all", false, "Auto reload all resources")
3636
cmd.PersistentFlags().StringVar(&options.ConfigmapUpdateOnChangeAnnotation, "configmap-annotation", "configmap.reloader.stakater.com/reload", "annotation to detect changes in configmaps, specified by name")
3737
cmd.PersistentFlags().StringVar(&options.SecretUpdateOnChangeAnnotation, "secret-annotation", "secret.reloader.stakater.com/reload", "annotation to detect changes in secrets, specified by name")
38-
cmd.PersistentFlags().StringVar(&options.ReloaderAutoAnnotation, "auto-annotation", "reloader.stakater.com/auto", "annotation to detect changes in secrets")
38+
cmd.PersistentFlags().StringVar(&options.ReloaderAutoAnnotation, "auto-annotation", "reloader.stakater.com/auto", "annotation to detect changes in secrets/configmaps")
39+
cmd.PersistentFlags().StringVar(&options.ConfigmapReloaderAutoAnnotation, "configmap-auto-annotation", "configmap.reloader.stakater.com/auto", "annotation to detect changes in configmaps")
40+
cmd.PersistentFlags().StringVar(&options.SecretReloaderAutoAnnotation, "secret-auto-annotation", "secret.reloader.stakater.com/auto", "annotation to detect changes in secrets")
3941
cmd.PersistentFlags().StringVar(&options.AutoSearchAnnotation, "auto-search-annotation", "reloader.stakater.com/search", "annotation to detect changes in configmaps or secrets tagged with special match annotation")
4042
cmd.PersistentFlags().StringVar(&options.SearchMatchAnnotation, "search-match-annotation", "reloader.stakater.com/match", "annotation to mark secrets or configmaps to match the search")
4143
cmd.PersistentFlags().StringVar(&options.LogFormat, "log-format", "", "Log format to use (empty string for text, or JSON")

‎internal/pkg/handler/upgrade.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,18 @@ func PerformRollingUpgrade(clients kube.Clients, config util.Config, upgradeFunc
197197
annotationValue, found := annotations[config.Annotation]
198198
searchAnnotationValue, foundSearchAnn := annotations[options.AutoSearchAnnotation]
199199
reloaderEnabledValue, foundAuto := annotations[options.ReloaderAutoAnnotation]
200-
if !found && !foundAuto && !foundSearchAnn {
200+
typedAutoAnnotationEnabledValue, foundTypedAuto := annotations[config.TypedAutoAnnotation]
201+
if !found && !foundAuto && !foundTypedAuto && !foundSearchAnn {
201202
annotations = upgradeFuncs.PodAnnotationsFunc(i)
202203
annotationValue = annotations[config.Annotation]
203204
searchAnnotationValue = annotations[options.AutoSearchAnnotation]
204205
reloaderEnabledValue = annotations[options.ReloaderAutoAnnotation]
206+
typedAutoAnnotationEnabledValue = annotations[config.TypedAutoAnnotation]
205207
}
206208
result := constants.NotUpdated
207209
reloaderEnabled, _ := strconv.ParseBool(reloaderEnabledValue)
208-
if reloaderEnabled || reloaderEnabledValue == "" && options.AutoReloadAll {
210+
typedAutoAnnotationEnabled, _ := strconv.ParseBool(typedAutoAnnotationEnabledValue)
211+
if reloaderEnabled || typedAutoAnnotationEnabled || reloaderEnabledValue == "" && typedAutoAnnotationEnabledValue == "" && options.AutoReloadAll {
209212
result = invokeReloadStrategy(upgradeFuncs, i, config, true)
210213
}
211214

0 commit comments

Comments
 (0)