What's the shortest way to add a label to a Pod using the Kubernetes go-client

后端 未结 2 450
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-27 05:18

I have a demo golang program to list Pods without a particular label. I want to modify it so it also can add a label to each pod.

(I\'m using the AWS hosted Kubernetes s

2条回答
  •  一生所求
    2021-01-27 05:53

    I was trying to add a new label to a node using client-go, based on OP's code snippet, the shortest path that I used is as follow.

    labelPatch := fmt.Sprintf(`[{"op":"add","path":"/metadata/labels/%s","value":"%s" }]`, labelkey, labelValue)
    _, err = kc.CoreV1().Nodes().Patch(node.Name, types.JSONPatchType, []byte(labelPatch))
    

    Note: add to /metadata/labels will overwrite all existing labels, so I choose the path to /metadata/labels/${LABEL_KEY} to only add the new label

提交回复
热议问题