diff --git a/cluster-autoscaler/cloudprovider/azure/azure_template.go b/cluster-autoscaler/cloudprovider/azure/azure_template.go index a80f304626e7..2977441e368e 100644 --- a/cluster-autoscaler/cloudprovider/azure/azure_template.go +++ b/cluster-autoscaler/cloudprovider/azure/azure_template.go @@ -31,7 +31,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/autoscaler/cluster-autoscaler/cloudprovider" "k8s.io/autoscaler/cluster-autoscaler/utils/gpu" - cloudvolume "k8s.io/cloud-provider/volume" "k8s.io/klog/v2" ) @@ -168,8 +167,14 @@ func buildGenericLabels(template *compute.VirtualMachineScaleSet, nodeName strin for k, v := range *template.Zones { failureDomains[k] = strings.ToLower(*template.Location) + "-" + v } - result[apiv1.LabelTopologyZone] = strings.Join(failureDomains[:], cloudvolume.LabelMultiZoneDelimiter) - result[azureDiskTopologyKey] = strings.Join(failureDomains[:], cloudvolume.LabelMultiZoneDelimiter) + //Picks random zones for Multi-zone nodepool when scaling from zero. + //This random zone will not be the same as the zone of the VMSS that is being created, the purpose of creating + //the node template with random zone is to initiate scaling from zero on the multi-zone nodepool. + //Note that the if the customer is to have some pod affinity picking exact zone, this logic won't work. + //For now, discourage the customers from using podAffinity to pick the availability zones. + randomZone := failureDomains[rand.Intn(len(failureDomains))] + result[apiv1.LabelTopologyZone] = randomZone + result[azureDiskTopologyKey] = randomZone } else { result[apiv1.LabelTopologyZone] = "0" result[azureDiskTopologyKey] = "" diff --git a/cluster-autoscaler/cloudprovider/azure/azure_template_test.go b/cluster-autoscaler/cloudprovider/azure/azure_template_test.go index 100ac7366730..d6d9b59d574f 100644 --- a/cluster-autoscaler/cloudprovider/azure/azure_template_test.go +++ b/cluster-autoscaler/cloudprovider/azure/azure_template_test.go @@ -20,10 +20,11 @@ import ( "fmt" "testing" - //nolint SA1019 - deprecated package - + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2022-08-01/compute" //nolint SA1019 - deprecated package + "github.com/Azure/go-autorest/autorest" "github.com/Azure/go-autorest/autorest/to" "github.com/stretchr/testify/assert" + apiv1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" ) @@ -129,3 +130,50 @@ func makeTaintSet(taints []apiv1.Taint) map[apiv1.Taint]bool { } return set } + +func TestTopologyFromScaleSet(t *testing.T) { + testNodeName := "test-node" + testSkuName := "test-sku" + testVmss := compute.VirtualMachineScaleSet{ + Response: autorest.Response{}, + Sku: &compute.Sku{Name: &testSkuName}, + Plan: nil, + VirtualMachineScaleSetProperties: &compute.VirtualMachineScaleSetProperties{ + VirtualMachineProfile: &compute.VirtualMachineScaleSetVMProfile{OsProfile: nil}}, + Zones: &[]string{"1", "2", "3"}, + Location: to.StringPtr("westus"), + } + expectedZoneValues := []string{"westus-1", "westus-2", "westus-3"} + labels := buildGenericLabels(&testVmss, testNodeName) + topologyZone, ok := labels[apiv1.LabelTopologyZone] + assert.True(t, ok) + azureDiskTopology, ok := labels[azureDiskTopologyKey] + assert.True(t, ok) + + assert.Contains(t, expectedZoneValues, topologyZone) + assert.Contains(t, expectedZoneValues, azureDiskTopology) +} + +func TestEmptyTopologyFromScaleSet(t *testing.T) { + testNodeName := "test-node" + testSkuName := "test-sku" + testVmss := compute.VirtualMachineScaleSet{ + Response: autorest.Response{}, + Sku: &compute.Sku{Name: &testSkuName}, + Plan: nil, + VirtualMachineScaleSetProperties: &compute.VirtualMachineScaleSetProperties{ + VirtualMachineProfile: &compute.VirtualMachineScaleSetVMProfile{OsProfile: nil}}, + Location: to.StringPtr("westus"), + } + expectedTopologyZone := "0" + expectedAzureDiskTopology := "" + labels := buildGenericLabels(&testVmss, testNodeName) + + topologyZone, ok := labels[apiv1.LabelTopologyZone] + assert.True(t, ok) + assert.Equal(t, expectedTopologyZone, topologyZone) + + azureDiskTopology, ok := labels[azureDiskTopologyKey] + assert.True(t, ok) + assert.Equal(t, expectedAzureDiskTopology, azureDiskTopology) +}