Skip to content

Upgrade fails when deployment.spect.template has a chart label #2494

Closed

Description

@ahawkins

I'm not sure why this is happening. I went ahead and added a chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}" to a Deployment's spec.template.metadata.labels. I upgraded the chart, and got this error:

Error: UPGRADE FAILED: Deployment.extensions "test-demo" is invalid: spec.template.metadata.labels: Invalid value: {"app":"test-demo","chart":"demo-0.1.1"}: `selector` does not match template `labels`

It seems that this label should not be applied to a deployment template?

Versions

Client: &version.Version{SemVer:"v2.2.3", GitCommit:"1402a4d6ec9fb349e17b912e32fe259ca21181e3", GitTreeState:"clean"}
Server: &version.Version{SemVer:"v2.2.2", GitCommit:"1b330722aafcb8123114ae51f69d1e884a326f3e", GitTreeState:"clean"}

Reproduction Steps

helm init demo
helm install --release test demo/
# add to the deployment's spect template labels: chart: "{{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}"
# bump the version
helm upgrade test demo/

Suggestion

I'm guessing that helm expects certain labels for anything in the chart. There are also cases were labels must not be present. helm lint should cover this.

Activity

  1. seh commented on May 25, 2017

    @seh
    Contributor

    This is a restriction in Kubernetes itself, unrelated to Helm:

    .spec.selector is an optional field that specifies a label selector for the Pods targeted by this deployment.
    If specified, .spec.selector must match .spec.template.metadata.labels, or it will be rejected by the API. If .spec.selector is unspecified, .spec.selector.matchLabels will be defaulted to .spec.template.metadata.labels.

    I take it you have a .spec.selector in your Deployment.

  2. mikesplain commented on Jun 28, 2017

    @mikesplain

    Just for future reference I ran into this because I was adding chart: "{{.Chart.Name}}-{{.Chart.Version}}" as .spec.template.metadata.labels which would also become .spec.selector.matchLabels after the first release. On the second release if I bumped the version number, I'd get the above error (since .spec.selector.matchLabels and .spec.template.metadata.labels would now be different). Removing the chart label from .spec.template.metadata.labels and only using it in .metadata.labels seems to make it work normally.

  3. jkamenik commented on Oct 20, 2017

    @jkamenik

    I have this situation in a production deployment. Is there a combination of steps I can perform to change the .spec.selector along with the .spec.template.metadata.labels to fix this?

  4. bacongobbler commented on Oct 31, 2017

    @bacongobbler
    Member

    deployment spec selectors are no longer mutable as of apps/v1beta2 and this is noted upstream in kubernetes/kubernetes#50719. Short of helm delete --purge myrelease and running helm install again on your chart, there is no way to mutate the selector labels.

  5. bacongobbler commented on Oct 31, 2017

    @bacongobbler
    Member

    closing as a wontfix/working as intended according to upstream.

  6. rcorre commented on Jun 7, 2018

    @rcorre

    @bacongobbler should https://github.com/kubernetes/helm/blob/master/docs/chart_best_practices/labels.md#standard-labels be updated to stop recommending a chart={{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }} label? Seems like anyone following the "best practices" will encounter this issue.

    I had a thought that maybe the chart label would be fine on the pod metadata as long as it is not in the selector, but it seems that is disallowed:

    .spec.selector must match .spec.template.metadata.labels, or it will be rejected by the API.

  7. rcorre commented on Jun 7, 2018

    @rcorre

    Short of helm delete --purge myrelease and running helm install again on your chart, there is no way to mutate the selector labels.

    Is --purge overkill? I'm not sure how wiping out your release history would help -- just deleting should remove the deployment and reset helm's internal state.

  8. desaintmartin commented on Aug 24, 2018

    @desaintmartin

    What is the official recommendation from Helm for Deployments regarding this issue? Simply removing .spec.selector.matchLabels? Deleting/recreating a Release or part of it during an upgrade is NOT an option in production, and --force would break any RollingUpdate.

  9. jkamenik commented on Aug 24, 2018

    @jkamenik

    Not sure this is "official" but what I did was describe the deployments that were failing then I hand edited the chart to make the labels static. That way when helm upgrades the charts those items are not seen as changed.

    And I learned from this not to put dynamic data in labels.

  10. rcorre commented on Dec 4, 2018

    @rcorre

    It has been about 6 months and the best practices still recommend a chart label.

    I'm seeing a lot of teams shooting themselves in the foot with this. You create a chart, deploy it successfully, everything seems fine. Then later on you try to upgrade and realize your only option is to delete --purge and incur downtime.

    I see some mention of this in https://github.com/helm/charts/issues/3011, but that seems to be some large meta-issue for a repo separate from these docs.

  11. Draiken commented on Mar 28, 2019

    @Draiken

    @rcorre I do think you can manually fix the issue. It's not what Helm should force you to do, but it is a workaround.
    You can remove the deployment/replicaset (without cascading, keeping the pods alive), then update the pods labels (and anything that depends on them, like services) then redeploy. The new deployment should recognize those same pods as up already. If not, the new deployment should go live and you can manually remove the old pods with no downtime.
    I believe a teammate of mine did this successfully, but I'd definitely recommend testing it out first 🙂
    Edit: See below for a proven method

  12. desaintmartin commented on Mar 28, 2019

    @desaintmartin
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions