Azure / arm-template-whatif

A repository to track issues related to what-if noise suppression
MIT License
90 stars 14 forks source link

Existing reference and idempotency #362

Closed TiTi closed 5 months ago

TiTi commented 5 months ago

Bicep version 0.26.54 (5e20b29b58)

Describe the bug When using an existing reference to compute properties for the resources to create in bicep, module is not idempotent. Indeed, successive re-execution of the whatif will show modifications even though it would resolve to the same values. For instance, i see this in the whatif:

~ name:                        "toAzure10.55" => "[format('toAzure{0}', join(take(split(parseCidr(reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', parameters('hubVnetSubscriptionID'), parameters('hubVnetResourceGroupName')), 'Microsoft.Network/virtualNetworks', parameters('hubVnetName')), '2023-04-01').addressSpace.addressPrefixes[0]).network, '.'), 2), '.'))]"

if i apply the deployment, no change appear as the computed name is (as expected) : "toAzure10.55"

To Reproduce

resource vnetHub 'Microsoft.Network/virtualNetworks@2023-04-01' existing = {
  scope: resourceGroup(hubVnetSubscriptionID, hubVnetResourceGroupName)
  name: hubVnetName
}

var networkBase = join(take(split(parseCidr(vnetHub.properties.addressSpace.addressPrefixes[0]).network, '.'), 2), '.') // ex: 10.128
var dnsServers = vnetHub.properties.dhcpOptions.dnsServers

resource routeTable 'Microsoft.Network/routeTables@2023-04-01' = {
  name: routeTableName
  location: location
  tags: tags
  properties: {
    disableBgpRoutePropagation: false
    routes: [
      {
        name: 'toAzure${networkBase}'
        properties: {
          addressPrefix: '${networkBase}.0.0/16'
          hasBgpOverride: false
          nextHopIpAddress: '${networkBase}.0.4'
          nextHopType: 'VirtualAppliance'
        }
      }
      # other routes...
    ]
  }
}

resource vnetSpoke 'Microsoft.Network/virtualNetworks@2023-04-01' = {
  name: vnetSpokeName
  location: location
  tags: tags
  properties: {
    addressSpace: {
      addressPrefixes: [
        addressSpace
      ]
    }
    dhcpOptions: empty(dnsServers) ? null : {
      dnsServers: dnsServers         # value from hub vnet
    }
    subnets: [
      # ....
    ]
  }
  dependsOn: [nsg, routeTable]
}

dnsServers also tried to replace computed value with

=> "[if(empty(reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', parameters('hubVnetSubscriptionID'), parameters('hubVnetResourceGroupName')), 'Microsoft.Network/virtualNetworks', parameters('hubVnetName')), '2023-04-01').dhcpOptions.dnsServers), null(), createObject('dnsServers', reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', parameters('hubVnetSubscriptionID'), parameters('hubVnetResourceGroupName')), 'Microsoft.Network/virtualNetworks', parameters('hubVnetName')), '2023-04-01').dhcpOptions.dnsServers))]"

despite resolving to the same values....

Creation is working fine. My issue is the non-idempotence when re-executing the same bicep a second time with whatif, to check infra is in proper/expected state.

ahelland commented 5 months ago

Yeah, there are known issues (ACKed bugs) with what-if - it's predictions are sort of wonky.

alex-frankel commented 5 months ago

Closing as a dup of #83