Skip to content

Commit

Permalink
Skip chart version upgrades between identical releases (#27)
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov authored Jul 30, 2024
1 parent d7f8252 commit 31c7499
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
6 changes: 4 additions & 2 deletions internal/controller/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (r *UpgradePlanReconciler) updateHelmChart(ctx context.Context, upgradePlan
chart.Annotations = map[string]string{}
}
chart.Annotations[upgrade.PlanAnnotation] = upgradePlan.Name
chart.Annotations[upgrade.ReleaseAnnotation] = upgradePlan.Spec.ReleaseVersion
chart.Spec.ChartContent = ""
chart.Spec.Chart = releaseChart.Name
chart.Spec.Version = releaseChart.Version
Expand All @@ -77,7 +78,7 @@ func (r *UpgradePlanReconciler) updateHelmChart(ctx context.Context, upgradePlan

// Creates a HelmChart resource in order to trigger an upgrade
// using the information from an existing Helm release.
func (r *UpgradePlanReconciler) createHelmChart(ctx context.Context, releaseChart *release.HelmChart, installedChart *helmrelease.Release, upgradePlanName string) error {
func (r *UpgradePlanReconciler) createHelmChart(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan, installedChart *helmrelease.Release, releaseChart *release.HelmChart) error {
backoffLimit := int32(6)
var values []byte

Expand All @@ -99,7 +100,8 @@ func (r *UpgradePlanReconciler) createHelmChart(ctx context.Context, releaseChar
Name: releaseChart.Name,
Namespace: upgrade.ChartNamespace,
Annotations: map[string]string{
upgrade.PlanAnnotation: upgradePlanName,
upgrade.PlanAnnotation: upgradePlan.Name,
upgrade.ReleaseAnnotation: upgradePlan.Spec.ReleaseVersion,
},
},
Spec: helmcattlev1.HelmChartSpec{
Expand Down
14 changes: 12 additions & 2 deletions internal/controller/reconcile_longhorn.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,26 @@ func (r *UpgradePlanReconciler) reconcileLonghorn(ctx context.Context, upgradePl
return ctrl.Result{}, err
}

if helmRelease.Chart.Metadata.Version == longhorn.Version {
setSkippedCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, versionAlreadyInstalledMessage(upgradePlan))
return ctrl.Result{Requeue: true}, nil
}

setInProgressCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, "Longhorn is being upgraded")
return ctrl.Result{}, r.createHelmChart(ctx, longhorn, helmRelease, upgradePlan.Name)
return ctrl.Result{}, r.createHelmChart(ctx, upgradePlan, helmRelease, longhorn)
}

if chart.Spec.Version != longhorn.Version {
setInProgressCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, "Longhorn is being upgraded")

return ctrl.Result{}, r.updateHelmChart(ctx, upgradePlan, chart, longhorn)
}

releaseVersion := chart.Annotations[upgrade.ReleaseAnnotation]
if releaseVersion != upgradePlan.Spec.ReleaseVersion {
setSkippedCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, versionAlreadyInstalledMessage(upgradePlan))
return ctrl.Result{Requeue: true}, nil
}

job := &batchv1.Job{}
if err = r.Get(ctx, types.NamespacedName{Name: chart.Status.JobName, Namespace: upgrade.ChartNamespace}, job); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand Down
14 changes: 12 additions & 2 deletions internal/controller/reconcile_rancher.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,26 @@ func (r *UpgradePlanReconciler) reconcileRancher(ctx context.Context, upgradePla
return ctrl.Result{}, err
}

if helmRelease.Chart.Metadata.Version == rancher.Version {
setSkippedCondition(upgradePlan, lifecyclev1alpha1.RancherUpgradedCondition, versionAlreadyInstalledMessage(upgradePlan))
return ctrl.Result{Requeue: true}, nil
}

setInProgressCondition(upgradePlan, lifecyclev1alpha1.RancherUpgradedCondition, "Rancher is being upgraded")
return ctrl.Result{}, r.createHelmChart(ctx, rancher, helmRelease, upgradePlan.Name)
return ctrl.Result{}, r.createHelmChart(ctx, upgradePlan, helmRelease, rancher)
}

if chart.Spec.Version != rancher.Version {
setInProgressCondition(upgradePlan, lifecyclev1alpha1.RancherUpgradedCondition, "Rancher is being upgraded")

return ctrl.Result{}, r.updateHelmChart(ctx, upgradePlan, chart, rancher)
}

releaseVersion := chart.Annotations[upgrade.ReleaseAnnotation]
if releaseVersion != upgradePlan.Spec.ReleaseVersion {
setSkippedCondition(upgradePlan, lifecyclev1alpha1.RancherUpgradedCondition, versionAlreadyInstalledMessage(upgradePlan))
return ctrl.Result{Requeue: true}, nil
}

job := &batchv1.Job{}
if err = r.Get(ctx, types.NamespacedName{Name: chart.Status.JobName, Namespace: upgrade.ChartNamespace}, job); err != nil {
return ctrl.Result{}, client.IgnoreNotFound(err)
Expand Down
4 changes: 4 additions & 0 deletions internal/controller/upgradeplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,10 @@ func setSkippedCondition(plan *lifecyclev1alpha1.UpgradePlan, conditionType, mes
meta.SetStatusCondition(&plan.Status.Conditions, condition)
}

func versionAlreadyInstalledMessage(plan *lifecyclev1alpha1.UpgradePlan) string {
return fmt.Sprintf("Component version is the same in release %s", plan.Spec.ReleaseVersion)
}

func (r *UpgradePlanReconciler) findUpgradePlanFromJob(ctx context.Context, job client.Object) []reconcile.Request {
jobLabels := job.GetLabels()
chartName, ok := jobLabels[chart.Label]
Expand Down
9 changes: 5 additions & 4 deletions internal/upgrade/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import (
)

const (
planNamespace = "cattle-system"
PlanAnnotation = "lifecycle.suse.com/upgrade-plan"
controlPlaneKey = "control-plane"
workersKey = "workers"
planNamespace = "cattle-system"
PlanAnnotation = "lifecycle.suse.com/upgrade-plan"
ReleaseAnnotation = "lifecycle.suse.com/release"
controlPlaneKey = "control-plane"
workersKey = "workers"

ControlPlaneLabel = "node-role.kubernetes.io/control-plane"
)
Expand Down

0 comments on commit 31c7499

Please sign in to comment.