diff --git a/cluster-autoscaler/cloudprovider/azure/azure_template.go b/cluster-autoscaler/cloudprovider/azure/azure_template.go index fdee130eabac..39afb09af222 100644 --- a/cluster-autoscaler/cloudprovider/azure/azure_template.go +++ b/cluster-autoscaler/cloudprovider/azure/azure_template.go @@ -30,7 +30,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" ) @@ -152,9 +151,14 @@ func buildGenericLabels(template compute.VirtualMachineScaleSet, nodeName string 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 9ca887fc6cc7..d8eaeb1d8fe0 100644 --- a/cluster-autoscaler/cloudprovider/azure/azure_template_test.go +++ b/cluster-autoscaler/cloudprovider/azure/azure_template_test.go @@ -20,8 +20,11 @@ import ( "fmt" "testing" + "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2022-08-01/compute" + "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" ) @@ -127,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) +}