首页 > 解决方案 > 在 GCP 中,如何在容器 k8s 集群中添加标签,而不是更新?

问题描述

我正在尝试添加标签k8s cluster,但不幸的是它删除了所有添加的新标签。

gcloud container clusters update example-cluster --zone us-west1-a --update-labels env=dev

我想在现有集群中添加标签(保留以前的标签)。有没有办法做到这一点?

谢谢

标签: google-cloud-platformgcloud

解决方案


因此,我在自动化创建中添加了几行 python 代码来解决上述问题。

cmd = "gcloud container clusters list --format='json(name,status,zone,resourceLabels)'"
a = { "container" :  json.loads(subprocess.check_output(shlex.split(cmd)))}
labels = a['container'][0]['resourceLabels']
labels["name"]= a['container'][0]['name']  #Adding new label
new_labels = (str(labels).replace("{","").replace("}", "").replace(" ", "").replace(":", "=").replace("'", ""))
CMD = "gcloud container clusters update {0} --zone {1} --project {2} --update-labels {3} ".format(rname, zone, PROJECTID, labels)
subprocess.call(CMD, shell=True) 

推荐阅读