-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Longhorn definition Signed-off-by: Atanas Dinov <[email protected]> * Reconcile Longhorn upgrades Signed-off-by: Atanas Dinov <[email protected]> * Upgrade Longhorn Signed-off-by: Atanas Dinov <[email protected]> --------- Signed-off-by: Atanas Dinov <[email protected]>
- Loading branch information
1 parent
758afa1
commit d7f8252
Showing
5 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"slices" | ||
|
||
helmcattlev1 "github.com/k3s-io/helm-controller/pkg/apis/helm.cattle.io/v1" | ||
lifecyclev1alpha1 "github.com/suse-edge/upgrade-controller/api/v1alpha1" | ||
"github.com/suse-edge/upgrade-controller/internal/upgrade" | ||
"github.com/suse-edge/upgrade-controller/pkg/release" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func (r *UpgradePlanReconciler) reconcileLonghorn(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan, longhorn *release.HelmChart) (ctrl.Result, error) { | ||
helmRelease, err := retrieveHelmRelease(longhorn.Name) | ||
if err != nil { | ||
return ctrl.Result{}, fmt.Errorf("retrieving helm release: %w", err) | ||
} | ||
|
||
if helmRelease == nil { | ||
setSkippedCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, "Longhorn installation is not found") | ||
return ctrl.Result{Requeue: true}, nil | ||
} | ||
|
||
chart := &helmcattlev1.HelmChart{} | ||
|
||
if err = r.Get(ctx, upgrade.ChartNamespacedName(helmRelease.Name), chart); err != nil { | ||
if !errors.IsNotFound(err) { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
setInProgressCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, "Longhorn is being upgraded") | ||
return ctrl.Result{}, r.createHelmChart(ctx, longhorn, helmRelease, upgradePlan.Name) | ||
} | ||
|
||
if chart.Spec.Version != longhorn.Version { | ||
setInProgressCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, "Longhorn is being upgraded") | ||
|
||
return ctrl.Result{}, r.updateHelmChart(ctx, upgradePlan, chart, longhorn) | ||
} | ||
|
||
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) | ||
} | ||
|
||
idx := slices.IndexFunc(job.Status.Conditions, func(condition batchv1.JobCondition) bool { | ||
return condition.Status == corev1.ConditionTrue && | ||
(condition.Type == batchv1.JobComplete || condition.Type == batchv1.JobFailed) | ||
}) | ||
|
||
if idx == -1 { | ||
// Upgrade job is still ongoing. | ||
return ctrl.Result{}, nil | ||
} | ||
|
||
condition := job.Status.Conditions[idx] | ||
|
||
switch condition.Type { | ||
case batchv1.JobComplete: | ||
setSuccessfulCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, "Longhorn is upgraded") | ||
case batchv1.JobFailed: | ||
setFailedCondition(upgradePlan, lifecyclev1alpha1.LonghornUpgradedCondition, fmt.Sprintf("Error occurred: %s", condition.Message)) | ||
} | ||
|
||
return ctrl.Result{Requeue: true}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters