Bylaw

Gallery › Kubernetes › Azure built-in policies

Configure Azure Arc enabled Kubernetes clusters to install the Azure Policy extension

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.1.0

Deploy Azure Policy's extension for Azure Arc to provide at-scale enforcements and safeguard your Arc enabled Kubernetes clusters in a centralized, consistent manner. Learn more at https://aka.ms/akspolicydoc.

ConvertTo-Bylaw converted this policy, and proved that the file compiles back to the same definition.

Azure Policy JSONArcPolicyExtension_DINE.json · 117 lines
{
  "properties": {
    "displayName": "Configure Azure Arc enabled Kubernetes clusters to install the Azure Policy extension",
    "policyType": "BuiltIn",
    "mode": "Indexed",
    "description": "Deploy Azure Policy's extension for Azure Arc to provide at-scale enforcements and safeguard your Arc enabled Kubernetes clusters in a centralized, consistent manner. Learn more at https://aka.ms/akspolicydoc.",
    "metadata": {
      "version": "1.1.0",
      "category": "Kubernetes"
    },
    "version": "1.1.0",
    "parameters": {
      "effect": {
        "type": "String",
        "defaultValue": "DeployIfNotExists",
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy"
        }
      }
    },
    "policyRule": {
      "if": {
        "allOf": [
          {
            "field": "type",
            "equals": "Microsoft.Kubernetes/connectedClusters"
          },
          {
            "field": "Microsoft.Kubernetes/connectedClusters/distribution",
            "notEquals": "aks"
          }
        ]
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.KubernetesConfiguration/extensions",
          "evaluationDelay": "AfterProvisioning",
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.KubernetesConfiguration/extensions/extensionType",
                "equals": "microsoft.policyinsights"
              },
              {
                "field": "Microsoft.KubernetesConfiguration/extensions/provisioningState",
                "equals": "Succeeded"
              }
            ]
          },
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/85cb6faf-e071-4c9b-8136-154b5a04f717"
          ],
          "deployment": {
            "properties": {
              "mode": "incremental",
              "parameters": {
                "clusterRegion": {
                  "value": "[field('location')]"
                },
                "clusterResourceId": {
                  "value": "[field('id')]"
                }
              },
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2018-05-01/subscriptionDeploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "clusterRegion": {
                    "type": "string"
                  },
                  "clusterResourceId": {
                    "type": "string"
                  }
                },
                "resources": [
                  {
                    "type": "Microsoft.KubernetesConfiguration/extensions",
                    "apiVersion": "2021-09-01",
                    "name": "azurepolicy",
                    "location": "[parameters('clusterRegion')]",
                    "identity": {
                      "type": "SystemAssigned"
                    },
                    "properties": {
                      "extensionType": "microsoft.policyinsights",
                      "configurationSettings": {},
                      "configurationProtectedSettings": {},
                      "autoUpgradeMinorVersion": true,
                      "releaseTrain": "Stable",
                      "scope": {
                        "Cluster": {
                          "releaseNamespace": "kube-system"
                        }
                      }
                    },
                    "scope": "[concat('Microsoft.Kubernetes/connectedClusters/', split(parameters('clusterResourceId'),'/')[8])]"
                  }
                ]
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.1.0"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/0adc5395-9169-4b9b-8687-af838d69410a",
  "name": "0adc5395-9169-4b9b-8687-af838d69410a"
}
Bylaw0adc5395-9169-4b9b-8687-af838d69410a.bylaw.ps1 · 37 lines
# Converted from ArcPolicyExtension_DINE.json by ConvertTo-Bylaw.
Policy '0adc5395-9169-4b9b-8687-af838d69410a' {

    Properties @{
        DisplayName   = 'Configure Azure Arc enabled Kubernetes clusters to install the Azure Policy extension'
        Description   = 'Deploy Azure Policy''s extension for Azure Arc to provide at-scale enforcements and safeguard your Arc enabled Kubernetes clusters in a centralized, consistent manner. Learn more at https://aka.ms/akspolicydoc.'
        Version       = '1.1.0'
        Category      = 'Kubernetes'
        PolicyVersion = '1.1.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
    }

    Resource 'Microsoft.Kubernetes/connectedClusters' {

        Require 'distribution is aks' {
            Field distribution | Must -Be 'aks'
        }
    }

    DeployIfNotExists 'Microsoft.KubernetesConfiguration/extensions' -Role 'Kubernetes Extension Contributor' -EvaluationDelay AfterProvisioning -Effect (Param Effect) {
        Require 'extensionType is microsoft.policyinsights and provisioningState is Succeeded' {
            Field extensionType | Must -Be 'microsoft.policyinsights'
            Field provisioningState | Must -Be 'Succeeded'
        }
        Deployment -TemplateFile './templates/0adc5395-9169-4b9b-8687-af838d69410a.json' -Parameters ([ordered]@{
                clusterRegion     = (Arm '[field(''location'')]')
                clusterResourceId = (Arm '[field(''id'')]')
            })
    }
}

Its deployment templates are written beside it:

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