Skip to content

Switch to new Azure SDK #729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,6 @@ announce:
milestones:
- close: true

snapshot:
name_template: "edge"

snapcrafts:
- summary: NGINX Plus Integration with Cloud Autoscaling
description: |
Expand Down
27 changes: 13 additions & 14 deletions LICENSES
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
github.com/Azure/azure-sdk-for-go,https://github.com/Azure/azure-sdk-for-go/blob/v68.0.0/LICENSE.txt,MIT
github.com/Azure/go-autorest/autorest,https://github.com/Azure/go-autorest/blob/autorest/v0.11.28/autorest/LICENSE,Apache-2.0
github.com/Azure/go-autorest/autorest/adal,https://github.com/Azure/go-autorest/blob/autorest/adal/v0.9.22/autorest/adal/LICENSE,Apache-2.0
github.com/Azure/go-autorest/autorest/azure/auth,https://github.com/Azure/go-autorest/blob/autorest/azure/auth/v0.5.13/autorest/azure/auth/LICENSE,Apache-2.0
github.com/Azure/go-autorest/autorest/azure/cli,https://github.com/Azure/go-autorest/blob/autorest/azure/cli/v0.4.6/autorest/azure/cli/LICENSE,Apache-2.0
github.com/Azure/go-autorest/autorest/date,https://github.com/Azure/go-autorest/blob/autorest/date/v0.3.0/autorest/date/LICENSE,Apache-2.0
github.com/Azure/go-autorest/autorest/to,https://github.com/Azure/go-autorest/blob/autorest/to/v0.4.0/autorest/to/LICENSE,Apache-2.0
github.com/Azure/go-autorest/autorest/validation,https://github.com/Azure/go-autorest/blob/autorest/validation/v0.3.1/autorest/validation/LICENSE,Apache-2.0
github.com/Azure/go-autorest/logger,https://github.com/Azure/go-autorest/blob/logger/v0.2.1/logger/LICENSE,Apache-2.0
github.com/Azure/go-autorest/tracing,https://github.com/Azure/go-autorest/blob/tracing/v0.6.0/tracing/LICENSE,Apache-2.0
github.com/Azure/azure-sdk-for-go/sdk/azcore,https://github.com/Azure/azure-sdk-for-go/blob/sdk/azcore/v1.13.0/sdk/azcore/LICENSE.txt,MIT
github.com/Azure/azure-sdk-for-go/sdk/azidentity,https://github.com/Azure/azure-sdk-for-go/blob/sdk/azidentity/v1.7.0/sdk/azidentity/LICENSE.txt,MIT
github.com/Azure/azure-sdk-for-go/sdk/internal,https://github.com/Azure/azure-sdk-for-go/blob/sdk/internal/v1.10.0/sdk/internal/LICENSE.txt,MIT
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6,https://github.com/Azure/azure-sdk-for-go/blob/sdk/resourcemanager/compute/armcompute/v6.0.0/sdk/resourcemanager/compute/armcompute/LICENSE.txt,MIT
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6,https://github.com/Azure/azure-sdk-for-go/blob/sdk/resourcemanager/network/armnetwork/v6.0.0/sdk/resourcemanager/network/armnetwork/LICENSE.txt,MIT
github.com/AzureAD/microsoft-authentication-library-for-go/apps,https://github.com/AzureAD/microsoft-authentication-library-for-go/blob/v1.2.2/LICENSE,MIT
github.com/aws/aws-sdk-go-v2,https://github.com/aws/aws-sdk-go-v2/blob/v1.30.4/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/config,https://github.com/aws/aws-sdk-go-v2/blob/config/v1.27.28/config/LICENSE.txt,Apache-2.0
github.com/aws/aws-sdk-go-v2/credentials,https://github.com/aws/aws-sdk-go-v2/blob/credentials/v1.17.28/credentials/LICENSE.txt,Apache-2.0
Expand All @@ -25,11 +21,14 @@ github.com/aws/aws-sdk-go-v2/service/ssooidc,https://github.com/aws/aws-sdk-go-v
github.com/aws/aws-sdk-go-v2/service/sts,https://github.com/aws/aws-sdk-go-v2/blob/service/sts/v1.30.4/service/sts/LICENSE.txt,Apache-2.0
github.com/aws/smithy-go,https://github.com/aws/smithy-go/blob/v1.20.4/LICENSE,Apache-2.0
github.com/aws/smithy-go/internal/sync/singleflight,https://github.com/aws/smithy-go/blob/v1.20.4/internal/sync/singleflight/LICENSE,BSD-3-Clause
github.com/dimchansky/utfbom,https://github.com/dimchansky/utfbom/blob/v1.1.1/LICENSE,Apache-2.0
github.com/golang-jwt/jwt/v4,https://github.com/golang-jwt/jwt/blob/v4.5.0/LICENSE,MIT
github.com/golang-jwt/jwt/v5,https://github.com/golang-jwt/jwt/blob/v5.2.1/LICENSE,MIT
github.com/google/uuid,https://github.com/google/uuid/blob/v1.6.0/LICENSE,BSD-3-Clause
github.com/jmespath/go-jmespath,https://github.com/jmespath/go-jmespath/blob/v0.4.0/LICENSE,Apache-2.0
github.com/mitchellh/go-homedir,https://github.com/mitchellh/go-homedir/blob/v1.1.0/LICENSE,MIT
github.com/kylelemons/godebug,https://github.com/kylelemons/godebug/blob/v1.1.0/LICENSE,Apache-2.0
github.com/nginxinc/nginx-asg-sync/cmd/sync,https://github.com/nginxinc/nginx-asg-sync/blob/HEAD/LICENSE,BSD-2-Clause
github.com/nginxinc/nginx-plus-go-client/client,https://github.com/nginxinc/nginx-plus-go-client/blob/v1.3.0/LICENSE,Apache-2.0
golang.org/x/crypto/pkcs12,https://cs.opensource.google/go/x/crypto/+/v0.17.0:LICENSE,BSD-3-Clause
github.com/pkg/browser,https://github.com/pkg/browser/blob/5ac0b6a4141c/LICENSE,BSD-2-Clause
golang.org/x/crypto/pkcs12,https://cs.opensource.google/go/x/crypto/+/v0.25.0:LICENSE,BSD-3-Clause
golang.org/x/net,https://cs.opensource.google/go/x/net/+/v0.27.0:LICENSE,BSD-3-Clause
golang.org/x/text,https://cs.opensource.google/go/x/text/+/v0.16.0:LICENSE,BSD-3-Clause
gopkg.in/yaml.v3,https://github.com/go-yaml/yaml/blob/v3.0.1/LICENSE,MIT
83 changes: 50 additions & 33 deletions cmd/sync/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ import (
"errors"
"fmt"

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

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

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

func (client *AzureClient) listScaleSetsNetworkInterfaces(ctx context.Context, resourceGroupName, vmssName string) ([]*armnetwork.Interface, error) {
var result []*armnetwork.Interface
pager := client.iFaceClient.NewListVirtualMachineScaleSetNetworkInterfacesPager(resourceGroupName, vmssName, nil)
for pager.More() {
resp, err := pager.NextPage(ctx)
if err != nil {
return nil, fmt.Errorf("listing network interfaces: %w", err)
}
result = append(result, resp.Value...)
}
return result, nil
}

// GetPrivateIPsForScalingGroup returns the list of IP addresses of instances of the Virtual Machine Scale Set.
func (client *AzureClient) GetPrivateIPsForScalingGroup(name string) ([]string, error) {
var ips []string

ctx := context.TODO()

for iFaces, err := client.iFaceClient.ListVirtualMachineScaleSetNetworkInterfaces(ctx, client.config.ResourceGroupName, name); iFaces.NotDone() || err != nil; err = iFaces.NextWithContext(ctx) {
if err != nil {
return nil, fmt.Errorf("couldn't get the list of network interfaces: %w", err)
}
iFaces, err := client.listScaleSetsNetworkInterfaces(ctx, client.config.ResourceGroupName, name)
if err != nil {
return nil, err
}

for _, iFace := range iFaces.Values() {
if iFace.VirtualMachine != nil && iFace.VirtualMachine.ID != nil && iFace.IPConfigurations != nil {
for _, n := range *iFace.IPConfigurations {
ip := getPrimaryIPFromInterfaceIPConfiguration(n)
if ip != "" {
ips = append(ips, *n.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress)
break
}
for _, iFace := range iFaces {
if iFace.Properties.VirtualMachine != nil && iFace.Properties.VirtualMachine.ID != nil && iFace.Properties.IPConfigurations != nil {
for _, n := range iFace.Properties.IPConfigurations {
ip := getPrimaryIPFromInterfaceIPConfiguration(n)
if ip != "" {
ips = append(ips, *n.Properties.PrivateIPAddress)
break
}
}
}
}

return ips, nil
}

func getPrimaryIPFromInterfaceIPConfiguration(ipConfig network.InterfaceIPConfiguration) string {
if ipConfig == (network.InterfaceIPConfiguration{}) {
func getPrimaryIPFromInterfaceIPConfiguration(ipConfig *armnetwork.InterfaceIPConfiguration) string {
if ipConfig.Properties == nil {
return ""
}

if ipConfig.Primary == nil {
if ipConfig.Properties.Primary == nil {
return ""
}

if !*ipConfig.Primary {
if !*ipConfig.Properties.Primary {
return ""
}

if ipConfig.InterfaceIPConfigurationPropertiesFormat == nil {
if ipConfig.Properties.PrivateIPAddress == nil {
return ""
}

if ipConfig.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress == nil {
return ""
}

return *ipConfig.InterfaceIPConfigurationPropertiesFormat.PrivateIPAddress
return *ipConfig.Properties.PrivateIPAddress
}

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

func (client *AzureClient) configure() error {
authorizer, err := auth.NewAuthorizerFromEnvironment()
cred, err := azidentity.NewDefaultAzureCredential(nil)
if err != nil {
return fmt.Errorf("couldn't create authorizer: %w", err)
}

client.vMSSClient = compute.NewVirtualMachineScaleSetsClient(client.config.SubscriptionID)
client.vMSSClient.Authorizer = authorizer
computeClientFactory, err := armcompute.NewClientFactory(client.config.SubscriptionID, cred, nil)
if err != nil {
return fmt.Errorf("couldn't create client factory: %w", err)
}
client.vMSSClient = computeClientFactory.NewVirtualMachineScaleSetsClient()

iclient, err := armnetwork.NewInterfacesClient(client.config.SubscriptionID, cred, nil)
if err != nil {
return fmt.Errorf("couldn't create interfaces client: %w", err)
}
client.iFaceClient = iclient

client.iFaceClient = network.NewInterfacesClient(client.config.SubscriptionID)
client.iFaceClient.Authorizer = authorizer
return nil
}

Expand Down
22 changes: 11 additions & 11 deletions cmd/sync/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package main
import (
"testing"

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

type testInputAzure struct {
Expand Down Expand Up @@ -105,8 +105,8 @@ func TestGetPrimaryIPFromInterfaceIPConfiguration(t *testing.T) {
t.Parallel()
primary := true
address := "127.0.0.1"
ipConfig := network.InterfaceIPConfiguration{
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
ipConfig := &network.InterfaceIPConfiguration{
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
Primary: &primary,
PrivateIPAddress: &address,
},
Expand All @@ -122,30 +122,30 @@ func TestGetPrimaryIPFromInterfaceIPConfigurationFail(t *testing.T) {
primaryFalse := false
primaryTrue := true
tests := []struct {
ipConfig network.InterfaceIPConfiguration
ipConfig *network.InterfaceIPConfiguration
msg string
}{
{
ipConfig: network.InterfaceIPConfiguration{},
ipConfig: &network.InterfaceIPConfiguration{},
msg: "empty primary",
},
{
ipConfig: network.InterfaceIPConfiguration{
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
ipConfig: &network.InterfaceIPConfiguration{
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
Primary: &primaryFalse,
},
},
msg: "not primary interface",
},
{
ipConfig: network.InterfaceIPConfiguration{
InterfaceIPConfigurationPropertiesFormat: nil,
ipConfig: &network.InterfaceIPConfiguration{
Properties: nil,
},
msg: "no interface properties",
},
{
ipConfig: network.InterfaceIPConfiguration{
InterfaceIPConfigurationPropertiesFormat: &network.InterfaceIPConfigurationPropertiesFormat{
ipConfig: &network.InterfaceIPConfiguration{
Properties: &network.InterfaceIPConfigurationPropertiesFormat{
Primary: &primaryTrue,
PrivateIPAddress: nil,
},
Expand Down
29 changes: 14 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module github.com/nginxinc/nginx-asg-sync
go 1.22.5

require (
github.com/Azure/azure-sdk-for-go v68.0.0+incompatible
github.com/Azure/go-autorest/autorest/azure/auth v0.5.13
github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.7.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v6 v6.0.0
github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v6 v6.0.0
github.com/aws/aws-sdk-go-v2 v1.30.4
github.com/aws/aws-sdk-go-v2/config v1.27.28
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.12
Expand All @@ -16,15 +17,9 @@ require (
)

require (
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.28 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.22 // indirect
github.com/Azure/go-autorest/autorest/azure/cli v0.4.6 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.3.1 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.13.0 // indirect
github.com/Azure/azure-sdk-for-go/sdk/internal v1.10.0 // indirect
github.com/AzureAD/microsoft-authentication-library-for-go v1.2.2 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.28 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.16 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.16 // indirect
Expand All @@ -35,9 +30,13 @@ require (
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.30.4 // indirect
github.com/aws/smithy-go v1.20.4 // indirect
github.com/dimchansky/utfbom v1.1.1 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
github.com/kylelemons/godebug v1.1.0 // indirect
github.com/pkg/browser v0.0.0-20240102092130-5ac0b6a4141c // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
)
Loading
Loading