Skip to content

Commit

Permalink
Merge pull request #576 from giantswarm/cherry-pick-bugfixes
Browse files Browse the repository at this point in the history
Cherry-pick bugfixes into v2.3.x release branch
  • Loading branch information
AndiDog authored Dec 14, 2023
2 parents c258494 + 943a18e commit 3784406
Show file tree
Hide file tree
Showing 16 changed files with 655 additions and 169 deletions.
5 changes: 5 additions & 0 deletions api/v1beta2/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,11 @@ const (

// MachineNameTagKey is the key for machine name.
MachineNameTagKey = "MachineName"

// LaunchTemplateBootstrapDataSecret is the tag we use to store the `<namespace>/<name>`
// of the bootstrap secret that was used to create the user data for the latest launch
// template version.
LaunchTemplateBootstrapDataSecret = NameAWSProviderPrefix + "bootstrap-data-secret"
)

// ClusterTagKey generates the key for resources associated with a cluster.
Expand Down
42 changes: 31 additions & 11 deletions exp/controllers/awsmachinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type AWSMachinePoolReconciler struct {
WatchFilterValue string
asgServiceFactory func(cloud.ClusterScoper) services.ASGInterface
ec2ServiceFactory func(scope.EC2Scope) services.EC2Interface
reconcileServiceFactory func(scope.EC2Scope) services.MachinePoolReconcileInterface
TagUnmanagedNetworkResources bool
}

Expand All @@ -78,6 +79,14 @@ func (r *AWSMachinePoolReconciler) getEC2Service(scope scope.EC2Scope) services.
return ec2.NewService(scope)
}

func (r *AWSMachinePoolReconciler) getReconcileService(scope scope.EC2Scope) services.MachinePoolReconcileInterface {
if r.reconcileServiceFactory != nil {
return r.reconcileServiceFactory(scope)
}

return ec2.NewService(scope)
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsmachinepools,verbs=get;list;watch;update;patch;delete
// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsmachinepools/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=machinepools;machinepools/status,verbs=get;list;watch;patch
Expand Down Expand Up @@ -225,14 +234,32 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, machineP

ec2Svc := r.getEC2Service(ec2Scope)
asgsvc := r.getASGService(clusterScope)
reconSvc := r.getReconcileService(ec2Scope)

// Find existing ASG
asg, err := r.findASG(machinePoolScope, asgsvc)
if err != nil {
conditions.MarkUnknown(machinePoolScope.AWSMachinePool, expinfrav1.ASGReadyCondition, expinfrav1.ASGNotFoundReason, err.Error())
return err
}

canUpdateLaunchTemplate := func() (bool, error) {
// If there is a change: before changing the template, check if there exist an ongoing instance refresh,
// because only 1 instance refresh can be "InProgress". If template is updated when refresh cannot be started,
// that change will not trigger a refresh. Do not start an instance refresh if only userdata changed.
if asg == nil {
// If the ASG hasn't been created yet, there is no need to check if we can start the instance refresh.
// But we want to update the LaunchTemplate because an error in the LaunchTemplate may be blocking the ASG creation.
return true, nil
}
return asgsvc.CanStartASGInstanceRefresh(machinePoolScope)
}
runPostLaunchTemplateUpdateOperation := func() error {
// skip instance refresh if ASG is not created yet
if asg == nil {
machinePoolScope.Debug("ASG does not exist yet, skipping instance refresh")
return nil
}
// skip instance refresh if explicitly disabled
if machinePoolScope.AWSMachinePool.Spec.RefreshPreferences != nil && machinePoolScope.AWSMachinePool.Spec.RefreshPreferences.Disable {
machinePoolScope.Debug("instance refresh disabled, skipping instance refresh")
Expand All @@ -250,7 +277,7 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, machineP
machinePoolScope.Info("starting instance refresh", "number of instances", machinePoolScope.MachinePool.Spec.Replicas)
return asgsvc.StartASGInstanceRefresh(machinePoolScope)
}
if err := ec2Svc.ReconcileLaunchTemplate(machinePoolScope, canUpdateLaunchTemplate, runPostLaunchTemplateUpdateOperation); err != nil {
if err := reconSvc.ReconcileLaunchTemplate(machinePoolScope, ec2Svc, canUpdateLaunchTemplate, runPostLaunchTemplateUpdateOperation); err != nil {
r.Recorder.Eventf(machinePoolScope.AWSMachinePool, corev1.EventTypeWarning, "FailedLaunchTemplateReconcile", "Failed to reconcile launch template: %v", err)
machinePoolScope.Error(err, "failed to reconcile launch template")
return err
Expand All @@ -259,13 +286,6 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, machineP
// set the LaunchTemplateReady condition
conditions.MarkTrue(machinePoolScope.AWSMachinePool, expinfrav1.LaunchTemplateReadyCondition)

// Find existing ASG
asg, err := r.findASG(machinePoolScope, asgsvc)
if err != nil {
conditions.MarkUnknown(machinePoolScope.AWSMachinePool, expinfrav1.ASGReadyCondition, expinfrav1.ASGNotFoundReason, err.Error())
return err
}

if asg == nil {
// Create new ASG
if err := r.createPool(machinePoolScope, clusterScope); err != nil {
Expand Down Expand Up @@ -305,7 +325,7 @@ func (r *AWSMachinePoolReconciler) reconcileNormal(ctx context.Context, machineP
ResourceService: asgsvc,
},
}
err = ec2Svc.ReconcileTags(machinePoolScope, resourceServiceToUpdate)
err = reconSvc.ReconcileTags(machinePoolScope, resourceServiceToUpdate)
if err != nil {
return errors.Wrap(err, "error updating tags")
}
Expand Down Expand Up @@ -366,7 +386,7 @@ func (r *AWSMachinePoolReconciler) reconcileDelete(machinePoolScope *scope.Machi
}

launchTemplateID := machinePoolScope.AWSMachinePool.Status.LaunchTemplateID
launchTemplate, _, err := ec2Svc.GetLaunchTemplate(machinePoolScope.LaunchTemplateName())
launchTemplate, _, _, err := ec2Svc.GetLaunchTemplate(machinePoolScope.LaunchTemplateName())
if err != nil {
return err
}
Expand Down Expand Up @@ -410,7 +430,7 @@ func (r *AWSMachinePoolReconciler) updatePool(machinePoolScope *scope.MachinePoo

asgDiff := diffASG(machinePoolScope, existingASG)
if asgDiff != "" {
machinePoolScope.Debug("asg diff detected", "diff", subnetDiff)
machinePoolScope.Debug("asg diff detected", "asgDiff", asgDiff, "subnetDiff", subnetDiff)
}
if asgDiff != "" || subnetDiff != "" {
machinePoolScope.Info("updating AutoScalingGroup")
Expand Down
Loading

0 comments on commit 3784406

Please sign in to comment.