【问题标题】:Create Cloud Run service with Deployment Manager使用 Deployment Manager 创建 Cloud Run 服务
【发布时间】:2021-07-07 22:17:04
【问题描述】:

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

但是,当我运行此配置以使用部署管理器创建服务时,我收到错误 404 - 未找到请求的实体。

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

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

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

以下是我的 DM 配置:

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

【问题讨论】:

标签: google-cloud-run google-deployment-manager


【解决方案1】:

在遇到与您相同的问题后,我设法得到了这个版本。

我的解决方案:

  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"

options.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',如示例 sn-p 中所示。父字段和一些字段也必须更改。经过一番反复试验,我得到了最终结果。

API 定义

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 2017-02-02
    • 2020-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多