如何 edit/patch kubernetes 部署使用 python 添加标签

How to edit/patch kubernetes deployment to add label using python

我是 kubernetes 的新手 - 我开发了 Web UI/API,它使用 Azure 机器学习服务将模型部署自动化到 Azure Kubernetes 服务 (AKS)。作为强化措施,我打算使用 this documentation. One of the step is to edit the deployment to add identity-feature label at /spec/template/metadata/labels for the deployment (see para starting like Edit the deployment to add ... in this section 为在 AKS 中部署的 pods 设置托管标识。

我希望使用 python kubernetes 客户端 (https://github.com/kubernetes-client/python) 自动执行此步骤。浏览可用的 API,我想知道 patch_namespaced_deployment 是否允许我编辑部署并在 /spec/template/metadata/labels 添加标签。我正在寻找一些使用 python 客户端的示例代码 - 任何对实现上述目标的帮助将不胜感激。

看看这个例子:

https://github.com/kubernetes-client/python/blob/master/examples/deployment_crud.py#L62-L70

def update_deployment(api_instance, deployment):
    # Update container image
    deployment.spec.template.spec.containers[0].image = "nginx:1.16.0"
    # Update the deployment
    api_response = api_instance.patch_namespaced_deployment(
        name=DEPLOYMENT_NAME,
        namespace="default",
        body=deployment)
    print("Deployment updated. status='%s'" % str(api_response.status))

标签在部署对象上,来自 App v1 API,

kind: Deployment
metadata:
  name: deployment-example
spec:
  replicas: 3
  revisionHistoryLimit: 10
  template:
    metadata:
      labels:
        app: nginx

这意味着您需要更新以下内容:

deployment.spec.template.metadata.labels.app = "nginx"