DequeueOpsRequestInClusterAnnotation when OpsRequest.status.phase is Succeeded or Failed we should remove the OpsRequest Annotation of cluster, then unlock cluster
(ctx context.Context, cli client.Client, opsRes *OpsResource)
| 39 | // DequeueOpsRequestInClusterAnnotation when OpsRequest.status.phase is Succeeded or Failed |
| 40 | // we should remove the OpsRequest Annotation of cluster, then unlock cluster |
| 41 | func DequeueOpsRequestInClusterAnnotation(ctx context.Context, cli client.Client, opsRes *OpsResource) error { |
| 42 | var ( |
| 43 | opsRequestSlice []opsv1alpha1.OpsRecorder |
| 44 | err error |
| 45 | ) |
| 46 | if opsRequestSlice, err = opsutil.GetOpsRequestSliceFromCluster(opsRes.Cluster); err != nil { |
| 47 | return err |
| 48 | } |
| 49 | index, _ := GetOpsRecorderFromSlice(opsRequestSlice, opsRes.OpsRequest.Name) |
| 50 | if index == -1 { |
| 51 | return nil |
| 52 | } |
| 53 | if opsRes.OpsRequest.Status.Phase == opsv1alpha1.OpsFailedPhase && index == 0 { |
| 54 | var newOpsRequestSlice []opsv1alpha1.OpsRecorder |
| 55 | // 1. update all pending opsRequest phase to Cancelled if the head opsRequest is Failed. |
| 56 | for i := 1; i < len(opsRequestSlice); i++ { |
| 57 | if !opsRequestSlice[i].InQueue { |
| 58 | // ignore the running opsRequests. |
| 59 | newOpsRequestSlice = append(newOpsRequestSlice, opsRequestSlice[i]) |
| 60 | continue |
| 61 | } |
| 62 | ops := &opsv1alpha1.OpsRequest{} |
| 63 | if err = cli.Get(ctx, client.ObjectKey{Name: opsRequestSlice[i].Name, Namespace: opsRes.OpsRequest.Namespace}, ops); err != nil { |
| 64 | if apierrors.IsNotFound(err) { |
| 65 | continue |
| 66 | } |
| 67 | return err |
| 68 | } |
| 69 | patch := client.MergeFrom(ops.DeepCopy()) |
| 70 | ops.Status.Phase = opsv1alpha1.OpsCancelledPhase |
| 71 | ops.Status.CompletionTimestamp = metav1.Time{Time: time.Now()} |
| 72 | ops.SetStatusCondition(metav1.Condition{ |
| 73 | Type: opsv1alpha1.ConditionTypeCancelled, |
| 74 | Reason: opsv1alpha1.ReasonOpsCancelByController, |
| 75 | Status: metav1.ConditionTrue, |
| 76 | Message: fmt.Sprintf(`Cancelled by controller due to the failure of previous OpsRequest "%s"`, opsRes.OpsRequest.Name), |
| 77 | }) |
| 78 | if err = cli.Status().Patch(ctx, ops, patch); err != nil && apierrors.IsNotFound(err) { |
| 79 | return err |
| 80 | } |
| 81 | } |
| 82 | // 2. cleanup opsRequest queue |
| 83 | opsRequestSlice = newOpsRequestSlice |
| 84 | } else { |
| 85 | // delete the opsRequest in Cluster.annotations |
| 86 | opsRequestSlice = slices.Delete(opsRequestSlice, index, index+1) |
| 87 | } |
| 88 | return opsutil.UpdateClusterOpsAnnotations(ctx, cli, opsRes.Cluster, opsRequestSlice) |
| 89 | } |
| 90 | |
| 91 | // enqueueOpsRequestToClusterAnnotation adds the OpsRequest Annotation to Cluster.metadata.Annotations to acquire the lock. |
| 92 | func enqueueOpsRequestToClusterAnnotation(ctx context.Context, cli client.Client, opsRes *OpsResource, opsBehaviour OpsBehaviour) (*opsv1alpha1.OpsRecorder, error) { |
no test coverage detected
searching dependent graphs…