简体   繁体   English

使用“kubectl patch”更新部署标签在 v1.18 中不起作用

[英]Update deployment labels using “kubectl patch” does not work in v1.18

I am trying to update a label using kubectl v1.18.我正在尝试使用 kubectl v1.18 更新 label。

I tried kubectl patch deployment my-deployment --patch "$(cat patch1.yaml)";我试过 kubectl 补丁部署 my-deployment --patch "$(cat patch1.yaml)"; it returns an error它返回一个错误

The Deployment "my-deployment" is invalid: spec.selector: Invalid value: v1.LabelSelector{MatchLabels:map[string]string{"app":"nginx"}, MatchExpressions:[]v1.LabelSelectorRequirement(nil)}: field is immutable部署“my-deployment”无效:spec.selector:无效值:v1.LabelSelector{MatchLabels:map[string]string{"app":"nginx"},MatchExpressions:[]v1.LabelSelectorRequirement(nil)}:字段是不可变的

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    client: user
  name:  my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: revproxy
  strategy:
     type: RollingUpdate
     rollingUpdate:
       maxUnavailable: 1
       maxSurge: 2
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: revproxy
    spec:
      containers:
      - image: nginx:1.7.9
        name: nginx
        resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: Always

The patch yaml is补丁 yaml 是

spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx

From the docs here这里的文档

Note: In API version apps/v1, a Deployment's label selector is immutable after it gets created.注意:在 API 版本 apps/v1 中,Deployment 的 label 选择器在创建后是不可变的。

Motivations for making label selector immutable are使 label 选择器不可变的动机是

  1. Changing selectors leads to undefined behaviors - users are not expected to change the selectors更改选择器会导致未定义的行为 - 用户不应该更改选择器
  2. Having selectors immutable ensures they always match created children, preventing events such as accidental bulk orphaning让选择器不可变可确保它们始终匹配创建的子代,防止意外批量孤儿等事件

If you want to make modification to label selector you will have to delete the exiting deployment and recreate it.如果要修改 label 选择器,则必须删除现有部署并重新创建它。

Modification to only metadata.labels should work though.只修改metadata.labels应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM