CUSTOM_CONSTRAINT:custom.allowClusterCreateIfAnnotationPresentACTION_TYPE:ALLOWMETHOD_TYPES:CREATERESOURCE_TYPES:gkemulticloud.googleapis.com/AttachedClusterDISPLAY_NAME:Allow new clusters only when certain annotations are set.
ERROR:(gcloud.container.attached.clusters.register)FAILED_PRECONDITION:Operationdeniedbyorgpolicyonresource'projects/PROJECT_NUMBER/locations/GOOGLE_CLOUD_REGION':["customConstraints/custom.allowClusterCreateIfAnnotationPresent":"Allow new clusters only when certain annotations are set."]'@type':type.googleapis.com/google.rpc.ErrorInfo
domain:googleapis.com
metadata:
customConstraints:customConstraints/custom.allowClusterCreateIfAnnotationPresent
service:gkemulticloud.googleapis.com
reason:CUSTOM_ORG_POLICY_VIOLATION
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-08-30 UTC。"],[],[],null,["# Create and manage custom constraints\n\nThis page shows you how to enable and use custom constraints within your GKE attached clusters environment. Google Cloud's [Organization Policy Service](/resource-manager/docs/organization-policy/overview) helps you manage resource configurations and create guardrails in your cloud environment.\n\nOverview\n--------\n\nWith custom organization policies, you can create granular resource policies across GKE Multi-Cloud environments to meet your organization's specific security and compliance requirements. You can also create organization policies in dry-run mode to test new policies without affecting your production workloads.\n\nTo learn more about organization policies, see [Introduction to the Organization Policy Service](/resource-manager/docs/organization-policy/overview#custom-organization-policies).\n\nBefore you begin\n----------------\n\nBefore you start, make sure you understand the following topics:\n\n### Policy inheritance\n\nBy default, organization policies are inherited by the descendants of the resources on which you enforce the policy. For example, if you enforce a policy on an organization, Google Cloud enforces the policy on all projects in the organization. To learn more about policy inheritance and how to change evaluation rules, see [Hierarchy evaluation rules](/resource-manager/docs/organization-policy/understanding-hierarchy#disallow_inheritance).\n\n### Limitations\n\nBefore you create custom constraints, keep in mind the following limitations:\n\n- Custom constraints can only be enforced on the `CREATE` or `UPDATE` methods for GKE attached clusters resources.\n- Newly enforced custom constraints don't automatically apply to existing resources. You must update existing resources to apply the constraint. To find existing resources that need to be updated, you can enforce a [dry-run organization policy](/resource-manager/docs/organization-policy/dry-run-policy).\n- To create constraints and enforce organization policies, you need the [Organization Policy Administrator](/iam/docs/understanding-roles#orgpolicy.policyAdmin) (`roles/orgpolicy.policyAdmin`) IAM role on your Google Cloud organization. For more information about the permissions required to manage organization policies with custom constraints, see [Required roles](/resource-manager/docs/organization-policy/creating-managing-custom-constraints#required-roles).\n\n### Pricing\n\nOrganizational policies and custom constraints are offered at no charge.\n| **Note:** This guide provides a general overview. Specific gcloud commands may vary based on your exact configuration and required policy enforcement.\n\nCreate custom constraints\n-------------------------\n\nCustom constraints must be specified by using the fields in the AttachedCluster [API resource spec](/kubernetes-engine/multi-cloud/docs/reference/rest/v1/projects.locations.attachedClusters#AttachedCluster), excluding fields that are described as \"Output only\".\n\n### Create a custom constraint\n\nTo create a new custom constraint, you define the constraint in a YAML file and apply the custom constraint in your organization using the Google Cloud CLI. This constraint must encapsulate the specific policy you want to enforce across your GKE attached clusters resources.\n\n1. Create a YAML file for the custom constraint:\n\n name: organizations/\u003cvar translate=\"no\"\u003eORGANIZATION_ID\u003c/var\u003e/customConstraints/\u003cvar translate=\"no\"\u003eCONSTRAINT_NAME\u003c/var\u003e\n resourceTypes:\n - gkemulticloud.googleapis.com/AttachedCluster\n methodTypes: \n - CREATE\n condition: \\\"\u003cvar translate=\"no\"\u003eCONDITION\u003c/var\u003e\\\"\n actionType: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eACTION\u003c/span\u003e\u003c/var\u003e\n displayName: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eDISPLAY_NAME\u003c/span\u003e\u003c/var\u003e\n description: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eDESCRIPTION\u003c/span\u003e\u003c/var\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eORGANIZATION_ID\u003c/var\u003e : your organization ID, such as `123456789`.\n\n - \u003cvar translate=\"no\"\u003eCONSTRAINT_NAME\u003c/var\u003e : the name of your new custom constraint. A custom constraint must start with `custom.`, and can only include uppercase letters, lowercase letters, or numbers. For example, `custom.allowClusterCreateIfAnnotationPresent`. The maximum length of this field is 70 characters, not counting the prefix, for example, `organizations/123456789/customConstraints/custom`.\n\n - \u003cvar translate=\"no\"\u003eCONDITION\u003c/var\u003e : a condition that is written against a representation of an attached cluster resource. Conditions are written in [Common Expression Language (CEL)](/resource-manager/docs/organization-policy/creating-managing-custom-constraints#common_expression_language). This field has a maximum length of 1,000 characters.\n For example, condition: `\"key\" in resource.annotations && resource.annotations.key == \"created-by\"`.\n\n - \u003cvar translate=\"no\"\u003eACTION\u003c/var\u003e : the action to take if the condition is met. This can be either `ALLOW` or `DENY`.\n\n - \u003cvar translate=\"no\"\u003eDISPLAY_NAME\u003c/var\u003e : a display name for the constraint. This field has a maximum length of 200 characters.\n\n - \u003cvar translate=\"no\"\u003eDESCRIPTION\u003c/var\u003e : a description of the constraint to display as an error message when the policy is violated, for example, `\"Allow new clusters only when certain annotations are set.\"` This field has a maximum length of 2,000 characters.\n\n For more information about how to create a custom constraint, see [Defining custom constraints](/resource-manager/docs/organization-policy/creating-managing-custom-constraints#defining_custom_constraints).\n\n### Associate the constraint with your resources\n\nAfter you have created the YAML file for a new custom constraint, you must set it up to make the constraint available for organization policies.\n\n1. To set up a custom constraint, use the `gcloud org-policies set-custom-constraint` command:\n\n gcloud org-policies set-custom-constraint \u003cvar translate=\"no\"\u003ePATH_TO_FILE\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003ePATH_TO_FILE\u003c/var\u003e with the path of your custom constraint YAML definition.\n2. To verify that the custom constraint is created, use the `gcloud org-policies list-custom-constraints` command:\n\n gcloud org-policies list-custom-constraints --organization=\u003cvar translate=\"no\"\u003eORGANIZATION_ID\u003c/var\u003e\n\n The output lists the created policies: \n\n CUSTOM_CONSTRAINT: custom.allowClusterCreateIfAnnotationPresent\n ACTION_TYPE: ALLOW\n METHOD_TYPES: CREATE\n RESOURCE_TYPES: gkemulticloud.googleapis.com/AttachedCluster\n DISPLAY_NAME: Allow new clusters only when certain annotations are set.\n\nEnforce the custom constraint\n-----------------------------\n\nTo enforce the new custom constraint, create an organization policy that references the constraint, and then apply the organization policy.\n\n1. Create a YAML file for the organization policy:\n\n name: \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-l devsite-syntax-l-Scalar devsite-syntax-l-Scalar-Plain\"\u003eRESOURCE_HIERARCHY\u003c/span\u003e\u003c/var\u003e/policies/\u003cvar translate=\"no\"\u003ePOLICY_NAME\u003c/var\u003e\n spec:\n rules:\n - enforce: true\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eRESOURCE_HIERARCHY\u003c/var\u003e: the location of the new policy, which affects the scope of enforcement. Use the Google Cloud resource hierarchy as a guide. For example, if you want to enforce the policy in a specific project, use `projects/`\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e. To enforce the policy in a specific organization, use `organizations/`\u003cvar translate=\"no\"\u003eORGANIZATION_ID\u003c/var\u003e.\n\n - \u003cvar translate=\"no\"\u003ePOLICY_NAME\u003c/var\u003e: the name of the new policy.\n\n2. Enforce the policy:\n\n gcloud org-policies set-policy \u003cvar translate=\"no\"\u003ePATH_TO_POLICY\u003c/var\u003e\n\n Replace \u003cvar translate=\"no\"\u003ePATH_TO_POLICY\u003c/var\u003e with the path to your policy definition file.\n3. Verify that the policy exists:\n\n gcloud org-policies list --\u003cvar translate=\"no\"\u003eRESOURCE_FLAG\u003c/var\u003e=\u003cvar translate=\"no\"\u003eRESOURCE_ID\u003c/var\u003e\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eRESOURCE_FLAG\u003c/var\u003e: the Google Cloud resource where you enforced the policy. For example, a project or organization.\n\n - \u003cvar translate=\"no\"\u003eRESOURCE_ID\u003c/var\u003e: the ID of the resource where you enforced the policy. For example, your project ID or organization ID.\n\n The output is similar to the following: \n\n CONSTRAINT: custom.allowClusterCreateIfAnnotationPresent\n LIST_POLICY: -\n BOOLEAN_POLICY: SET\n ETAG: CPjb27wGEOijhL4B-\n\nTest the policy\n---------------\n\nTest the organization policy by registering an AKS cluster in a restricted project.\n\n1. Register an AKS cluster in a restricted project.\n\n gcloud container attached clusters register CLUSTER_NAME \\\n --location=GOOGLE_CLOUD_REGION \\\n --fleet-project=PROJECT_NUMBER \\\n --platform-version=PLATFORM_VERSION \\\n --distribution=aks \\\n --issuer-url=ISSUER_URL \\\n --context=KUBECONFIG_CONTEXT \\\n --kubeconfig=KUBECONFIG_PATH\n\n2. The output is similar to the following:\n\n ERROR: (gcloud.container.attached.clusters.register) FAILED_PRECONDITION: Operation denied by org policy on resource 'projects/PROJECT_NUMBER/locations/GOOGLE_CLOUD_REGION': [\"customConstraints/custom.allowClusterCreateIfAnnotationPresent\": \"Allow new clusters only when certain annotations are set.\"]\n\n '@type': type.googleapis.com/google.rpc.ErrorInfo\n domain: googleapis.com\n metadata:\n customConstraints: customConstraints/custom.allowClusterCreateIfAnnotationPresent\n service: gkemulticloud.googleapis.com\n reason: CUSTOM_ORG_POLICY_VIOLATION"]]