...
1 package object
2
3 import (
4 "errors"
5 "time"
6
7 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
8 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
9 "k8s.io/apimachinery/pkg/runtime"
10
11 edgeunstructured "edge-infra.dev/pkg/k8s/unstructured"
12 )
13
14 var (
15 ErrObservedGenerationNotFound = errors.New("observed generation not found")
16 ErrRequeueIntervalNotFound = errors.New("requeue interval not found")
17 )
18
19
20
21 func GetStatusObservedGeneration(obj runtime.Object) (int64, error) {
22 u, err := edgeunstructured.FromRuntime(obj)
23 if err != nil {
24 return 0, err
25 }
26 og, found, err := unstructured.NestedInt64(u.Object, "status", "observedGeneration")
27 if err != nil {
28 return 0, err
29 }
30 if !found {
31 return 0, ErrObservedGenerationNotFound
32 }
33 return og, nil
34 }
35
36
37
38 func GetRequeueInterval(obj runtime.Object) (time.Duration, error) {
39 period := time.Second
40 u, err := edgeunstructured.FromRuntime(obj)
41 if err != nil {
42 return period, err
43 }
44 interval, found, err := unstructured.NestedString(u.Object, "spec", "interval")
45 if err != nil {
46 return period, err
47 }
48 if !found {
49 return period, ErrRequeueIntervalNotFound
50 }
51 return time.ParseDuration(interval)
52 }
53
54
55
56
57
58
59
60
61
62
63 func GetConditions(obj runtime.Object) []metav1.Condition {
64 u, err := edgeunstructured.FromRuntime(obj)
65 if err != nil {
66 return nil
67 }
68 conditions := []metav1.Condition{}
69 if err := edgeunstructured.UnmarshalField(u, &conditions, "status", "conditions"); err != nil {
70 return nil
71 }
72 return conditions
73 }
74
View as plain text