gpt4 book ai didi

google-cloud-run - 使用 Deployment Manager 创建 Cloud Run 服务

转载 作者:行者123 更新时间:2023-12-05 06:47:33 28 4
gpt4 key购买 nike

我正在尝试使用 Deployment Manager 创建 Cloud Run 服务,因为没有对 Cloud Run 资源类型的本地支持,我通过在 https://run.googleapis.com/$discovery/rest?version=v1 提供它的描述符为 Cloud Run API 创建了一个类型提供程序。 .

但是,当我运行此配置以使用 Deployment Manager 创建服务时,出现错误 404 - 未找到请求的实体

这是因为当 Deployment Manager 尝试执行创建操作时,它不是调用特定于位置的服务端点(例如 https://us-central1-run.googleapis.com ),而是使用发现 url 返回的全局端点 ( https://run.googleapis.com ),并且作为API文档中说明,全局端点仅支持列表方法。

为了克服这个问题,我创建了一个自定义描述文件,并通过将其指向特定于位置的服务端点来替换 rootUrl,并且我能够使用 DM 创建 Cloud Run 服务。

我不打算保留自定义描述 rune 件,因为它可能会随着 API 的发展而过时,我想知道是否有另一种推荐的方法可以从 Google 的官方 API 发现文档中获取正确的 API url。

以下是我的私信配置:

resources:
- name: cloudrun-type-provider
type: deploymentmanager.v2beta.typeProvider
properties:
descriptorUrl: https://us-central1-run.googleapis.com/$discovery/rest?version=v1
options:
inputMappings:
- fieldName: Authorization
location: HEADER
value: >
$.concat("Bearer ", $.googleOauth2AccessToken())
collectionOverrides:
- collection: namespaces.services
options:
virtualProperties: |
schema: http://json-schema.org/draft-04/schema#
type: object
properties:
metadata:
type: object
description: https://cloud.google.com/run/docs/reference/rest/v1/ObjectMeta
spec:
type: object
description: https://cloud.google.com/run/docs/reference/rest/v1/namespaces.services#ServiceSpec
status:
type: object
description: https://cloud.google.com/run/docs/reference/rest/v1/namespaces.services#ServiceStatus
inputMappings:
- methodMatch: ^create$
location: PATH
fieldName: parent
value: $.concat("namespaces/", $.project)

- methodMatch: ^(get|replaceService|delete)$
location: PATH
fieldName: name
value: $.concat("namespaces/", $.project, "/services/", $.resource.name)

- methodMatch: ^create$
location: BODY
fieldName: apiVersion
value: $.concat("serving.knative.dev/v1")

- methodMatch: ^create$
location: BODY
fieldName: kind
value: $.concat("Service")

- methodMatch: ^create$
location: BODY
fieldName: metadata.name
value: $.resource.name

- methodMatch: ^replaceService$
location: BODY
fieldName: metadata
value: $.resource.self.metadata

- methodMatch: ^(create|replaceService)$
location: BODY
fieldName: spec.template.spec
value: $.resource.properties.spec

- name: dm-cloud-run
type: MY-PROJECT/cloudrun-type-provider:namespaces.services
metadata:
dependsOn:
- cloudrun-type-provider
properties:
spec:
containerConcurrency: 50
timeoutSeconds: 300
containers:
- image: marketplace.gcr.io/google/nginx1
env:
- name: ENV_VAR_1
value: VALUE_1
- name: ENV_VAR_1
value: VALUE_2
resources:
limits:
memory: 512Mi

最佳答案

在遇到与您相同的问题后,我已经设法让这个版本工作。

我的解决方案:

  1. 创建部署管理器类型
gcloud beta deployment-manager --project $PROJECT_ID type-providers create cloudrun --api-options-file=$DIR/../deployment/options.yaml --descriptor-url="https://run.googleapis.com/\$discovery/rest?version=v1"

选项.yaml:

options:
inputMappings:
- fieldName: Authorization
location: HEADER
value: >
$.concat("Bearer ", $.googleOauth2AccessToken())
  1. 创建 cloudrun.py 模板
"""Creates the Cloud Run endpoint."""


def GenerateConfig(context):
name: str = context.env['name']
project = context.env['project']
region = context.properties['region']

resources = [{
'name': name,
'type': context.env['project'] + '/cloudrun:run.projects.locations.services.create',
'properties': {
'parent': 'projects/{}/locations/{}'.format(project, region),
'apiVersion': 'serving.knative.dev/v1',
'kind': 'Service',
'metadata': {
'name': '{}-blah'.format(name), # TODO rename
'namespace': 'YOUR_PROJECT_NAME_HERE', # TODO change to your value
},
'spec': {
'template': {
'spec': {
'containerConcurrency': 80,
'timeoutSeconds': 300,
'serviceAccountName': 'YOUR_SERVICE_ACCOUNT_HERE', # TODO change to your value
'containers': [{'image': 'eu.gcr.io/YOUR_PROJECT_HERE/YOUR_IMAGE_HERE'}], # TODO change to your value
},
},
'traffic': [
{'percent': 100, 'latestRevision': True}
],
},
}
}]

return {'resources': resources}

解决方法

使用 type: context.env['project'] + '/cloudrun:run.namespaces.services.create'(和父级:namespaces/{namespace} ) 对我不起作用,我也打了那个 404。请注意这种方法是文档描述如何使用 fully managed 的方式。 .
而是使用 type: context.env['project'] + '/cloudrun:run.projects.locations.services.create' 如示例片段所示。父项和一些字段也必须更改。一些反复试验让我得到了最终结果。

API定义

https://run.googleapis.com//$discovery/rest?version=v1
这看起来不完整或不准确,反复试验并依赖于响应错误来准确确定需要什么,不需要什么。

关于google-cloud-run - 使用 Deployment Manager 创建 Cloud Run 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67065062/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com