import googleapiclient import json import time import requests import os from googleapiclient import discovery from oauth2client.client import GoogleCredentials credentials = GoogleCredentials.get_application_default() containerClient = discovery.build('container', 'v1', credentials=credentials) computeClient = discovery.build('compute', 'v1', credentials=credentials) BOT_WEBHOOK = 'https://open.feishu.cn/open-apis/bot/v2/hook/0e2e4541-bb50-4261-aace-b8c100802ef3' def post_to_feishu(): headers = { 'Content-Type': 'application/json' } payload_message = { "timestamp": str(int(time.time())), "msg_type": "interactive", "card": { "i18n_elements": { "en_us": [ { "tag": "div", "text": { "tag": "lark_md", "content": "**GCP Seed Robot**" }, "fields": [ { "is_short": False, "text": { "tag": "lark_md", "content": "" } }, { "is_short": True, "text": { "tag": "lark_md", "content": "自动关闭GCP Seed 异常" } } ] }, ] } } } response = requests.request( "POST", BOT_WEBHOOK, headers=headers, data=json.dumps(payload_message)) print(response.text) def scale_down_gkes(): gkes = { 'projects/pingcap-gardener/locations/' \ 'us-west1/clusters/staging-seed-us-west1-73cb72c': 'projects/pingcap-gardener/locations/us-west1/clusters/staging-seed-us-west1-73cb72c/nodePools/gardener-node-pool-8734cb0', 'projects/pingcap-gardener/locations/' \ 'us-west2/clusters/dev-seed-us-west2-9f7b847': 'projects/pingcap-gardener/locations/us-west2/clusters/dev-seed-us-west2-9f7b847/nodePools/gardener-node-pool-fe8bf71', 'projects/pingcap-gardener/locations/' \ 'us-west1/clusters/dev-seed-us-west1-3137aff': 'projects/pingcap-gardener/locations/us-west1/clusters/dev-seed-us-west1-3137aff/nodePools/gardener-node-pool-33f537a' } for location in gkes.keys(): try: nodePoolId = gkes[location] request= containerClient.projects().locations().clusters().nodePools().setSize( name=nodePoolId, body={'nodeCount': 0}, ) request.execute() except: pass def scaledown(data, context): try: scale_down_gkes() except: post_to_feishu() return "ok"