Bylaw

Gallery › Kubernetes › Azure built-in policies

[Preview]: Kubernetes resources should have required annotations

Doesn't convert yet Policy Audit Deny Disabled Mode Microsoft.Kubernetes.Data Version 4.0.0-preview Preview

Ensure that required annotations are attached on a given Kubernetes resource kind for improved resource management of your Kubernetes resources. This policy is generally available for Kubernetes Service (AKS), and preview for Azure Arc enabled Kubernetes. For more information, see https://aka.ms/kubepolicydoc.

ConvertTo-Bylaw can't convert this policy yet:

Azure Policy JSONEnforceResourceAnnotation.json · 219 lines
{
  "properties": {
    "displayName": "[Preview]: Kubernetes resources should have required annotations",
    "policyType": "BuiltIn",
    "mode": "Microsoft.Kubernetes.Data",
    "description": "Ensure that required annotations are attached on a given Kubernetes resource kind for improved resource management of your Kubernetes resources. This policy is generally available for Kubernetes Service (AKS), and preview for Azure Arc enabled Kubernetes. For more information, see https://aka.ms/kubepolicydoc.",
    "metadata": {
      "version": "4.0.0-preview",
      "category": "Kubernetes",
      "preview": true
    },
    "version": "4.0.0-preview",
    "parameters": {
      "source": {
        "type": "String",
        "metadata": {
          "displayName": "Source",
          "description": "The source k8s object for constraint evaluation. 'Original' means only evaluate against the specific GroupVersionKind specified in the policy definition. 'Generated' means only evaluate against k8s objects generated by Gatekeeper ExpansionTemplates. 'All' means evaluate against both the original object and any generated ones."
        },
        "defaultValue": "Original",
        "allowedValues": [
          "All",
          "Generated",
          "Original"
        ]
      },
      "warn": {
        "type": "Boolean",
        "metadata": {
          "displayName": "Warn",
          "description": "Whether or not to return warnings back to the user in the kubectl cli"
        },
        "defaultValue": false
      },
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "'Audit' allows a non-compliant resource to be created or updated, but flags it as non-compliant. 'Deny' blocks the non-compliant resource creation or update. 'Disabled' turns off the policy.",
          "portalReview": true
        },
        "allowedValues": [
          "Audit",
          "Deny",
          "Disabled"
        ],
        "defaultValue": "Audit"
      },
      "excludedNamespaces": {
        "type": "Array",
        "metadata": {
          "displayName": "Namespace exclusions",
          "description": "List of Kubernetes namespaces to exclude from policy evaluation. System namespaces \"kube-system\", \"gatekeeper-system\" and \"azure-arc\" are always excluded by design. \"azure-extensions-usage-system\" is optional to remove."
        },
        "defaultValue": [
          "kube-system",
          "gatekeeper-system",
          "azure-arc",
          "azure-extensions-usage-system"
        ]
      },
      "namespaces": {
        "type": "Array",
        "metadata": {
          "displayName": "Namespace inclusions",
          "description": "List of Kubernetes namespaces to only include in policy evaluation. An empty list means the policy is applied to all resources in all namespaces."
        },
        "defaultValue": []
      },
      "labelSelector": {
        "type": "object",
        "metadata": {
          "displayName": "Kubernetes label selector",
          "description": "Label query to select Kubernetes resources for policy evaluation. An empty label selector matches all Kubernetes resources."
        },
        "defaultValue": {},
        "schema": {
          "description": "A label selector is a label query over a set of resources. The result of matchLabels and matchExpressions are ANDed. An empty label selector matches all resources.",
          "type": "object",
          "properties": {
            "matchLabels": {
              "description": "matchLabels is a map of {key,value} pairs.",
              "type": "object",
              "additionalProperties": {
                "type": "string"
              },
              "minProperties": 1
            },
            "matchExpressions": {
              "description": "matchExpressions is a list of values, a key, and an operator.",
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "key": {
                    "description": "key is the label key that the selector applies to.",
                    "type": "string"
                  },
                  "operator": {
                    "description": "operator represents a key's relationship to a set of values.",
                    "type": "string",
                    "enum": [
                      "In",
                      "NotIn",
                      "Exists",
                      "DoesNotExist"
                    ]
                  },
                  "values": {
                    "description": "Values is an array of string values. If the operator is In or NotIn, the values array should not be empty. If the operator is Exists or DoesNotExist, the values array must be empty.",
                    "type": "array",
                    "items": {
                      "type": "string"
                    }
                  }
                },
                "required": [
                  "key",
                  "operator"
                ],
                "additionalProperties": false
              },
              "minItems": 1
            }
          },
          "additionalProperties": false
        }
      },
      "kind": {
        "type": "String",
        "metadata": {
          "displayName": "Resource Kind",
          "description": "Kubernetes resource kind to enforce annotations for. One assignment can only restrict single resource kind. It is case-sensitive, please start with upper case. For example \"Pod\"",
          "portalReview": true
        }
      },
      "annotations": {
        "type": "Object",
        "metadata": {
          "displayName": "Required Annotations",
          "description": "Annotations to be forced on certain Kubernetes resource. The annotations requires key and value allowed regex. Note that annotation values only support string values",
          "portalReview": true
        },
        "schema": {
          "type": "object",
          "properties": {
            "annotations": {
              "type": "array",
              "items": {
                "type": "object",
                "properties": {
                  "key": {
                    "type": "string",
                    "description": "Provide a required annotation key to enforce on a Kubernetes resource kind."
                  },
                  "allowedRegex": {
                    "type": "string",
                    "description": "Provide the allowed annotation value as a regex match."
                  }
                },
                "required": [
                  "key",
                  "allowedRegex"
                ],
                "additionalProperties": false
              }
            }
          },
          "required": [
            "annotations"
          ],
          "additionalProperties": false
        },
        "defaultValue": {
          "annotations": []
        }
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "in": [
          "Microsoft.Kubernetes/connectedClusters",
          "Microsoft.ContainerService/managedClusters"
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "source": "[parameters('source')]",
          "warn": "[parameters('warn')]",
          "templateInfo": {
            "sourceType": "PublicURL",
            "url": "https://store.policy.core.windows.net/kubernetes/enforce-required-annotation/v2/template.yaml"
          },
          "apiGroups": [
            ""
          ],
          "kinds": [
            "[parameters('kind')]"
          ],
          "excludedNamespaces": "[parameters('excludedNamespaces')]",
          "namespaces": "[parameters('namespaces')]",
          "labelSelector": "[parameters('labelSelector')]",
          "values": {
            "annotations": "[parameters('annotations').annotations]"
          }
        }
      }
    },
    "versions": [
      "4.0.0-PREVIEW",
      "3.2.0",
      "3.1.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/9a5f4e39-e427-4d5d-ae73-93db00328bec",
  "name": "9a5f4e39-e427-4d5d-ae73-93db00328bec"
}

Source: built-in-policies/policyDefinitions/Kubernetes/EnforceResourceAnnotation.json in Azure/azure-policy, under the MIT licence.