|
| 1 | +--- |
| 2 | +Title: "Custom Policy Template Resource" |
| 3 | +Description: |- |
| 4 | + Creating a custom policy template. |
| 5 | +--- |
| 6 | + |
| 7 | +# Custom Policy Template Resource |
| 8 | + |
| 9 | +This resource enables users to create custom policy template in TMC. |
| 10 | + |
| 11 | +For more information regarding custom policy template, see [Custom Policy Template][custom-policy-template]. |
| 12 | + |
| 13 | +[custom-policy-template]: https://docs.vmware.com/en/VMware-Tanzu-Mission-Control/services/tanzumc-using/GUID-F147492B-04FD-4CFD-8D1F-66E36D40D49C.html |
| 14 | + |
| 15 | +## Example Usage |
| 16 | + |
| 17 | +```terraform |
| 18 | +resource "tanzu-mission-control_custom_policy_template" "sample" { |
| 19 | + name = "tf-custom-template-test" |
| 20 | +
|
| 21 | + spec { |
| 22 | + object_type = "ConstraintTemplate" |
| 23 | + template_type = "OPAGatekeeper" |
| 24 | +
|
| 25 | + data_inventory { |
| 26 | + kind = "ConfigMap" |
| 27 | + group = "admissionregistration.k8s.io" |
| 28 | + version = "v1" |
| 29 | + } |
| 30 | +
|
| 31 | + data_inventory { |
| 32 | + kind = "Deployment" |
| 33 | + group = "extensions" |
| 34 | + version = "v1" |
| 35 | + } |
| 36 | +
|
| 37 | + template_manifest = <<YAML |
| 38 | +apiVersion: templates.gatekeeper.sh/v1beta1 |
| 39 | +kind: ConstraintTemplate |
| 40 | +metadata: |
| 41 | + name: tf-custom-template-test |
| 42 | + annotations: |
| 43 | + description: Requires Pods to have readiness and/or liveness probes. |
| 44 | +spec: |
| 45 | + crd: |
| 46 | + spec: |
| 47 | + names: |
| 48 | + kind: tf-custom-template-test |
| 49 | + validation: |
| 50 | + openAPIV3Schema: |
| 51 | + properties: |
| 52 | + probes: |
| 53 | + type: array |
| 54 | + items: |
| 55 | + type: string |
| 56 | + probeTypes: |
| 57 | + type: array |
| 58 | + items: |
| 59 | + type: string |
| 60 | + targets: |
| 61 | + - target: admission.k8s.gatekeeper.sh |
| 62 | + rego: | |
| 63 | + package k8srequiredprobes |
| 64 | + probe_type_set = probe_types { |
| 65 | + probe_types := {type | type := input.parameters.probeTypes[_]} |
| 66 | + } |
| 67 | + violation[{"msg": msg}] { |
| 68 | + container := input.review.object.spec.containers[_] |
| 69 | + probe := input.parameters.probes[_] |
| 70 | + probe_is_missing(container, probe) |
| 71 | + msg := get_violation_message(container, input.review, probe) |
| 72 | + } |
| 73 | + probe_is_missing(ctr, probe) = true { |
| 74 | + not ctr[probe] |
| 75 | + } |
| 76 | + probe_is_missing(ctr, probe) = true { |
| 77 | + probe_field_empty(ctr, probe) |
| 78 | + } |
| 79 | + probe_field_empty(ctr, probe) = true { |
| 80 | + probe_fields := {field | ctr[probe][field]} |
| 81 | + diff_fields := probe_type_set - probe_fields |
| 82 | + count(diff_fields) == count(probe_type_set) |
| 83 | + } |
| 84 | + get_violation_message(container, review, probe) = msg { |
| 85 | + msg := sprintf("Container <%v> in your <%v> <%v> has no <%v>", [container.name, review.kind.kind, review.object.metadata.name, probe]) |
| 86 | + } |
| 87 | +YAML |
| 88 | + } |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +<!-- schema generated by tfplugindocs --> |
| 93 | +## Schema |
| 94 | + |
| 95 | +### Required |
| 96 | + |
| 97 | +- `name` (String) The name of the custom policy template |
| 98 | +- `spec` (Block List, Min: 1, Max: 1) Spec block of the custom policy template (see [below for nested schema](#nestedblock--spec)) |
| 99 | + |
| 100 | +### Optional |
| 101 | + |
| 102 | +- `meta` (Block List, Max: 1) Metadata for the resource (see [below for nested schema](#nestedblock--meta)) |
| 103 | + |
| 104 | +### Read-Only |
| 105 | + |
| 106 | +- `id` (String) The ID of this resource. |
| 107 | + |
| 108 | +<a id="nestedblock--spec"></a> |
| 109 | +### Nested Schema for `spec` |
| 110 | + |
| 111 | +Required: |
| 112 | + |
| 113 | +- `template_manifest` (String) YAML formatted Kubernetes resource. |
| 114 | +The Kubernetes object has to be of the type defined in ObjectType ('ConstraintTemplate'). |
| 115 | +The object name must match the name of the wrapping policy template. |
| 116 | +This will be applied on the cluster after a policy is created using this version of the template. |
| 117 | +This contains the latest version of the object. For previous versions, check Versions API. |
| 118 | + |
| 119 | +Optional: |
| 120 | + |
| 121 | +- `data_inventory` (Block List) List of Kubernetes api-resource kinds that need to be synced/replicated in Gatekeeper in order to enforce policy rules on those resources. |
| 122 | +Note: This is used for OPAGatekeeper based templates, and should be used if the policy enforcement logic in Rego code uses cached data using "data.inventory" fields. (see [below for nested schema](#nestedblock--spec--data_inventory)) |
| 123 | +- `is_deprecated` (Boolean) Flag representing whether the custom policy template is deprecated. |
| 124 | +- `object_type` (String) The type of Kubernetes resource encoded in Object. |
| 125 | +Currently, we only support OPAGatekeeper based 'ConstraintTemplate' object. |
| 126 | +- `template_type` (String) The type of policy template. |
| 127 | +Currently, we only support 'OPAGatekeeper' based policy templates. |
| 128 | + |
| 129 | +<a id="nestedblock--spec--data_inventory"></a> |
| 130 | +### Nested Schema for `spec.data_inventory` |
| 131 | + |
| 132 | +Required: |
| 133 | + |
| 134 | +- `group` (String) API resource group |
| 135 | +- `kind` (String) API resource kind |
| 136 | +- `version` (String) API resource version |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +<a id="nestedblock--meta"></a> |
| 141 | +### Nested Schema for `meta` |
| 142 | + |
| 143 | +Optional: |
| 144 | + |
| 145 | +- `annotations` (Map of String) Annotations for the resource |
| 146 | +- `description` (String) Description of the resource |
| 147 | +- `labels` (Map of String) Labels for the resource |
| 148 | + |
| 149 | +Read-Only: |
| 150 | + |
| 151 | +- `resource_version` (String) Resource version of the resource |
| 152 | +- `uid` (String) UID of the resource |
0 commit comments