fix: merge serviceMonitor additionalLabels with chart defaults#2365
Open
Shion1305 wants to merge 1 commit into
Open
fix: merge serviceMonitor additionalLabels with chart defaults#2365Shion1305 wants to merge 1 commit into
Shion1305 wants to merge 1 commit into
Conversation
The ServiceMonitor template appended `metrics.serviceMonitor.additionalLabels` verbatim under `metadata.labels`, producing duplicate keys when the user overrode a label that the `harbor.labels` helper already emits (most commonly `release`, set to `kube-prometheus-stack` for Prometheus Operator discovery). Strict YAML validators (kubeconform --strict, kubectl-validate) reject the output; the API server silently keeps one entry, unpredictably. Merge `additionalLabels` over the helper-provided defaults so user-supplied keys win and the rendered `metadata.labels` map has a single value per key. Closes goharbor#2364 Signed-off-by: Shion Ichikawa <shion1305@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #2364.
Problem
templates/metrics/metrics-svcmon.yamlrenderedmetadata.labelsby emitting theharbor.labelshelper output and appendingmetrics.serviceMonitor.additionalLabelsverbatim. When a user supplies a label that the helper already emits — most commonlyrelease: kube-prometheus-stackso kube-prometheus-stack'srelease: kube-prometheus-stackselector picks the ServiceMonitor up — the rendered manifest contains tworelease:keys in the same map.This is invalid YAML by strict-schema rules and is rejected by client-side validators (
kubeconform --strict,kubectl-validate, GitOps render-validation pipelines). The Kubernetes API server silently keeps one entry, unpredictably.Reproduce (before this PR)
Fix
Merge
additionalLabelsover the helper-provided defaults before rendering, so user-supplied keys win and the renderedmetadata.labelsmap has a single value per key. This matches the convention used by most charts that combine helper labels withadditionalLabels/labelsoverrides.mergekeeps keys already present in the destination (the user'sadditionalLabels) and only fills in missing keys from$defaults— so user overrides win, and the no-override case (empty dict) is unchanged.Verification
helm lint .→ pass (only the pre-existingenginefield warning).helm unittest -f 'test/unittest/*/*.yaml' .→ 115/115 pass (3 new).release: harbor(chart default preserved):release: kube-prometheus-stack(override wins):helm template ... --set ...additionalLabels.release=kube-prometheus-stack | kubeconform -strict -kubernetes-version 1.31.0 -ignore-missing-schemas -→ no errors.Tests added
test/unittest/metrics/metrics_svcmon_test.yaml— 3 cases covering:additionalLabelsset.additionalLabelskeys are merged alongside chart defaults.additionalLabels.releaseoverrides the chart defaultreleasevalue (single key, override wins).Background
Found while building a
kubeconform --strictrender-validation pipeline for a GitOps repo (every PR re-renders all ArgoCD apps and validates the output). The duplicate-key warning surfaced as soon asrelease: kube-prometheus-stackwas set — the only way to make Prometheus Operator pick this ServiceMonitor up alongside everything else managed by kube-prometheus-stack.