Skip to content

Commit

Permalink
Set AzureMachinePool BootstrapSucceeded condition using VMSS extensio…
Browse files Browse the repository at this point in the history
…n script
  • Loading branch information
Cecile Robert-Michon committed Mar 16, 2021
1 parent 75bd223 commit ccd0576
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 29 deletions.
11 changes: 11 additions & 0 deletions azure/scope/machinepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"context"
"encoding/base64"
"fmt"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"
"sigs.k8s.io/cluster-api/util/conditions"

"github.com/Azure/go-autorest/autorest/to"
"github.com/go-logr/logr"
Expand Down Expand Up @@ -259,6 +261,15 @@ func (m *MachinePoolScope) SetFailureReason(v capierrors.MachineStatusError) {
m.AzureMachinePool.Status.FailureReason = &v
}

// SetCondition sets a condition on the AzureMachinePool.
func (m *MachinePoolScope) SetCondition(condition clusterv1.ConditionType, reason string, severity clusterv1.ConditionSeverity, value bool) {
if value {
conditions.MarkTrue(m.AzureMachinePool, condition)
} else {
conditions.MarkFalse(m.AzureMachinePool, condition, reason, severity, "")
}
}

// AdditionalTags merges AdditionalTags from the scope's AzureCluster and AzureMachinePool. If the same key is present in both,
// the value from AzureMachinePool takes precedence.
func (m *MachinePoolScope) AdditionalTags() infrav1.Tags {
Expand Down
2 changes: 1 addition & 1 deletion azure/services/scalesets/scalesets.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func (s *Service) generateExtensions() []compute.VirtualMachineScaleSetExtension
Type: to.StringPtr(extensionSpec.Name),
TypeHandlerVersion: to.StringPtr(extensionSpec.Version),
Settings: nil,
ProtectedSettings: nil,
ProtectedSettings: extensionSpec.ProtectedSettings,
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion azure/services/vmextensions/vmextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package vmextensions

import (
"context"
"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30/compute"
"github.com/Azure/go-autorest/autorest/to"
"github.com/go-logr/logr"
"github.com/pkg/errors"
Expand Down
50 changes: 23 additions & 27 deletions azure/services/vmssextensions/vmssextensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ package vmssextensions

import (
"context"

"github.com/Azure/azure-sdk-for-go/profiles/latest/compute/mgmt/compute"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-30/compute"
"github.com/Azure/go-autorest/autorest/to"
"github.com/go-logr/logr"
"github.com/pkg/errors"
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha4"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha4"

"github.com/go-logr/logr"
"sigs.k8s.io/cluster-api-provider-azure/azure"
"sigs.k8s.io/cluster-api-provider-azure/util/tele"
)
Expand All @@ -33,6 +34,7 @@ type VMSSExtensionScope interface {
logr.Logger
azure.ClusterDescriber
VMSSExtensionSpecs() []azure.VMSSExtensionSpec
SetCondition(clusterv1.ConditionType, string, clusterv1.ConditionSeverity, bool)
}

// Service provides operations on azure resources
Expand All @@ -55,32 +57,26 @@ func (s *Service) Reconcile(ctx context.Context) error {
defer span.End()

for _, extensionSpec := range s.Scope.VMSSExtensionSpecs() {
if _, err := s.client.Get(ctx, s.Scope.ResourceGroup(), extensionSpec.ScaleSetName, extensionSpec.Name); err == nil {
// check for the extension and don't update if already exists
// TODO: set conditions based on extension status
continue
if existing, err := s.client.Get(ctx, s.Scope.ResourceGroup(), extensionSpec.ScaleSetName, extensionSpec.Name); err == nil {
// check the extension status and set the associated conditions.
switch compute.ProvisioningState(to.String(existing.ProvisioningState)) {
case compute.ProvisioningStateSucceeded:
s.Scope.V(4).Info("extension provisioning state is succeeded", "vm extension", extensionSpec.Name, "scale set", extensionSpec.ScaleSetName)
s.Scope.SetCondition(infrav1.BootstrapSucceededCondition, "", "", true)
case compute.ProvisioningStateCreating:
s.Scope.V(4).Info("extension provisioning state is creating", "vm extension", extensionSpec.Name, "scale set", extensionSpec.ScaleSetName)
s.Scope.SetCondition(infrav1.BootstrapSucceededCondition, infrav1.BootstrapInProgressReason, clusterv1.ConditionSeverityInfo, false)
return errors.New("extension still provisioning")
case compute.ProvisioningStateFailed:
s.Scope.V(4).Info("extension provisioning state is failed", "vm extension", extensionSpec.Name, "scale set", extensionSpec.ScaleSetName)
s.Scope.SetCondition(infrav1.BootstrapSucceededCondition, infrav1.BootstrapFailedReason, clusterv1.ConditionSeverityError, false)
return errors.New("extension state failed")
}
} else if !azure.ResourceNotFound(err) {
return errors.Wrapf(err, "failed to get vm extension %s on scale set %s", extensionSpec.Name, extensionSpec.ScaleSetName)
}

s.Scope.V(2).Info("creating VMSS extension", "vssm extension", extensionSpec.Name)
err := s.client.CreateOrUpdate(
ctx,
s.Scope.ResourceGroup(),
extensionSpec.ScaleSetName,
extensionSpec.Name,
compute.VirtualMachineScaleSetExtension{
VirtualMachineScaleSetExtensionProperties: &compute.VirtualMachineScaleSetExtensionProperties{
Publisher: to.StringPtr(extensionSpec.Publisher),
Type: to.StringPtr(extensionSpec.Name),
TypeHandlerVersion: to.StringPtr(extensionSpec.Version),
Settings: nil,
ProtectedSettings: nil,
},
},
)
if err != nil {
return errors.Wrapf(err, "failed to create VMSS extension %s on scale set %s in resource group %s", extensionSpec.Name, extensionSpec.ScaleSetName, s.Scope.ResourceGroup())
}
s.Scope.V(2).Info("successfully created VMSS extension", "vm extension", extensionSpec.Name)
// Nothing else to do here, the extensions are applied to the model as part of the scale set Reconcile.
}
return nil
}
Expand Down

0 comments on commit ccd0576

Please sign in to comment.