-
Notifications
You must be signed in to change notification settings - Fork 2
/
reconciler.go
64 lines (57 loc) · 1.5 KB
/
reconciler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package greetings
import (
"context"
"fmt"
"github.com/verifa/horizon/pkg/hz"
)
type GreetingReconciler struct {
GreetingClient hz.ObjectClient[Greeting]
}
// Reconcile implements hz.Reconciler.
func (r *GreetingReconciler) Reconcile(
ctx context.Context,
req hz.Request,
) (hz.Result, error) {
greeting, err := r.GreetingClient.Get(ctx, hz.WithGetKey(req.Key))
if err != nil {
return hz.Result{}, hz.IgnoreNotFound(err)
}
applyGreet, err := hz.ExtractManagedFields(
greeting,
r.GreetingClient.Client.Manager,
)
if err != nil {
return hz.Result{}, fmt.Errorf("extracting managed fields: %w", err)
}
if greeting.DeletionTimestamp.IsPast() {
// Handle any cleanup logic here.
return hz.Result{}, nil
}
// Obviously we don't need to run an action here, but this is just an
// example.
reply, err := r.GreetingClient.Run(
ctx,
&GreetingsHelloAction{},
greeting,
)
if err != nil {
applyGreet.Status = &GreetingStatus{
Ready: false,
Error: fmt.Sprintf("running hello action: %s", err),
Response: "",
}
if _, err := r.GreetingClient.Apply(ctx, applyGreet); err != nil {
return hz.Result{}, fmt.Errorf("updating greeting: %w", err)
}
return hz.Result{}, fmt.Errorf("running hello action: %w", err)
}
applyGreet.Status = &GreetingStatus{
Ready: true,
Error: "",
Response: reply.Status.Response,
}
if _, err := r.GreetingClient.Apply(ctx, applyGreet); err != nil {
return hz.Result{}, fmt.Errorf("updating greeting: %w", err)
}
return hz.Result{}, nil
}