...
1 package reconcile
2
3 import (
4 "errors"
5 "fmt"
6
7 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
8 "k8s.io/apimachinery/pkg/runtime"
9
10 edgeunstructured "edge-infra.dev/pkg/k8s/unstructured"
11 )
12
13 const (
14
15
16
17 ReconcileRequestAnnotation string = "reconcile.f8n.ncr.com/requestedAt"
18 )
19
20 var ErrLastHandledReconcileAtNotFound = errors.New("last handled reconcile at not found")
21
22
23
24
25
26
27
28
29 type RequestStatus struct {
30
31
32
33
34 LastHandledReconcileAt string `json:"lastHandledReconcileAt,omitempty"`
35 }
36
37
38
39 func GetStatusLastHandledReconcileAt(obj runtime.Object) (string, error) {
40 u, err := edgeunstructured.FromRuntime(obj)
41 if err != nil {
42 return "", err
43 }
44 ra, found, err := unstructured.NestedString(u.Object, "status", "lastHandledReconcileAt")
45 if err != nil {
46 return "", err
47 }
48 if !found {
49 return "", fmt.Errorf("%w", ErrLastHandledReconcileAtNotFound)
50 }
51 return ra, nil
52 }
53
54
55
56 func SetStatusLastHandledReconcileAt(obj runtime.Object, val string) error {
57 u, err := edgeunstructured.FromRuntime(obj)
58 if err != nil {
59 return err
60 }
61 if err := unstructured.SetNestedField(u.Object, val, "status", "lastHandledReconcileAt"); err != nil {
62 return err
63 }
64 return runtime.DefaultUnstructuredConverter.FromUnstructured(u.Object, obj)
65 }
66
67
68
69
70 func GetAnnotation(annotations map[string]string) (string, bool) {
71 requestedAt, ok := annotations[ReconcileRequestAnnotation]
72 return requestedAt, ok
73 }
74
View as plain text