@@ -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
1515type 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
5669func (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
106115func (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
116126func (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
0 commit comments