From 5c28bc0b2ebdde8b996eca0f27cdfcc5ac44d922 Mon Sep 17 00:00:00 2001 From: Matt Boersma Date: Mon, 20 Mar 2023 12:57:18 -0600 Subject: [PATCH] Replace some usage of Azure/go-autorest --- .golangci.yml | 3 +++ azure/converters/extendedlocation.go | 6 +++--- azure/converters/extendedlocation_test.go | 6 +++--- azure/defaults.go | 7 ++++--- azure/scope/machinepool.go | 6 +++--- azure/services/identities/client.go | 6 +++--- azure/services/natgateways/spec.go | 6 +++--- azure/services/scalesetvms/scalesetvms.go | 6 +++--- azure/services/scalesetvms/scalesetvms_test.go | 2 +- azure/services/virtualmachines/client.go | 5 +++-- go.mod | 2 +- test/e2e/azure_edgezone.go | 6 +++--- test/e2e/azure_logcollector.go | 12 ++++++------ test/e2e/azure_privatecluster.go | 10 +++++----- test/e2e/azure_vmextensions.go | 14 +++++++------- 15 files changed, 51 insertions(+), 46 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index d0b16dd3600..9a0cb2c5889 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -106,6 +106,9 @@ linters-settings: # Azure - pkg: github.com/Azure/go-autorest/autorest/azure alias: azureautorest + # Deprecated + - pkg: github.com/Azure/go-autorest/autorest/to + alias: deprecated-use-k8s.io-utils-pointer gocritic: enabled-tags: - "experimental" diff --git a/azure/converters/extendedlocation.go b/azure/converters/extendedlocation.go index 12b13a12e8a..60e931cad2d 100644 --- a/azure/converters/extendedlocation.go +++ b/azure/converters/extendedlocation.go @@ -19,7 +19,7 @@ package converters import ( "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute" "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network" - "github.com/Azure/go-autorest/autorest/to" + "k8s.io/utils/pointer" infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" ) @@ -29,7 +29,7 @@ func ExtendedLocationToNetworkSDK(src *infrav1.ExtendedLocationSpec) *network.Ex return nil } return &network.ExtendedLocation{ - Name: to.StringPtr(src.Name), + Name: pointer.String(src.Name), Type: network.ExtendedLocationTypes(src.Type), } } @@ -40,7 +40,7 @@ func ExtendedLocationToComputeSDK(src *infrav1.ExtendedLocationSpec) *compute.Ex return nil } return &compute.ExtendedLocation{ - Name: to.StringPtr(src.Name), + Name: pointer.String(src.Name), Type: compute.ExtendedLocationTypes(src.Type), } } diff --git a/azure/converters/extendedlocation_test.go b/azure/converters/extendedlocation_test.go index 31da55d9a85..bee3153d3da 100644 --- a/azure/converters/extendedlocation_test.go +++ b/azure/converters/extendedlocation_test.go @@ -22,7 +22,7 @@ import ( "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute" "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network" - "github.com/Azure/go-autorest/autorest/to" + "k8s.io/utils/pointer" infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" ) @@ -39,7 +39,7 @@ func TestExtendedLocationToNetworkSDK(t *testing.T) { Type: "Edge", }, want: &network.ExtendedLocation{ - Name: to.StringPtr("value"), + Name: pointer.String("value"), Type: network.ExtendedLocationTypes("Edge"), }, }, @@ -71,7 +71,7 @@ func TestExtendedLocationToComputeSDK(t *testing.T) { Type: "Edge", }, want: &compute.ExtendedLocation{ - Name: to.StringPtr("value"), + Name: pointer.String("value"), Type: compute.ExtendedLocationTypes("Edge"), }, }, diff --git a/azure/defaults.go b/azure/defaults.go index 9f348fdce7e..76397dde5a0 100644 --- a/azure/defaults.go +++ b/azure/defaults.go @@ -21,7 +21,6 @@ import ( "net/http" "github.com/Azure/go-autorest/autorest" - azureautorest "github.com/Azure/go-autorest/autorest/azure" "sigs.k8s.io/cluster-api-provider-azure/util/tele" "sigs.k8s.io/cluster-api-provider-azure/version" ) @@ -31,6 +30,8 @@ const ( DefaultUserName = "capi" // DefaultAKSUserName is the default username for a created AKS VM. DefaultAKSUserName = "azureuser" + // PublicCloudName is the name of the Azure public cloud. + PublicCloudName = "AzurePublicCloud" ) const ( @@ -300,7 +301,7 @@ func ManagedClusterID(subscriptionID, resourceGroup, managedClusterName string) // Its role is to detect and report Kubernetes bootstrap failure or success. func GetBootstrappingVMExtension(osType string, cloud string, vmName string) *ExtensionSpec { // currently, the bootstrap extension is only available in AzurePublicCloud. - if osType == LinuxOS && cloud == azureautorest.PublicCloud.Name { + if osType == LinuxOS && cloud == PublicCloudName { // The command checks for the existence of the bootstrapSentinelFile on the machine, with retries and sleep between retries. return &ExtensionSpec{ Name: BootstrappingExtensionLinux, @@ -311,7 +312,7 @@ func GetBootstrappingVMExtension(osType string, cloud string, vmName string) *Ex "commandToExecute": LinuxBootstrapExtensionCommand, }, } - } else if osType == WindowsOS && cloud == azureautorest.PublicCloud.Name { + } else if osType == WindowsOS && cloud == PublicCloudName { // This command for the existence of the bootstrapSentinelFile on the machine, with retries and sleep between reties. // If the file is not present after the retries are exhausted the extension fails with return code '-2' - ERROR_FILE_NOT_FOUND. return &ExtensionSpec{ diff --git a/azure/scope/machinepool.go b/azure/scope/machinepool.go index acd34212844..bc436278f91 100644 --- a/azure/scope/machinepool.go +++ b/azure/scope/machinepool.go @@ -24,7 +24,7 @@ import ( "io" "strings" - azureautorest "github.com/Azure/go-autorest/autorest/azure" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/pkg/errors" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -377,11 +377,11 @@ func (m *MachinePoolScope) createMachine(ctx context.Context, machine azure.VMSS ctx, _, done := tele.StartSpanWithLogger(ctx, "scope.MachinePoolScope.createMachine") defer done() - parsed, err := azureautorest.ParseResourceID(machine.ID) + parsed, err := arm.ParseResourceID(machine.ID) if err != nil { return errors.Wrap(err, fmt.Sprintf("failed to parse resource id %q", machine.ID)) } - instanceID := strings.ReplaceAll(parsed.ResourceName, "_", "-") + instanceID := strings.ReplaceAll(parsed.Name, "_", "-") ampm := infrav1exp.AzureMachinePoolMachine{ ObjectMeta: metav1.ObjectMeta{ diff --git a/azure/services/identities/client.go b/azure/services/identities/client.go index d05e1e7feb5..34591e39fbb 100644 --- a/azure/services/identities/client.go +++ b/azure/services/identities/client.go @@ -19,9 +19,9 @@ package identities import ( "context" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/services/msi/mgmt/2018-11-30/msi" "github.com/Azure/go-autorest/autorest" - azureautorest "github.com/Azure/go-autorest/autorest/azure" "sigs.k8s.io/cluster-api-provider-azure/azure" "sigs.k8s.io/cluster-api-provider-azure/util/tele" ) @@ -63,11 +63,11 @@ func (ac *AzureClient) GetClientID(ctx context.Context, providerID string) (stri ctx, _, done := tele.StartSpanWithLogger(ctx, "identities.GetClientID") defer done() - parsed, err := azureautorest.ParseResourceID(providerID) + parsed, err := arm.ParseResourceID(providerID) if err != nil { return "", err } - ident, err := ac.Get(ctx, parsed.ResourceGroup, parsed.ResourceName) + ident, err := ac.Get(ctx, parsed.ResourceGroupName, parsed.Name) if err != nil { return "", err } diff --git a/azure/services/natgateways/spec.go b/azure/services/natgateways/spec.go index 9009be1c033..f45f4341a80 100644 --- a/azure/services/natgateways/spec.go +++ b/azure/services/natgateways/spec.go @@ -19,8 +19,8 @@ package natgateways import ( "context" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network" - azureautorest "github.com/Azure/go-autorest/autorest/azure" "github.com/pkg/errors" "k8s.io/utils/pointer" infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" @@ -97,11 +97,11 @@ func hasPublicIP(natGateway network.NatGateway, publicIPName string) bool { } for _, publicIP := range *natGateway.PublicIPAddresses { - resource, err := azureautorest.ParseResourceID(*publicIP.ID) + resource, err := arm.ParseResourceID(*publicIP.ID) if err != nil { continue } - if resource.ResourceName == publicIPName { + if resource.Name == publicIPName { return true } } diff --git a/azure/services/scalesetvms/scalesetvms.go b/azure/services/scalesetvms/scalesetvms.go index f257497d3d4..43283e61423 100644 --- a/azure/services/scalesetvms/scalesetvms.go +++ b/azure/services/scalesetvms/scalesetvms.go @@ -22,7 +22,7 @@ import ( "strings" "time" - azureautorest "github.com/Azure/go-autorest/autorest/azure" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/go-logr/logr" "github.com/pkg/errors" infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1" @@ -147,11 +147,11 @@ func (s *Service) deleteVMSSFlexVM(ctx context.Context, resourceID string) error } }() - parsed, err := azureautorest.ParseResourceID(resourceID) + parsed, err := arm.ParseResourceID(resourceID) if err != nil { return errors.Wrap(err, fmt.Sprintf("failed to parse resource id %q", resourceID)) } - resourceGroup, resourceName := parsed.ResourceGroup, parsed.ResourceName + resourceGroup, resourceName := parsed.ResourceGroupName, parsed.Name log.V(4).Info("entering delete") future := s.Scope.GetLongRunningOperationState(resourceName, serviceName, infrav1.DeleteFuture) diff --git a/azure/services/scalesetvms/scalesetvms_test.go b/azure/services/scalesetvms/scalesetvms_test.go index be4373ee715..e1f7545bc19 100644 --- a/azure/services/scalesetvms/scalesetvms_test.go +++ b/azure/services/scalesetvms/scalesetvms_test.go @@ -293,7 +293,7 @@ func TestService_Delete(t *testing.T) { s.OrchestrationMode().Return(infrav1.FlexibleOrchestrationMode) v.GetByID(gomock2.AContext(), "foo").Return(compute.VirtualMachine{}, nil) }, - Err: errors.Wrap(fmt.Errorf("parsing failed for %s. Invalid resource Id format", "foo"), fmt.Sprintf("failed to parse resource id %q", "foo")), + Err: errors.Wrap(fmt.Errorf("invalid resource ID: resource id '%s' must start with '/'", "foo"), fmt.Sprintf("failed to parse resource id %q", "foo")), }, } diff --git a/azure/services/virtualmachines/client.go b/azure/services/virtualmachines/client.go index a2951069927..6438fe54fcb 100644 --- a/azure/services/virtualmachines/client.go +++ b/azure/services/virtualmachines/client.go @@ -23,6 +23,7 @@ import ( "fmt" "time" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute" "github.com/Azure/go-autorest/autorest" azureautorest "github.com/Azure/go-autorest/autorest/azure" @@ -89,14 +90,14 @@ func (ac *AzureClient) GetByID(ctx context.Context, resourceID string) (compute. ctx, log, done := tele.StartSpanWithLogger(ctx, "virtualmachines.AzureClient.GetByID") defer done() - parsed, err := azureautorest.ParseResourceID(resourceID) + parsed, err := arm.ParseResourceID(resourceID) if err != nil { return compute.VirtualMachine{}, errors.Wrap(err, fmt.Sprintf("failed parsing the VM resource id %q", resourceID)) } log.V(4).Info("parsed VM resourceID", "parsed", parsed) - return ac.virtualmachines.Get(ctx, parsed.ResourceGroup, parsed.ResourceName, "") + return ac.virtualmachines.Get(ctx, parsed.ResourceGroupName, parsed.Name, "") } // CreateOrUpdateAsync creates or updates a virtual machine asynchronously. diff --git a/go.mod b/go.mod index ee708a88731..3038baa5596 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,6 @@ require ( github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.2.2 github.com/Azure/go-autorest/autorest v0.11.28 github.com/Azure/go-autorest/autorest/azure/auth v0.5.12 - github.com/Azure/go-autorest/autorest/to v0.4.0 github.com/Azure/go-autorest/tracing v0.6.0 github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d github.com/blang/semver v3.5.1+incompatible @@ -61,6 +60,7 @@ require ( github.com/Azure/go-autorest/autorest/azure/cli v0.4.5 // indirect github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect github.com/Azure/go-autorest/autorest/mocks v0.4.2 // indirect + github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect github.com/Azure/go-autorest/logger v0.2.1 // indirect github.com/AzureAD/microsoft-authentication-library-for-go v0.9.0 // indirect diff --git a/test/e2e/azure_edgezone.go b/test/e2e/azure_edgezone.go index 71cfe699321..02ac387bf50 100644 --- a/test/e2e/azure_edgezone.go +++ b/test/e2e/azure_edgezone.go @@ -23,8 +23,8 @@ import ( "context" "strings" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute" - azureautorest "github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure/auth" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -88,10 +88,10 @@ func AzureEdgeZoneClusterSpec(ctx context.Context, inputGetter func() AzureEdgeZ // get the resource group name resourceID := strings.TrimPrefix(*machineList.Items[0].Spec.ProviderID, azure.ProviderIDPrefix) - resource, err := azureautorest.ParseResourceID(resourceID) + resource, err := arm.ParseResourceID(resourceID) Expect(err).NotTo(HaveOccurred()) - vmListResults, err := vmClient.List(ctx, resource.ResourceGroup, "") + vmListResults, err := vmClient.List(ctx, resource.ResourceGroupName, "") Expect(err).NotTo(HaveOccurred()) By("Verifying VMs' extendedLocation property is correct") diff --git a/test/e2e/azure_logcollector.go b/test/e2e/azure_logcollector.go index dd11c506235..e3f34a09d02 100644 --- a/test/e2e/azure_logcollector.go +++ b/test/e2e/azure_logcollector.go @@ -28,8 +28,8 @@ import ( "strings" "time" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute" - azureautorest "github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure/auth" "github.com/pkg/errors" apierrors "k8s.io/apimachinery/pkg/api/errors" @@ -402,7 +402,7 @@ func collectVMBootLog(ctx context.Context, am *infrav1.AzureMachine, outputPath } resourceID := strings.TrimPrefix(*am.Spec.ProviderID, azure.ProviderIDPrefix) - resource, err := azureautorest.ParseResourceID(resourceID) + resource, err := arm.ParseResourceID(resourceID) if err != nil { return errors.Wrap(err, "failed to parse resource id") } @@ -418,7 +418,7 @@ func collectVMBootLog(ctx context.Context, am *infrav1.AzureMachine, outputPath return errors.Wrap(err, "failed to get authorizer") } - bootDiagnostics, err := vmClient.RetrieveBootDiagnosticsData(ctx, resource.ResourceGroup, resource.ResourceName, nil) + bootDiagnostics, err := vmClient.RetrieveBootDiagnosticsData(ctx, resource.ResourceGroupName, resource.Name, nil) if err != nil { return errors.Wrap(err, "failed to get boot diagnostics data") } @@ -432,12 +432,12 @@ func collectVMSSBootLog(ctx context.Context, providerID string, outputPath strin v := strings.Split(resourceID, "/") instanceID := v[len(v)-1] resourceID = strings.TrimSuffix(resourceID, "/virtualMachines/"+instanceID) - resource, err := azureautorest.ParseResourceID(resourceID) + resource, err := arm.ParseResourceID(resourceID) if err != nil { return errors.Wrap(err, "failed to parse resource id") } - Logf("Collecting boot logs for VMSS instance %s of scale set %s\n", instanceID, resource.ResourceName) + Logf("Collecting boot logs for VMSS instance %s of scale set %s\n", instanceID, resource.Name) settings, err := auth.GetSettingsFromEnvironment() if err != nil { @@ -450,7 +450,7 @@ func collectVMSSBootLog(ctx context.Context, providerID string, outputPath strin return errors.Wrap(err, "failed to get authorizer") } - bootDiagnostics, err := vmssClient.RetrieveBootDiagnosticsData(ctx, resource.ResourceGroup, resource.ResourceName, instanceID, nil) + bootDiagnostics, err := vmssClient.RetrieveBootDiagnosticsData(ctx, resource.ResourceGroupName, resource.Name, instanceID, nil) if err != nil { return errors.Wrap(err, "failed to get boot diagnostics data") } diff --git a/test/e2e/azure_privatecluster.go b/test/e2e/azure_privatecluster.go index 953d39c28d0..59eb92acc2b 100644 --- a/test/e2e/azure_privatecluster.go +++ b/test/e2e/azure_privatecluster.go @@ -26,12 +26,12 @@ import ( "path/filepath" "strings" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute" "github.com/Azure/azure-sdk-for-go/services/msi/mgmt/2018-11-30/msi" "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network" "github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns" "github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources" - azureautorest "github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure/auth" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -421,14 +421,14 @@ func SetupExistingVNet(ctx context.Context, vnetCidr string, cpSubnetCidrs, node } func getAPIVersion(resourceID string) (string, error) { - parsed, err := azureautorest.ParseResourceID(resourceID) + parsed, err := arm.ParseResourceID(resourceID) if err != nil { return "", errors.Wrap(err, fmt.Sprintf("unable to parse resource ID %q", resourceID)) } switch parsed.Provider { case "Microsoft.Network": - if parsed.ResourceType == "privateDnsZones" { + if parsed.ResourceType.String() == "privateDnsZones" { return getAPIVersionFromUserAgent(privatedns.UserAgent()), nil } return getAPIVersionFromUserAgent(network.UserAgent()), nil @@ -455,10 +455,10 @@ func getClientIDforMSI(resourceID string) string { msiClient := msi.NewUserAssignedIdentitiesClient(subscriptionID) msiClient.Authorizer = authorizer - parsed, err := azureautorest.ParseResourceID(resourceID) + parsed, err := arm.ParseResourceID(resourceID) Expect(err).NotTo(HaveOccurred()) - id, err := msiClient.Get(context.TODO(), parsed.ResourceGroup, parsed.ResourceName) + id, err := msiClient.Get(context.TODO(), parsed.ResourceGroupName, parsed.Name) Expect(err).NotTo(HaveOccurred()) return id.ClientID.String() diff --git a/test/e2e/azure_vmextensions.go b/test/e2e/azure_vmextensions.go index 3bfabd8ee54..b52b9c3d312 100644 --- a/test/e2e/azure_vmextensions.go +++ b/test/e2e/azure_vmextensions.go @@ -23,8 +23,8 @@ import ( "context" "strings" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/arm" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute" - azureautorest "github.com/Azure/go-autorest/autorest/azure" "github.com/Azure/go-autorest/autorest/azure/auth" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -99,15 +99,15 @@ func AzureVMExtensionsSpec(ctx context.Context, inputGetter func() AzureVMExtens // get the resource group name resourceID := strings.TrimPrefix(*machineList.Items[0].Spec.ProviderID, azure.ProviderIDPrefix) - resource, err := azureautorest.ParseResourceID(resourceID) + resource, err := arm.ParseResourceID(resourceID) Expect(err).NotTo(HaveOccurred()) - vmListResults, err := vmClient.List(ctx, resource.ResourceGroup, "") + vmListResults, err := vmClient.List(ctx, resource.ResourceGroupName, "") Expect(err).NotTo(HaveOccurred()) By("Verifying specified VM extensions are created on Azure") for _, machine := range vmListResults.Values() { - vmExtensionListResult, err := vmExtensionsClient.List(ctx, resource.ResourceGroup, *machine.Name, "") + vmExtensionListResult, err := vmExtensionsClient.List(ctx, resource.ResourceGroupName, *machine.Name, "") Expect(err).NotTo(HaveOccurred()) vmExtensionList := *vmExtensionListResult.Value var vmExtensionNames []string @@ -147,15 +147,15 @@ func AzureVMExtensionsSpec(ctx context.Context, inputGetter func() AzureVMExtens // get the resource group name resourceID := strings.TrimPrefix(machinePoolList.Items[0].Spec.ProviderID, azure.ProviderIDPrefix) - resource, err := azureautorest.ParseResourceID(resourceID) + resource, err := arm.ParseResourceID(resourceID) Expect(err).NotTo(HaveOccurred()) - vmssListResults, err := vmssClient.List(ctx, resource.ResourceGroup) + vmssListResults, err := vmssClient.List(ctx, resource.ResourceGroupName) Expect(err).NotTo(HaveOccurred()) By("Verifying VMSS extensions are created on Azure") for _, machinePool := range vmssListResults.Values() { - vmssExtensionListResult, err := vmssExtensionsClient.List(ctx, resource.ResourceGroup, *machinePool.Name) + vmssExtensionListResult, err := vmssExtensionsClient.List(ctx, resource.ResourceGroupName, *machinePool.Name) Expect(err).NotTo(HaveOccurred()) vmssExtensionList := vmssExtensionListResult.Values() var vmssExtensionNames []string