Skip to content

fix(helm): exclude commonLabels from immutable selector matchLabels#2571

Merged
pepol merged 5 commits intowundergraph:mainfrom
Cloudsky01:fix/helm-commonLabels-selector
Mar 4, 2026
Merged

fix(helm): exclude commonLabels from immutable selector matchLabels#2571
pepol merged 5 commits intowundergraph:mainfrom
Cloudsky01:fix/helm-commonLabels-selector

Conversation

@Cloudsky01
Copy link
Copy Markdown
Contributor

@Cloudsky01 Cloudsky01 commented Feb 28, 2026

commonLabels values were included in the selectorLabels helper, which is used for spec.selector.matchLabels. Since Kubernetes makes this field immutable after creation, adding or changing commonLabels on an existing release fails with "field is immutable" as stated in #1523

Move commonLabels from selectorLabels into the labels helper (deployment metadata) and add them to pod template metadata.labels directly. This preserves commonLabels on all resource metadata and pod labels. This was first proposed here

Affected charts: cdn, controlplane, graphqlmetrics, otelcollector, router, studio.

This should (hopefully) fix #1523

Summary by CodeRabbit

  • New Features
    • Charts now support applying user-provided common labels to resource metadata across all components, enabling consistent organization and filtering.
    • Labels are added to pod/template metadata while selector labels remain immutable, preserving Kubernetes selector immutability and avoiding selector drift.

Checklist

  • I have discussed my proposed changes in an issue and have received approval to proceed.
  • I have followed the coding standards of the project.
  • Tests or benchmarks have been added or updated. N/A ?
  • Documentation has been updated on https://github.com/wundergraph/cosmo-docs. N/A ?
  • I have read the Contributors Guide.

commonLabels values were included in the selectorLabels helper, which
is used for spec.selector.matchLabels. Since Kubernetes makes this field
immutable after creation, adding or changing commonLabels on an existing
release fails with "field is immutable".

Move commonLabels from selectorLabels into the labels helper (deployment
metadata) and add them to pod template metadata.labels directly. This
preserves commonLabels on all resource metadata and pod labels without
polluting the immutable selector.

Affected charts: cdn, controlplane, graphqlmetrics, otelcollector,
router, studio.

Fixes wundergraph#1523
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 28, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 0692c63a-a78c-4c3a-bc07-93d74ccff63c

📥 Commits

Reviewing files that changed from the base of the PR and between f0ee17a and 9bac667.

📒 Files selected for processing (1)
  • helm/cosmo/charts/controlplane/templates/deployment.yaml
🚧 Files skipped from review as they are similar to previous changes (1)
  • helm/cosmo/charts/controlplane/templates/deployment.yaml

Walkthrough

Moved rendering of .Values.commonLabels out of selector label templates and into pod metadata labels across Helm charts, so commonLabels are emitted in top-level labels but not in selector.matchLabels to avoid immutable selector conflicts.

Changes

Cohort / File(s) Summary
Template helpers
helm/cosmo/charts/cdn/templates/_helpers.tpl, helm/cosmo/charts/controlplane/templates/_helpers.tpl, helm/cosmo/charts/graphqlmetrics/templates/_helpers.tpl, helm/cosmo/charts/otelcollector/templates/_helpers.tpl, helm/cosmo/charts/router/templates/_helpers.tpl, helm/cosmo/charts/studio/templates/_helpers.tpl
Removed iteration over .Values.commonLabels from selectorLabels templates and added iteration over .Values.commonLabels inside the corresponding labels templates. Updated explanatory comments to reflect that commonLabels are rendered in labels (not in immutable selectors).
Deployment templates
helm/cosmo/charts/cdn/templates/deployment.yaml, helm/cosmo/charts/controlplane/templates/deployment.yaml, helm/cosmo/charts/graphqlmetrics/templates/deployment.yaml, helm/cosmo/charts/otelcollector/templates/deployment.yaml, helm/cosmo/charts/router/templates/deployment.yaml, helm/cosmo/charts/studio/templates/deployment.yaml
Inserted a loop over .Values.commonLabels into pod template metadata.labels, emitting each key/value pair (values quoted) so commonLabels appear on Pod/Deployment metadata but are not included in spec.selector.matchLabels.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main objective: excluding commonLabels from immutable selector matchLabels to fix Helm upgrade failures.
Linked Issues check ✅ Passed All code changes directly address issue #1523 by removing commonLabels from selectorLabels and adding them to pod template metadata labels instead.
Out of Scope Changes check ✅ Passed All changes are scoped to fixing the commonLabels/selector issue across the six affected charts; no unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@helm/cosmo/charts/controlplane/templates/deployment.yaml`:
- Around line 47-49: When merging .Values.commonLabels into the pod template
labels (the {{- range $key, $value := .Values.commonLabels }} block), guard
against overriding selector keys so spec.selector.matchLabels (the Deployment
selector labels such as app.kubernetes.io/name and app.kubernetes.io/instance)
remain authoritative; modify the merge to either filter out those selector keys
(skip keys "app.kubernetes.io/name" and "app.kubernetes.io/instance" when
iterating .Values.commonLabels) or move the explicit selector labels to be
applied after the commonLabels merge so they win, ensuring the pod labels still
match spec.selector.matchLabels.

In `@helm/cosmo/charts/graphqlmetrics/templates/deployment.yaml`:
- Around line 35-37: User-supplied commonLabels are currently rendered into pod
template labels unconditionally, which can override critical selector labels
like app.kubernetes.io/name and app.kubernetes.io/instance and break
spec.selector.matchLabels vs spec.template.metadata.labels; update the template
loop that renders .Values.commonLabels so it skips/filters out keys that
conflict with the Deployment selector labels (at minimum
"app.kubernetes.io/name" and "app.kubernetes.io/instance", or any other keys you
use in spec.selector.matchLabels) before emitting them, and apply the same
guarded pattern to all other deployment templates (cdn, controlplane,
otelcollector, router, studio) so the selector and pod template labels always
remain consistent.

In `@helm/cosmo/charts/otelcollector/templates/deployment.yaml`:
- Around line 35-37: The patch appends .Values.commonLabels into
spec.template.metadata.labels after the selector labels, which allows
user-provided keys like app.kubernetes.io/name or app.kubernetes.io/instance in
commonLabels to override spec.selector.matchLabels; fix by ensuring selector
keys take precedence—merge commonLabels before or only add keys that do not
collide with selector matchLabels so spec.template.metadata.labels remains
identical to spec.selector.matchLabels; update the template rendering around the
labels loop (the block that iterates {{- range $key, $value :=
.Values.commonLabels }}) to skip keys present in the selector or to render
commonLabels before emitting selector labels so selector labels always win.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3168ab0 and f0ee17a.

📒 Files selected for processing (12)
  • helm/cosmo/charts/cdn/templates/_helpers.tpl
  • helm/cosmo/charts/cdn/templates/deployment.yaml
  • helm/cosmo/charts/controlplane/templates/_helpers.tpl
  • helm/cosmo/charts/controlplane/templates/deployment.yaml
  • helm/cosmo/charts/graphqlmetrics/templates/_helpers.tpl
  • helm/cosmo/charts/graphqlmetrics/templates/deployment.yaml
  • helm/cosmo/charts/otelcollector/templates/_helpers.tpl
  • helm/cosmo/charts/otelcollector/templates/deployment.yaml
  • helm/cosmo/charts/router/templates/_helpers.tpl
  • helm/cosmo/charts/router/templates/deployment.yaml
  • helm/cosmo/charts/studio/templates/_helpers.tpl
  • helm/cosmo/charts/studio/templates/deployment.yaml

Copy link
Copy Markdown
Member

@pepol pepol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Copy Markdown
Contributor

@StarpTech StarpTech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@pepol pepol enabled auto-merge (squash) March 4, 2026 17:38
@pepol pepol merged commit 5758b4f into wundergraph:main Mar 4, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using commonLabels in helm charts breaks selectors and thus patch deployments

4 participants