Bylaw

Gallery › Kubernetes › Azure built-in policies

Configure Node OS Auto upgrade on Azure Kubernetes Cluster

Converted Policy DeployIfNotExists Disabled Mode Indexed Version 1.2.0

Use Node OS auto-upgrade to control node-level OS security updates of Azure Kubernetes Service (AKS) clusters. For more info, visit https://learn.microsoft.com/en-us/azure/aks/auto-upgrade-node-image.

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

Azure Policy JSONAKS_Autoupgrade_NodeOS_DINE.json · 208 lines
{
  "properties": {
    "policyType": "BuiltIn",
    "displayName": "Configure Node OS Auto upgrade on Azure Kubernetes Cluster",
    "description": "Use Node OS auto-upgrade to control node-level OS security updates of Azure Kubernetes Service (AKS) clusters. For more info, visit https://learn.microsoft.com/en-us/azure/aks/auto-upgrade-node-image.",
    "mode": "Indexed",
    "metadata": {
      "version": "1.2.0",
      "category": "Kubernetes"
    },
    "version": "1.2.0",
    "parameters": {
      "effect": {
        "type": "String",
        "metadata": {
          "displayName": "Effect",
          "description": "Enable or disable the execution of the policy",
          "portalReview": true
        },
        "allowedValues": [
          "DeployIfNotExists",
          "Disabled"
        ],
        "defaultValue": "DeployIfNotExists"
      },
      "nodeOSUpgradeChannel": {
        "type": "String",
        "metadata": {
          "displayName": "Node OS upgrade Channel to set",
          "description": "Manner in which the OS on your nodes is updated. Existed channel will not change unless it is None",
          "portalReview": true
        },
        "allowedValues": [
          "NodeImage",
          "Unmanaged"
        ],
        "defaultValue": "NodeImage"
      }
    },
    "policyRule": {
      "if": {
        "field": "type",
        "equals": "Microsoft.ContainerService/managedClusters"
      },
      "then": {
        "effect": "[parameters('effect')]",
        "details": {
          "type": "Microsoft.ContainerService/managedClusters",
          "name": "[field('name')]",
          "roleDefinitionIds": [
            "/providers/Microsoft.Authorization/roleDefinitions/ed7f3fbd-7b88-4dd4-9017-9adb7ce333f8",
            "/providers/Microsoft.Authorization/roleDefinitions/18ed5180-3e48-46fd-8541-4ea054d57064"
          ],
          "existenceCondition": {
            "allOf": [
              {
                "field": "Microsoft.ContainerService/managedClusters/autoUpgradeProfile.nodeOSUpgradeChannel",
                "exists": true
              },
              {
                "field": "Microsoft.ContainerService/managedClusters/autoUpgradeProfile.nodeOSUpgradeChannel",
                "notequals": "None"
              }
            ]
          },
          "deployment": {
            "properties": {
              "mode": "incremental",
              "template": {
                "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                "contentVersion": "1.0.0.0",
                "parameters": {
                  "clusterName": {
                    "type": "string"
                  },
                  "clusterResourceGroupName": {
                    "type": "string"
                  },
                  "nodeOSUpgradeChannel": {
                    "type": "string"
                  }
                },
                "variables": {
                  "clusterGetDeploymentName": "[take(concat('Policy-Get-', parameters('clusterName')), 64)]",
                  "clusterUpdateDeploymentName": "[take(concat('Policy-Update-', parameters('clusterName')), 64)]"
                },
                "resources": [
                  {
                    "apiVersion": "2020-06-01",
                    "type": "Microsoft.Resources/deployments",
                    "name": "[variables('clusterGetDeploymentName')]",
                    "properties": {
                      "mode": "Incremental",
                      "template": {
                        "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "resources": [],
                        "outputs": {
                          "aksCluster": {
                            "type": "object",
                            "value": "[reference(resourceId(parameters('clusterResourceGroupName'), 'Microsoft.ContainerService/managedClusters', parameters('clusterName')), '2023-06-01', 'Full')]"
                          }
                        }
                      }
                    }
                  },
                  {
                    "apiVersion": "2020-06-01",
                    "type": "Microsoft.Resources/deployments",
                    "name": "[variables('clusterUpdateDeploymentName')]",
                    "properties": {
                      "mode": "Incremental",
                      "expressionEvaluationOptions": {
                        "scope": "inner"
                      },
                      "template": {
                        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
                        "contentVersion": "1.0.0.0",
                        "parameters": {
                          "aksClusterName": {
                            "type": "string"
                          },
                          "aksClusterContent": {
                            "type": "object"
                          },
                          "nodeOSUpgradeChannel": {
                            "type": "string"
                          }
                        },
                        "resources": [
                          {
                            "apiVersion": "2023-06-01",
                            "type": "Microsoft.ContainerService/managedClusters",
                            "name": "[parameters('aksClusterName')]",
                            "location": "[parameters('aksClusterContent').location]",
                            "sku": "[parameters('aksClusterContent').sku]",
                            "tags": "[if(contains(parameters('aksClusterContent'), 'tags'), parameters('aksClusterContent').tags, json('null'))]",
                            "properties": {
                              "kubernetesVersion": "[parameters('aksClusterContent').properties.kubernetesVersion]",
                              "agentPoolProfiles": "[if(contains(parameters('aksClusterContent').properties, 'agentPoolProfiles'), parameters('aksClusterContent').properties.agentPoolProfiles, json('null'))]",
                              "linuxProfile": "[if(contains(parameters('aksClusterContent').properties, 'linuxProfile'), parameters('aksClusterContent').properties.linuxProfile, json('null'))]",
                              "windowsProfile": "[if(contains(parameters('aksClusterContent').properties, 'windowsProfile'), parameters('aksClusterContent').properties.windowsProfile, json('null'))]",
                              "servicePrincipalProfile": "[if(contains(parameters('aksClusterContent').properties, 'servicePrincipalProfile'), parameters('aksClusterContent').properties.servicePrincipalProfile, json('null'))]",
                              "nodeResourceGroup": "[parameters('aksClusterContent').properties.nodeResourceGroup]",
                              "enableRBAC": "[if(contains(parameters('aksClusterContent').properties, 'enableRBAC'), parameters('aksClusterContent').properties.enableRBAC, json('null'))]",
                              "enablePodSecurityPolicy": "[if(contains(parameters('aksClusterContent').properties, 'enablePodSecurityPolicy'), parameters('aksClusterContent').properties.enablePodSecurityPolicy, json('null'))]",
                              "networkProfile": "[if(contains(parameters('aksClusterContent').properties, 'networkProfile'), parameters('aksClusterContent').properties.networkProfile, json('null'))]",
                              "addonProfiles": "[if(contains(parameters('aksClusterContent').properties, 'addonProfiles'), parameters('aksClusterContent').properties.addonProfiles, json('null'))]",
                              "oidcIssuerProfile": "[if(contains(parameters('aksClusterContent').properties, 'oidcIssuerProfile'), parameters('aksClusterContent').properties.oidcIssuerProfile, json('null'))]",
                              "aadProfile": "[if(contains(parameters('aksClusterContent').properties, 'aadProfile'), parameters('aksClusterContent').properties.aadProfile, json('null'))]",
                              "autoScalerProfile": "[if(contains(parameters('aksClusterContent').properties, 'autoScalerProfile'), parameters('aksClusterContent').properties.autoScalerProfile, json('null'))]",
                              "autoUpgradeProfile": {
                                "nodeOSUpgradeChannel": "[parameters('nodeOSUpgradeChannel')]"
                              },
                              "apiServerAccessProfile": "[if(contains(parameters('aksClusterContent').properties, 'apiServerAccessProfile'), parameters('aksClusterContent').properties.apiServerAccessProfile, json('null'))]",
                              "diskEncryptionSetID": "[if(contains(parameters('aksClusterContent').properties, 'diskEncryptionSetID'), parameters('aksClusterContent').properties.diskEncryptionSetID, json('null'))]",
                              "disableLocalAccounts": "[if(contains(parameters('aksClusterContent').properties, 'disableLocalAccounts'), parameters('aksClusterContent').properties.disableLocalAccounts, json('null'))]",
                              "fqdnSubdomain": "[if(contains(parameters('aksClusterContent').properties, 'fqdnSubdomain'), parameters('aksClusterContent').properties.fqdnSubdomain, json('null'))]",
                              "httpProxyConfig": "[if(contains(parameters('aksClusterContent').properties, 'httpProxyConfig'), parameters('aksClusterContent').properties.httpProxyConfig, json('null'))]",
                              "podIdentityProfile": "[if(contains(parameters('aksClusterContent').properties, 'podIdentityProfile'), parameters('aksClusterContent').properties.podIdentityProfile, json('null'))]",
                              "privateLinkResources": "[if(contains(parameters('aksClusterContent').properties, 'privateLinkResources'), parameters('aksClusterContent').properties.privateLinkResources, json('null'))]",
                              "securityProfile": "[if(contains(parameters('aksClusterContent').properties, 'securityProfile'), parameters('aksClusterContent').properties.securityProfile, json('null'))]",
                              "identityProfile": "[if(contains(parameters('aksClusterContent').properties, 'identityProfile'), parameters('aksClusterContent').properties.identityProfile, json('null'))]"
                            }
                          }
                        ],
                        "outputs": {}
                      },
                      "parameters": {
                        "aksClusterName": {
                          "value": "[parameters('clusterName')]"
                        },
                        "aksClusterContent": {
                          "value": "[reference(variables('clusterGetDeploymentName')).outputs.aksCluster.value]"
                        },
                        "nodeOSUpgradeChannel": {
                          "value": "[parameters('nodeOSUpgradeChannel')]"
                        }
                      }
                    }
                  }
                ]
              },
              "parameters": {
                "clusterName": {
                  "value": "[field('name')]"
                },
                "clusterResourceGroupName": {
                  "value": "[resourceGroup().name]"
                },
                "nodeOSUpgradeChannel": {
                  "value": "[parameters('nodeOSUpgradeChannel')]"
                }
              }
            }
          }
        }
      }
    },
    "versions": [
      "1.2.0",
      "1.1.0",
      "1.0.1"
    ]
  },
  "id": "/providers/Microsoft.Authorization/policyDefinitions/40f1aee2-4db4-4b74-acb1-c6972e24cca8",
  "name": "40f1aee2-4db4-4b74-acb1-c6972e24cca8"
}
Bylaw40f1aee2-4db4-4b74-acb1-c6972e24cca8.bylaw.ps1 · 40 lines
# Converted from AKS_Autoupgrade_NodeOS_DINE.json by ConvertTo-Bylaw.
Policy '40f1aee2-4db4-4b74-acb1-c6972e24cca8' {

    Properties @{
        DisplayName   = 'Configure Node OS Auto upgrade on Azure Kubernetes Cluster'
        Description   = 'Use Node OS auto-upgrade to control node-level OS security updates of Azure Kubernetes Service (AKS) clusters. For more info, visit https://learn.microsoft.com/en-us/azure/aks/auto-upgrade-node-image.'
        Version       = '1.2.0'
        Category      = 'Kubernetes'
        PolicyVersion = '1.2.0'
        Mode          = 'Indexed'
    }

    Parameter Effect @{
        AllowedValues = @('DeployIfNotExists', 'Disabled')
        Default       = 'DeployIfNotExists'
        DisplayName   = 'Effect'
        Description   = 'Enable or disable the execution of the policy'
        Metadata      = [ordered]@{ portalReview = $true }
    }
    Parameter NodeOSUpgradeChannel @{
        AllowedValues = @('NodeImage', 'Unmanaged')
        Default       = 'NodeImage'
        DisplayName   = 'Node OS upgrade Channel to set'
        Description   = 'Manner in which the OS on your nodes is updated. Existed channel will not change unless it is None'
        Metadata      = [ordered]@{ portalReview = $true }
    }

    Resource 'Microsoft.ContainerService/managedClusters'

    DeployIfNotExists 'Microsoft.ContainerService/managedClusters' -Role 'Azure Kubernetes Service Contributor Role', 'Azure Kubernetes Service Policy Add-on Deployment' -Name (Arm '[field(''name'')]') -Effect (Param Effect) {
        Require 'autoUpgradeProfile.nodeOSUpgradeChannel is not None' {
            Field autoUpgradeProfile.nodeOSUpgradeChannel | Must -Not -Be 'None'
        }
        Deployment -TemplateFile './templates/40f1aee2-4db4-4b74-acb1-c6972e24cca8.json' -Parameters ([ordered]@{
                clusterName              = (Arm '[field(''name'')]')
                clusterResourceGroupName = (Arm '[resourceGroup().name]')
                nodeOSUpgradeChannel     = (Param NodeOSUpgradeChannel)
            })
    }
}

Its deployment templates are written beside it:

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