<p>Has anyone tried to add or update the Clusters from Google Kubernetes Engine through Python API?</p>
<p>I managed to do this for Compute instances, but the guide for Kubernetes Engine says its deprecated:</p>
<p><a href="https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1/projects.zones.clusters.nodePools/update" rel="nofollow noreferrer">https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1/projects.zones.clusters.nodePools/update</a></p>
<p>Tried it and it fails saying it does not find &quot;labels&quot;:</p>
<blockquote>
<p>googleapiclient.errors.HttpError: &lt;HttpError 400 when requesting
<a href="https://container.googleapis.com/v1/projects/testingproject/zones/us-east1/clusters/testing-cluster/resourceLabels?alt=json" rel="nofollow noreferrer">https://container.googleapis.com/v1/projects/testingproject/zones/us-east1/clusters/testing-cluster/resourceLabels?alt=json</a>
returned &quot;Invalid JSON payload received. Unknown name &quot;labels&quot;: Cannot
find field.&quot;. Details: &quot;[{'@type':
'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations':
[{'description': 'Invalid JSON payload received. Unknown name
&quot;labels&quot;: Cannot find field.'}]}]&quot;&gt;</p>
</blockquote>
<p>My code is this:</p>
<pre><code>credentials = GoogleCredentials.get_application_default()

service = discovery.build('container', 'v1', credentials=credentials)

# Deprecated. The Google Developers Console [project ID or project
# number](https://developers.google.com/console/help/new/#projectnumber).
# This field has been deprecated and replaced by the name field.
project_id = 'testingproject'  # TODO: Update placeholder value.

# Deprecated. The name of the Google Compute Engine
# [zone](/compute/docs/zones#available) in which the cluster
# resides.
# This field has been deprecated and replaced by the name field.
zone = 'us-east1'  # TODO: Update placeholder value.

# Deprecated. The name of the cluster.
# This field has been deprecated and replaced by the name field.
cluster_id = 'testing-cluster'  # TODO: Update placeholder value.

set_labels_request_body = {
    'labels': 'value'
}

request = service.projects().zones().clusters().resourceLabels(projectId=project_id, zone=zone, clusterId=cluster_id, body=set_labels_request_body)
response = request.execute()

# TODO: Change code below to process the `response` dict:
pprint(response)
</code></pre>
<p>I want to update the Workload named 'matei-testing-2000-gke-ops' inside the cluster 'testing-cluster'.</p>
<p>Any ideas?
Thank you</p>
<p>Update: It does not find the labels because the name is resourceLabels. But I get the following error after:</p>
<blockquote>
<p>googleapiclient.errors.HttpError: &lt;HttpError 400 when requesting
<a href="https://container.googleapis.com/v1/projects//zones//clusters//resourceLabels?alt=json" rel="nofollow noreferrer">https://container.googleapis.com/v1/projects//zones//clusters//resourceLabels?alt=json</a>
returned &quot;Invalid value at 'resource_labels'
(type.googleapis.com/google.container.v1.SetLabelsRequest.ResourceLabelsEntry),
&quot;value&quot;&quot;. Details: &quot;[{'@type':
'type.googleapis.com/google.rpc.BadRequest', 'fieldViolations':
[{'field': 'resource_labels', 'description': 'Invalid value at
'resource_labels'
(type.googleapis.com/google.container.v1.SetLabelsRequest.ResourceLabelsEntry),
&quot;value&quot;'}]}]&quot;&gt;</p>
</blockquote>

happymatei
<p>I've <strike>not</strike> now tried this.</p>
<p>But IIUC, you'll need to:</p>
<ul>
<li><strike>ditch (or use defaults) for e.g. <code>project_id</code>, <code>zone</code> and <code>cluster_id</code> parameters of <code>resourceLabels</code></strike></li>
<li>add <code>name</code> to your body and it should be of the form: <code>projects/*/locations/*/clusters/*</code></li>
</ul>
<p>i.e.</p>
<pre><code>import os
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

service = discovery.build('container', 'v1', credentials=credentials)

PROJECT = os.getenv(&quot;PROJECT&quot;)
LOCATION = os.getenv(&quot;ZONE&quot;)
CLUSTER = os.getenv(&quot;CLUSTER&quot;)

NAME = &quot;projects/{project}/locations/{location}/clusters/{cluster}&quot;.format(
        project=project_id,
        location=zone,
        cluster=cluster_id)


# To update `resourceLabels` you must first fingerprint them
# To get the current `labelFingerprint`, you must `get` the cluster

body = {
    'name': NAME,
}

request = service.projects().zones().clusters().get(
    projectId=project_id,
    zone=zone,
    clusterId=cluster_id)
response = request.execute()

labelFingerprint = response[&quot;labelFingerprint&quot;]

if &quot;resourceLabels&quot; in response:
    print(&quot;Existing labels&quot;)
    resourceLabels = response[&quot;resourceLabels&quot;]
else:
    print(&quot;No labels&quot;)
    resourceLabels = {}

# Add|update a label
resourceLabels[&quot;dog&quot;] = &quot;freddie&quot;

# Construct `resourceLabels` request
body = {
    'name': NAME,
    'resourceLabels': resourceLabels,
     'labelFingerprint': labelFingerprint,
}

request = service.projects().zones().clusters().resourceLabels(
    projectId=project_id,
    zone=zone,
    clusterId=cluster_id,
    body=body)

# Do something with the `response`
response = request.execute()
</code></pre>
<ul>
<li><a href="https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1/projects.zones.clusters/get" rel="nofollow noreferrer"><code>clusters.get</code></a></li>
<li><a href="https://cloud.google.com/kubernetes-engine/docs/reference/rest/v1/projects.locations.clusters#Cluster" rel="nofollow noreferrer">`clusters#CLuster</a></li>
</ul>
<p>And</p>
<pre class="lang-sh prettyprint-override"><code>gcloud container clusters describe ${CLUSTER} \
--zone=${ZONE} \
--project=${PROJECT} \
--format=&quot;value(labelFingerprint,resourceLabels)&quot;
</code></pre>
<p>Before:</p>
<pre class="lang-sh prettyprint-override"><code>a9dc16a7    
</code></pre>
<p>After:</p>
<pre class="lang-sh prettyprint-override"><code>b2c32ec0    dog=freddie

</code></pre>
