Skip to content

Commit

Permalink
Replace some usage of Azure/go-autorest
Browse files Browse the repository at this point in the history
  • Loading branch information
mboersma committed Mar 20, 2023
1 parent 5a4257d commit a7fc477
Show file tree
Hide file tree
Showing 15 changed files with 50 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions azure/converters/extendedlocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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),
}
}
Expand All @@ -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),
}
}
6 changes: 3 additions & 3 deletions azure/converters/extendedlocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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"),
},
},
Expand Down Expand Up @@ -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"),
},
},
Expand Down
7 changes: 4 additions & 3 deletions azure/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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 (
Expand Down Expand Up @@ -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,
Expand All @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down
6 changes: 3 additions & 3 deletions azure/services/identities/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions azure/services/natgateways/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}
Expand Down
6 changes: 3 additions & 3 deletions azure/services/scalesetvms/scalesetvms.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion azure/services/scalesetvms/scalesetvms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
},
}

Expand Down
5 changes: 3 additions & 2 deletions azure/services/virtualmachines/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/e2e/azure_edgezone.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
12 changes: 6 additions & 6 deletions test/e2e/azure_logcollector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
}
Expand All @@ -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")
}
Expand All @@ -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 {
Expand All @@ -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")
}
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/azure_privatecluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -421,7 +421,7 @@ 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))
}
Expand Down Expand Up @@ -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()
Expand Down
Loading

0 comments on commit a7fc477

Please sign in to comment.