Abdul Rehman
Abdul Rehman

Reputation: 5644

Get the status of google cloud container cluster provisioning using api python client

I'm creating a container engine cluster using API python client for google cloud platform.I have done with container creation successfully, Now I need to apply some yaml configurations but before applying any kubernetes yaml configurations the cluster should be provisioned otherwise kubernetes API not available. I need to do both(Container creation & Apply yaml configs) things in a single request. How can i get the provision status of a cluster using api?

Here's what i have tried:

After cluster creation: From views.py:

print('Fetching Cluster configs ....')
cc = subprocess.call(
                'gcloud container clusters get-credentials ' + deployment.deploymentName.lower() + ' --zone ' + deployment.region + ' --project ' + deployment.project,
                shell=True)
print(cc)
while cc == 1:
     cc = subprocess.call(
                    'gcloud container clusters get-credentials ' + deployment.deploymentName.lower() + ' --zone ' + deployment.region + ' --project ' + deployment.project,
                    shell=True)
     print(cc)

Help me, please!

Thanks in Advance!

Upvotes: 0

Views: 1644

Answers (2)

DevLounge
DevLounge

Reputation: 8437

This is how I do in my code:

"""
If you have a credentials issue, run:

gcloud beta auth application-default login

"""
import time

import googleapiclient.discovery

service = googleapiclient.discovery.build('container', 'v1')
clusters_resource = service.projects().zones().clusters()
operations_resource = service.projects().zones().operations()


def create_cluster(project_id, zone, config, async=False):
    req = clusters_resource.create(projectId=project_id, zone=zone, body=config)
    operation = req.execute()

    if async:
        return

    while operation['status'] == 'RUNNING':
        time.sleep(1)
        req = operations_resource.get(projectId=project_id, zone=zone, operationId=operation['name'])
        operation = req.execute()

    return operation['status'] == 'DONE'

Upvotes: 4

Robert Bailey
Robert Bailey

Reputation: 18200

What you are looking for is the status of the operation whose ID is returned from the create cluster call. You then need to get the operation (via the container API, not the compute API) and check the status of the operation to see if it is DONE. Once it is done, you can determine if there was an error by looking at the status message in the operation. If it is blank, then the create cluster API call succeeded. If it is non-empty then the call failed and the status message will tell you why. Once the operation for creating the cluster is done the get-credentials call will succeed.

Upvotes: 3

Related Questions