@@ -5,17 +5,17 @@ import (
5
5
"errors"
6
6
"fmt"
7
7
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 "
11
11
yaml "gopkg.in/yaml.v3"
12
12
)
13
13
14
14
// AzureClient allows you to get the list of IP addresses of VirtualMachines of a VirtualMachine Scale Set. It implements the CloudProvider interface.
15
15
type AzureClient struct {
16
16
config * azureConfig
17
- vMSSClient compute .VirtualMachineScaleSetsClient
18
- iFaceClient network .InterfacesClient
17
+ vMSSClient * armcompute .VirtualMachineScaleSetsClient
18
+ iFaceClient * armnetwork .InterfacesClient
19
19
}
20
20
21
21
// NewAzureClient creates an AzureClient.
@@ -52,60 +52,70 @@ func parseAzureConfig(data []byte) (*azureConfig, error) {
52
52
return cfg , nil
53
53
}
54
54
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
+
55
68
// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Virtual Machine Scale Set.
56
69
func (client * AzureClient ) GetPrivateIPsForScalingGroup (name string ) ([]string , error ) {
57
70
var ips []string
58
71
59
72
ctx := context .TODO ()
60
73
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
+ }
65
78
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
74
86
}
75
87
}
76
88
}
77
89
}
90
+
78
91
return ips , nil
79
92
}
80
93
81
- func getPrimaryIPFromInterfaceIPConfiguration (ipConfig network .InterfaceIPConfiguration ) string {
82
- if ipConfig == (network. InterfaceIPConfiguration {}) {
94
+ func getPrimaryIPFromInterfaceIPConfiguration (ipConfig * armnetwork .InterfaceIPConfiguration ) string {
95
+ if ipConfig . Properties == nil {
83
96
return ""
84
97
}
85
98
86
- if ipConfig .Primary == nil {
99
+ if ipConfig .Properties . Primary == nil {
87
100
return ""
88
101
}
89
102
90
- if ! * ipConfig .Primary {
103
+ if ! * ipConfig .Properties . Primary {
91
104
return ""
92
105
}
93
106
94
- if ipConfig .InterfaceIPConfigurationPropertiesFormat == nil {
107
+ if ipConfig .Properties . PrivateIPAddress == nil {
95
108
return ""
96
109
}
97
110
98
- if ipConfig .InterfaceIPConfigurationPropertiesFormat .PrivateIPAddress == nil {
99
- return ""
100
- }
101
-
102
- return * ipConfig .InterfaceIPConfigurationPropertiesFormat .PrivateIPAddress
111
+ return * ipConfig .Properties .PrivateIPAddress
103
112
}
104
113
105
114
// CheckIfScalingGroupExists checks if the Virtual Machine Scale Set exists.
106
115
func (client * AzureClient ) CheckIfScalingGroupExists (name string ) (bool , error ) {
107
116
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 })
109
119
if err != nil {
110
120
return false , fmt .Errorf ("couldn't check if a Virtual Machine Scale Set exists: %w" , err )
111
121
}
@@ -114,16 +124,23 @@ func (client *AzureClient) CheckIfScalingGroupExists(name string) (bool, error)
114
124
}
115
125
116
126
func (client * AzureClient ) configure () error {
117
- authorizer , err := auth . NewAuthorizerFromEnvironment ( )
127
+ cred , err := azidentity . NewDefaultAzureCredential ( nil )
118
128
if err != nil {
119
129
return fmt .Errorf ("couldn't create authorizer: %w" , err )
120
130
}
121
131
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
124
143
125
- client .iFaceClient = network .NewInterfacesClient (client .config .SubscriptionID )
126
- client .iFaceClient .Authorizer = authorizer
127
144
return nil
128
145
}
129
146
0 commit comments