VinayC
VinayC

Reputation: 49245

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

I am fairly new to kubernetes - I have developed web UI/API that automates model deployment using Azure Machine Learning Services to Azure Kubernetes Services (AKS). As a hardening measure, I am tying to set up managed identity for deployed pods in AKS using 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).

I wish to automate this step using python kubernetes client (https://github.com/kubernetes-client/python). Browsing the available API, I was wondering that perhaps patch_namespaced_deployment will allow me to edit deployment and add label at /spec/template/metadata/labels. I was looking for some example code using the python client for the same - any help to achieve above will be appreciated.

Upvotes: 3

Views: 10781

Answers (1)

djsly
djsly

Reputation: 1628

Have a look at this example:

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))

The Labels are on the deployment object, from the App v1 API,

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

which means you need to update the following:

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

Upvotes: 6

Related Questions