|
| 1 | +package status |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/pdok/smooth-operator/model" |
| 8 | + "k8s.io/apimachinery/pkg/api/equality" |
| 9 | + "k8s.io/apimachinery/pkg/api/meta" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + reconciledConditionType = "Reconciled" |
| 18 | + reconciledConditionReasonSuccess = "Success" |
| 19 | + reconciledConditionReasonError = "Error" |
| 20 | +) |
| 21 | + |
| 22 | +type ObjectWithStatus interface { |
| 23 | + client.Object |
| 24 | + OperatorStatus() *model.OperatorStatus |
| 25 | +} |
| 26 | + |
| 27 | +func LogAndUpdateStatusError[O ObjectWithStatus](ctx context.Context, k8sClient client.Client, obj O, err error) { |
| 28 | + lgr := log.FromContext(ctx) |
| 29 | + lgr.Error(err, "reconcile error") |
| 30 | + |
| 31 | + generation := obj.GetGeneration() |
| 32 | + updateStatus(ctx, k8sClient, obj, []metav1.Condition{{ |
| 33 | + Type: reconciledConditionType, |
| 34 | + Status: metav1.ConditionFalse, |
| 35 | + Reason: reconciledConditionReasonError, |
| 36 | + Message: err.Error(), |
| 37 | + ObservedGeneration: generation, |
| 38 | + LastTransitionTime: metav1.NewTime(time.Now()), |
| 39 | + }}, nil) |
| 40 | +} |
| 41 | + |
| 42 | +func LogAndUpdateStatusFinished[O ObjectWithStatus](ctx context.Context, k8sClient client.Client, obj O, operationResults map[string]controllerutil.OperationResult) { |
| 43 | + lgr := log.FromContext(ctx) |
| 44 | + lgr.Info("operation results", "results", operationResults) |
| 45 | + |
| 46 | + generation := obj.GetGeneration() |
| 47 | + updateStatus(ctx, k8sClient, obj, []metav1.Condition{{ |
| 48 | + Type: reconciledConditionType, |
| 49 | + Status: metav1.ConditionTrue, |
| 50 | + Reason: reconciledConditionReasonSuccess, |
| 51 | + ObservedGeneration: generation, |
| 52 | + LastTransitionTime: metav1.NewTime(time.Now()), |
| 53 | + }}, operationResults) |
| 54 | +} |
| 55 | + |
| 56 | +func updateStatus[O ObjectWithStatus](ctx context.Context, k8sClient client.Client, obj O, conditions []metav1.Condition, operationResults map[string]controllerutil.OperationResult) { |
| 57 | + lgr := log.FromContext(ctx) |
| 58 | + if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(obj), obj); err != nil { |
| 59 | + log.FromContext(ctx).Error(err, "unable to update status") |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + status := obj.OperatorStatus() |
| 64 | + if status == nil { |
| 65 | + status = &model.OperatorStatus{} |
| 66 | + } |
| 67 | + |
| 68 | + podSummary, err := getPodSummary(ctx, k8sClient, obj) |
| 69 | + if err != nil { |
| 70 | + lgr.Error(err, "unable to get pod summary for status update") |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + changed := false |
| 75 | + if !equality.Semantic.DeepEqual(status.PodSummary, podSummary) { |
| 76 | + status.PodSummary = podSummary |
| 77 | + changed = true |
| 78 | + } |
| 79 | + for _, condition := range conditions { |
| 80 | + if meta.SetStatusCondition(&status.Conditions, condition) { |
| 81 | + changed = true |
| 82 | + } |
| 83 | + } |
| 84 | + if !equality.Semantic.DeepEqual(status.OperationResults, operationResults) { |
| 85 | + status.OperationResults = operationResults |
| 86 | + changed = true |
| 87 | + } |
| 88 | + if !changed { |
| 89 | + return |
| 90 | + } |
| 91 | + if err := k8sClient.Status().Update(ctx, obj); err != nil { |
| 92 | + lgr.Error(err, "unable to update status") |
| 93 | + } |
| 94 | +} |
0 commit comments