diff --git a/pkg/acsengine/transform/transform.go b/pkg/acsengine/transform/transform.go index 6563813e3c..9217baef98 100644 --- a/pkg/acsengine/transform/transform.go +++ b/pkg/acsengine/transform/transform.go @@ -36,10 +36,12 @@ const ( vmssResourceType = "Microsoft.Compute/virtualMachineScaleSets" vmExtensionType = "Microsoft.Compute/virtualMachines/extensions" nicResourceType = "Microsoft.Network/networkInterfaces" + vnetResourceType = "Microsoft.Network/virtualNetworks" // resource ids - nsgID = "nsgID" - rtID = "routeTableID" + nsgID = "nsgID" + rtID = "routeTableID" + vnetID = "vnetID" ) // Translator defines all required interfaces for i18n.Translator. @@ -104,6 +106,7 @@ func (t *Transformer) NormalizeForK8sVMASScalingUp(logger *logrus.Entry, templat } rtIndex := -1 nsgIndex := -1 + vnetIndex := -1 resources := templateMap[resourcesFieldName].([]interface{}) for index, resource := range resources { resourceMap, ok := resource.(map[string]interface{}) @@ -129,6 +132,14 @@ func (t *Transformer) NormalizeForK8sVMASScalingUp(logger *logrus.Entry, templat } rtIndex = index } + if ok && resourceType == vnetResourceType { + if vnetIndex != -1 { + err := t.Translator.Errorf("Found 2 resources with type %s in the template. There should only be 1", vnetResourceType) + logger.Warnf(err.Error()) + return err + } + vnetIndex = index + } dependencies, ok := resourceMap[dependsOnFieldName].([]interface{}) if !ok { @@ -138,7 +149,8 @@ func (t *Transformer) NormalizeForK8sVMASScalingUp(logger *logrus.Entry, templat for dIndex := len(dependencies) - 1; dIndex >= 0; dIndex-- { dependency := dependencies[dIndex].(string) if strings.Contains(dependency, nsgResourceType) || strings.Contains(dependency, nsgID) || - strings.Contains(dependency, rtResourceType) || strings.Contains(dependency, rtID) { + strings.Contains(dependency, rtResourceType) || strings.Contains(dependency, rtID) || + strings.Contains(dependency, vnetResourceType) || strings.Contains(dependency, vnetID) { dependencies = append(dependencies[:dIndex], dependencies[dIndex+1:]...) } } @@ -161,6 +173,11 @@ func (t *Transformer) NormalizeForK8sVMASScalingUp(logger *logrus.Entry, templat } else { indexesToRemove = append(indexesToRemove, rtIndex) } + if vnetIndex == -1 { + logger.Infof("Found no resources with type %s in the template.", vnetResourceType) + } else { + indexesToRemove = append(indexesToRemove, vnetIndex) + } indexesToRemove = append(indexesToRemove, nsgIndex) templateMap[resourcesFieldName] = removeIndexesFromArray(resources, indexesToRemove) @@ -283,7 +300,25 @@ func (t *Transformer) NormalizeResourcesForK8sMasterUpgrade(logger *logrus.Entry continue } - if !(resourceType == vmResourceType || resourceType == vmExtensionType || resourceType == nicResourceType) { + dependencies, ok := resourceMap[dependsOnFieldName].([]interface{}) + if !ok { + continue + } + + for dIndex := len(dependencies) - 1; dIndex >= 0; dIndex-- { + dependency := dependencies[dIndex].(string) + if strings.Contains(dependency, vnetResourceType) || strings.Contains(dependency, vnetID) { + dependencies = append(dependencies[:dIndex], dependencies[dIndex+1:]...) + } + } + + if len(dependencies) > 0 { + resourceMap[dependsOnFieldName] = dependencies + } else { + delete(resourceMap, dependsOnFieldName) + } + + if !(resourceType == vmResourceType || resourceType == vmExtensionType || resourceType == nicResourceType || resourceType == vnetResourceType) { continue } @@ -293,6 +328,11 @@ func (t *Transformer) NormalizeResourcesForK8sMasterUpgrade(logger *logrus.Entry continue } + if resourceType == vnetResourceType { + filteredResources = filteredResources[:len(filteredResources)-1] + continue + } + if resourceType == nicResourceType { if strings.Contains(resourceName, "variables('masterVMNamePrefix')") { continue diff --git a/pkg/acsengine/transform/transformtestfiles/k8s_agent_upgrade_template.json b/pkg/acsengine/transform/transformtestfiles/k8s_agent_upgrade_template.json index 6cdd0ffb67..39c09e0c24 100644 --- a/pkg/acsengine/transform/transformtestfiles/k8s_agent_upgrade_template.json +++ b/pkg/acsengine/transform/transformtestfiles/k8s_agent_upgrade_template.json @@ -1659,9 +1659,6 @@ "count": "[sub(variables('agentppol1Count'), variables('agentppol1Offset'))]", "name": "loop" }, - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[concat(variables('agentppol1VMNamePrefix'), 'nic-', copyIndex(variables('agentppol1Offset')))]", "properties": { @@ -1710,9 +1707,6 @@ "count": "[sub(variables('agentpool2Count'), variables('agentpool2Offset'))]", "name": "loop" }, - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[concat(variables('agentpool2VMNamePrefix'), 'nic-', copyIndex(variables('agentpool2Offset')))]", "properties": { @@ -1766,33 +1760,6 @@ }, "type": "Microsoft.Compute/availabilitySets" }, - { - "apiVersion": "[variables('apiVersionDefault')]", - "location": "[variables('location')]", - "name": "[variables('virtualNetworkName')]", - "properties": { - "addressSpace": { - "addressPrefixes": [ - "[variables('vnetCidr')]" - ] - }, - "subnets": [ - { - "name": "[variables('subnetName')]", - "properties": { - "addressPrefix": "[variables('subnet')]", - "networkSecurityGroup": { - "id": "[variables('nsgID')]" - }, - "routeTable": { - "id": "[variables('routeTableID')]" - } - } - } - ] - }, - "type": "Microsoft.Network/virtualNetworks" - }, { "apiVersion": "[variables('apiVersionDefault')]", "dependsOn": [ @@ -1854,9 +1821,6 @@ }, { "apiVersion": "[variables('apiVersionDefault')]", - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[variables('masterInternalLbName')]", "properties": { @@ -1950,7 +1914,6 @@ "name": "nicLoopNode" }, "dependsOn": [ - "[variables('vnetID')]", "[concat(variables('masterLbID'),'/inboundNatRules/SSH-',variables('masterVMNamePrefix'),copyIndex(variables('masterOffset')))]", "[variables('masterInternalLbName')]" ], diff --git a/pkg/acsengine/transform/transformtestfiles/k8s_master_upgrade_template.json b/pkg/acsengine/transform/transformtestfiles/k8s_master_upgrade_template.json index 4ade5fb5a7..9b7580877a 100644 --- a/pkg/acsengine/transform/transformtestfiles/k8s_master_upgrade_template.json +++ b/pkg/acsengine/transform/transformtestfiles/k8s_master_upgrade_template.json @@ -1659,9 +1659,6 @@ "count": "[sub(variables('agentppol1Count'), variables('agentppol1Offset'))]", "name": "loop" }, - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[concat(variables('agentppol1VMNamePrefix'), 'nic-', copyIndex(variables('agentppol1Offset')))]", "properties": { @@ -1710,9 +1707,6 @@ "count": "[sub(variables('agentpool2Count'), variables('agentpool2Offset'))]", "name": "loop" }, - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[concat(variables('agentpool2VMNamePrefix'), 'nic-', copyIndex(variables('agentpool2Offset')))]", "properties": { @@ -1857,37 +1851,6 @@ }, "type": "Microsoft.Compute/availabilitySets" }, - { - "apiVersion": "[variables('apiVersionDefault')]", - "dependsOn": [ - "[concat('Microsoft.Network/networkSecurityGroups/', variables('nsgName'))]", - "[concat('Microsoft.Network/routeTables/', variables('routeTableName'))]" - ], - "location": "[variables('location')]", - "name": "[variables('virtualNetworkName')]", - "properties": { - "addressSpace": { - "addressPrefixes": [ - "[variables('vnetCidr')]" - ] - }, - "subnets": [ - { - "name": "[variables('subnetName')]", - "properties": { - "addressPrefix": "[variables('subnet')]", - "networkSecurityGroup": { - "id": "[variables('nsgID')]" - }, - "routeTable": { - "id": "[variables('routeTableID')]" - } - } - } - ] - }, - "type": "Microsoft.Network/virtualNetworks" - }, { "apiVersion": "[variables('apiVersionDefault')]", "location": "[variables('location')]", @@ -1993,9 +1956,6 @@ }, { "apiVersion": "[variables('apiVersionDefault')]", - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[variables('masterInternalLbName')]", "properties": { @@ -2089,7 +2049,6 @@ "name": "nicLoopNode" }, "dependsOn": [ - "[variables('vnetID')]", "[concat(variables('masterLbID'),'/inboundNatRules/SSH-',variables('masterVMNamePrefix'),copyIndex(variables('masterOffset')))]", "[variables('masterInternalLbName')]" ], diff --git a/pkg/acsengine/transform/transformtestfiles/k8s_scale_template.json b/pkg/acsengine/transform/transformtestfiles/k8s_scale_template.json index 64ba134f89..d0e5a446b9 100644 --- a/pkg/acsengine/transform/transformtestfiles/k8s_scale_template.json +++ b/pkg/acsengine/transform/transformtestfiles/k8s_scale_template.json @@ -1659,9 +1659,6 @@ "count": "[sub(variables('agentppol1Count'), variables('agentppol1Offset'))]", "name": "loop" }, - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[concat(variables('agentppol1VMNamePrefix'), 'nic-', copyIndex(variables('agentppol1Offset')))]", "properties": { @@ -1801,9 +1798,6 @@ "count": "[sub(variables('agentpool2Count'), variables('agentpool2Offset'))]", "name": "loop" }, - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[concat(variables('agentpool2VMNamePrefix'), 'nic-', copyIndex(variables('agentpool2Offset')))]", "properties": { @@ -1948,33 +1942,6 @@ }, "type": "Microsoft.Compute/availabilitySets" }, - { - "apiVersion": "[variables('apiVersionDefault')]", - "location": "[variables('location')]", - "name": "[variables('virtualNetworkName')]", - "properties": { - "addressSpace": { - "addressPrefixes": [ - "[variables('vnetCidr')]" - ] - }, - "subnets": [ - { - "name": "[variables('subnetName')]", - "properties": { - "addressPrefix": "[variables('subnet')]", - "networkSecurityGroup": { - "id": "[variables('nsgID')]" - }, - "routeTable": { - "id": "[variables('routeTableID')]" - } - } - } - ] - }, - "type": "Microsoft.Network/virtualNetworks" - }, { "apiVersion": "[variables('apiVersionDefault')]", "dependsOn": [ @@ -2036,9 +2003,6 @@ }, { "apiVersion": "[variables('apiVersionDefault')]", - "dependsOn": [ - "[variables('vnetID')]" - ], "location": "[variables('location')]", "name": "[variables('masterInternalLbName')]", "properties": { @@ -2132,7 +2096,6 @@ "name": "nicLoopNode" }, "dependsOn": [ - "[variables('vnetID')]", "[concat(variables('masterLbID'),'/inboundNatRules/SSH-',variables('masterVMNamePrefix'),copyIndex(variables('masterOffset')))]", "[variables('masterInternalLbName')]" ],