Skip to content

Commit

Permalink
feat: Emit events when finalizers complete (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellistarn authored Nov 19, 2024
1 parent a0fd766 commit 3a30256
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions status/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Controller[T Object] struct {
kubeClient client.Client
eventRecorder record.EventRecorder
observedConditions sync.Map // map[reconcile.Request]ConditionSet
observedFinalizers sync.Map // map[reconcile.Request]Finalizer
terminatingObjects sync.Map // map[reconcile.Request]DeletionTimestamp
emitDeprecatedMetrics bool
ConditionDuration pmetrics.ObservationMetric
Expand Down Expand Up @@ -128,19 +129,40 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
if deletionTS, ok := c.terminatingObjects.Load(req); ok {
c.observeHistogram(c.TerminationDuration, TerminationDuration, time.Since(deletionTS.(*metav1.Time).Time).Seconds(), map[string]string{})
}
if finalizers, ok := c.observedFinalizers.LoadAndDelete(req); ok {
for _, finalizer := range finalizers.([]string) {
c.eventRecorder.Event(o, v1.EventTypeNormal, "Finalized", fmt.Sprintf("Finalized %s", finalizer))
}
}
return reconcile.Result{}, nil
}
return reconcile.Result{}, fmt.Errorf("getting object, %w", err)
}

// Detect and record terminations
observedFinalizers, _ := c.observedFinalizers.Swap(req, o.GetFinalizers())
if observedFinalizers != nil {
for _, finalizer := range lo.Without(observedFinalizers.([]string), o.GetFinalizers()...) {
c.eventRecorder.Event(o, v1.EventTypeNormal, "Finalized", fmt.Sprintf("Finalized %s", finalizer))
}
}

if o.GetDeletionTimestamp() != nil {
c.setGaugeMetric(c.TerminationCurrentTimeSeconds, TerminationCurrentTimeSeconds, time.Since(o.GetDeletionTimestamp().Time).Seconds(), map[string]string{
MetricLabelNamespace: req.Namespace,
MetricLabelName: req.Name,
})
c.terminatingObjects.Store(req, o.GetDeletionTimestamp())
}

// Detect and record condition counts
currentConditions := o.StatusConditions()
observedConditions := ConditionSet{}
if v, ok := c.observedConditions.Load(req); ok {
observedConditions = v.(ConditionSet)
}
c.observedConditions.Store(req, currentConditions)

// Detect and record condition counts
for _, condition := range o.GetConditions() {
c.setGaugeMetric(c.ConditionCount, ConditionCount, 1, map[string]string{
MetricLabelNamespace: req.Namespace,
Expand All @@ -157,13 +179,7 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
pmetrics.LabelReason: condition.Reason,
})
}
if o.GetDeletionTimestamp() != nil {
c.setGaugeMetric(c.TerminationCurrentTimeSeconds, TerminationCurrentTimeSeconds, time.Since(o.GetDeletionTimestamp().Time).Seconds(), map[string]string{
MetricLabelNamespace: req.Namespace,
MetricLabelName: req.Name,
})
c.terminatingObjects.Store(req, o.GetDeletionTimestamp())
}

for _, observedCondition := range observedConditions.List() {
if currentCondition := currentConditions.Get(observedCondition.Type); currentCondition == nil || currentCondition.Status != observedCondition.Status {
c.deleteGaugeMetric(c.ConditionCount, ConditionCount, map[string]string{
Expand Down

0 comments on commit 3a30256

Please sign in to comment.