client-go 动态补丁不能去掉nodeSelector标签?

client-go dynamic patch can not remove nodeSelector labels?

客户端版本:v0.15.10

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

这是原始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() 不进行替换?

Notice that the tolerations list in the PodSpec was replaced, not merged

替换策略适用于列表,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

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

如果对象中的每一个元素都可以直接清楚地表示出来,就没有必要替换任何东西。