Skip to content

Commit fd25a83

Browse files
authored
Merge pull request #372 from tatehanawalt/open_kstatus_aggregator
feat: kStatusAggregator user supplied gvk compute and abnormal conditions fns
2 parents 8e6cf52 + 6de4512 commit fd25a83

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

pkg/patterns/addon/pkg/status/kstatus.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,59 @@ import (
1212
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
1313
)
1414

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+
1519
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+
}
1668
}
1769

1870
// 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
68120
statusMap[status.UnknownStatus] = true
69121
continue
70122
}
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)
72130
if err != nil {
73131
log.Error(err, "error getting status of resource")
74132
statusMap[status.UnknownStatus] = true
@@ -79,7 +137,13 @@ func (k *kstatusAggregator) BuildStatus(ctx context.Context, info *declarative.S
79137
log.Info("resource status was nil")
80138
statusMap[status.UnknownStatus] = true
81139
}
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)
83147
abnormalConditions = append(abnormalConditions, conds...)
84148
}
85149

0 commit comments

Comments
 (0)