From 67165456b28a010b8774bf4ffa7c9574914367f7 Mon Sep 17 00:00:00 2001 From: Christian Muirhead Date: Tue, 10 Nov 2020 15:50:54 +1300 Subject: [PATCH] Remove GlobalCredentials usage from pkg/resourcemanager/vmss Credentials are now passed in when a client is constructed. --- controllers/suite_test.go | 1 + main.go | 1 + pkg/resourcemanager/vmss/client.go | 22 +++++++++++--------- pkg/resourcemanager/vmss/reconcile.go | 30 +++++++++++++-------------- 4 files changed, 29 insertions(+), 25 deletions(-) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index c5efe1734cf..e40954aa3bf 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -631,6 +631,7 @@ func setup() error { Reconciler: &AsyncReconciler{ Client: k8sManager.GetClient(), AzureClient: vmss.NewAzureVMScaleSetClient( + config.GlobalCredentials(), secretClient, k8sManager.GetScheme(), ), diff --git a/main.go b/main.go index 9ec8dfa85fb..3c531eb2ff2 100644 --- a/main.go +++ b/main.go @@ -865,6 +865,7 @@ func main() { Reconciler: &controllers.AsyncReconciler{ Client: mgr.GetClient(), AzureClient: vmss.NewAzureVMScaleSetClient( + config.GlobalCredentials(), secretClient, mgr.GetScheme(), ), diff --git a/pkg/resourcemanager/vmss/client.go b/pkg/resourcemanager/vmss/client.go index 94aaf36d6fb..e481b05e0a4 100644 --- a/pkg/resourcemanager/vmss/client.go +++ b/pkg/resourcemanager/vmss/client.go @@ -19,28 +19,30 @@ import ( ) type AzureVMScaleSetClient struct { + Creds config.Credentials SecretClient secrets.SecretClient Scheme *runtime.Scheme } -func NewAzureVMScaleSetClient(secretclient secrets.SecretClient, scheme *runtime.Scheme) *AzureVMScaleSetClient { +func NewAzureVMScaleSetClient(creds config.Credentials, secretclient secrets.SecretClient, scheme *runtime.Scheme) *AzureVMScaleSetClient { return &AzureVMScaleSetClient{ + Creds: creds, SecretClient: secretclient, Scheme: scheme, } } -func getVMScaleSetClient() compute.VirtualMachineScaleSetsClient { - computeClient := compute.NewVirtualMachineScaleSetsClientWithBaseURI(config.BaseURI(), config.GlobalCredentials().SubscriptionID()) - a, _ := iam.GetResourceManagementAuthorizer(config.GlobalCredentials()) +func getVMScaleSetClient(creds config.Credentials) compute.VirtualMachineScaleSetsClient { + computeClient := compute.NewVirtualMachineScaleSetsClientWithBaseURI(config.BaseURI(), creds.SubscriptionID()) + a, _ := iam.GetResourceManagementAuthorizer(creds) computeClient.Authorizer = a computeClient.AddToUserAgent(config.UserAgent()) return computeClient } -func (m *AzureVMScaleSetClient) CreateVMScaleSet(ctx context.Context, location string, resourceGroupName string, resourceName string, vmSize string, capacity int64, osType string, adminUserName string, adminPassword string, sshPublicKeyData string, platformImageURN string, vnetName string, subnetName string, loadBalancerName string, backendAddressPoolName string, inboundNatPoolName string) (future compute.VirtualMachineScaleSetsCreateOrUpdateFuture, err error) { +func (c *AzureVMScaleSetClient) CreateVMScaleSet(ctx context.Context, location string, resourceGroupName string, resourceName string, vmSize string, capacity int64, osType string, adminUserName string, adminPassword string, sshPublicKeyData string, platformImageURN string, vnetName string, subnetName string, loadBalancerName string, backendAddressPoolName string, inboundNatPoolName string) (future compute.VirtualMachineScaleSetsCreateOrUpdateFuture, err error) { - client := getVMScaleSetClient() + client := getVMScaleSetClient(c.Creds) // Construct OS Profile provisionVMAgent := true @@ -193,9 +195,9 @@ func (m *AzureVMScaleSetClient) CreateVMScaleSet(ctx context.Context, location s return future, err } -func (m *AzureVMScaleSetClient) DeleteVMScaleSet(ctx context.Context, vmssName string, resourcegroup string) (status string, err error) { +func (c *AzureVMScaleSetClient) DeleteVMScaleSet(ctx context.Context, vmssName string, resourcegroup string) (status string, err error) { - client := getVMScaleSetClient() + client := getVMScaleSetClient(c.Creds) _, err = client.Get(ctx, resourcegroup, vmssName) if err == nil { // vmss present, so go ahead and delete @@ -207,9 +209,9 @@ func (m *AzureVMScaleSetClient) DeleteVMScaleSet(ctx context.Context, vmssName s } -func (m *AzureVMScaleSetClient) GetVMScaleSet(ctx context.Context, resourcegroup string, vmssName string) (vmss compute.VirtualMachineScaleSet, err error) { +func (c *AzureVMScaleSetClient) GetVMScaleSet(ctx context.Context, resourcegroup string, vmssName string) (vmss compute.VirtualMachineScaleSet, err error) { - client := getVMScaleSetClient() + client := getVMScaleSetClient(c.Creds) return client.Get(ctx, resourcegroup, vmssName) } diff --git a/pkg/resourcemanager/vmss/reconcile.go b/pkg/resourcemanager/vmss/reconcile.go index b101268a892..ff5f270109d 100644 --- a/pkg/resourcemanager/vmss/reconcile.go +++ b/pkg/resourcemanager/vmss/reconcile.go @@ -15,14 +15,14 @@ import ( "k8s.io/apimachinery/pkg/types" ) -func (g *AzureVMScaleSetClient) Ensure(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) { +func (c *AzureVMScaleSetClient) Ensure(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) { - instance, err := g.convert(obj) + instance, err := c.convert(obj) if err != nil { return true, err } - client := getVMScaleSetClient() + client := getVMScaleSetClient(c.Creds) location := instance.Spec.Location resourceGroup := instance.Spec.ResourceGroup @@ -40,12 +40,12 @@ func (g *AzureVMScaleSetClient) Ensure(ctx context.Context, obj runtime.Object, natPoolName := instance.Spec.InboundNatPoolName // Check to see if secret exists and if yes retrieve the admin login and password - secret, err := g.GetOrPrepareSecret(ctx, instance) + secret, err := c.GetOrPrepareSecret(ctx, instance) if err != nil { return false, err } // Update secret - err = g.AddVMScaleSetCredsToSecrets(ctx, instance.Name, secret, instance) + err = c.AddVMScaleSetCredsToSecrets(ctx, instance.Name, secret, instance) if err != nil { return false, err } @@ -55,7 +55,7 @@ func (g *AzureVMScaleSetClient) Ensure(ctx context.Context, obj runtime.Object, instance.Status.Provisioning = true // Check if this item already exists. This is required // to overcome the issue with the lack of idempotence of the Create call - item, err := g.GetVMScaleSet(ctx, resourceGroup, resourceName) + item, err := c.GetVMScaleSet(ctx, resourceGroup, resourceName) if err == nil { instance.Status.Provisioned = true instance.Status.Provisioning = false @@ -64,7 +64,7 @@ func (g *AzureVMScaleSetClient) Ensure(ctx context.Context, obj runtime.Object, return true, nil } - future, err := g.CreateVMScaleSet( + future, err := c.CreateVMScaleSet( ctx, location, resourceGroup, @@ -149,9 +149,9 @@ func (g *AzureVMScaleSetClient) Ensure(ctx context.Context, obj runtime.Object, return true, nil } -func (g *AzureVMScaleSetClient) Delete(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) { +func (c *AzureVMScaleSetClient) Delete(ctx context.Context, obj runtime.Object, opts ...resourcemanager.ConfigOption) (bool, error) { - instance, err := g.convert(obj) + instance, err := c.convert(obj) if err != nil { return true, err } @@ -159,7 +159,7 @@ func (g *AzureVMScaleSetClient) Delete(ctx context.Context, obj runtime.Object, resourceGroup := instance.Spec.ResourceGroup resourceName := instance.Name - status, err := g.DeleteVMScaleSet( + status, err := c.DeleteVMScaleSet( ctx, resourceName, resourceGroup, @@ -191,9 +191,9 @@ func (g *AzureVMScaleSetClient) Delete(ctx context.Context, obj runtime.Object, return true, nil } -func (g *AzureVMScaleSetClient) GetParents(obj runtime.Object) ([]resourcemanager.KubeParent, error) { +func (c *AzureVMScaleSetClient) GetParents(obj runtime.Object) ([]resourcemanager.KubeParent, error) { - instance, err := g.convert(obj) + instance, err := c.convert(obj) if err != nil { return nil, err } @@ -209,16 +209,16 @@ func (g *AzureVMScaleSetClient) GetParents(obj runtime.Object) ([]resourcemanage }, nil } -func (g *AzureVMScaleSetClient) GetStatus(obj runtime.Object) (*azurev1alpha1.ASOStatus, error) { +func (c *AzureVMScaleSetClient) GetStatus(obj runtime.Object) (*azurev1alpha1.ASOStatus, error) { - instance, err := g.convert(obj) + instance, err := c.convert(obj) if err != nil { return nil, err } return &instance.Status, nil } -func (g *AzureVMScaleSetClient) convert(obj runtime.Object) (*azurev1alpha1.AzureVMScaleSet, error) { +func (c *AzureVMScaleSetClient) convert(obj runtime.Object) (*azurev1alpha1.AzureVMScaleSet, error) { local, ok := obj.(*azurev1alpha1.AzureVMScaleSet) if !ok { return nil, fmt.Errorf("failed type assertion on kind: %s", obj.GetObjectKind().GroupVersionKind().String())