问题描述
客户端版本: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的field标签,:
NodeSelector map[string]string `json:"nodeSelector,omitempty" protobuf:"bytes,7,rep,name=nodeSelector"`
没有“patchStrategy”标签,那么为什么 patch() 不进行替换?
解决方法
注意 PodSpec 中的 tolerations list 被替换了,没有合并
替换策略适用于列表,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
使用列表替换是因为列表的顺序很容易弄乱,而且很难预测您引用的是列表的哪个元素。这就是替换整个列表更容易的原因。
如果对象可以直接清晰地处理每个元素,则无需替换任何内容。