3

I'm trying to remove a key/value pair from an existing deployment's spec.selector.matchLabels config. For example, I'm trying to remove the some.old.label: blah label from spec.selector.matchLabels and spec.template.metadata.labels. So this is an excerpt of what I'm sending to kubectl apply -f:

    spec:
      selector:
        matchLabels:
          app: my-app
      template:
        metadata:
          labels:
            app: my-app

but that gives me the following error:

selector does not match template labels

I also tried kubectl replace, which gives me this error:

v1.LabelSelector{MatchLabels:map[string]string{“app”: "my-app"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable

which makes sense once I checked the deployment's config in prod:

metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
       # my config is trying to mutate the matchLabels here:
       {"apiVersion":"apps/v1", ... "selector":{"matchLabels":{"app":"my-app"} ... }
  # etc...
spec:
  selector:
    matchLabels:
      app: my-app
      some.old.label: blah  # how do I remove this label from both the metadata.labels and matchLabels?
  template:
    metadata:
      labels:
        app: my-app
        some.old.label: blah # I want to remove this label

Notice how the some.old.label: blah key/value is set under selector.matchLabels and template.metadata.labels.

Will I have to delete-then-recreate my deployment? Or perhaps call kubectl replace --force?

Notes

I came across this section in the Kubernetes Deployment docs:

Selector removals removes an existing key from the Deployment selector -- do not require any changes in the Pod template labels. Existing ReplicaSets are not orphaned, and a new ReplicaSet is not created, but note that the removed label still exists in any existing Pods and ReplicaSets.

as well as this PR and this Github issue which speak about the reasoning behind the problem, but I can't figure out how I'm can safely update my deployment to remove this selector.

1 Answer 1

5

When the error message says "field is immutable", it means you can't change it once it's been set. You need to delete and recreate the Deployment with the label selector you want (which will also temporarily delete all of the matching Pods).

kubectl delete deployment my-app
kubectl apply -f ./deployment.yaml
3
  • 1
    That makes sense - I guess delete-then-create is the only way to make this work. Thanks for the answer! It sounds like I can also run kubectl apply --force -f ./deployment.yaml, or kubectl replace --force -f ./deployment.yaml, which is the same as above, but in a single command.
    – modulitos
    May 25, 2021 at 0:47
  • 1
    I guess I was confused because the doc linked above says "Selector removals removes an existing key from the Deployment selector...", which makes it sounds like a label selector can be removed from a Deployment without having to delete-then-recreate the Deployment...
    – modulitos
    May 25, 2021 at 1:03
  • @modulitos I had the same confusion! I suspect that part of the doc is left over from previous versions of the Kubernetes API, before the selector was made immutable in version apps/v1. And yes, you can achieve the same thing with kubectl replace --force
    – Seth
    Jan 7, 2022 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.