0

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!

2 Answers 2

4

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'
Sign up to request clarification or add additional context in comments.

1 Comment

Hi @Anthony ! thanks for your answer, can you help me getting IP address for the created cluster, please!
3

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.

2 Comments

how can I retrieve the status from response cluster object, I have tried: response['status'] but it's not returning the actual status, it returns something like {'name': 'newyear', 'initialNodeCount': 8, 'status': 'PROVISIONING', } mean complete cluster object!
The cluster status can be PROVISIONING, RUNNING, RECONCILING, STOPPING, or ERROR. The cluster status should transition from PROVISIONING to RUNNING at the same time that the operation for creating the cluster is finished.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.