使用api python客户端获取Google云容器群集配置的状态

问题描述 投票:0回答:2

我正在使用API​​ python客户端为谷歌云平台创建一个容器引擎集群。我已成功完成容器创建,现在我需要应用一些yaml配置,但在应用任何kubernetes yaml配置之前,应该配置集群,否则kubernetes API不可用。我需要在一个请求中同时执行这两项操作(容器创建和应用yaml配置)。如何使用api获取集群的配置状态?

这是我尝试过的:

创建集群后:从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)

请帮帮我!

提前致谢!

python google-cloud-platform google-kubernetes-engine google-api-python-client
2个回答
3
投票

这是我在我的代码中的方式:

"""
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'

3
投票

您要查找的是从create cluster call返回ID的操作的状态。然后,您需要获取操作(通过容器API,而不是计算API)并检查操作的状态以查看它是否已完成。完成后,您可以通过查看操作中的状态消息来确定是否存在错误。如果为空,则创建集群API调用成功。如果它是非空的,则呼叫失败,状态消息将告诉您原因。完成创建集群的操作后,get-credentials调用将成功。

© www.soinside.com 2019 - 2024. All rights reserved.