Создание задания Google Cloud Kubernetes не удается с несанкционированным сообщением
Я реализовал механизм создания заданий в Google Cloud Kubernetes с помощью приложения Django / Python. Код приведен ниже :
credentials, project = google.auth.default(
scopes=['https://www.googleapis.com/auth/cloud-platform', ])
credentials.refresh(google.auth.transport.requests.Request())
cluster_manager = ClusterManagerClient(credentials=credentials)
cluster = cluster_manager.get_cluster(name=f"projects/MYPROJECT/locations/us-central1/clusters/cluster-1")
with NamedTemporaryFile(delete=False) as ca_cert:
ca_cert.write(base64.b64decode(cluster.master_auth.cluster_ca_certificate))
config = client.Configuration()
config.host = f'https://{cluster.endpoint}:443'
config.verify_ssl = True
config.api_key = {"authorization": "Bearer " + credentials.token}
config.username = credentials._service_account_email
config.ssl_ca_cert = ca_cert.name
client.Configuration.set_default(config)
# Setup K8 configs
api_instance = kubernetes.client.BatchV1Api(kubernetes.client.ApiClient(config))
def kube_create_job(manifest, output_uuid, output_signed_url, webhook_url):
# Create the job definition
# container_image = "gcr.io/videoo2/github.com/videoo-io/videoo-render:87fc058a1fc8f30d5a6bda15391a990e5e0f0b80"
container_image = get_first_success_build_from_list_builds()
name = id_generator()
body = kube_create_job_object(name, container_image,
env_vars={})
try:
api_response = api_instance.create_namespaced_job("default", body, pretty=True)
print(api_response)
except ApiException as e:
print("Exception when calling BatchV1Api->create_namespaced_job: %s\n" % e)
return body
Почему-то все работает нормально в локальной среде, но когда я развертываю приложение Django в Cloud Run, оно выдает следующую ошибку :
Default
2022-08-19T00:36:51.688763Z
Exception when calling BatchV1Api->create_namespaced_job: (401)
Default
2022-08-19T00:36:51.688788Z
Reason: Unauthorized
Default
2022-08-19T00:36:51.688800Z
HTTP response headers: HTTPHeaderDict({'Audit-Id': '7e39553a-264a-43e7-bd08-575604c13397', 'Cache-Control': 'no-cache, private', 'Content-Type': 'application/json', 'Date': 'Fri, 19 Aug 2022 00:36:51 GMT', 'Content-Length': '165'})
Default
2022-08-19T00:36:51.688809Z
HTTP response body: {
Default
2022-08-19T00:36:51.688818Z
"kind": "Status",
Что я должен сделать, чтобы преодолеть этот сбой авторизации для запуска задания на Google Cloud Kubernetes Engine?