0

客户端版本:v0.15.10

当 PatchType 为 MergePatchType 或 StrategicMergePatchType 时,无法从 Deployment nodeSelector 中删除 lebels?

这是原始 yaml 文件“test1.yaml”:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  labels:
    k8s-app: frontgateway
  name: frontgateway
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: frontgateway
  template:
    metadata:
      labels:
        k8s-app: frontgateway
    spec:
      nodeSelector:
        CLUSTER: WX
        GROUP: IAD

补丁代码:

playLoadBytes,_ :=json.Marshal(unstructuredObj)
_,err=DynamicClient.Resource(mapping.Resource).Namespace(namespace).Patch(name,types.StrategicMergePatchType,playLoadBytes,metav1.PatchOptions{})

补丁yaml:

apiVersion: apps/v1beta2
kind: Deployment
metadata:
  labels:
    k8s-app: frontgateway
  name: frontgateway
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: frontgateway
  template:
    metadata:
      labels:
        k8s-app: frontgateway
    spec:
      nodeSelector:
        GROUP: IAD

当我从 test1.yaml 中删除“CLUSTER: WX”行并执行 patch() 方法时,部署资源仍然具有“CLUSTER: WX”标签,但添加新标签可以工作。

看了官方文档https://kubernetes.io/docs/tasks/manage-kubernetes-objects/update-api-object-kubectl-patch/,上面写着Notice that the tolerations list in the PodSpec was replaced, not merged. This is because the Tolerations field of PodSpec does not have a patchStrategy key in its field tag. So the strategic merge patch uses the default patch strategy, which is replace.

所以我检查了Kubernetes源代码中NodeSelector的字段标签,:

NodeSelector map[string]string `json:"nodeSelector,omitempty" protobuf:"bytes,7,rep,name=nodeSelector"`

没有“patchStrategy”标签,那么为什么 patch() 不做替换?

4

1 回答 1

0

请注意, PodSpec 中的 tolerations列表已被替换,而不是合并

replace 策略适用于列表,nodeSelector 不是列表。

spec:
  template:
    spec:
      tolerations:
      - effect: NoSchedule     <- list
        key: disktype
        value: ssd

spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: frontgateway
  template:
    metadata:
      labels:
        k8s-app: frontgateway
    spec:
      nodeSelector:
        CLUSTER: WX        <- not a list
        GROUP: IAD

使用列表替换是因为列表的顺序很容易弄乱,并且很难预测您引用的是列表的哪个元素。这就是为什么替换整个列表更容易的原因。

如果您可以直接清楚地处理每个元素的对象,则无需替换任何内容。

于 2021-01-19T11:11:55.610 回答