使用 api python 客户端获取 google 云容器集群配置的状态
Get the status of google cloud container cluster provisioning using api python client
我正在使用 API python 客户端为 google 云 platform.I 创建容器引擎集群 platform.I 已成功创建容器,现在我需要应用一些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)
请帮帮我!
提前致谢!
您要查找的是从 create cluster call 返回 ID 的操作的状态。然后您需要获取操作(通过容器 API,而不是计算 API)并检查操作的状态以查看它是否已完成。完成后,您可以通过查看操作中的状态消息来确定是否存在错误。如果为空,则创建集群 API 调用成功。如果它非空,则调用失败,状态消息将告诉您原因。创建集群的操作完成后,get-credentials 调用将成功。
这是我在代码中的做法:
"""
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'
我正在使用 API python 客户端为 google 云 platform.I 创建容器引擎集群 platform.I 已成功创建容器,现在我需要应用一些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)
请帮帮我!
提前致谢!
您要查找的是从 create cluster call 返回 ID 的操作的状态。然后您需要获取操作(通过容器 API,而不是计算 API)并检查操作的状态以查看它是否已完成。完成后,您可以通过查看操作中的状态消息来确定是否存在错误。如果为空,则创建集群 API 调用成功。如果它非空,则调用失败,状态消息将告诉您原因。创建集群的操作完成后,get-credentials 调用将成功。
这是我在代码中的做法:
"""
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'