本頁面詳細說明如何為 Cloud Run 服務和工作建立自訂限制,並在專案層級強制執行。如要瞭解自訂機構政策,請參閱「建立及管理自訂機構政策」。
如果您已使用 gcloud functions
指令或 Cloud Functions v2 API 建立或部署 Cloud Run 函式,請參閱使用自訂限制管理函式資源。
Google Cloud 機構政策可讓您透過程式以集中方式控管機構資源。身為機構政策管理員,您可以定義機構政策,也就是一組稱為「限制」的限制,適用於 Google Cloud 資源階層中的Google Cloud 資源和這些資源的子系。您可以在機構、資料夾或專案層級強制執行機構政策。
機構政策為各種Google Cloud 服務提供預先定義的限制。不過,如果您想進一步控管機構政策中受限制的特定欄位,並進一步細分及自訂這些欄位,也可以建立自訂機構政策。
優點
- 成本管理:使用機構政策限制貴機構可使用的 VM 執行個體和磁碟大小和類型。您也可以限制用於 VM 執行個體的機器系列。
- 安全性、法規遵循和治理:
- 如要強制執行安全性規定,您可以要求 VM 使用特定的防火牆通訊埠規則。
- 如要支援硬體隔離或授權法規遵循,您可以要求特定專案或資料夾中的所有 VM 在單一用戶群節點上執行。
- 如要管理自動化指令碼,您可以使用自訂機構政策,驗證標籤是否符合必要的運算式。
Cloud Run 可讓您使用 Cloud Run Admin API 中大部分使用者設定的欄位,編寫任意數量的自訂限制。舉例來說,您可以建立自訂限制,指定服務設為內部,或防止非 GA 啟動階段。
套用後,如果要求違反強制自訂限制的政策,gcloud CLI 和 Cloud Run 記錄中就會顯示錯誤訊息。錯誤訊息包含限制 ID 和違反自訂限制的說明。
政策繼承
根據預設,機構政策會由您強制執行政策的資源子系繼承。舉例來說,如果您對資料夾套用政策, Google Cloud 就會對資料夾中的所有專案套用該政策。如要進一步瞭解這項行為和如何變更,請參閱「階層評估規則」。
定價
機構政策服務 (包括預先定義和自訂機構政策) 均免費提供。
限制
- 您必須使用 Cloud Run Admin API v1 欄位規格,才能指定 Cloud Run 自訂限制。
- 不支援標籤和頂層欄位。
事前準備
- 請務必確認您知道自己的機構 ID。
必要的角色
如要取得管理機構政策所需的權限,請要求管理員授予您機構資源的 機構政策管理員 (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 檔案中定義,並由您要套用機構政策的服務支援的資源、方法、條件和動作定義。您可以使用一般運算語言 (CEL) 定義自訂限制條件。如要進一步瞭解如何使用 CEL 在自訂限制中建立條件,請參閱「建立及管理自訂限制」一文的 CEL 相關部分。
如要為 Cloud Run 自訂限制建立 YAML 檔案,請參考以下範例:
name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- run.googleapis.com/Service
methodTypes:
- CREATE
- UPDATE
condition: "CONDITION"
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION
更改下列內容:
ORGANIZATION_ID
:貴機構 ID,例如123456789
。CONSTRAINT_NAME
:您要為新自訂限制設定的名稱。自訂限制的開頭必須為custom.
,且只能包含大寫英文字母、小寫英文字母或數字,例如 custom.ingressInternal。這個欄位的長度上限為 70 個半形字元,不含前置字元 (例如organizations/123456789/customConstraints/custom
)。CONDITION
:針對支援的服務資源表示法編寫的 CEL 條件。這個欄位的長度上限為 1000 個半形字元。例如:condition: "'run.googleapis.com/ingress' in resource.metadata.annotations && resource.metadata.annotations['run.googleapis.com/ingress'] == 'internal'"。ACTION
:如果符合condition
,則採取的動作。這個欄位可以是ALLOW
或DENY
。DISPLAY_NAME
:限制的易讀名稱。這個欄位的長度上限為 200 個半形字元。DESCRIPTION
:在違反政策時,要以錯誤訊息形式顯示的限制說明,例如「Require ingress to be set to internal.」這個欄位的長度上限為 2000 個半形字元。
如要進一步瞭解如何建立自訂限制,請參閱「定義自訂限制」。
設定自訂限制
為新自訂限制建立 YAML 檔案後,您必須設定該檔案,以便在貴機構中使用機構政策。如要設定自訂限制條件,請使用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 資源,藉此強制執行限制。控制台
- 在 Google Cloud 控制台中,前往「Organization policies」(機構政策) 頁面。
- 在專案選擇工具中,選取要設定機構政策的專案。
- 在「Organization policies」頁面的清單中選取限制條件,即可查看該限制條件的「Policy details」頁面。
- 如要設定這項資源的機構政策,請按一下「管理政策」。
- 在「Edit policy」頁面中,選取「Override parent's policy」。
- 按一下「新增規則」。
- 在「Enforcement」(強制執行) 部分,選取是否要啟用這項機構政策的強制執行機制。
- 選用:如要讓機構政策依標記而定,請按一下「新增條件」。請注意,如果您在組織政策中新增條件規則,則必須至少新增一項無條件規則,否則無法儲存政策。詳情請參閱「設定含有標記的組織政策」。
- 按一下「測試變更」,模擬機構政策的效果。舊版受管理限制條件無法進行政策模擬。詳情請參閱「 使用 Policy Simulator 測試組織政策變更」。
- 如要完成並套用機構政策,請按一下「設定政策」。這項政策最多需要 15 分鐘才會生效。
gcloud
如要建立包含布林值規則的機構政策,請建立參照限制的政策 YAML 檔案:
name: projects/PROJECT_ID/policies/CONSTRAINT_NAME spec: rules: - enforce: true
請依指示取代下列內容:
-
PROJECT_ID
:您要套用限制的專案。 -
CONSTRAINT_NAME
:您為自訂限制定義的名稱。例如custom.ingressInternal
。
如要強制執行包含限制的機構政策,請執行下列指令:
gcloud org-policies set-policy POLICY_PATH
將 POLICY_PATH
替換為機構政策 YAML 檔案的完整路徑。這項政策最多需要 15 分鐘才會生效。
測試自訂限制
如要測試限制輸入設定的範例,請嘗試在專案中部署 Cloud Run 服務,並將輸入設為 all
:
gcloud run deploy org-policy-test \
--project=PROJECT_ID \
--region=REGION_ID \
--ingress=all
輸出內容如下:
Operation denied by custom org policies: ["customConstraints/custom.ingressConstraint": "Require ingress to be set to internal."]
常見用途的自訂機構政策範例
下表列出一些您可能會在 Cloud Run 服務和工作中覺得實用的自訂限制範例:
說明 | 限制條件語法 |
---|---|
要求將 Cloud Run 服務設為內部。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.ingressInternal resourceTypes: - run.googleapis.com/Service methodTypes: - CREATE - UPDATE condition: "'run.googleapis.com/ingress' in resource.metadata.annotations && resource.metadata.annotations['run.googleapis.com/ingress'] == 'internal'" actionType: ALLOW displayName: IngressInternal description: Require ingress to be set to internal. |
說明 | 限制條件語法 |
要求 Cloud Run 服務的所有容器都設有自訂記憶體限制。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.memoryLimit resourceTypes: - run.googleapis.com/Service methodTypes: - CREATE - UPDATE condition: "resource.spec.template.spec.containers.all(container, 'memory' in container.resources.limits && container.resources.limits['memory'] <= 'MEMORY_LIMIT')" actionType: ALLOW displayName: memoryLimitCap description: Require the container memory limit to be set to <= MEMORY_LIMIT. |
說明 | 限制條件語法 |
禁止將 Cloud Run 發布階段從預設 GA 變更為非 GA 發布階段。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.launchStage resourceTypes: - run.googleapis.com/Service methodTypes: - CREATE - UPDATE condition: "!('run.googleapis.com/launch-stage' in resource.metadata.annotations) || resource.metadata.annotations['run.googleapis.com/launch-stage'] == 'GA'" actionType: ALLOW displayName: launchStage description: Only allow users to create and update Cloud Run services with either an unset launch stage (default is GA) or a launch stage explicitly set to GA. |
說明 | 限制條件語法 |
要求預設設定為「二進位授權」。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.binaryAuthorization resourceTypes: - run.googleapis.com/Service methodTypes: - CREATE - UPDATE condition: "'run.googleapis.com/binary-authorization' in resource.metadata.annotations && resource.metadata.annotations['run.googleapis.com/binary-authorization'] == 'default'" actionType: ALLOW displayName: binaryAuthorization description: Require binaryAuthorization to be set to default. |
說明 | 限制條件語法 |
要求服務為每個容器提供存活性檢查程序。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.livenessProbe resourceTypes: - run.googleapis.com/Service methodTypes: - CREATE - UPDATE condition: "resource.spec.template.spec.containers.all(container, has(container.livenessProbe.initialDelaySeconds))" actionType: ALLOW displayName: livenessProbe description: Require all containers to have a liveness probe configured with initialDelaySeconds. |
說明 | 限制條件語法 |
要求服務至少要有一個側邊容器,使用開頭為特定前置字元的映像檔,且通訊埠等於指定的數字。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.requireSidecar resourceTypes: - run.googleapis.com/Service methodTypes: - CREATE - UPDATE condition: "resource.spec.template.spec.containers.exists(container, container.image.startsWith('us-docker.pkg.dev/cloud-ops-agents-artifacts/cloud-run-gmp-sidecar/') && container.ports.exists(port, port.containerPort == 8081))" actionType: ALLOW displayName: requireSidecar description: Require at least one container with an image that starts with "us-docker.pkg.dev/cloud-ops-agents-artifacts/cloud-run-gmp-sidecar/" and uses port 8081. |
說明 | 限制條件語法 |
只允許建立及編輯函式。 |
name: organizations/ORGANIZATION_ID/customConstraints/custom.allowcrf resource_types: run.googleapis.com/Service method_types: - CREATE - UPDATE condition: "resource.spec.template.spec.containers.exists(container, container.image.startsWith('gcr.io/cloudrun/placeholder')) || (has(resource.metadata.annotations) && 'run.googleapis.com/build-function-target' in resource.metadata.annotations)" action_type: ALLOW display_name: runFunctionsOnly description: Only allows the creation and editing of Cloud Run functions |