From bd9b6cdcfada69109353f16f3292ad2bc68388f7 Mon Sep 17 00:00:00 2001 From: Qi Ni Date: Fri, 12 Mar 2021 15:04:36 +0800 Subject: [PATCH] chore: move consts in azure_vmss.go to consts.go --- pkg/consts/consts.go | 9 ++++++++ .../ipam/cloud_cidr_allocator_test.go | 17 +++++++------- pkg/provider/azure_backoff.go | 2 +- pkg/provider/azure_backoff_test.go | 2 +- pkg/provider/azure_vmss.go | 23 +++++-------------- pkg/provider/azure_vmss_test.go | 16 ++++++------- 6 files changed, 34 insertions(+), 35 deletions(-) diff --git a/pkg/consts/consts.go b/pkg/consts/consts.go index 0160d1d532..d84e966cc9 100644 --- a/pkg/consts/consts.go +++ b/pkg/consts/consts.go @@ -106,6 +106,15 @@ const ( // StrRawVersion is the raw version string StrRawVersion string = "raw" + + // VirtualMachineScaleSetsDeallocating indicates VMSS instances are in Deallocating state. + VirtualMachineScaleSetsDeallocating = "Deallocating" + // VmssMachineIDTemplate is the vmss manchine ID template + VmssMachineIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s/virtualMachines/%s" + // VMSetCIDRIPV4TagKey specifies the node ipv4 CIDR mask of the instances on the VMSS or VMAS + VMSetCIDRIPV4TagKey = "kubernetesNodeCIDRMaskIPV4" + // VMSetCIDRIPV6TagKey specifies the node ipv6 CIDR mask of the instances on the VMSS or VMAS + VMSetCIDRIPV6TagKey = "kubernetesNodeCIDRMaskIPV6" ) // azure cloud config diff --git a/pkg/nodeipam/ipam/cloud_cidr_allocator_test.go b/pkg/nodeipam/ipam/cloud_cidr_allocator_test.go index 4a5b073743..a9c2acf0c8 100644 --- a/pkg/nodeipam/ipam/cloud_cidr_allocator_test.go +++ b/pkg/nodeipam/ipam/cloud_cidr_allocator_test.go @@ -33,6 +33,7 @@ import ( "k8s.io/client-go/kubernetes/fake" "sigs.k8s.io/cloud-provider-azure/pkg/azureclients/vmssclient/mockvmssclient" + "sigs.k8s.io/cloud-provider-azure/pkg/consts" azureprovider "sigs.k8s.io/cloud-provider-azure/pkg/provider" "sigs.k8s.io/cloud-provider-azure/pkg/util/controller/testutil" ) @@ -189,8 +190,8 @@ func TestUpdateNodeSubnetMaskSizes(t *testing.T) { description: "updateNodeSubnetMaskSizes should put the correct mask sizes on the map", providerID: "azure:///subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss/virtualMachines/0", tags: map[string]*string{ - azureprovider.VMSetCIDRIPV4TagKey: to.StringPtr("25"), - azureprovider.VMSetCIDRIPV6TagKey: to.StringPtr("65"), + consts.VMSetCIDRIPV4TagKey: to.StringPtr("25"), + consts.VMSetCIDRIPV6TagKey: to.StringPtr("65"), }, expectedNodeNameSubnetMaskSizesMap: map[string][]int{"vmss-0": {25, 65}}, }, @@ -198,8 +199,8 @@ func TestUpdateNodeSubnetMaskSizes(t *testing.T) { description: "updateNodeSubnetMaskSizes should put the default mask sizes on the map if the providerID is invalid", providerID: "invalid", tags: map[string]*string{ - azureprovider.VMSetCIDRIPV4TagKey: to.StringPtr("24"), - azureprovider.VMSetCIDRIPV6TagKey: to.StringPtr("64"), + consts.VMSetCIDRIPV4TagKey: to.StringPtr("24"), + consts.VMSetCIDRIPV6TagKey: to.StringPtr("64"), }, expectedNodeNameSubnetMaskSizesMap: map[string][]int{"vmss-0": {24, 64}}, }, @@ -207,8 +208,8 @@ func TestUpdateNodeSubnetMaskSizes(t *testing.T) { description: "updateNodeSubnetMaskSizes should report an error if the ipv4 mask is smaller than the cluster mask", providerID: "azure:///subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss/virtualMachines/0", tags: map[string]*string{ - azureprovider.VMSetCIDRIPV4TagKey: to.StringPtr("15"), - azureprovider.VMSetCIDRIPV6TagKey: to.StringPtr("65"), + consts.VMSetCIDRIPV4TagKey: to.StringPtr("15"), + consts.VMSetCIDRIPV6TagKey: to.StringPtr("65"), }, expectedNodeNameSubnetMaskSizesMap: map[string][]int{}, expectedErr: fmt.Errorf("updateNodeSubnetMaskSizes: invalid ipv4 mask size %d of node %s because it is out of the range of the cluster CIDR with the mask size %d", 15, "vmss-0", 16), @@ -217,8 +218,8 @@ func TestUpdateNodeSubnetMaskSizes(t *testing.T) { description: "updateNodeSubnetMaskSizes should report an error if the ipv6 mask is smaller than the cluster mask", providerID: "azure:///subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss/virtualMachines/0", tags: map[string]*string{ - azureprovider.VMSetCIDRIPV4TagKey: to.StringPtr("25"), - azureprovider.VMSetCIDRIPV6TagKey: to.StringPtr("45"), + consts.VMSetCIDRIPV4TagKey: to.StringPtr("25"), + consts.VMSetCIDRIPV6TagKey: to.StringPtr("45"), }, expectedNodeNameSubnetMaskSizesMap: map[string][]int{}, expectedErr: fmt.Errorf("updateNodeSubnetMaskSizes: invalid ipv6 mask size %d of node %s because it is out of the range of the cluster CIDR with the mask size %d", 45, "vmss-0", 48), diff --git a/pkg/provider/azure_backoff.go b/pkg/provider/azure_backoff.go index 9926fcd4ab..8efee9510a 100644 --- a/pkg/provider/azure_backoff.go +++ b/pkg/provider/azure_backoff.go @@ -436,7 +436,7 @@ func (az *Cloud) CreateOrUpdateVMSS(resourceGroupName string, VMScaleSetName str klog.Errorf("CreateOrUpdateVMSS: error getting vmss(%s): %v", VMScaleSetName, rerr) return rerr } - if vmss.ProvisioningState != nil && strings.EqualFold(*vmss.ProvisioningState, virtualMachineScaleSetsDeallocating) { + if vmss.ProvisioningState != nil && strings.EqualFold(*vmss.ProvisioningState, consts.VirtualMachineScaleSetsDeallocating) { klog.V(3).Infof("CreateOrUpdateVMSS: found vmss %s being deleted, skipping", VMScaleSetName) return nil } diff --git a/pkg/provider/azure_backoff_test.go b/pkg/provider/azure_backoff_test.go index 61a68f24d7..00d8b0af3f 100644 --- a/pkg/provider/azure_backoff_test.go +++ b/pkg/provider/azure_backoff_test.go @@ -506,7 +506,7 @@ func TestCreateOrUpdateVMSS(t *testing.T) { { vmss: compute.VirtualMachineScaleSet{ VirtualMachineScaleSetProperties: &compute.VirtualMachineScaleSetProperties{ - ProvisioningState: to.StringPtr(virtualMachineScaleSetsDeallocating), + ProvisioningState: to.StringPtr(consts.VirtualMachineScaleSetsDeallocating), }, }, }, diff --git a/pkg/provider/azure_vmss.go b/pkg/provider/azure_vmss.go index 775177320f..cb2ea0793a 100644 --- a/pkg/provider/azure_vmss.go +++ b/pkg/provider/azure_vmss.go @@ -36,6 +36,7 @@ import ( utilnet "k8s.io/utils/net" azcache "sigs.k8s.io/cloud-provider-azure/pkg/cache" + "sigs.k8s.io/cloud-provider-azure/pkg/consts" "sigs.k8s.io/cloud-provider-azure/pkg/metrics" ) @@ -50,18 +51,6 @@ var ( vmssVMProviderIDRE = regexp.MustCompile(`azure:///subscriptions/(?:.*)/resourceGroups/(.+)/providers/Microsoft.Compute/virtualMachineScaleSets/(.+)/virtualMachines/(?:\d+)`) ) -const ( - // virtualMachineScaleSetsDeallocating indicates VMSS instances are in Deallocating state. - virtualMachineScaleSetsDeallocating = "Deallocating" - - vmssMachineIDTemplate = "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachineScaleSets/%s/virtualMachines/%s" - - // VMSetCIDRIPV4TagKey specifies the node ipv4 CIDR mask of the instances on the VMSS or VMAS - VMSetCIDRIPV4TagKey = "kubernetesNodeCIDRMaskIPV4" - // VMSetCIDRIPV6TagKey specifies the node ipv6 CIDR mask of the instances on the VMSS or VMAS - VMSetCIDRIPV6TagKey = "kubernetesNodeCIDRMaskIPV6" -) - // vmssMetaInfo contains the metadata for a VMSS. type vmssMetaInfo struct { vmssName string @@ -562,7 +551,7 @@ func (ss *ScaleSet) getPrimaryInterfaceID(machine compute.VirtualMachineScaleSet // getVmssMachineID returns the full identifier of a vmss virtual machine. func (az *Cloud) getVmssMachineID(subscriptionID, resourceGroup, scaleSetName, instanceID string) string { return fmt.Sprintf( - vmssMachineIDTemplate, + consts.VmssMachineIDTemplate, subscriptionID, strings.ToLower(resourceGroup), scaleSetName, @@ -1098,7 +1087,7 @@ func (ss *ScaleSet) ensureVMSSInPool(service *v1.Service, nodes []*v1.Node, back // When vmss is being deleted, CreateOrUpdate API would report "the vmss is being deleted" error. // Since it is being deleted, we shouldn't send more CreateOrUpdate requests for it. - if vmss.ProvisioningState != nil && strings.EqualFold(*vmss.ProvisioningState, virtualMachineScaleSetsDeallocating) { + if vmss.ProvisioningState != nil && strings.EqualFold(*vmss.ProvisioningState, consts.VirtualMachineScaleSetsDeallocating) { klog.V(3).Infof("ensureVMSSInPool: found vmss %s being deleted, skipping", vmssName) continue } @@ -1447,7 +1436,7 @@ func (ss *ScaleSet) ensureBackendPoolDeletedFromVMSS(service *v1.Service, backen // When vmss is being deleted, CreateOrUpdate API would report "the vmss is being deleted" error. // Since it is being deleted, we shouldn't send more CreateOrUpdate requests for it. - if vmss.ProvisioningState != nil && strings.EqualFold(*vmss.ProvisioningState, virtualMachineScaleSetsDeallocating) { + if vmss.ProvisioningState != nil && strings.EqualFold(*vmss.ProvisioningState, consts.VirtualMachineScaleSetsDeallocating) { klog.V(3).Infof("ensureVMSSInPool: found vmss %s being deleted, skipping", vmssName) continue } @@ -1655,13 +1644,13 @@ func (ss *ScaleSet) GetNodeCIDRMasksByProviderID(providerID string) (int, int, e } var ipv4Mask, ipv6Mask int - if v4, ok := vmss.Tags[VMSetCIDRIPV4TagKey]; ok && v4 != nil { + if v4, ok := vmss.Tags[consts.VMSetCIDRIPV4TagKey]; ok && v4 != nil { ipv4Mask, err = strconv.Atoi(to.String(v4)) if err != nil { klog.Errorf(" GetNodeCIDRMasksByProviderID: error when paring the value of the ipv4 mask size %s: %v", to.String(v4), err) } } - if v6, ok := vmss.Tags[VMSetCIDRIPV6TagKey]; ok && v6 != nil { + if v6, ok := vmss.Tags[consts.VMSetCIDRIPV6TagKey]; ok && v6 != nil { ipv6Mask, err = strconv.Atoi(to.String(v6)) if err != nil { klog.Errorf(" GetNodeCIDRMasksByProviderID: error when paring the value of the ipv6 mask size%s: %v", to.String(v6), err) diff --git a/pkg/provider/azure_vmss_test.go b/pkg/provider/azure_vmss_test.go index b19b9775c3..0334cfa8f6 100644 --- a/pkg/provider/azure_vmss_test.go +++ b/pkg/provider/azure_vmss_test.go @@ -1207,7 +1207,7 @@ func TestGetVmssMachineID(t *testing.T) { subscriptionID, resourceGroup, scaleSetName, instanceID := "sub", "RG", "vmss", "id" VMSSMachineID := ss.cloud.getVmssMachineID(subscriptionID, resourceGroup, scaleSetName, instanceID) - expectedVMSSMachineID := fmt.Sprintf(vmssMachineIDTemplate, subscriptionID, strings.ToLower(resourceGroup), scaleSetName, instanceID) + expectedVMSSMachineID := fmt.Sprintf(consts.VmssMachineIDTemplate, subscriptionID, strings.ToLower(resourceGroup), scaleSetName, instanceID) assert.Equal(t, expectedVMSSMachineID, VMSSMachineID, "GetVmssMachineID should return the correct VMSS machine ID") } @@ -1977,7 +1977,7 @@ func TestEnsureVMSSInPool(t *testing.T) { expectedVMSS := buildTestVMSSWithLB(testVMSSName, "vmss-vm-", []string{testLBBackendpoolID0}, test.setIPv6Config) if test.isVMSSDeallocating { - expectedVMSS.ProvisioningState = to.StringPtr(virtualMachineScaleSetsDeallocating) + expectedVMSS.ProvisioningState = to.StringPtr(consts.VirtualMachineScaleSetsDeallocating) } if test.isVMSSNilNICConfig { expectedVMSS.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations = nil @@ -2251,7 +2251,7 @@ func TestEnsureBackendPoolDeletedFromVMSS(t *testing.T) { expectedVMSS := buildTestVMSSWithLB(testVMSSName, "vmss-vm-", []string{testLBBackendpoolID0}, false) if test.isVMSSDeallocating { - expectedVMSS.ProvisioningState = to.StringPtr(virtualMachineScaleSetsDeallocating) + expectedVMSS.ProvisioningState = to.StringPtr(consts.VirtualMachineScaleSetsDeallocating) } if test.isVMSSNilNICConfig { expectedVMSS.VirtualMachineProfile.NetworkProfile.NetworkInterfaceConfigurations = nil @@ -2464,8 +2464,8 @@ func TestGetNodeCIDRMasksByProviderID(t *testing.T) { description: "GetNodeCIDRMaksByProviderID should return the correct mask sizes", providerID: "azure:///subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss/virtualMachines/0", tags: map[string]*string{ - VMSetCIDRIPV4TagKey: to.StringPtr("24"), - VMSetCIDRIPV6TagKey: to.StringPtr("64"), + consts.VMSetCIDRIPV4TagKey: to.StringPtr("24"), + consts.VMSetCIDRIPV6TagKey: to.StringPtr("64"), }, expectedIPV4MaskSize: 24, expectedIPV6MaskSize: 64, @@ -2474,7 +2474,7 @@ func TestGetNodeCIDRMasksByProviderID(t *testing.T) { description: "GetNodeCIDRMaksByProviderID should return the correct mask sizes even if some of the tags are not specified", providerID: "azure:///subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss/virtualMachines/0", tags: map[string]*string{ - VMSetCIDRIPV4TagKey: to.StringPtr("24"), + consts.VMSetCIDRIPV4TagKey: to.StringPtr("24"), }, expectedIPV4MaskSize: 24, }, @@ -2482,8 +2482,8 @@ func TestGetNodeCIDRMasksByProviderID(t *testing.T) { description: "GetNodeCIDRMaksByProviderID should fail even if some of the tag is invalid", providerID: "azure:///subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/vmss/virtualMachines/0", tags: map[string]*string{ - VMSetCIDRIPV4TagKey: to.StringPtr("abc"), - VMSetCIDRIPV6TagKey: to.StringPtr("64"), + consts.VMSetCIDRIPV4TagKey: to.StringPtr("abc"), + consts.VMSetCIDRIPV6TagKey: to.StringPtr("64"), }, expectedIPV6MaskSize: 64, },