Skip to content

Commit be4a1e4

Browse files
committed
Switch to new Azure SDK
1 parent 87b69eb commit be4a1e4

File tree

4 files changed

+126
-144
lines changed

4 files changed

+126
-144
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.v2"
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, 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)
128+
if err != nil {
129+
return err
130+
}
131+
132+
computeClientFactory, err := armcompute.NewClientFactory(client.config.SubscriptionID, cred, nil)
118133
if err != nil {
119134
return err
120135
}
136+
client.vMSSClient = computeClientFactory.NewVirtualMachineScaleSetsClient()
121137

122-
client.vMSSClient = compute.NewVirtualMachineScaleSetsClient(client.config.SubscriptionID)
123-
client.vMSSClient.Authorizer = authorizer
138+
iclient, err := armnetwork.NewInterfacesClient(client.config.SubscriptionID, cred, nil)
139+
if err != nil {
140+
return 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 {
@@ -102,8 +102,8 @@ func TestValidateAzureConfigValid(t *testing.T) {
102102
func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
103103
primary := true
104104
address := "127.0.0.1"
105-
ipConfig := network.InterfaceIPConfiguration{
106-
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
105+
ipConfig := &network.InterfaceIPConfiguration{
106+
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
107107
Primary: &primary,
108108
PrivateIPAddress: &address,
109109
},
@@ -118,30 +118,30 @@ func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
118118
primaryFalse := false
119119
primaryTrue := true
120120
tests := []struct {
121-
ipConfig network.InterfaceIPConfiguration
121+
ipConfig *network.InterfaceIPConfiguration
122122
msg string
123123
}{
124124
{
125-
ipConfig: network.InterfaceIPConfiguration{},
125+
ipConfig: &network.InterfaceIPConfiguration{},
126126
msg: "empty primary",
127127
},
128128
{
129-
ipConfig: network.InterfaceIPConfiguration{
130-
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
129+
ipConfig: &network.InterfaceIPConfiguration{
130+
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
131131
Primary: &primaryFalse,
132132
},
133133
},
134134
msg: "not primary interface",
135135
},
136136
{
137-
ipConfig: network.InterfaceIPConfiguration{
138-
InterfaceIPConfigurationPropertiesFormat: nil,
137+
ipConfig: &network.InterfaceIPConfiguration{
138+
Properties: nil,
139139
},
140140
msg: "no interface properties",
141141
},
142142
{
143-
ipConfig: network.InterfaceIPConfiguration{
144-
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
143+
ipConfig: &network.InterfaceIPConfiguration{
144+
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
145145
Primary: &primaryTrue,
146146
PrivateIPAddress: nil,
147147
},

go.mod

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

3-
go 1.21.3
3+
go 1.22.5
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.3
910
github.com/aws/aws-sdk-go-v2/config v1.27.27
1011
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.11
1112
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.43.3
12-
github.com/aws/aws-sdk-go-v2/service/ec2 v1.171.0
13+
github.com/aws/aws-sdk-go-v2/service/ec2 v1.173.0
1314
github.com/nginxinc/nginx-plus-go-client v1.2.2
1415
gopkg.in/yaml.v2 v2.4.0
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.27 // indirect
2823
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.15 // indirect
2924
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.15 // indirect
@@ -34,9 +29,13 @@ require (
3429
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
3530
github.com/aws/aws-sdk-go-v2/service/sts v1.30.3 // indirect
3631
github.com/aws/smithy-go v1.20.3 // 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)