Skip to content

Commit

Permalink
Reconcile delete requests
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Aug 28, 2024
1 parent bc107ed commit 57343aa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ rules:
- plans
verbs:
- create
- delete
- get
- list
- watch
68 changes: 64 additions & 4 deletions internal/controller/upgradeplan_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/log"
Expand All @@ -57,9 +58,9 @@ type UpgradePlanReconciler struct {
// +kubebuilder:rbac:groups=lifecycle.suse.com,resources=upgradeplans,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=lifecycle.suse.com,resources=upgradeplans/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=lifecycle.suse.com,resources=upgradeplans/finalizers,verbs=update
// +kubebuilder:rbac:groups=upgrade.cattle.io,resources=plans,verbs=create;list;get;watch
// +kubebuilder:rbac:groups=upgrade.cattle.io,resources=plans,verbs=create;list;get;watch;delete
// +kubebuilder:rbac:groups="",resources=nodes,verbs=watch;list
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;delete;create;watch
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;delete;create;watch;delete
// +kubebuilder:rbac:groups=core,resources=events,verbs=create;patch
// +kubebuilder:rbac:groups=batch,resources=jobs,verbs=get;list;watch;create
// +kubebuilder:rbac:groups=batch,resources=jobs/status,verbs=get
Expand All @@ -80,13 +81,72 @@ func (r *UpgradePlanReconciler) Reconcile(ctx context.Context, req ctrl.Request)
logger := log.FromContext(ctx)
logger.Info("Reconciling UpgradePlan")

result, err := r.executePlan(ctx, plan)
if !plan.ObjectMeta.DeletionTimestamp.IsZero() {
return ctrl.Result{}, r.reconcileDelete(ctx, plan)
} else if !controllerutil.ContainsFinalizer(plan, lifecyclev1alpha1.UpgradePlanFinalizer) {
controllerutil.AddFinalizer(plan, lifecyclev1alpha1.UpgradePlanFinalizer)
return ctrl.Result{}, r.Update(ctx, plan)
}

result, err := r.reconcileNormal(ctx, plan)

// Attempt to update the plan status before returning.
return result, errors.Join(err, r.Status().Update(ctx, plan))
}

func (r *UpgradePlanReconciler) executePlan(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan) (ctrl.Result, error) {
func (r *UpgradePlanReconciler) reconcileDelete(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan) error {
if !controllerutil.ContainsFinalizer(upgradePlan, lifecyclev1alpha1.UpgradePlanFinalizer) {
return nil
}

sucPlans := &upgradecattlev1.PlanList{}

if err := r.List(ctx, sucPlans, &client.ListOptions{
Namespace: upgrade.SUCNamespace,
}); err != nil {
return fmt.Errorf("retrieving SUC plans: %w", err)
}

for _, plan := range sucPlans.Items {
if plan.Annotations[upgrade.PlanNameAnnotation] != upgradePlan.Name ||
plan.Annotations[upgrade.PlanNamespaceAnnotation] != upgradePlan.Namespace {
continue
}

if len(plan.Status.Applying) != 0 {
// Wait for the upgrade to be completed.
return nil
}

if err := r.Delete(ctx, &plan); err != nil {
return fmt.Errorf("deleting SUC plan %s: %w", plan.Name, err)
}
}

secrets := &corev1.SecretList{}

if err := r.List(ctx, secrets, &client.ListOptions{
Namespace: upgrade.SUCNamespace,
}); err != nil {
return fmt.Errorf("retrieving SUC secrets: %w", err)
}

for _, secret := range secrets.Items {
if secret.Annotations[upgrade.PlanNameAnnotation] != upgradePlan.Name ||
secret.Annotations[upgrade.PlanNamespaceAnnotation] != upgradePlan.Namespace {
continue
}

if err := r.Delete(ctx, &secret); err != nil {
return fmt.Errorf("deleting SUC secret %s: %w", secret.Name, err)
}
}

controllerutil.RemoveFinalizer(upgradePlan, lifecyclev1alpha1.UpgradePlanFinalizer)
return r.Update(ctx, upgradePlan)
}

func (r *UpgradePlanReconciler) reconcileNormal(ctx context.Context, upgradePlan *lifecyclev1alpha1.UpgradePlan) (ctrl.Result, error) {
release, err := r.retrieveReleaseManifest(ctx, upgradePlan)
if err != nil {
if !errors.Is(err, errReleaseManifestNotFound) {
Expand Down

0 comments on commit 57343aa

Please sign in to comment.