<p>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 <a href="https://learn.microsoft.com/en-us/azure/machine-learning/how-to-use-azure-ad-identity" rel="nofollow noreferrer">this documentation</a>. One of the step is to edit the deployment to add identity-feature label at <code>/spec/template/metadata/labels</code> for the deployment (see para starting like <code>Edit the deployment to add ...</code> in <a href="https://learn.microsoft.com/en-us/azure/machine-learning/how-to-use-azure-ad-identity#assign-azure-identity-to-machine-learning-web-service" rel="nofollow noreferrer">this section</a>). </p>

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

<p>Have a look at this example:</p>
<p><a href="https://github.com/kubernetes-client/python/blob/master/examples/deployment_crud.py#L62-L70" rel="nofollow noreferrer">https://github.com/kubernetes-client/python/blob/master/examples/deployment_crud.py#L62-L70</a></p>
<pre class="lang-py prettyprint-override"><code>def update_deployment(api_instance, deployment):
    # Update container image
    deployment.spec.template.spec.containers[0].image = &quot;nginx:1.16.0&quot;
    # Update the deployment
    api_response = api_instance.patch_namespaced_deployment(
        name=DEPLOYMENT_NAME,
        namespace=&quot;default&quot;,
        body=deployment)
    print(&quot;Deployment updated. status='%s'&quot; % str(api_response.status))
</code></pre>
<p>The Labels are on the deployment object, from the App v1 API,</p>
<pre><code>kind: Deployment
metadata:
  name: deployment-example
spec:
  replicas: 3
  revisionHistoryLimit: 10
  template:
    metadata:
      labels:
        app: nginx
</code></pre>
<p>which means you need to update the following:</p>
<p><code>deployment.spec.template.metadata.labels.app = &quot;nginx&quot;</code></p>
