<p>I would like to be able to access and manage a GKE (kubernetes) cluster from a Google Cloud function written in python.
I managed to access and retrieve data from the created cluster (endpoint, username, and password at least), however I dont know how to use them with the kubernetes package api.</p>

<p>Here are my imports :</p>

<pre><code>import google.cloud.container_v1 as container
from google.auth import compute_engine
from google.cloud.container_v1 import ClusterManagerClient
from kubernetes import client, config
</code></pre>

<p>Here is the code for cluster data :</p>

<pre><code>project_id = 'my-gcp-project'
zone = 'my-zone'
cluster_id = 'my-existing-cluster'

credentials = compute_engine.Credentials()

gclient: ClusterManagerClient = container.ClusterManagerClient(credentials=credentials)

cluster = gclient.get_cluster(project_id,zone,cluster_id)
cluster_endpoint = cluster.endpoint
print("*** CLUSTER ENDPOINT ***")
print(cluster_endpoint)

cluster_master_auth = cluster.master_auth
print("*** CLUSTER MASTER USERNAME PWD ***")
cluster_username = cluster_master_auth.username
cluster_password = cluster_master_auth.password
print("USERNAME : %s - PASSWORD : %s" % (cluster_username, cluster_password))
</code></pre>

<p>I would like to do something like this after that :</p>

<pre><code>config.load_kube_config()
v1 = client.CoreV1Api()
print("Listing pods with their IPs:")
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
    print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
</code></pre>

<p>However, I can't figure out how to set my endpoint and authentification informations.
Can anyone help me please ?</p>

<p>You can use a bearer token rather than using basic authentication:</p>
<pre><code>from google.auth import compute_engine
from google.cloud.container_v1 import ClusterManagerClient
from kubernetes import client


def test_gke(request):
    project_id = &quot;my-gcp-project&quot;
    zone = &quot;my-zone&quot;
    cluster_id = &quot;my-existing-cluster&quot;

    credentials = compute_engine.Credentials()

    cluster_manager_client = ClusterManagerClient(credentials=credentials)
    cluster = cluster_manager_client.get_cluster(name=f'projects/{project_id}/locations/{zone}/clusters/{cluster_id}')

    configuration = client.Configuration()
    configuration.host = f&quot;https://{cluster.endpoint}:443&quot;
    configuration.verify_ssl = False
    configuration.api_key = {&quot;authorization&quot;: &quot;Bearer &quot; + credentials.token}
    client.Configuration.set_default(configuration)

    v1 = client.CoreV1Api()
    print(&quot;Listing pods with their IPs:&quot;)
    pods = v1.list_pod_for_all_namespaces(watch=False)
    for i in pods.items:
        print(&quot;%s\t%s\t%s&quot; % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
</code></pre>
