@@ -12,7 +12,59 @@ import (
12
12
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
13
13
)
14
14
15
+ // Returns the set of abnormal conditions for a given resource.
16
+ // abnormal conditions reflect any condition indicating an other than nominal state
17
+ type AbnormalConditionsMethod func (ctx context.Context , unstruct * unstructured.Unstructured ) []status.Condition
18
+
15
19
type kstatusAggregator struct {
20
+ // Map of GVK specific 'Compute' methods. Methods find the status of
21
+ // a given unstructured resource.
22
+ //
23
+ // The returned result contains the status of the resource, which will be
24
+ // one of
25
+ // - InProgress
26
+ // - Current
27
+ // - Failed
28
+ // - Terminating
29
+ //
30
+ // It also contains a message that provides more information on why
31
+ // the resource has the given status. Finally, the result also contains
32
+ // a list of standard resources that would belong on the given resource.
33
+ GVKComputeMethods map [string ]status.GetConditionsFn
34
+
35
+ // Map of GVK specific methods which return the set of abnormal conditions for a given resource
36
+ // abnormal conditions reflect any condition indicating an other than nominal state
37
+ GVKAbnormalConditionsMethods map [string ]AbnormalConditionsMethod
38
+ }
39
+
40
+ // NewOpenKstatusAgregator creates a kstatusAggregator with support for supplied gvk string specific 'Compute' and 'AbnormalConditions' methods
41
+ // Compute and Abnormal conditions methods should be supplied in assocation with a string of an explicit
42
+ // k8s.io/apimachinery/pkg/runtime/schema.GroupVersionKind (GVK) string
43
+ //
44
+ // e.g.
45
+ // computMethods := make(map[string]GetConditionsFn)
46
+ // abnormalConditionsMethods := make(map[string]AbnormalConditionsMethod)
47
+ //
48
+ // resourceGVK := schema.GroupVersionKind{...}
49
+ // computMethods[resourceGVK.String()] = <user supplied gvk specific 'Compute' method>
50
+ // abnormalConditionsMethods[resourceGVK.String()] = <user supplied gvk specific Abnormal Conditions method>
51
+ //
52
+ // statusBuilder := &declarative.StatusBuilder {
53
+ // BuildStatusImpl: status.NewOpenKStatusAggregator(computeMethods, abnormalConditionsMethods),
54
+ // }
55
+ //
56
+ // ...
57
+ //
58
+ // return r.Reconciler.Init(
59
+ //
60
+ // WithStatus(statusBuilder),
61
+ //
62
+ // )
63
+ func NewOpenKstatusAgregator (gvkCMs map [string ]status.GetConditionsFn , gvkACMs map [string ]AbnormalConditionsMethod ) * kstatusAggregator {
64
+ return & kstatusAggregator {
65
+ GVKComputeMethods : gvkCMs ,
66
+ GVKAbnormalConditionsMethods : gvkACMs ,
67
+ }
16
68
}
17
69
18
70
// TODO: Create a version that doesn't need reconciler or client?
@@ -68,7 +120,13 @@ func (k *kstatusAggregator) BuildStatus(ctx context.Context, info *declarative.S
68
120
statusMap [status .UnknownStatus ] = true
69
121
continue
70
122
}
71
- res , err := status .Compute (unstruct )
123
+
124
+ // Use the user supplied compute method if it exists, otherwise use the default.
125
+ var gvkComputeMethod status.GetConditionsFn = status .Compute
126
+ if computeMethod , ok := k .GVKComputeMethods [gvk .String ()]; ok && computeMethod != nil {
127
+ gvkComputeMethod = computeMethod
128
+ }
129
+ res , err := gvkComputeMethod (unstruct )
72
130
if err != nil {
73
131
log .Error (err , "error getting status of resource" )
74
132
statusMap [status .UnknownStatus ] = true
@@ -79,7 +137,13 @@ func (k *kstatusAggregator) BuildStatus(ctx context.Context, info *declarative.S
79
137
log .Info ("resource status was nil" )
80
138
statusMap [status .UnknownStatus ] = true
81
139
}
82
- conds := getAbnormalConditions (ctx , unstruct )
140
+
141
+ // Use the user supplied abnormal conditions method if it exists, otherwise use the default.
142
+ var getConditionsMethod AbnormalConditionsMethod = getAbnormalConditions
143
+ if conditionsMethod , ok := k .GVKAbnormalConditionsMethods [gvk .String ()]; ok && conditionsMethod != nil {
144
+ getConditionsMethod = conditionsMethod
145
+ }
146
+ conds := getConditionsMethod (ctx , unstruct )
83
147
abnormalConditions = append (abnormalConditions , conds ... )
84
148
}
85
149
0 commit comments