建立自訂限制條件
Google Cloud 組織政策可讓您透過程式輔助,集中控管組織的資源。組織政策管理員可以定義組織政策,也就是一組稱為「限制」的限制,適用於Google Cloud 資源和這些資源在Google Cloud 資源階層中的子系。您可以在組織、資料夾或專案層級強制執行組織政策。
機構政策提供各種Google Cloud 服務的預先定義限制。不過,如要更精細地自訂組織政策中受限的特定欄位,您也可以建立自訂限制,並用於組織政策。
優點
您可以使用自訂機構政策,允許或拒絕對 Managed Service for Apache Spark 批次、工作階段和工作階段範本執行特定作業。舉例來說,如果建立批次工作負載的要求未通過機構政策設定的自訂限制驗證,要求就會失敗,並向呼叫端傳回錯誤。
政策繼承
根據預設,您強制執行政策的資源子系會繼承組織政策。舉例來說,如果您對資料夾強制執行政策, Google Cloud 會對該資料夾中的所有專案強制執行政策。如要進一步瞭解這項行為以及如何變更,請參閱這篇文章。
定價
組織政策服務 (包括預先定義和自訂限制) 免費提供。
事前準備
- 設定專案
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Managed Service for Apache Spark API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Managed Service for Apache Spark API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init - 請確認您知道組織 ID。
必要的角色
如要取得管理組織政策所需的權限,請要求管理員授予組織資源的組織政策管理員 (roles/orgpolicy.policyAdmin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
這個預先定義的角色具備管理組織政策所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:
所需權限
必須具備下列權限,才能管理組織政策:。
-
orgpolicy.constraints.list -
orgpolicy.policies.create -
orgpolicy.policies.delete -
orgpolicy.policies.list -
orgpolicy.policies.update -
orgpolicy.policy.get -
orgpolicy.policy.set
建立自訂限制
自訂限制是在 YAML 檔案中定義,其中包含限制適用的資源、方法、條件和動作。Managed Service for Apache Spark 支援套用至批次和工作階段資源 CREATE 方法的自訂限制。
想進一步瞭解如何建立自訂限制,請參閱「建立及管理自訂組織政策」。
為批次資源建立自訂限制
如要為批次資源的 Managed Service for Apache Spark 自訂限制建立 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 條件。這個欄位的長度上限為 1000 個字元 如要進一步瞭解可編寫條件的資源,請參閱「Dataproc Serverless 資源和作業的限制」。範例條件:("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service'])。ACTION:符合條件時採取的動作。可以是ALLOW或DENY。DISPLAY_NAME:容易記得的限制名稱。顯示名稱範例:「Enforce batch 'category' label requirement」。 這個欄位的長度上限為 200 個字元。DESCRIPTION:違反政策時,會以錯誤訊息形式顯示且易於理解的限制說明。這個欄位的長度上限為 2,000 個字元。範例說明:「只有在 Dataproc 批次作業具有『category』標籤,且值為『retail』、『ads』或『service』時,才允許建立該作業」。
為工作階段資源建立自訂限制
如要為工作階段資源的 Managed Service for Apache Spark 自訂限制建立 YAML 檔案,請使用下列格式:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- dataproc.googleapis.com/Session
methodTypes:
- CREATE
condition: CONDITION
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION
更改下列內容:
ORGANIZATION_ID:組織 ID,例如123456789。CONSTRAINT_NAME:新自訂限制的名稱。自訂限制必須以custom.開頭,且只能包含大/小寫英文字母或數字,例如custom.SessionNameMustStartWithTeamName。這個欄位的長度上限為 70 個字元,不含前置字元,例如organizations/123456789/customConstraints/custom.。CONDITION:針對支援服務資源表示法所撰寫的 CEL 條件。這個欄位的長度上限為 1000 個字元 如要進一步瞭解可編寫條件的資源,請參閱「Dataproc Serverless 資源和作業的限制」。範例條件:(resource.name.startsWith("dataproc")。ACTION:符合條件時採取的動作。可以是ALLOW或DENY。DISPLAY_NAME:容易記得的限制名稱。顯示名稱範例:「強制工作階段的存留時間 < 2 小時」。這個欄位的長度上限為 200 個字元。DESCRIPTION:違反政策時,會以錯誤訊息形式顯示且易於理解的限制說明。這個欄位的長度上限為 2,000 個字元。說明範例:「只有在設定允許的 TTL 時,才允許建立工作階段」。
為工作階段範本資源建立自訂限制
如要為工作階段範本資源的 Managed Service for Apache Spark 自訂限制建立 YAML 檔案,請使用下列格式:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- dataproc.googleapis.com/SessionTemplate
methodTypes:
- CREATE
- UPDATE
condition: CONDITION
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION
更改下列內容:
ORGANIZATION_ID:組織 ID,例如123456789。CONSTRAINT_NAME:新自訂限制的名稱。自訂限制必須以custom.開頭,且只能包含大/小寫英文字母或數字,例如custom.SessionTemplateNameMustStartWithTeamName。這個欄位的長度上限為 70 個字元,不含前置字元,例如organizations/123456789/customConstraints/custom.。CONDITION:針對支援服務資源表示法所撰寫的 CEL 條件。這個欄位的長度上限為 1000 個字元 如要進一步瞭解可編寫條件的資源,請參閱「資源和作業的限制」。範例條件:(resource.name.startsWith("dataproc")。ACTION:符合條件時採取的動作。可以是ALLOW或DENY。DISPLAY_NAME:容易記得的限制名稱。顯示名稱範例:「Enforce session template to have a ttl < 2 hours」(強制工作階段範本的 TTL 小於 2 小時)。這個欄位的長度上限為 200 個字元。DESCRIPTION:違反政策時,會以錯誤訊息形式顯示且易於理解的限制說明。這個欄位的長度上限為 2,000 個字元。範例說明:「Only allow session template creation if it sets an allowable TTL」(只允許建立工作階段範本,前提是範本設定了允許的 TTL)。
設定自訂限制
控制台
如要建立自訂限制,請按照下列步驟操作:
- 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。
- 在專案選擇工具中,選取要設定組織政策的專案。
- 按一下「自訂限制」。
- 在「顯示名稱」方塊中,輸入容易理解的限制名稱。這個名稱會顯示在錯誤訊息中,可用於識別和偵錯。請勿在顯示名稱中使用個人識別資訊 (PII) 或私密資料,因為錯誤訊息可能會顯示這類名稱。這個欄位最多可包含 200 個半形字元。
-
在「Constraint ID」(限制 ID) 方塊中,輸入新自訂限制的 ID。自訂限制只能包含字母 (包括大寫和小寫) 或數字,例如
。這個欄位最多可包含 70 個字元,不含前置字元 (custom.batchMustHaveSpecifiedCategoryLabelcustom.),例如organizations/123456789/customConstraints/custom。請勿在限制 ID 中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。 - 在「說明」方塊中,輸入使用者可理解的限制說明。違反政策時,系統會顯示這項說明做為錯誤訊息。請提供違反政策的詳細原因,以及如何解決問題。請勿在說明中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。這個欄位最多可輸入 2000 個字元。
-
在「Resource type」方塊中,選取包含要限制物件和欄位的 Google Cloud REST 資源名稱,例如
container.googleapis.com/NodePool。大多數資源類型最多支援 20 項自訂限制。如果您嘗試建立更多自訂限制,作業會失敗。 -
這項限制只能在 REST
CREATE方法上強制執行。 - 如要定義條件,請按一下「編輯條件」。
-
在「Add condition」(新增條件) 面板中,建立參照支援服務資源的 CEL 條件,例如
resource.management.autoUpgrade == false。這個欄位最多可輸入 1000 個字元。如要進一步瞭解如何使用 CEL,請參閱「 一般運算語言」。如要進一步瞭解自訂限制中可使用的服務資源,請參閱「 自訂限制支援的服務」。 - 按一下 [儲存]。
- 在「動作」下方,選取符合條件時要允許或拒絕評估方法。
- 按一下「建立限制」。
如要查看各項服務支援的方法,請在「 支援自訂限制的服務」中找出該服務。
如果條件評估結果為 true,系統會禁止建立或更新資源。
允許動作是指只有在條件評估為 true 時,才允許建立或更新資源的作業。除了條件中明確列出的情況外,其他所有情況都會遭到封鎖。
在每個欄位中輸入值後,右側會顯示這個自訂限制的對等 YAML 設定。
gcloud
- 如要建立自訂限制,請使用下列格式建立 YAML 檔案:
-
ORGANIZATION_ID:您的機構 ID,例如123456789。 -
CONSTRAINT_NAME:新自訂限制的名稱。自訂限制只能包含字母 (包括大寫和小寫) 或數字,例如。這個欄位最多可包含 70 個字元,不含前置字元 (custom.batchMustHaveSpecifiedCategoryLabelcustom.),例如organizations/123456789/customConstraints/custom。請勿在限制 ID 中輸入 PII 或機密資料,因為錯誤訊息可能會顯示上述資訊。 -
RESOURCE_NAME:內含要限制的物件和欄位的 Google Cloud資源完整名稱,例如:dataproc.googleapis.com/batch。大多數資源類型最多支援 20 項自訂限制。如果您嘗試建立更多自訂限制,作業會失敗。 -
methodTypes:強制執行限制的 REST 方法。只能是CREATE。 -
CONDITION:針對支援服務資源表示法所撰寫的 CEL 條件。這個欄位最多可輸入 1000 個字元。例如:。("category" in resource.labels) && (resource.labels['category'] in ['retail', 'ads', 'service']) -
ACTION:符合condition時採取的動作。只能是ALLOW。 -
DISPLAY_NAME:人類可讀的限制條件名稱。這個名稱會顯示在錯誤訊息中,可用於識別和偵錯。請勿在顯示名稱中使用 PII 或機密資料,因為錯誤訊息可能會顯示這類名稱。這個欄位最多可輸入 200 個半形字元。 -
DESCRIPTION:違反政策時,會以錯誤訊息形式顯示且易於理解的限制說明。這個欄位最多可輸入 2000 個字元。 -
為新的自訂限制建立 YAML 檔案後,您必須加以設定,才能用於組織的組織政策。如要設定自訂限制條件,請使用
gcloud org-policies set-custom-constraint指令: -
如要驗證是否存在自訂限制條件,請使用
gcloud org-policies list-custom-constraints指令:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME resourceTypes: RESOURCE_NAME methodTypes: - CREATE condition: "CONDITION" actionType: ACTION displayName: DISPLAY_NAME description: DESCRIPTION
請替換下列項目:
如要查看各項服務支援的方法,請在「 支援自訂限制的服務」中找出該服務。
如要進一步瞭解可編寫條件的資源,請參閱「支援的資源」。
允許動作是指如果條件評估結果為 true,系統就會允許建立或更新資源的作業。這也表示系統會封鎖條件中明確列出的情況以外的所有其他情況。
gcloud org-policies set-custom-constraint CONSTRAINT_PATH
請將 CONSTRAINT_PATH 替換成自訂限制檔案的完整路徑。例如:/home/user/customconstraint.yaml。
這項作業完成後,自訂限制會顯示在 Google Cloud 組織政策清單中,供組織政策使用。
gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
請將 ORGANIZATION_ID 替換成組織資源的 ID。
詳情請參閱「 查看組織政策」。
強制執行自訂限制
如要強制執行限制,請建立參照該限制的組織政策,然後將該政策套用至 Google Cloud 資源。控制台
- 前往 Google Cloud 控制台的「Organization policies」(組織政策) 頁面。
- 在專案選擇工具中,選取要設定組織政策的專案。
- 在「Organization policies」(組織政策) 頁面的清單中選取限制,即可查看該限制的「Policy details」(政策詳細資料) 頁面。
- 如要為這項資源設定組織政策,請按一下「Manage policy」(管理政策)。
- 在「Edit policy」(編輯政策) 頁面,選取「Override parent's policy」(覆寫上層政策)。
- 按一下「Add a rule」(新增規則)。
- 在「強制執行」部分,選取是否要強制執行這項機構政策。
- 選用:如要根據標記設定組織政策的條件,請按一下「Add condition」(新增條件)。請注意,如果為組織政策新增條件式規則,您必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「 使用標記設定組織政策範圍」。
- 按一下「Test changes」(測試變更),模擬組織政策的影響。詳情請參閱「 使用 Policy Simulator 測試組織政策變更」。
- 如要在模擬測試模式下強制執行組織政策,請按一下「設定模擬測試政策」。詳情請參閱「 測試組織政策」。
- 確認試營運模式中的機構政策運作正常後,請按一下「設定政策」,設定正式政策。
gcloud
- 如要建立含有布林規則的組織政策,請建立參照限制的政策 YAML 檔案:
-
PROJECT_ID:要強制執行限制的專案。 -
CONSTRAINT_NAME:要為自訂限制定義的名稱,例如:。custom.batchMustHaveSpecifiedCategoryLabel -
如要以模擬測試模式強制執行組織政策,請執行下列指令並加上
dryRunSpec旗標: -
確認模擬測試模式中的機構政策能發揮預期效果後,請使用
org-policies set-policy指令和spec旗標設定正式政策:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true dryRunSpec: rules: - enforce: true
請替換下列項目:
gcloud org-policies set-policy POLICY_PATH --update-mask=dryRunSpec
請將 POLICY_PATH 替換為組織政策 YAML 檔案的完整路徑。政策最多需要 15 分鐘才會生效。
gcloud org-policies set-policy POLICY_PATH --update-mask=spec
請將 POLICY_PATH 替換為組織政策 YAML 檔案的完整路徑。政策最多需要 15 分鐘才會生效。
測試自訂限制
本節說明如何測試批次、工作階段和工作階段範本資源的自訂限制。
測試批次資源的自訂限制
下列批次建立範例假設已建立自訂限制,並在批次建立時強制執行,要求批次附加「category」標籤,且值為「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""]
測試工作階段資源的自訂限制
下列工作階段建立範例假設已建立自訂限制,並在工作階段建立時強制執行,要求工作階段的 name 以 orgName 開頭。
gcloud beta dataproc sessions create spark test-session
--location us-central1
輸出內容範例:
Operation denied by custom org policy:
["customConstraints/custom.denySessionNameNotStartingWithOrgName": "Deny session
creation if its name does not start with 'orgName'"]
測試工作階段範本資源的自訂限制
下列工作階段範本建立範例假設已建立自訂限制,並在工作階段範本建立和更新時強制執行,要求工作階段範本的 name 以 orgName 開頭。
gcloud beta dataproc session-templates import test-session-template
--source=saved-template.yaml
輸出內容範例:
Operation denied by custom org policy:
["customConstraints/custom.denySessionTemplateNameNotStartingWithOrgName":
"Deny session template creation or update if its name does not start with
'orgName'"]
資源和作業的限制
本節列出適用於批次和工作階段資源的 Managed Service for Apache Spark 自訂限制。
支援的批次限制
建立 (提交) 批次工作負載時,可以使用下列 Managed Service for Apache Spark 自訂限制:
一般
resource.labels
PySparkBatch
resource.pysparkBatch.mainPythonFileUriresource.pysparkBatch.argsresource.pysparkBatch.pythonFileUrisresource.pysparkBatch.jarFileUrisresource.pysparkBatch.fileUrisresource.pysparkBatch.archiveUris
SparkBatch
resource.sparkBatch.mainJarFileUriresource.sparkBatch.mainClassresource.sparkBatch.argsresource.sparkBatch.jarFileUrisresource.sparkBatch.fileUrisresource.sparkBatch.archiveUris
SparRBatch
resource.sparkRBatch.mainRFileUriresource.sparkRBatch.argsresource.sparkRBatch.fileUrisresource.sparkRBatch.archiveUris
SparkSqlBatch
resource.sparkSqlBatch.queryFileUriresource.sparkSqlBatch.queryVariablesresource.sparkSqlBatch.jarFileUris
RuntimeConfig
resource.runtimeConfig.versionresource.runtimeConfig.containerImageresource.runtimeConfig.propertiesresource.runtimeConfig.repositoryConfig.pypiRepositoryConfig.pypiRepositoryresource.runtimeConfig.autotuningConfig.scenariosresource.runtimeConfig.cohort
ExecutionConfig
resource.environmentConfig.executionConfig.serviceAccountresource.environmentConfig.executionConfig.networkUriresource.environmentConfig.executionConfig.subnetworkUriresource.environmentConfig.executionConfig.networkTagsresource.environmentConfig.executionConfig.kmsKeyresource.environmentConfig.executionConfig.idleTtlresource.environmentConfig.executionConfig.ttlresource.environmentConfig.executionConfig.stagingBucketresource.environmentConfig.executionConfig.authenticationConfig.userWorkloadAuthenticationType
PeripheralsConfig
resource.environmentConfig.peripheralsConfig.metastoreServiceresource.environmentConfig.peripheralsConfig.sparkHistoryServerConfig.dataprocCluster
支援的工作階段限制
在無伺服器工作階段中建立自訂限制時,可以使用下列工作階段屬性:
一般
resource.nameresource.sparkConnectSessionresource.userresource.sessionTemplate
JupyterSession
resource.jupyterSession.kernelresource.jupyterSession.displayName
RuntimeConfig
resource.runtimeConfig.versionresource.runtimeConfig.containerImageresource.runtimeConfig.propertiesresource.runtimeConfig.repositoryConfig.pypiRepositoryConfig.pypiRepositoryresource.runtimeConfig.autotuningConfig.scenariosresource.runtimeConfig.cohort
ExecutionConfig
resource.environmentConfig.executionConfig.serviceAccountresource.environmentConfig.executionConfig.networkUriresource.environmentConfig.executionConfig.subnetworkUriresource.environmentConfig.executionConfig.networkTagsresource.environmentConfig.executionConfig.kmsKeyresource.environmentConfig.executionConfig.idleTtlresource.environmentConfig.executionConfig.ttlresource.environmentConfig.executionConfig.stagingBucketresource.environmentConfig.executionConfig.authenticationConfig.userWorkloadAuthenticationType
PeripheralsConfig
resource.environmentConfig.peripheralsConfig.metastoreServiceresource.environmentConfig.peripheralsConfig.sparkHistoryServerConfig.dataprocCluster
支援的工作階段範本限制
在無伺服器工作階段範本上建立自訂限制時,可以使用下列工作階段範本屬性:
一般
resource.nameresource.descriptionresource.sparkConnectSession
JupyterSession
resource.jupyterSession.kernelresource.jupyterSession.displayName
RuntimeConfig
resource.runtimeConfig.versionresource.runtimeConfig.containerImageresource.runtimeConfig.propertiesresource.runtimeConfig.repositoryConfig.pypiRepositoryConfig.pypiRepositoryresource.runtimeConfig.autotuningConfig.scenariosresource.runtimeConfig.cohort
ExecutionConfig
resource.environmentConfig.executionConfig.serviceAccountresource.environmentConfig.executionConfig.networkUriresource.environmentConfig.executionConfig.subnetworkUriresource.environmentConfig.executionConfig.networkTagsresource.environmentConfig.executionConfig.kmsKeyresource.environmentConfig.executionConfig.idleTtlresource.environmentConfig.executionConfig.ttlresource.environmentConfig.executionConfig.stagingBucketresource.environmentConfig.executionConfig.authenticationConfig.userWorkloadAuthenticationType
PeripheralsConfig
resource.environmentConfig.peripheralsConfig.metastoreServiceresource.environmentConfig.peripheralsConfig.sparkHistoryServerConfig.dataprocCluster
常見用途的自訂限制範例
本節提供批次和工作階段資源的常見用途自訂限制範例。
批次資源的自訂限制範例
下表提供 Managed Service for Apache Spark 批次自訂限制的範例:
| 說明 | 限制語法 |
|---|---|
| 批次必須附加「category」標籤,並使用允許的值。 |
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. |
| 必須使用 SparkSQL。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchMustUseSparkSQL resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: (has(resource.sparkSqlBatch)) actionType: ALLOW displayName: Enforce batch only use SparkSQL Batch. description: Only allow creation of SparkSQL Batch. |
| 批次作業必須將存留時間設為少於 2 小時。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchMustSetLessThan2hTtl resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: (has(resource.environmentConfig.executionConfig.ttl)) && (resource.environmentConfig.executionConfig.ttl <= duration('2h')) actionType: ALLOW displayName: Enforce batch TTL. description: Only allow batch creation if it sets an allowable TTL. |
| 批次最多只能設定 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( |
批次執行器記憶體設定必須以 m 後置字元結尾,且小於 20000 m。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.batchExecutorMemoryMax resourceTypes: - dataproc.googleapis.com/Batch methodTypes: - CREATE condition: ('spark.executor.memory' in resource.runtimeConfig.properties) && (resource.runtimeConfig.properties['spark.executor.memory'].endsWith('m')) && (int(resource.runtimeConfig.properties['spark.executor.memory'].split('m')[0])<20000) actionType: ALLOW displayName: Enforce batch executor maximum memory. description: Only allow batch creation if the executor memory setting ends with a suffix 'm' and is less than 20000 m. |
工作階段資源的自訂限制範例
下表提供 Managed Service for Apache Spark 工作階段自訂限制的範例:
| 說明 | 限制語法 |
|---|---|
工作階段必須將 sessionTemplate 設為空白字串。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionTemplateMustBeEmpty resourceTypes: - dataproc.googleapis.com/Session methodTypes: - CREATE condition: resource.sessionTemplate == "" actionType: ALLOW displayName: Enforce empty session templates. description: Only allow session creation if session template is empty string. |
sessionTemplate 必須等於核准的範本 ID。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionTemplateIdMustBeApproved resourceTypes: - dataproc.googleapis.com/Session methodTypes: - CREATE condition: resource.sessionTemplate.startsWith("https://www.googleapis.com/compute/v1/projects/") && resource.sessionTemplate.contains("/locations/") && resource.sessionTemplate.contains("/sessionTemplates/") && ( resource.sessionTemplate.endsWith("/1") || resource.sessionTemplate.endsWith("/2") || resource.sessionTemplate.endsWith("/13") ) actionType: ALLOW displayName: Enforce templateId must be 1, 2, or 13. description: Only allow session creation if session template ID is in the approved list, that is, 1, 2 and 13. |
| 工作階段必須使用使用者憑證驗證工作負載。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.AllowEUCSessions resourceTypes: - dataproc.googleapis.com/Session methodTypes: - CREATE condition: resource.environmentConfig.executionConfig.authenticationConfig.userWorkloadAuthenticationType=="END_USER_CREDENTIALS" actionType: ALLOW displayName: Require end user credential authenticated sessions. description: Allow session creation only if the workload is authenticated using end-user credentials. |
| 工作階段必須設定允許的執行階段版本。 |
name: organizations/ORGANIZATION_ID/custom.sessionMustUseAllowedVersion resourceTypes: - dataproc.googleapis.com/Session methodTypes: - CREATE condition: (has(resource.runtimeConfig.version)) && (resource.runtimeConfig.version in ["2.0.45", "2.0.48"]) actionType: ALLOW displayName: Enforce session runtime version. description: Only allow session creation if it sets an allowable runtime version. |
| 工作階段必須將存留時間設為少於 2 小時。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionMustSetLessThan2hTtl resourceTypes: - dataproc.googleapis.com/Session methodTypes: - CREATE condition: (has(resource.environmentConfig.executionConfig.ttl)) && (resource.environmentConfig.executionConfig.ttl <= duration('2h')) actionType: ALLOW displayName: Enforce session TTL. description: Only allow session creation if it sets an allowable TTL. |
| 工作階段最多只能設定 20 個 Spark 初始執行器。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionInitialExecutorMax20 resourceTypes: - dataproc.googleapis.com/Session 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 session Spark executor instances. description: Deny session creation if it specifies more than 20 Spark executor instances. |
| 工作階段最多只能設定 20 個 Spark 動態分配初始執行器。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionDynamicAllocationInitialExecutorMax20 resourceTypes: - dataproc.googleapis.com/Session 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 session dynamic allocation initial executors. description: Deny session creation if it specifies more than 20 Spark dynamic allocation initial executors. |
| 工作階段必須將 KMS 金鑰設為允許的模式。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionKmsPattern resourceTypes: - dataproc.googleapis.com/Session methodTypes: - CREATE condition: matches(resource.environmentConfig.executionConfig.kmsKey, '^keypattern[a-z]$') actionType: ALLOW displayName: Enforce session KMS Key pattern. description: Only allow session creation if it sets the KMS key to an allowable pattern. |
| 工作階段必須將暫存值區前置字元設為允許的值。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionStagingBucketPrefix
resourceTypes:
- dataproc.googleapis.com/Session
methodTypes:
- CREATE
condition: resource.environmentConfig.executionConfig.stagingBucket.startsWith( |
工作階段執行器記憶體設定必須以 m
結尾,且小於 20000 m。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionExecutorMemoryMax resourceTypes: - dataproc.googleapis.com/Session methodTypes: - CREATE condition: ('spark.executor.memory' in resource.runtimeConfig.properties) && (resource.runtimeConfig.properties['spark.executor.memory'].endsWith('m')) && (int(resource.runtimeConfig.properties['spark.executor.memory'].split('m')[0])<20000) actionType: ALLOW displayName: Enforce session executor maximum memory. description: Only allow session creation if the executor memory setting ends with a suffix 'm' and is less than 20000 m. |
工作階段範本資源的自訂限制範例
下表提供 Managed Service for Apache Spark 工作階段範本自訂限制的範例:
| 說明 | 限制語法 |
|---|---|
工作階段範本名稱結尾必須是 org-name。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.denySessionTemplateNameNotEndingWithOrgName resourceTypes: - dataproc.googleapis.com/SessionTemplate methodTypes: - CREATE - UPDATE condition: '!resource.name.endsWith(''org-name'')' actionType: DENY displayName: DenySessionTemplateNameNotEndingWithOrgName description: Deny session template creation/update if its name does not end with 'org-name' |
| 工作階段範本必須設定允許的執行階段版本。 |
name: organizations/ORGANIZATION_ID/custom.sessionTemplateMustUseAllowedVersion resourceTypes: - dataproc.googleapis.com/SessionTemplate methodTypes: - CREATE - UPDATE condition: (has(resource.runtimeConfig.version)) && (resource.runtimeConfig.version in ["2.0.45", "2.0.48"]) actionType: ALLOW displayName: Enforce session template runtime version. description: Only allow session template creation or update if it sets an allowable runtime version. |
| 工作階段範本的存留時間必須少於 2 小時。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionTemplateMustSetLessThan2hTtl resourceTypes: - dataproc.googleapis.com/SessionTemplate methodTypes: - CREATE - UPDATE condition: (has(resource.environmentConfig.executionConfig.ttl)) && (resource.environmentConfig.executionConfig.ttl <= duration('2h')) actionType: ALLOW displayName: Enforce session template TTL. description: Only allow session template creation or update if it sets an allowable TTL. |
| 工作階段範本最多只能設定 20 個 Spark 初始執行器。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionTemplateInitialExecutorMax20 resourceTypes: - dataproc.googleapis.com/SessionTemplate methodTypes: - CREATE - UPDATE 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 session Spark executor instances. description: Deny session template creation or update if it specifies more than 20 Spark executor instances. |
| 工作階段範本最多只能設定 20 個 Spark 動態分配初始執行器。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionTemplateDynamicAllocationInitialExecutorMax20 resourceTypes: - dataproc.googleapis.com/SessionTemplate methodTypes: - CREATE - UPDATE 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 session dynamic allocation initial executors. description: Deny session template creation or update if it specifies more than 20 Spark dynamic allocation initial executors. |
| 工作階段範本必須將 KMS 金鑰設為允許的模式。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionTemplateKmsPattern resourceTypes: - dataproc.googleapis.com/SessionTemplate methodTypes: - CREATE - UPDATE condition: matches(resource.environmentConfig.executionConfig.kmsKey, '^keypattern[a-z]$') actionType: ALLOW displayName: Enforce session KMS Key pattern. description: Only allow session template creation or update if it sets the KMS key to an allowable pattern. |
| 工作階段範本必須將暫存值區前置字元設為允許的值。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionTemplateStagingBucketPrefix
resourceTypes:
- dataproc.googleapis.com/SessionTemplate
methodTypes:
- CREATE
- UPDATE
condition: resource.environmentConfig.executionConfig.stagingBucket.startsWith( |
工作階段範本執行器記憶體設定必須以 m
結尾,且小於 20000 m。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.sessionTemplateExecutorMemoryMax resourceTypes: - dataproc.googleapis.com/SessionTemplate methodTypes: - CREATE - UPDATE condition: ('spark.executor.memory' in resource.runtimeConfig.properties) && (resource.runtimeConfig.properties['spark.executor.memory'].endsWith('m')) && (int(resource.runtimeConfig.properties['spark.executor.memory'].split('m')[0])<20000) actionType: ALLOW displayName: Enforce session executor maximum memory. description: Only allow session template creation or update if the executor memory setting ends with a suffix 'm' and is less than 20000 m. |