-
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.
Signed-off-by: Atanas Dinov <[email protected]>
- Loading branch information
1 parent
2805764
commit 1d8b9a6
Showing
12 changed files
with
243 additions
and
22 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
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
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,80 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
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" | ||
"golang.org/x/exp/slices" | ||
batchv1 "k8s.io/api/batch/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/utils/ptr" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
func (r *UpgradePlanReconciler) reconcileRancher(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan, rancher *release.HelmChart) (ctrl.Result, error) { | ||
chart := &helmcattlev1.HelmChart{} | ||
|
||
if err := r.Get(ctx, upgrade.ChartNamespacedName(rancher.Name), chart); err != nil { | ||
if !errors.IsNotFound(err) { | ||
return ctrl.Result{}, err | ||
} | ||
|
||
setSkippedCondition(upgradePlan, lifecyclev1alpha1.RancherUpgradedCondition, "Rancher installation is not found") | ||
return ctrl.Result{Requeue: true}, nil | ||
} | ||
|
||
if chart.Spec.Version != rancher.Version { | ||
setInProgressCondition(upgradePlan, lifecyclev1alpha1.RancherUpgradedCondition, "Rancher is being upgraded") | ||
|
||
return ctrl.Result{}, r.updateHelmChart(ctx, upgradePlan, chart, rancher) | ||
} | ||
|
||
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.RancherUpgradedCondition, "Rancher is upgraded") | ||
case batchv1.JobFailed: | ||
setFailedCondition(upgradePlan, lifecyclev1alpha1.RancherUpgradedCondition, fmt.Sprintf("Error occurred: %s", condition.Message)) | ||
} | ||
|
||
return ctrl.Result{Requeue: true}, nil | ||
} | ||
|
||
func (r *UpgradePlanReconciler) updateHelmChart(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan, chart *helmcattlev1.HelmChart, releaseChart *release.HelmChart) error { | ||
const backoffLimit = 6 | ||
|
||
if chart.Annotations == nil { | ||
chart.Annotations = map[string]string{} | ||
} | ||
chart.Annotations[upgrade.PlanAnnotation] = upgradePlan.Name | ||
chart.Spec.ChartContent = "" | ||
chart.Spec.Chart = releaseChart.Name | ||
chart.Spec.Version = releaseChart.Version | ||
chart.Spec.Repo = releaseChart.Repository | ||
chart.Spec.BackOffLimit = ptr.To[int32](backoffLimit) | ||
|
||
return r.Update(ctx, chart) | ||
} |
Oops, something went wrong.