簡體   English   中英

在創建后標記 Google Cloud Platform 部署

[英]Labeling a Google Cloud Platform Deployment after it has been created

我們有一個批處理標記器,用於處理新創建資源的活動日志,然后我們將標簽應用於計費目的。 我們正在嘗試以這種方式自動標記我們的部署,因為我們發現失敗的部署正在累積並產生相當數量的成本。 我的問題是我無法標記部署。

我需要向部署添加全局標簽,但我以編程方式嘗試的所有內容似乎都不起作用。

我嘗試使用這樣的現有配置:

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返回的長時間運行的操作完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM