Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert various references to SDKv2 #3992

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions azure/converters/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,11 @@ package converters

import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network"
"k8s.io/utils/ptr"
"sigs.k8s.io/cluster-api-provider-azure/azure"
)

// GetSubnetAddresses returns the address prefixes contained in a subnet.
func GetSubnetAddresses(subnet network.Subnet) []string {
var addresses []string
if subnet.SubnetPropertiesFormat != nil && subnet.SubnetPropertiesFormat.AddressPrefix != nil {
addresses = []string{ptr.Deref(subnet.SubnetPropertiesFormat.AddressPrefix, "")}
} else if subnet.SubnetPropertiesFormat != nil && subnet.SubnetPropertiesFormat.AddressPrefixes != nil {
addresses = azure.StringSlice(subnet.SubnetPropertiesFormat.AddressPrefixes)
}
return addresses
}

// GetSubnetAddressesV2 returns the address prefixes contained in an SDK v2 subnet.
func GetSubnetAddressesV2(subnet *armnetwork.Subnet) []string {
// GetSubnetAddresses returns the address prefixes contained in an SDK v2 subnet.
func GetSubnetAddresses(subnet *armnetwork.Subnet) []string {
var addresses []string
if subnet.Properties != nil && subnet.Properties.AddressPrefix != nil {
addresses = []string{ptr.Deref(subnet.Properties.AddressPrefix, "")}
Expand Down
43 changes: 1 addition & 42 deletions azure/converters/subnets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,11 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network"
. "github.com/onsi/gomega"
"k8s.io/utils/ptr"
)

func TestGetSubnetAddresses(t *testing.T) {
tests := []struct {
name string
subnet network.Subnet
want []string
}{
{
name: "nil properties subnet",
subnet: network.Subnet{},
},
{
name: "subnet with single address prefix",
subnet: network.Subnet{
SubnetPropertiesFormat: &network.SubnetPropertiesFormat{
AddressPrefix: ptr.To("test-address-prefix"),
},
},
want: []string{"test-address-prefix"},
},
{
name: "subnet with multiple address prefixes",
subnet: network.Subnet{
SubnetPropertiesFormat: &network.SubnetPropertiesFormat{
AddressPrefixes: &[]string{"test-address-prefix-1", "test-address-prefix-2"},
},
},
want: []string{"test-address-prefix-1", "test-address-prefix-2"},
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
got := GetSubnetAddresses(tt.subnet)
g.Expect(got).To(Equal(tt.want), fmt.Sprintf("got: %v, want: %v", got, tt.want))
})
}
}

func TestGetSubnetAddressesV2(t *testing.T) {
tests := []struct {
name string
subnet armnetwork.Subnet
Expand Down Expand Up @@ -100,7 +59,7 @@ func TestGetSubnetAddressesV2(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
got := GetSubnetAddressesV2(&tt.subnet)
got := GetSubnetAddresses(&tt.subnet)
g.Expect(got).To(Equal(tt.want), fmt.Sprintf("got: %v, want: %v", got, tt.want))
})
}
Expand Down
6 changes: 3 additions & 3 deletions azure/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"encoding/json"
"strings"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -147,9 +147,9 @@ func (m *MachineScope) InitMachineCache(ctx context.Context) error {
return errors.Wrapf(err, "failed to get VM SKU %s in compute api", m.AzureMachine.Spec.VMSize)
}

m.cache.availabilitySetSKU, err = skuCache.Get(ctx, string(compute.AvailabilitySetSkuTypesAligned), resourceskus.AvailabilitySets)
m.cache.availabilitySetSKU, err = skuCache.Get(ctx, string(armcompute.AvailabilitySetSKUTypesAligned), resourceskus.AvailabilitySets)
if err != nil {
return errors.Wrapf(err, "failed to get availability set SKU %s in compute api", string(compute.AvailabilitySetSkuTypesAligned))
return errors.Wrapf(err, "failed to get availability set SKU %s in compute api", string(armcompute.AvailabilitySetSKUTypesAligned))
}
}

Expand Down
6 changes: 3 additions & 3 deletions azure/services/async/async_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"testing"
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
"github.com/Azure/go-autorest/autorest"
azureautorest "github.com/Azure/go-autorest/autorest/azure"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -57,8 +57,8 @@ var (
ResourceGroup: "test-group",
Data: "ZmFrZSBiNjQgZnV0dXJlIGRhdGEK",
}
fakeExistingResource = resources.GenericResource{}
fakeResourceParameters = resources.GenericResource{}
fakeExistingResource = armresources.GenericResource{}
fakeResourceParameters = armresources.GenericResource{}
fakeInternalError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusInternalServerError}, "Internal Server Error")
fakeNotFoundError = autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusNotFound}, "Not Found")
errCtxExceeded = errors.New("ctx exceeded")
Expand Down
5 changes: 3 additions & 2 deletions azure/services/roleassignments/roleassignments.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/pkg/errors"
"sigs.k8s.io/cluster-api-provider-azure/azure"
Expand Down Expand Up @@ -160,9 +161,9 @@ func (s *Service) getVMSSPrincipalID(ctx context.Context) (*string, error) {
if err != nil {
return nil, errors.Wrap(err, "failed to get principal ID for VMSS")
}
resultVMSS, ok := resultVMSSIface.(compute.VirtualMachineScaleSet)
resultVMSS, ok := resultVMSSIface.(armcompute.VirtualMachineScaleSet)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are tests failing on main without this change now that scalesets are using SDK v2?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They haven't, no, which is...interesting. Let's see if they pass here.

if !ok {
return nil, errors.Errorf("%T is not a compute.VirtualMachineScaleSet", resultVMSSIface)
return nil, errors.Errorf("%T is not an armcompute.VirtualMachineScaleSet", resultVMSSIface)
}

return resultVMSS.Identity.PrincipalID, nil
Expand Down
11 changes: 6 additions & 5 deletions azure/services/roleassignments/roleassignments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/go-autorest/autorest"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -174,8 +175,8 @@ func TestReconcileRoleAssignmentsVMSS(t *testing.T) {
s.RoleAssignmentResourceType().Return(azure.VirtualMachineScaleSet)
s.ResourceGroup().Return("my-rg")
s.Name().Return("test-vmss")
mvmss.Get(gomockinternal.AContext(), &fakeVMSSSpec).Return(compute.VirtualMachineScaleSet{
Identity: &compute.VirtualMachineScaleSetIdentity{
mvmss.Get(gomockinternal.AContext(), &fakeVMSSSpec).Return(armcompute.VirtualMachineScaleSet{
Identity: &armcompute.VirtualMachineScaleSetIdentity{
PrincipalID: &fakePrincipalID,
},
}, nil)
Expand All @@ -192,7 +193,7 @@ func TestReconcileRoleAssignmentsVMSS(t *testing.T) {
s.ResourceGroup().Return("my-rg")
s.Name().Return("test-vmss")
s.HasSystemAssignedIdentity().Return(true)
mvmss.Get(gomockinternal.AContext(), &fakeVMSSSpec).Return(compute.VirtualMachineScaleSet{},
mvmss.Get(gomockinternal.AContext(), &fakeVMSSSpec).Return(armcompute.VirtualMachineScaleSet{},
autorest.NewErrorWithResponse("", "", &http.Response{StatusCode: http.StatusInternalServerError}, "Internal Server Error"))
},
},
Expand All @@ -207,8 +208,8 @@ func TestReconcileRoleAssignmentsVMSS(t *testing.T) {
s.RoleAssignmentResourceType().Return(azure.VirtualMachineScaleSet)
s.ResourceGroup().Return("my-rg")
s.Name().Return("test-vmss")
mvmss.Get(gomockinternal.AContext(), &fakeVMSSSpec).Return(compute.VirtualMachineScaleSet{
Identity: &compute.VirtualMachineScaleSetIdentity{
mvmss.Get(gomockinternal.AContext(), &fakeVMSSSpec).Return(armcompute.VirtualMachineScaleSet{
Identity: &armcompute.VirtualMachineScaleSetIdentity{
PrincipalID: &fakePrincipalID,
},
}, nil)
Expand Down
2 changes: 1 addition & 1 deletion azure/services/subnets/subnets.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
return errors.Errorf("%T is not an armnetwork.Subnet", result)
}
s.Scope.UpdateSubnetID(subnetSpec.ResourceName(), ptr.Deref(subnet.ID, ""))
s.Scope.UpdateSubnetCIDRs(subnetSpec.ResourceName(), converters.GetSubnetAddressesV2(&subnet))
s.Scope.UpdateSubnetCIDRs(subnetSpec.ResourceName(), converters.GetSubnetAddresses(&subnet))
}
}

Expand Down
2 changes: 1 addition & 1 deletion azure/services/virtualnetworks/virtualnetworks.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (s *Service) Reconcile(ctx context.Context) error {
// Subnets that are not part of this cluster spec are silently ignored.
if existingVnet.Properties.Subnets != nil {
for _, subnet := range existingVnet.Properties.Subnets {
s.Scope.UpdateSubnetCIDRs(ptr.Deref(subnet.Name, ""), converters.GetSubnetAddressesV2(subnet))
s.Scope.UpdateSubnetCIDRs(ptr.Deref(subnet.Name, ""), converters.GetSubnetAddresses(subnet))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions exp/api/v1beta1/azuremachinepool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"fmt"
"reflect"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/blang/semver"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -298,7 +298,7 @@ func (amp *AzureMachinePool) ValidateDiagnostics() error {
func (amp *AzureMachinePool) ValidateOrchestrationMode(c client.Client) func() error {
return func() error {
// Only Flexible orchestration mode requires validation.
if amp.Spec.OrchestrationMode == infrav1.OrchestrationModeType(compute.OrchestrationModeFlexible) {
if amp.Spec.OrchestrationMode == infrav1.OrchestrationModeType(armcompute.OrchestrationModeFlexible) {
parent, err := azureutil.FindParentMachinePoolWithRetry(amp.Name, c, 5)
if err != nil {
return errors.Wrap(err, "failed to find parent MachinePool")
Expand Down
10 changes: 5 additions & 5 deletions exp/api/v1beta1/azuremachinepool_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"fmt"
"testing"

"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
guuid "github.com/google/uuid"
. "github.com/onsi/gomega"
"github.com/pkg/errors"
Expand Down Expand Up @@ -222,19 +222,19 @@ func TestAzureMachinePool_ValidateCreate(t *testing.T) {
},
{
name: "azuremachinepool with Flexible orchestration mode",
amp: createMachinePoolWithOrchestrationMode(compute.OrchestrationModeFlexible),
amp: createMachinePoolWithOrchestrationMode(armcompute.OrchestrationModeFlexible),
version: "v1.26.0",
wantErr: false,
},
{
name: "azuremachinepool with Flexible orchestration mode and invalid Kubernetes version",
amp: createMachinePoolWithOrchestrationMode(compute.OrchestrationModeFlexible),
amp: createMachinePoolWithOrchestrationMode(armcompute.OrchestrationModeFlexible),
version: "v1.25.6",
wantErr: true,
},
{
name: "azuremachinepool with Flexible orchestration mode and invalid Kubernetes version, no owner",
amp: createMachinePoolWithOrchestrationMode(compute.OrchestrationModeFlexible),
amp: createMachinePoolWithOrchestrationMode(armcompute.OrchestrationModeFlexible),
version: "v1.25.6",
ownerNotFound: true,
wantErr: true,
Expand Down Expand Up @@ -631,7 +631,7 @@ func createMachinePoolWithStrategy(strategy AzureMachinePoolDeploymentStrategy)
}
}

func createMachinePoolWithOrchestrationMode(mode compute.OrchestrationMode) *AzureMachinePool {
func createMachinePoolWithOrchestrationMode(mode armcompute.OrchestrationMode) *AzureMachinePool {
return &AzureMachinePool{
Spec: AzureMachinePoolSpec{
OrchestrationMode: infrav1.OrchestrationModeType(mode),
Expand Down