Skip to content

Commit

Permalink
Merge pull request #3996 from mboersma/sdk-v2-vms
Browse files Browse the repository at this point in the history
Convert virtualmachines service to SDKv2
  • Loading branch information
k8s-ci-robot authored Sep 14, 2023
2 parents 4f076e6 + f49b5ac commit af1be1c
Show file tree
Hide file tree
Showing 24 changed files with 437 additions and 909 deletions.
34 changes: 2 additions & 32 deletions azure/converters/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,12 @@ package converters

import (
"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"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)

// GetDiagnosticsProfileV2 converts a CAPZ Diagnostics option to a Azure SDK Diagnostics Profile.
func GetDiagnosticsProfileV2(diagnostics *infrav1.Diagnostics) *armcompute.DiagnosticsProfile {
// GetDiagnosticsProfile converts a CAPZ Diagnostics option to a Azure SDK Diagnostics Profile.
func GetDiagnosticsProfile(diagnostics *infrav1.Diagnostics) *armcompute.DiagnosticsProfile {
if diagnostics != nil && diagnostics.Boot != nil {
switch diagnostics.Boot.StorageAccountType {
case infrav1.DisabledDiagnosticsStorage:
Expand All @@ -51,32 +50,3 @@ func GetDiagnosticsProfileV2(diagnostics *infrav1.Diagnostics) *armcompute.Diagn

return nil
}

// GetDiagnosticsProfile converts a CAPZ Diagnostics option to a Azure SDK Diagnostics Profile.
func GetDiagnosticsProfile(diagnostics *infrav1.Diagnostics) *compute.DiagnosticsProfile {
if diagnostics != nil && diagnostics.Boot != nil {
switch diagnostics.Boot.StorageAccountType {
case infrav1.DisabledDiagnosticsStorage:
return &compute.DiagnosticsProfile{
BootDiagnostics: &compute.BootDiagnostics{
Enabled: ptr.To(false),
},
}
case infrav1.ManagedDiagnosticsStorage:
return &compute.DiagnosticsProfile{
BootDiagnostics: &compute.BootDiagnostics{
Enabled: ptr.To(true),
},
}
case infrav1.UserManagedDiagnosticsStorage:
return &compute.DiagnosticsProfile{
BootDiagnostics: &compute.BootDiagnostics{
Enabled: ptr.To(true),
StorageURI: &diagnostics.Boot.UserManaged.StorageAccountURI,
},
}
}
}

return nil
}
72 changes: 1 addition & 71 deletions azure/converters/diagnostics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import (
"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/google/go-cmp/cmp"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)

func TestGetDiagnosticsProfileV2(t *testing.T) {
func TestGetDiagnosticsProfile(t *testing.T) {
tests := []struct {
name string
diagnostics *infrav1.Diagnostics
Expand Down Expand Up @@ -83,75 +82,6 @@ func TestGetDiagnosticsProfileV2(t *testing.T) {
want: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := GetDiagnosticsProfileV2(tt.diagnostics)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("GetDiagnosticsProfileV2(%s) mismatch (-want +got):\n%s", tt.name, diff)
}
})
}
}

func TestGetDiagnosticsProfile(t *testing.T) {
tests := []struct {
name string
diagnostics *infrav1.Diagnostics
want *compute.DiagnosticsProfile
}{
{
name: "managed diagnostics",
diagnostics: &infrav1.Diagnostics{
Boot: &infrav1.BootDiagnostics{
StorageAccountType: infrav1.ManagedDiagnosticsStorage,
},
},
want: &compute.DiagnosticsProfile{
BootDiagnostics: &compute.BootDiagnostics{
Enabled: ptr.To(true),
},
},
},
{
name: "user managed diagnostics",
diagnostics: &infrav1.Diagnostics{
Boot: &infrav1.BootDiagnostics{
StorageAccountType: infrav1.UserManagedDiagnosticsStorage,
UserManaged: &infrav1.UserManagedBootDiagnostics{
StorageAccountURI: "https://fake",
},
},
},
want: &compute.DiagnosticsProfile{
BootDiagnostics: &compute.BootDiagnostics{
Enabled: ptr.To(true),
StorageURI: ptr.To("https://fake"),
},
},
},
{
name: "disabled diagnostics",
diagnostics: &infrav1.Diagnostics{
Boot: &infrav1.BootDiagnostics{
StorageAccountType: infrav1.DisabledDiagnosticsStorage,
},
},
want: &compute.DiagnosticsProfile{
BootDiagnostics: &compute.BootDiagnostics{
Enabled: ptr.To(false),
},
},
},
{
name: "nil diagnostics boot",
diagnostics: &infrav1.Diagnostics{
Boot: nil,
},
want: nil,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand Down
10 changes: 5 additions & 5 deletions azure/converters/extendedlocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ limitations under the License.
package converters

import (
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)
Expand All @@ -34,13 +34,13 @@ func ExtendedLocationToNetworkSDK(src *infrav1.ExtendedLocationSpec) *armnetwork
}
}

// ExtendedLocationToComputeSDK converts infrav1.ExtendedLocationSpec to compute.ExtendedLocation.
func ExtendedLocationToComputeSDK(src *infrav1.ExtendedLocationSpec) *compute.ExtendedLocation {
// ExtendedLocationToComputeSDK converts an infrav1.ExtendedLocationSpec to an armcompute.ExtendedLocation.
func ExtendedLocationToComputeSDK(src *infrav1.ExtendedLocationSpec) *armcompute.ExtendedLocation {
if src == nil {
return nil
}
return &compute.ExtendedLocation{
return &armcompute.ExtendedLocation{
Name: ptr.To(src.Name),
Type: compute.ExtendedLocationTypes(src.Type),
Type: ptr.To(armcompute.ExtendedLocationTypes(src.Type)),
}
}
10 changes: 5 additions & 5 deletions azure/converters/extendedlocation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ import (
"reflect"
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v4"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-11-01/compute"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)

func TestExtendedLocationToNetworkSDKv2(t *testing.T) {
func TestExtendedLocationToNetworkSDK(t *testing.T) {
tests := []struct {
name string
args *infrav1.ExtendedLocationSpec
Expand Down Expand Up @@ -62,17 +62,17 @@ func TestExtendedLocationToComputeSDK(t *testing.T) {
tests := []struct {
name string
args *infrav1.ExtendedLocationSpec
want *compute.ExtendedLocation
want *armcompute.ExtendedLocation
}{
{
name: "normal extendedLocation instance",
args: &infrav1.ExtendedLocationSpec{
Name: "value",
Type: "Edge",
},
want: &compute.ExtendedLocation{
want: &armcompute.ExtendedLocation{
Name: ptr.To("value"),
Type: compute.ExtendedLocationTypes("Edge"),
Type: ptr.To(armcompute.ExtendedLocationTypes("Edge")),
},
},
{
Expand Down
18 changes: 9 additions & 9 deletions azure/converters/identity.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"strings"

"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"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
azureutil "sigs.k8s.io/cluster-api-provider-azure/util/azure"
)
Expand All @@ -30,10 +30,10 @@ import (
var ErrUserAssignedIdentitiesNotFound = errors.New("the user-assigned identity provider ids must not be null or empty for 'UserAssigned' identity type")

// VMIdentityToVMSDK converts CAPZ VM identity to Azure SDK identity.
func VMIdentityToVMSDK(identity infrav1.VMIdentity, uami []infrav1.UserAssignedIdentity) (*compute.VirtualMachineIdentity, error) {
func VMIdentityToVMSDK(identity infrav1.VMIdentity, uami []infrav1.UserAssignedIdentity) (*armcompute.VirtualMachineIdentity, error) {
if identity == infrav1.VMIdentitySystemAssigned {
return &compute.VirtualMachineIdentity{
Type: compute.ResourceIdentityTypeSystemAssigned,
return &armcompute.VirtualMachineIdentity{
Type: ptr.To(armcompute.ResourceIdentityTypeSystemAssigned),
}, nil
}

Expand All @@ -43,8 +43,8 @@ func VMIdentityToVMSDK(identity infrav1.VMIdentity, uami []infrav1.UserAssignedI
return nil, errors.Wrap(err, "failed to assign VM identity")
}

return &compute.VirtualMachineIdentity{
Type: compute.ResourceIdentityTypeUserAssigned,
return &armcompute.VirtualMachineIdentity{
Type: ptr.To(armcompute.ResourceIdentityTypeUserAssigned),
UserAssignedIdentities: userIdentitiesMap,
}, nil
}
Expand All @@ -55,14 +55,14 @@ func VMIdentityToVMSDK(identity infrav1.VMIdentity, uami []infrav1.UserAssignedI
// UserAssignedIdentitiesToVMSDK converts CAPZ user assigned identities associated with the Virtual Machine to Azure SDK identities
// The user identity dictionary key references will be ARM resource ids in the form:
// '/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.ManagedIdentity/userAssignedIdentities/{identityName}'.
func UserAssignedIdentitiesToVMSDK(identities []infrav1.UserAssignedIdentity) (map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue, error) {
func UserAssignedIdentitiesToVMSDK(identities []infrav1.UserAssignedIdentity) (map[string]*armcompute.UserAssignedIdentitiesValue, error) {
if len(identities) == 0 {
return nil, ErrUserAssignedIdentitiesNotFound
}
userIdentitiesMap := make(map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue, len(identities))
userIdentitiesMap := make(map[string]*armcompute.UserAssignedIdentitiesValue, len(identities))
for _, id := range identities {
key := sanitized(id.ProviderID)
userIdentitiesMap[key] = &compute.VirtualMachineIdentityUserAssignedIdentitiesValue{}
userIdentitiesMap[key] = &armcompute.UserAssignedIdentitiesValue{}
}

return userIdentitiesMap, nil
Expand Down
30 changes: 15 additions & 15 deletions azure/converters/identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"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/onsi/gomega"
"k8s.io/utils/ptr"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
)

Expand All @@ -37,7 +37,7 @@ var sampleSubjectFactory = []infrav1.UserAssignedIdentity{
},
}

var expectedVMSDKObject = map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue{
var expectedVMSDKObject = map[string]*armcompute.UserAssignedIdentitiesValue{
"/foo": {},
"/bar": {},
"/without/prefix": {},
Expand All @@ -54,27 +54,27 @@ func Test_VMIdentityToVMSDK(t *testing.T) {
Name string
identityType infrav1.VMIdentity
uami []infrav1.UserAssignedIdentity
Expect func(*GomegaWithT, *compute.VirtualMachineIdentity, error)
Expect func(*GomegaWithT, *armcompute.VirtualMachineIdentity, error)
}{
{
Name: "Should return a system assigned identity when identity is system assigned",
identityType: infrav1.VMIdentitySystemAssigned,
Expect: func(g *GomegaWithT, m *compute.VirtualMachineIdentity, err error) {
Expect: func(g *GomegaWithT, m *armcompute.VirtualMachineIdentity, err error) {
g.Expect(err).Should(BeNil())
g.Expect(m).Should(Equal(&compute.VirtualMachineIdentity{
Type: compute.ResourceIdentityTypeSystemAssigned,
g.Expect(m).Should(Equal(&armcompute.VirtualMachineIdentity{
Type: ptr.To(armcompute.ResourceIdentityTypeSystemAssigned),
}))
},
},
{
Name: "Should return user assigned identities when identity is user assigned",
identityType: infrav1.VMIdentityUserAssigned,
uami: []infrav1.UserAssignedIdentity{{ProviderID: "my-uami-1"}, {ProviderID: "my-uami-2"}},
Expect: func(g *GomegaWithT, m *compute.VirtualMachineIdentity, err error) {
Expect: func(g *GomegaWithT, m *armcompute.VirtualMachineIdentity, err error) {
g.Expect(err).Should(BeNil())
g.Expect(m).Should(Equal(&compute.VirtualMachineIdentity{
Type: compute.ResourceIdentityTypeUserAssigned,
UserAssignedIdentities: map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue{
g.Expect(m).Should(Equal(&armcompute.VirtualMachineIdentity{
Type: ptr.To(armcompute.ResourceIdentityTypeUserAssigned),
UserAssignedIdentities: map[string]*armcompute.UserAssignedIdentitiesValue{
"my-uami-1": {},
"my-uami-2": {},
},
Expand All @@ -85,14 +85,14 @@ func Test_VMIdentityToVMSDK(t *testing.T) {
Name: "Should fail when no user assigned identities are specified and identity is user assigned",
identityType: infrav1.VMIdentityUserAssigned,
uami: []infrav1.UserAssignedIdentity{},
Expect: func(g *GomegaWithT, m *compute.VirtualMachineIdentity, err error) {
Expect: func(g *GomegaWithT, m *armcompute.VirtualMachineIdentity, err error) {
g.Expect(err.Error()).Should(ContainSubstring(ErrUserAssignedIdentitiesNotFound.Error()))
},
},
{
Name: "Should return nil if no known identity is specified",
identityType: "",
Expect: func(g *GomegaWithT, m *compute.VirtualMachineIdentity, err error) {
Expect: func(g *GomegaWithT, m *armcompute.VirtualMachineIdentity, err error) {
g.Expect(err).Should(BeNil())
g.Expect(m).Should(BeNil())
},
Expand All @@ -114,12 +114,12 @@ func Test_UserAssignedIdentitiesToVMSDK(t *testing.T) {
cases := []struct {
Name string
SubjectFactory []infrav1.UserAssignedIdentity
Expect func(*GomegaWithT, map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue, error)
Expect func(*GomegaWithT, map[string]*armcompute.UserAssignedIdentitiesValue, error)
}{
{
Name: "ShouldPopulateWithData",
SubjectFactory: sampleSubjectFactory,
Expect: func(g *GomegaWithT, m map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue, err error) {
Expect: func(g *GomegaWithT, m map[string]*armcompute.UserAssignedIdentitiesValue, err error) {
g.Expect(err).Should(BeNil())
g.Expect(m).Should(Equal(expectedVMSDKObject))
},
Expand All @@ -128,7 +128,7 @@ func Test_UserAssignedIdentitiesToVMSDK(t *testing.T) {
{
Name: "ShouldFailWithError",
SubjectFactory: []infrav1.UserAssignedIdentity{},
Expect: func(g *GomegaWithT, m map[string]*compute.VirtualMachineIdentityUserAssignedIdentitiesValue, err error) {
Expect: func(g *GomegaWithT, m map[string]*armcompute.UserAssignedIdentitiesValue, err error) {
g.Expect(err).Should(Equal(ErrUserAssignedIdentitiesNotFound))
},
},
Expand Down
Loading

0 comments on commit af1be1c

Please sign in to comment.