Skip to content

Commit 5d50bb1

Browse files
Merge pull request #36 from PDOK/jd/status-update
Moved status update to smoothoperator
2 parents 352c1b9 + 21667f5 commit 5d50bb1

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

model/zz_generated.deepcopy.go

Lines changed: 39 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/status/pod_summary.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ func getOwnerRefOfKind(childObj client.Object, kind string) *metav1.OwnerReferen
6464
return nil
6565
}
6666

67-
// GetPodSummary returns a pod summary that includes the status of the last two replica sets that belong to obj based on its labels
68-
func GetPodSummary(ctx context.Context, k8sClient client.Client, obj client.Object) (model.PodSummary, error) {
67+
// getPodSummary returns a pod summary that includes the status of the last two replica sets that belong to obj based on its labels
68+
func getPodSummary(ctx context.Context, k8sClient client.Client, obj client.Object) (model.PodSummary, error) {
6969
var replicaSetList appsv1.ReplicaSetList
7070
err := k8sClient.List(ctx, &replicaSetList, client.MatchingLabels(obj.GetLabels()))
7171
if err != nil {

pkg/status/status.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)