在创建Google云平台部署后对其进行标记



我们有一个批处理标记器,用于处理新创建资源的活动日志,然后为其应用标签以进行计费。我们正试图以这种方式自动标记我们的部署,因为我们发现失败的部署正在积累并产生相当大的成本。我的问题是无法标记部署。

我需要为部署添加一个全局标签,而我以编程方式尝试的所有操作似乎都不起作用。

我尝试使用现有的配置如下:

manifests = self.deploymentManagerService.manifests().list(project=project, 
deployment=deployment_name).execute()
config = manifests['manifests'][0]['config']
...
content_dict = eval(json.dumps(json.loads(config['content'])))
output = StringIO.StringIO()
yaml.dump(content_dict, output, encoding=None)
body = {'labels': labels, 'fingerprint': fingerprint, 'name': deployment_name,  'target': {'config': { 'content': output.getvalue()}}}
print "BODY=", body
deploymentManagerService.deployments().patch(project=project,
deployment=deployment_name,
body=body).execute()

这正确地标记了部署,但由于路径问题导致更新出错。

我尝试了一个空资源部分:

body = {'labels': labels, 'fingerprint': fingerprint, 'name': deployment_name,  'target': {'config': { 'content': 'resources:n'}}}

这标记了部署,但省略了配置(不好(。

我不尝试配置或目标,得到了400分。我不知所措。

不需要获取清单。使用patch是正确的想法,但您需要添加一个额外的标头。这里有一个例子:

dep = self.deploymentManagerService.deployments().get(
deployment=deployment, project=project).execute()
body = {'labels': [{'key': 'foo', 'value': 'bar'}],
'fingerprint': dep['fingerprint']}
req = self.deploymentManagerService.deployments().patch(
project=project, deployment=deployment, body=body)
# This header is required for patch requests to work correctly.
req.headers['X-Cloud-DM-Patch'] = 'True'
res = req.execute()

此外,您可能希望:

  1. 如果存在并发修改(HTTP 409冲突(,请重试整个块
  2. 考虑一下您希望如何处理现有标签(如果您的用例中可能存在现有标签(
  3. 让一个循环等待从execute返回的长时间运行的操作完成

最新更新