Skip to content

Commit

Permalink
🌱 Clean up resources on upgrade without k8s change
Browse files Browse the repository at this point in the history
Clean up resources for upgrades that do not trigger the hook server
because they don't upgrade the Kubernetes version
Signed-off-by: janiskemper <[email protected]>
  • Loading branch information
janiskemper committed Jun 3, 2024
1 parent deba9bc commit 3a115a0
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions internal/controller/clusteraddon_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,6 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
return reconcile.Result{}, fmt.Errorf("failed to get cluster addon config path: %w", err)
}

logger := log.FromContext(ctx)

// Check whether current Helm chart has been applied in the workload cluster. If not, then we need to apply the helm chart (again).
// the spec.clusterStack is only set after a Helm chart from a ClusterStack has been applied successfully.
// If it is not set, the Helm chart has never been applied.
Expand Down Expand Up @@ -372,6 +370,28 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
}
}

// create the list of old release objects
oldClusterStackObjectList, err := r.getOldReleaseObjects(ctx, in, clusterAddonConfig, oldRelease)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get old cluster stack object list from helm charts: %w", err)
}

newClusterStackObjectList, err := r.getNewReleaseObjects(ctx, in, clusterAddonConfig)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get new cluster stack object list from helm charts: %w", err)
}

shouldRequeue, err := r.cleanUpResources(ctx, in, oldClusterStackObjectList, newClusterStackObjectList)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to clean up resources: %w", err)
}
if shouldRequeue {
return reconcile.Result{RequeueAfter: 20 * time.Second}, nil
}

// set upgrade annotation once done
clusterAddon.SetStageAnnotations(csov1alpha1.StageUpgraded)

// Helm chart has been applied successfully
conditions.MarkTrue(clusterAddon, csov1alpha1.HelmChartAppliedCondition)

Expand Down Expand Up @@ -404,29 +424,24 @@ func (r *ClusterAddonReconciler) Reconcile(ctx context.Context, req reconcile.Re
}
}

logger.Info("the hook is here", "hook", in.clusterAddon.Spec.Hook)

if clusterAddon.Spec.Hook == "AfterControlPlaneInitialized" || clusterAddon.Spec.Hook == "BeforeClusterUpgrade" {
if clusterAddon.Spec.Hook == "BeforeClusterUpgrade" {
// create the list of old release objects
oldClusterStackObjectList, err := r.getOldReleaseObjects(ctx, in, clusterAddonConfig, oldRelease)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get old cluster stack object list from helm charts: %w", err)
}
logger.Info("here is the old cluster stack object list", "list", oldClusterStackObjectList)

newClusterStackObjectList, err := r.getNewReleaseObjects(ctx, in, clusterAddonConfig)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to get new cluster stack object list from helm charts: %w", err)
}

logger.Info("here in the clean up begins", "currentList", newClusterStackObjectList)
shouldReque, err := r.cleanUpResources(ctx, in, oldClusterStackObjectList, newClusterStackObjectList)
logger.Info("here in the clean up done", "currentList", newClusterStackObjectList, "reque", shouldReque)
shouldRequeue, err := r.cleanUpResources(ctx, in, oldClusterStackObjectList, newClusterStackObjectList)
if err != nil {
return reconcile.Result{}, fmt.Errorf("failed to clean up resources: %w", err)
}
if shouldReque {
if shouldRequeue {
return reconcile.Result{RequeueAfter: 20 * time.Second}, nil
}

Expand Down

0 comments on commit 3a115a0

Please sign in to comment.