使用自定义限制条件

Google Cloud 组织政策可让您以编程方式集中控制组织的资源。作为组织政策管理员,您可以定义组织政策,这是一组称为限制条件的限制,会应用于 Google Cloud 资源层次结构中的 Google Cloud 资源及其后代。您可以在组织、文件夹或项目级强制执行组织政策。

组织政策为各种 Google Cloud 服务提供预定义限制条件。但是,如果您想要更精细地控制和自定义组织政策中受限的特定字段,还可以创建自定义限制条件并在组织政策中使用这些自定义限制条件。

优势

您可以使用自定义组织政策来允许或拒绝 Dataproc Serverless 批次中的特定操作。例如,如果创建批量工作负载的请求未能满足组织政策设置的自定义限制条件验证,该请求将失败,并向调用方返回错误。

政策继承

如果您对资源强制执行政策,默认情况下,该资源的后代会继承组织政策。例如,如果您对某个文件夹强制执行一项政策,Google Cloud 会对该文件夹中的所有项目强制执行该政策。如需详细了解此行为及其更改方式,请参阅层次结构评估规则

价格

组织政策服务(包括预定义限制条件和自定义限制条件)可免费使用。

准备工作

  1. 设置项目
    1. 登录您的 Google Cloud 账号。如果您是 Google Cloud 新手,请创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
    2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    3. 确保您的 Google Cloud 项目已启用结算功能

    4. 启用 Dataproc Serverless API。

      启用 API

    5. Install the Google Cloud CLI.
    6. To initialize the gcloud CLI, run the following command:

      gcloud init
    7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

      Go to project selector

    8. 确保您的 Google Cloud 项目已启用结算功能

    9. 启用 Dataproc Serverless API。

      启用 API

    10. Install the Google Cloud CLI.
    11. To initialize the gcloud CLI, run the following command:

      gcloud init
    12. 确保您知道您的组织 ID

所需的角色

如需获取管理组织政策所需的权限,请让管理员向您授予组织资源的 Organization Policy Administrator (roles/orgpolicy.policyAdmin) IAM 角色。如需详细了解如何授予角色,请参阅管理访问权限

此预定义角色包含管理组织政策所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

如需管理组织政策,您必须拥有以下权限:

  • orgpolicy.constraints.list
  • orgpolicy.policies.create
  • orgpolicy.policies.delete
  • orgpolicy.policies.list
  • orgpolicy.policies.update
  • orgpolicy.policy.get
  • orgpolicy.policy.set

您也可以使用自定义角色或其他预定义角色来获取这些权限。

创建自定义限制条件

自定义限制条件在 YAML 文件中由应用该限制条件的资源、方法、条件和操作定义。Dataproc Serverless 支持应用于 BATCH 资源的 CREATE 方法的自定义限制条件(请参阅 Dataproc 无服务器资源和操作限制条件)。

如需为 Dataproc Serverless 自定义限制条件创建 YAML 文件,请执行以下操作:

name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- dataproc.googleapis.com/Batch
methodTypes:
- CREATE
condition: CONDITION
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION

替换以下内容:

  • ORGANIZATION_ID:您的组织 ID,例如 123456789

  • CONSTRAINT_NAME:新的自定义限制条件的名称。 自定义限制条件必须以 custom. 开头,并且只能包含大写字母、小写字母或数字,例如 custom.batchMustHaveSpecifiedCategoryLabel。此字段的最大长度为 70 个字符,不包括前缀,例如 organizations/123456789/customConstraints/custom

  • CONDITION:针对受支持的服务资源的表示法编写的 CEL 条件。此字段的最大长度为 1,000 个字符。如需详细了解可用于写入条件的资源,请参阅支持的资源。 示例条件:("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service'])

  • ACTION:满足条件时要执行的操作。可以是 ALLOWDENY

  • DISPLAY_NAME:限制条件的直观易记名称。 示例显示名称:“强制执行批量‘类别’标签要求”。 此字段的最大长度为 200 个字符。

  • DESCRIPTION:直观易懂的限制条件说明,在违反政策时显示为错误消息。 此字段的长度上限为 2000 个字符。示例说明:“仅当 Dataproc 具有包含‘零售’、‘广告’或‘服务’值的‘类别’标签时,才允许进行批量创建”。

如需详细了解如何创建自定义限制条件,请参阅定义自定义限制条件

设置自定义限制条件

使用 Google Cloud CLI 创建新的自定义限制条件后,您必须对其进行设置,以使其可用于组织中的组织政策。如需设置自定义限制条件,请使用 gcloud org-policies set-custom-constraint 命令:
gcloud org-policies set-custom-constraint CONSTRAINT_PATH
CONSTRAINT_PATH 替换为自定义限制条件文件的完整路径。例如 /home/user/customconstraint.yaml。 完成后,您可发现您的自定义限制条件已成为 Google Cloud 组织政策列表中的可用组织政策。 如需验证自定义限制条件是否存在,请使用 gcloud org-policies list-custom-constraints 命令:
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
ORGANIZATION_ID 替换为您的组织资源的 ID。 如需了解详情,请参阅查看组织政策

强制执行自定义限制条件

如需强制执行布尔值限制条件,您可以创建引用该限制条件的组织政策,并将该组织政策应用于 Google Cloud 资源。

控制台

如需强制执行布尔值限制条件,请执行以下操作:

  1. 在 Google Cloud 控制台中,转到组织政策页面。

    转到“组织政策”

  2. 选择页面顶部的项目选择器。
  3. 从项目选择器中,选择要为其设置组织政策的项目。
  4. 组织政策页面的列表中选择限制条件。 此时应显示该限制条件的政策详情页面。
  5. 如需为该资源配置组织政策,请点击管理政策
  6. 修改政策页面,选择覆盖父级政策
  7. 点击添加规则
  8. 强制执行下,选择开启还是关闭此组织政策的强制执行。
  9. (可选)如需使组织政策成为基于某个标记的条件性政策,请点击添加条件。请注意,如果您向组织政策添加条件规则,则必须至少添加一个无条件规则,否则无法保存政策。如需了解详情,请参阅使用标记设置组织政策
  10. 如果是自定义限制条件,您可以点击测试更改来模拟此组织政策的效果。如需了解详情,请参阅使用 Policy Simulator 测试组织政策更改
  11. 若要完成并应用组织政策,请点击设置政策。该政策将在 15 分钟内生效。

gcloud

如需创建强制执行布尔值限制条件的组织政策,请创建引用该限制条件的 YAML 政策文件:

      name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
      spec:
        rules:
        - enforce: true
    

请替换以下内容:

  • PROJECT_ID:要对其实施限制条件的项目。
  • CONSTRAINT_NAME:您为自定义限制条件定义的名称。例如,custom.batchMustHaveSpecifiedCategoryLabel

如需强制执行包含限制条件的组织政策,请运行以下命令:

    gcloud org-policies set-policy POLICY_PATH
    

POLICY_PATH 替换为组织政策 YAML 文件的完整路径。该政策将在 15 分钟内生效。

测试自定义限制条件

以下批量创建示例假定已创建自定义限制条件,并在批量创建时强制执行该限制条件,以要求该批次具有附加了值“retail”“ads”或“service: ("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service'])”的“类别”标签。请注意,此示例中的“类别”标签不含任何必填值。

gcloud dataproc batches submit spark \
  --region us-west1
  --jars file:///usr/lib/spark/examples/jars/spark-examples.jar \
  --class org.apache.spark.examples.SparkPi  \
  --network default \
  --labels category=foo \
  -- 100

示例输出:

Operation denied by custom org policies: ["customConstraints/custom.batchMustHaveSpecifiedCategoryLabel": ""Only allow Dataproc batch creation if it has a 'category' label with
  a 'retail', 'ads', or 'service' value""]

Dataproc 无服务器的资源和操作限制

创建(提交)批量工作负载时,您可以使用以下 Dataproc Serverless 自定义限制条件。

常规

  • resource.labels

RuntimeConfig

  • resource.runtimeConfig.version
  • resource.runtimeConfig.containerImage
  • resource.runtimeConfig.properties

ExecutionConfig

  • resource.environmentConfig.executionConfig.serviceAccount
  • resource.environmentConfig.executionConfig.networkTags
  • resource.environmentConfig.executionConfig.kmsKey
  • resource.environmentConfig.executionConfig.stagingBucket

PeripheralsConfig

  • resource.environmentConfig.peripheralsConfig.metastoreService
  • resource.environmentConfig.peripheralsConfig.sparkHistoryServerConfig.dataprocCluster

常见用例的自定义限制条件示例

下表提供了 Dataproc Serverless 批处理自定义限制条件的示例:

说明 限制条件语法
批次必须附加包含允许值的“类别”标签。

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchMustHaveSpecifiedCategoryLabel
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition: ("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service'])
    actionType: ALLOW
    displayName: Enforce batch "category" label requirement.
    description: Only allow batch creation if it attaches a "category" label with an allowable value.
Batch 必须设置允许的运行时版本。

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchMustUseAllowedVersion
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition:  (has(resource.runtimeConfig.version)) && (resource.runtimeConfig.version in ["2.0.45", "2.0.48"])
    actionType: ALLOW
    displayName: Enforce batch runtime version.
    description: Only allow batch creation if it sets an allowable runtime version.
批次最多只能设置 20 个 Spark 初始执行器。

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchInitialExecutorMax20
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition: (has(resource.runtimeConfig.properties)) && ('spark.executor.instances' in resource.runtimeConfig.properties)
     && (int(resource.runtimeConfig.properties['spark.executor.instances'])>20)
    actionType: DENY
    displayName: Enforce maximum number of batch Spark executor instances.
    description: Deny batch creation if it specifies more than 20 Spark executor instances.
批次最多只能设置 20 个 Spark 动态分配初始执行程序。

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchDynamicAllocationInitialExecutorMax20
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition: (has(resource.runtimeConfig.properties)) && ('spark.dynamicAllocation.initialExecutors' in resource.runtimeConfig.properties)
     && (int(resource.runtimeConfig.properties['spark.dynamicAllocation.initialExecutors'])>20)
    actionType: DENY
    displayName: Enforce maximum number of batch dynamic allocation initial executors.
    description: Deny batch creation if it specifies more than 20 Spark dynamic allocation initial executors.
批次允许的动态分配执行程序不得超过 20 个。

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchDynamicAllocationMaxExecutorMax20
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition: (resource.runtimeConfig.properties['spark.dynamicAllocation.enabled']=='false') || (('spark.dynamicAllocation.maxExecutors' in resource.runtimeConfig.properties) && (int(resource.runtimeConfig.properties['spark.dynamicAllocation.maxExecutors'])<=20))
    actionType: ALLOW
    displayName: Enforce batch maximum number of dynamic allocation executors.
    description:  Only allow batch creation if dynamic allocation is disabled or
    the maximum number of dynamic allocation executors is set to less than or equal to 20.
批次必须将 KMS 密钥设置为允许的格式。

    name: organizations/ORGANIZATION_ID/custom.batchKmsPattern
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition:  matches(resource.environmentConfig.executionConfig.kmsKey, '^keypattern[a-z]$')
    actionType: ALLOW
    displayName: Enforce batch KMS Key pattern.
    description: Only allow batch creation if it sets the KMS key to an allowable pattern.
批次必须将暂存存储桶前缀设置为允许的值。

    name: organizations/ORGANIZATION_ID/customConstraints/custom.batchStagingBucketPrefix
    resourceTypes:
    - dataproc.googleapis.com/Batch
    methodTypes:
    - CREATE
    condition:  resource.environmentConfig.executionConfig.stagingBucket.startsWith(ALLOWED_PREFIX)
    actionType: ALLOW
    displayName: Enforce batch staging bucket prefix.
    description: Only allow batch creation if it sets the staging bucket prefix to ALLOWED_PREFIX.

后续步骤