Skip to content

Commit

Permalink
Remove GlobalCredentials usage from pkg/resourcemanager/vmss
Browse files Browse the repository at this point in the history
Credentials are now passed in when a client is constructed.
  • Loading branch information
babbageclunk committed Nov 10, 2020
1 parent 4ad11fc commit 6716545
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 25 deletions.
1 change: 1 addition & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@ func setup() error {
Reconciler: &AsyncReconciler{
Client: k8sManager.GetClient(),
AzureClient: vmss.NewAzureVMScaleSetClient(
config.GlobalCredentials(),
secretClient,
k8sManager.GetScheme(),
),
Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,7 @@ func main() {
Reconciler: &controllers.AsyncReconciler{
Client: mgr.GetClient(),
AzureClient: vmss.NewAzureVMScaleSetClient(
config.GlobalCredentials(),
secretClient,
mgr.GetScheme(),
),
Expand Down
22 changes: 12 additions & 10 deletions pkg/resourcemanager/vmss/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
30 changes: 15 additions & 15 deletions pkg/resourcemanager/vmss/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -149,17 +149,17 @@ 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
}

resourceGroup := instance.Spec.ResourceGroup
resourceName := instance.Name

status, err := g.DeleteVMScaleSet(
status, err := c.DeleteVMScaleSet(
ctx,
resourceName,
resourceGroup,
Expand Down Expand Up @@ -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
}
Expand All @@ -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())
Expand Down

0 comments on commit 6716545

Please sign in to comment.