|
| 1 | +/* |
| 2 | +Copyright (c) Microsoft Corporation. |
| 3 | +Licensed under the MIT license. |
| 4 | +*/ |
| 5 | + |
| 6 | +// Package updaterun features a controller to reconcile the clusterStagedUpdateRun objects. |
| 7 | +package updaterun |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "time" |
| 13 | + |
| 14 | + "go.goms.io/fleet/pkg/utils" |
| 15 | + "go.goms.io/fleet/pkg/utils/controller" |
| 16 | + "k8s.io/apimachinery/pkg/types" |
| 17 | + "k8s.io/client-go/tools/record" |
| 18 | + "k8s.io/client-go/util/workqueue" |
| 19 | + "k8s.io/klog/v2" |
| 20 | + runtime "sigs.k8s.io/controller-runtime" |
| 21 | + "sigs.k8s.io/controller-runtime/pkg/builder" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 23 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 24 | + "sigs.k8s.io/controller-runtime/pkg/event" |
| 25 | + "sigs.k8s.io/controller-runtime/pkg/handler" |
| 26 | + "sigs.k8s.io/controller-runtime/pkg/predicate" |
| 27 | + "sigs.k8s.io/controller-runtime/pkg/reconcile" |
| 28 | + |
| 29 | + placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1" |
| 30 | +) |
| 31 | + |
| 32 | +// Reconciler reconciles a ClusterStagedUpdateRun object. |
| 33 | +type Reconciler struct { |
| 34 | + client.Client |
| 35 | + recorder record.EventRecorder |
| 36 | +} |
| 37 | + |
| 38 | +func (r *Reconciler) Reconcile(ctx context.Context, req runtime.Request) (runtime.Result, error) { |
| 39 | + startTime := time.Now() |
| 40 | + klog.V(2).InfoS("ClusterStagedUpdateRun reconciliation starts", "clusterStagedUpdateRun", req.NamespacedName) |
| 41 | + defer func() { |
| 42 | + latency := time.Since(startTime).Milliseconds() |
| 43 | + klog.V(2).InfoS("ClusterStagedUpdateRun reconciliation ends", "clusterStagedUpdateRun", req.NamespacedName, "latency", latency) |
| 44 | + }() |
| 45 | + |
| 46 | + var updateRun placementv1alpha1.ClusterStagedUpdateRun |
| 47 | + if err := r.Client.Get(ctx, req.NamespacedName, &updateRun); err != nil { |
| 48 | + klog.ErrorS(err, "Failed to get clusterStagedUpdateRun object", "clusterStagedUpdateRun", req.Name) |
| 49 | + return runtime.Result{}, client.IgnoreNotFound(err) |
| 50 | + } |
| 51 | + runObjRef := klog.KObj(&updateRun) |
| 52 | + |
| 53 | + // Handle the deletion of the clusterStagedUpdateRun. |
| 54 | + if !updateRun.DeletionTimestamp.IsZero() { |
| 55 | + klog.V(2).InfoS("The clusterStagedUpdateRun is being deleted", "clusterStagedUpdateRun", runObjRef) |
| 56 | + deleted, waitTime, err := r.handleDelete(ctx, updateRun.DeepCopy()) |
| 57 | + if err != nil { |
| 58 | + return runtime.Result{}, err |
| 59 | + } |
| 60 | + if deleted { |
| 61 | + return runtime.Result{}, nil |
| 62 | + } |
| 63 | + return runtime.Result{RequeueAfter: waitTime}, nil |
| 64 | + } |
| 65 | + |
| 66 | + // Add the finalizer to the clusterStagedUpdateRun. |
| 67 | + if err := r.ensureFinalizer(ctx, &updateRun); err != nil { |
| 68 | + klog.ErrorS(err, "Failed to add the finalizer to the clusterStagedUpdateRun", "clusterStagedUpdateRun", runObjRef) |
| 69 | + return runtime.Result{}, err |
| 70 | + } |
| 71 | + |
| 72 | + // TODO(wantjian): reconcile the clusterStagedUpdateRun. |
| 73 | + return runtime.Result{}, nil |
| 74 | +} |
| 75 | + |
| 76 | +// handleDelete handles the deletion of the clusterStagedUpdateRun object. |
| 77 | +// We delete all the dependent resources, including approvalRequest objects, of the clusterStagedUpdateRun object. |
| 78 | +func (r *Reconciler) handleDelete(ctx context.Context, updateRun *placementv1alpha1.ClusterStagedUpdateRun) (bool, time.Duration, error) { |
| 79 | + runObjRef := klog.KObj(updateRun) |
| 80 | + // delete all the associated approvalRequests. |
| 81 | + approvalRequest := &placementv1alpha1.ClusterApprovalRequest{} |
| 82 | + if err := r.Client.DeleteAllOf(ctx, approvalRequest, client.MatchingLabels{placementv1alpha1.TargetUpdateRunLabel: updateRun.GetName()}); err != nil { |
| 83 | + klog.ErrorS(err, "Failed to delete all associated approvalRequests", "clusterStagedUpdateRun", runObjRef) |
| 84 | + return false, 0, controller.NewAPIServerError(false, err) |
| 85 | + } |
| 86 | + klog.V(2).InfoS("Deleted all approvalRequests associated with the clusterStagedUpdateRun", "clusterStagedUpdateRun", runObjRef) |
| 87 | + controllerutil.RemoveFinalizer(updateRun, placementv1alpha1.ClusterStagedUpdateRunFinalizer) |
| 88 | + if err := r.Client.Update(ctx, updateRun); err != nil { |
| 89 | + klog.ErrorS(err, "Failed to remove updateRun finalizer", "clusterStagedUpdateRun", runObjRef) |
| 90 | + return false, 0, controller.NewUpdateIgnoreConflictError(err) |
| 91 | + } |
| 92 | + return true, 0, nil |
| 93 | +} |
| 94 | + |
| 95 | +// ensureFinalizer makes sure that the ClusterStagedUpdateRun CR has a finalizer on it. |
| 96 | +func (r *Reconciler) ensureFinalizer(ctx context.Context, updateRun *placementv1alpha1.ClusterStagedUpdateRun) error { |
| 97 | + if controllerutil.ContainsFinalizer(updateRun, placementv1alpha1.ClusterStagedUpdateRunFinalizer) { |
| 98 | + return nil |
| 99 | + } |
| 100 | + klog.InfoS("Added the staged update run finalizer", "stagedUpdateRun", klog.KObj(updateRun)) |
| 101 | + controllerutil.AddFinalizer(updateRun, placementv1alpha1.ClusterStagedUpdateRunFinalizer) |
| 102 | + return r.Update(ctx, updateRun, client.FieldOwner(utils.UpdateRunControllerFieldManagerName)) |
| 103 | +} |
| 104 | + |
| 105 | +// SetupWithManager sets up the controller with the Manager. |
| 106 | +func (r *Reconciler) SetupWithManager(mgr runtime.Manager) error { |
| 107 | + r.recorder = mgr.GetEventRecorderFor("clusterresource-stagedupdaterun-controller") |
| 108 | + return runtime.NewControllerManagedBy(mgr). |
| 109 | + Named("clusterresource-stagedupdaterun-controller"). |
| 110 | + For(&placementv1alpha1.ClusterStagedUpdateRun{}, builder.WithPredicates(predicate.GenerationChangedPredicate{})). |
| 111 | + Watches(&placementv1alpha1.ClusterApprovalRequest{}, &handler.Funcs{ |
| 112 | + // We only care about when an approval request is approved. |
| 113 | + UpdateFunc: func(ctx context.Context, e event.UpdateEvent, q workqueue.RateLimitingInterface) { |
| 114 | + klog.V(2).InfoS("Handling a clusterApprovalRequest update event", "clusterApprovalRequest", klog.KObj(e.ObjectNew)) |
| 115 | + handleClusterApprovalRequest(e.ObjectNew, q) |
| 116 | + }, |
| 117 | + GenericFunc: func(ctx context.Context, e event.GenericEvent, q workqueue.RateLimitingInterface) { |
| 118 | + klog.V(2).InfoS("Handling a clusterApprovalRequest generic event", "clusterApprovalRequest", klog.KObj(e.Object)) |
| 119 | + handleClusterApprovalRequest(e.Object, q) |
| 120 | + }, |
| 121 | + }).Complete(r) |
| 122 | +} |
| 123 | + |
| 124 | +// handleClusterApprovalRequest finds the ClusterStagedUpdateRun creating the ClusterApprovalRequest, |
| 125 | +// and enqueues it to the ClusterStagedUpdateRun controller queue. |
| 126 | +func handleClusterApprovalRequest(obj client.Object, q workqueue.RateLimitingInterface) { |
| 127 | + approvalRequest, ok := obj.(*placementv1alpha1.ClusterApprovalRequest) |
| 128 | + if !ok { |
| 129 | + klog.V(2).ErrorS(controller.NewUnexpectedBehaviorError(fmt.Errorf("cannot cast runtime object to ClusterApprovalRequest")), |
| 130 | + "Invalid object type", "object", klog.KObj(obj)) |
| 131 | + return |
| 132 | + } |
| 133 | + updateRun := approvalRequest.Spec.TargetUpdateRun |
| 134 | + if len(updateRun) == 0 { |
| 135 | + klog.V(2).ErrorS(controller.NewUnexpectedBehaviorError(fmt.Errorf("TargetUpdateRun field in ClusterApprovalRequest is empty")), |
| 136 | + "Invalid clusterApprovalRequest", "clusterApprovalRequest", klog.KObj(approvalRequest)) |
| 137 | + return |
| 138 | + } |
| 139 | + // enqueue to the updaterun controller queue. |
| 140 | + q.Add(reconcile.Request{ |
| 141 | + NamespacedName: types.NamespacedName{Name: updateRun}, |
| 142 | + }) |
| 143 | +} |
0 commit comments