Skip to content

Commit 3f10b4f

Browse files
committed
Switch to new Azure SDK
1 parent 13474fa commit 3f10b4f

File tree

4 files changed

+121
-141
lines changed

4 files changed

+121
-141
lines changed

cmd/sync/azure.go

+50-33
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ import (
55
"errors"
66
"fmt"
77

8-
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
9-
"github.com/Azure/azure-sdk-for-go/profiles/latest/network/mgmt/network"
10-
"github.com/Azure/go-autorest/autorest/azure/auth"
8+
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
9+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6"
10+
"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6"
1111
yaml "gopkg.in/yaml.v3"
1212
)
1313

1414
// AzureClient allows you to get the list of IP addresses of VirtualMachines of a VirtualMachine Scale Set. It implements the CloudProvider interface.
1515
type AzureClient struct {
1616
config *azureConfig
17-
vMSSClient compute.VirtualMachineScaleSetsClient
18-
iFaceClient network.InterfacesClient
17+
vMSSClient *armcompute.VirtualMachineScaleSetsClient
18+
iFaceClient *armnetwork.InterfacesClient
1919
}
2020

2121
// NewAzureClient creates an AzureClient.
@@ -52,60 +52,70 @@ func parseAzureConfig(data []byte) (*azureConfig, error) {
5252
return cfg, nil
5353
}
5454

55+
func (client *AzureClient) listScaleSetsNetworkInterfaces(ctx context.Context, resourceGroupName, vmssName string) ([]*armnetwork.Interface, error) {
56+
var result []*armnetwork.Interface
57+
pager := client.iFaceClient.NewListVirtualMachineScaleSetNetworkInterfacesPager(resourceGroupName, vmssName, nil)
58+
for pager.More() {
59+
resp, err := pager.NextPage(ctx)
60+
if err != nil {
61+
return nil, fmt.Errorf("listing network interfaces: %w", err)
62+
}
63+
result = append(result, resp.Value...)
64+
}
65+
return result, nil
66+
}
67+
5568
// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Virtual Machine Scale Set.
5669
func (client *AzureClient) GetPrivateIPsForScalingGroup(name string) ([]string, error) {
5770
var ips []string
5871

5972
ctx := context.TODO()
6073

61-
for iFaces, err := client.iFaceClient.ListVirtualMachineScaleSetNetworkInterfaces(ctx, client.config.ResourceGroupName, name); iFaces.NotDone() || err != nil; err = iFaces.NextWithContext(ctx) {
62-
if err != nil {
63-
return nil, fmt.Errorf("couldn't get the list of network interfaces: %w", err)
64-
}
74+
iFaces, err := client.listScaleSetsNetworkInterfaces(ctx, client.config.ResourceGroupName, name)
75+
if err != nil {
76+
return nil, err
77+
}
6578

66-
for _, iFace := range iFaces.Values() {
67-
if iFace.VirtualMachine != nil && iFace.VirtualMachine.ID != nil && iFace.IPConfigurations != nil {
68-
for _, n := range *iFace.IPConfigurations {
69-
ip := getPrimaryIPFromInterfaceIPConfiguration(n)
70-
if ip != "" {
71-
ips = append(ips, *n.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress)
72-
break
73-
}
79+
for _, iFace := range iFaces {
80+
if iFace.Properties.VirtualMachine != nil && iFace.Properties.VirtualMachine.ID != nil && iFace.Properties.IPConfigurations != nil {
81+
for _, n := range iFace.Properties.IPConfigurations {
82+
ip := getPrimaryIPFromInterfaceIPConfiguration(n)
83+
if ip != "" {
84+
ips = append(ips, *n.Properties.PrivateIPAddress)
85+
break
7486
}
7587
}
7688
}
7789
}
90+
7891
return ips, nil
7992
}
8093

81-
func getPrimaryIPFromInterfaceIPConfiguration(ipConfig network.InterfaceIPConfiguration) string {
82-
if ipConfig == (network.InterfaceIPConfiguration{}) {
94+
func getPrimaryIPFromInterfaceIPConfiguration(ipConfig *armnetwork.InterfaceIPConfiguration) string {
95+
if ipConfig.Properties == nil {
8396
return ""
8497
}
8598

86-
if ipConfig.Primary == nil {
99+
if ipConfig.Properties.Primary == nil {
87100
return ""
88101
}
89102

90-
if !*ipConfig.Primary {
103+
if !*ipConfig.Properties.Primary {
91104
return ""
92105
}
93106

94-
if ipConfig.InterfaceIPConfigurationPropertiesFormat == nil {
107+
if ipConfig.Properties.PrivateIPAddress == nil {
95108
return ""
96109
}
97110

98-
if ipConfig.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress == nil {
99-
return ""
100-
}
101-
102-
return *ipConfig.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress
111+
return *ipConfig.Properties.PrivateIPAddress
103112
}
104113

105114
// CheckIfScalingGroupExists checks if the Virtual Machine Scale Set exists.
106115
func (client *AzureClient) CheckIfScalingGroupExists(name string) (bool, error) {
107116
ctx := context.TODO()
108-
vmss, err := client.vMSSClient.Get(ctx, client.config.ResourceGroupName, name, "userData")
117+
expandType := armcompute.ExpandTypesForGetVMScaleSetsUserData
118+
vmss, err := client.vMSSClient.Get(ctx, client.config.ResourceGroupName, name, &armcompute.VirtualMachineScaleSetsClientGetOptions{Expand: &expandType})
109119
if err != nil {
110120
return false, fmt.Errorf("couldn't check if a Virtual Machine Scale Set exists: %w", err)
111121
}
@@ -114,16 +124,23 @@ func (client *AzureClient) CheckIfScalingGroupExists(name string) (bool, error)
114124
}
115125

116126
func (client *AzureClient) configure() error {
117-
authorizer, err := auth.NewAuthorizerFromEnvironment()
127+
cred, err := azidentity.NewDefaultAzureCredential(nil)
118128
if err != nil {
119129
return fmt.Errorf("couldn't create authorizer: %w", err)
120130
}
121131

122-
client.vMSSClient = compute.NewVirtualMachineScaleSetsClient(client.config.SubscriptionID)
123-
client.vMSSClient.Authorizer = authorizer
132+
computeClientFactory, err := armcompute.NewClientFactory(client.config.SubscriptionID, cred, nil)
133+
if err != nil {
134+
return fmt.Errorf("couldn't create client factory: %w", err)
135+
}
136+
client.vMSSClient = computeClientFactory.NewVirtualMachineScaleSetsClient()
137+
138+
iclient, err := armnetwork.NewInterfacesClient(client.config.SubscriptionID, cred, nil)
139+
if err != nil {
140+
return fmt.Errorf("couldn't create interfaces client: %w", err)
141+
}
142+
client.iFaceClient = iclient
124143

125-
client.iFaceClient = network.NewInterfacesClient(client.config.SubscriptionID)
126-
client.iFaceClient.Authorizer = authorizer
127144
return nil
128145
}
129146

cmd/sync/azure_test.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package main
33
import (
44
"testing"
55

6-
"github.com/Azure/azure-sdk-for-go/profiles/latest/network/mgmt/network"
6+
network "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6"
77
)
88

99
type testInputAzure struct {
@@ -105,8 +105,8 @@ func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
105105
t.Parallel()
106106
primary := true
107107
address := "127.0.0.1"
108-
ipConfig := network.InterfaceIPConfiguration{
109-
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
108+
ipConfig := &network.InterfaceIPConfiguration{
109+
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
110110
Primary: &primary,
111111
PrivateIPAddress: &address,
112112
},
@@ -122,30 +122,30 @@ func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
122122
primaryFalse := false
123123
primaryTrue := true
124124
tests := []struct {
125-
ipConfig network.InterfaceIPConfiguration
125+
ipConfig *network.InterfaceIPConfiguration
126126
msg string
127127
}{
128128
{
129-
ipConfig: network.InterfaceIPConfiguration{},
129+
ipConfig: &network.InterfaceIPConfiguration{},
130130
msg: "empty primary",
131131
},
132132
{
133-
ipConfig: network.InterfaceIPConfiguration{
134-
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
133+
ipConfig: &network.InterfaceIPConfiguration{
134+
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
135135
Primary: &primaryFalse,
136136
},
137137
},
138138
msg: "not primary interface",
139139
},
140140
{
141-
ipConfig: network.InterfaceIPConfiguration{
142-
InterfaceIPConfigurationPropertiesFormat: nil,
141+
ipConfig: &network.InterfaceIPConfiguration{
142+
Properties: nil,
143143
},
144144
msg: "no interface properties",
145145
},
146146
{
147-
ipConfig: network.InterfaceIPConfiguration{
148-
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
147+
ipConfig: &network.InterfaceIPConfiguration{
148+
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
149149
Primary: &primaryTrue,
150150
PrivateIPAddress: nil,
151151
},

go.mod

+15-16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module github.com/nginxinc/nginx-asg-sync
22

3-
go 1.22.5
3+
go 1.23.0
44

55
require (
6-
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
7-
github.com/Azure/go-autorest/autorest/azure/auth v0.5.13
6+
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0
7+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6 v6.0.0
8+
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6 v6.0.0
89
github.com/aws/aws-sdk-go-v2 v1.30.4
910
github.com/aws/aws-sdk-go-v2/config v1.27.28
1011
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.12
@@ -15,15 +16,9 @@ require (
1516
)
1617

1718
require (
18-
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
19-
github.com/Azure/go-autorest/autorest v0.11.28 // indirect
20-
github.com/Azure/go-autorest/autorest/adal v0.9.22 // indirect
21-
github.com/Azure/go-autorest/autorest/azure/cli v0.4.6 // indirect
22-
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
23-
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
24-
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
25-
github.com/Azure/go-autorest/logger v0.2.1 // indirect
26-
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
19+
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0 // indirect
20+
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
21+
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
2722
github.com/aws/aws-sdk-go-v2/credentials v1.17.28 // indirect
2823
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect
2924
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect
@@ -34,9 +29,13 @@ require (
3429
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.5 // indirect
3530
github.com/aws/aws-sdk-go-v2/service/sts v1.30.4 // indirect
3631
github.com/aws/smithy-go v1.20.4 // indirect
37-
github.com/dimchansky/utfbom v1.1.1 // indirect
38-
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
32+
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
33+
github.com/google/uuid v1.6.0 // indirect
3934
github.com/jmespath/go-jmespath v0.4.0 // indirect
40-
github.com/mitchellh/go-homedir v1.1.0 // indirect
41-
golang.org/x/crypto v0.17.0 // indirect
35+
github.com/kylelemons/godebug v1.1.0 // indirect
36+
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
37+
golang.org/x/crypto v0.25.0 // indirect
38+
golang.org/x/net v0.27.0 // indirect
39+
golang.org/x/sys v0.22.0 // indirect
40+
golang.org/x/text v0.16.0 // indirect
4241
)

0 commit comments

Comments
 (0)