Skip to content

Commit

Permalink
Merge pull request #372 from tatehanawalt/open_kstatus_aggregator
Browse files Browse the repository at this point in the history
feat: kStatusAggregator user supplied gvk compute and abnormal conditions fns
  • Loading branch information
k8s-ci-robot committed Jan 31, 2024
2 parents 8e6cf52 + 6de4512 commit fd25a83
Showing 1 changed file with 66 additions and 2 deletions.
68 changes: 66 additions & 2 deletions pkg/patterns/addon/pkg/status/kstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,59 @@ import (
"sigs.k8s.io/kubebuilder-declarative-pattern/pkg/patterns/declarative"
)

// Returns the set of abnormal conditions for a given resource.
// abnormal conditions reflect any condition indicating an other than nominal state
type AbnormalConditionsMethod func(ctx context.Context, unstruct *unstructured.Unstructured) []status.Condition

type kstatusAggregator struct {
// Map of GVK specific 'Compute' methods. Methods find the status of
// a given unstructured resource.
//
// The returned result contains the status of the resource, which will be
// one of
// - InProgress
// - Current
// - Failed
// - Terminating
//
// It also contains a message that provides more information on why
// the resource has the given status. Finally, the result also contains
// a list of standard resources that would belong on the given resource.
GVKComputeMethods map[string]status.GetConditionsFn

// Map of GVK specific methods which return the set of abnormal conditions for a given resource
// abnormal conditions reflect any condition indicating an other than nominal state
GVKAbnormalConditionsMethods map[string]AbnormalConditionsMethod
}

// NewOpenKstatusAgregator creates a kstatusAggregator with support for supplied gvk string specific 'Compute' and 'AbnormalConditions' methods
// Compute and Abnormal conditions methods should be supplied in assocation with a string of an explicit
// k8s.io/apimachinery/pkg/runtime/schema.GroupVersionKind (GVK) string
//
// e.g.
// computMethods := make(map[string]GetConditionsFn)
// abnormalConditionsMethods := make(map[string]AbnormalConditionsMethod)
//
// resourceGVK := schema.GroupVersionKind{...}
// computMethods[resourceGVK.String()] = <user supplied gvk specific 'Compute' method>
// abnormalConditionsMethods[resourceGVK.String()] = <user supplied gvk specific Abnormal Conditions method>
//
// statusBuilder := &declarative.StatusBuilder {
// BuildStatusImpl: status.NewOpenKStatusAggregator(computeMethods, abnormalConditionsMethods),
// }
//
// ...
//
// return r.Reconciler.Init(
//
// WithStatus(statusBuilder),
//
// )
func NewOpenKstatusAgregator(gvkCMs map[string]status.GetConditionsFn, gvkACMs map[string]AbnormalConditionsMethod) *kstatusAggregator {
return &kstatusAggregator{
GVKComputeMethods: gvkCMs,
GVKAbnormalConditionsMethods: gvkACMs,
}
}

// TODO: Create a version that doesn't need reconciler or client?
Expand Down Expand Up @@ -68,7 +120,13 @@ func (k *kstatusAggregator) BuildStatus(ctx context.Context, info *declarative.S
statusMap[status.UnknownStatus] = true
continue
}
res, err := status.Compute(unstruct)

// Use the user supplied compute method if it exists, otherwise use the default.
var gvkComputeMethod status.GetConditionsFn = status.Compute
if computeMethod, ok := k.GVKComputeMethods[gvk.String()]; ok && computeMethod != nil {
gvkComputeMethod = computeMethod
}
res, err := gvkComputeMethod(unstruct)
if err != nil {
log.Error(err, "error getting status of resource")
statusMap[status.UnknownStatus] = true
Expand All @@ -79,7 +137,13 @@ func (k *kstatusAggregator) BuildStatus(ctx context.Context, info *declarative.S
log.Info("resource status was nil")
statusMap[status.UnknownStatus] = true
}
conds := getAbnormalConditions(ctx, unstruct)

// Use the user supplied abnormal conditions method if it exists, otherwise use the default.
var getConditionsMethod AbnormalConditionsMethod = getAbnormalConditions
if conditionsMethod, ok := k.GVKAbnormalConditionsMethods[gvk.String()]; ok && conditionsMethod != nil {
getConditionsMethod = conditionsMethod
}
conds := getConditionsMethod(ctx, unstruct)
abnormalConditions = append(abnormalConditions, conds...)
}

Expand Down

0 comments on commit fd25a83

Please sign in to comment.