@@ -31,6 +31,7 @@ type Controller[T Object] struct {
31
31
kubeClient client.Client
32
32
eventRecorder record.EventRecorder
33
33
observedConditions sync.Map // map[reconcile.Request]ConditionSet
34
+ observedFinalizers sync.Map // map[reconcile.Request]Finalizer
34
35
terminatingObjects sync.Map // map[reconcile.Request]DeletionTimestamp
35
36
emitDeprecatedMetrics bool
36
37
ConditionDuration pmetrics.ObservationMetric
@@ -128,19 +129,40 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
128
129
if deletionTS , ok := c .terminatingObjects .Load (req ); ok {
129
130
c .observeHistogram (c .TerminationDuration , TerminationDuration , time .Since (deletionTS .(* metav1.Time ).Time ).Seconds (), map [string ]string {})
130
131
}
132
+ if finalizers , ok := c .observedFinalizers .LoadAndDelete (req ); ok {
133
+ for _ , finalizer := range finalizers .([]string ) {
134
+ c .eventRecorder .Event (o , v1 .EventTypeNormal , "Finalized" , fmt .Sprintf ("Finalized %s" , finalizer ))
135
+ }
136
+ }
131
137
return reconcile.Result {}, nil
132
138
}
133
139
return reconcile.Result {}, fmt .Errorf ("getting object, %w" , err )
134
140
}
135
141
142
+ // Detect and record terminations
143
+ observedFinalizers , _ := c .observedFinalizers .Swap (req , o .GetFinalizers ())
144
+ if observedFinalizers != nil {
145
+ for _ , finalizer := range lo .Without (observedFinalizers .([]string ), o .GetFinalizers ()... ) {
146
+ c .eventRecorder .Event (o , v1 .EventTypeNormal , "Finalized" , fmt .Sprintf ("Finalized %s" , finalizer ))
147
+ }
148
+ }
149
+
150
+ if o .GetDeletionTimestamp () != nil {
151
+ c .setGaugeMetric (c .TerminationCurrentTimeSeconds , TerminationCurrentTimeSeconds , time .Since (o .GetDeletionTimestamp ().Time ).Seconds (), map [string ]string {
152
+ MetricLabelNamespace : req .Namespace ,
153
+ MetricLabelName : req .Name ,
154
+ })
155
+ c .terminatingObjects .Store (req , o .GetDeletionTimestamp ())
156
+ }
157
+
158
+ // Detect and record condition counts
136
159
currentConditions := o .StatusConditions ()
137
160
observedConditions := ConditionSet {}
138
161
if v , ok := c .observedConditions .Load (req ); ok {
139
162
observedConditions = v .(ConditionSet )
140
163
}
141
164
c .observedConditions .Store (req , currentConditions )
142
165
143
- // Detect and record condition counts
144
166
for _ , condition := range o .GetConditions () {
145
167
c .setGaugeMetric (c .ConditionCount , ConditionCount , 1 , map [string ]string {
146
168
MetricLabelNamespace : req .Namespace ,
@@ -157,13 +179,7 @@ func (c *Controller[T]) reconcile(ctx context.Context, req reconcile.Request, o
157
179
pmetrics .LabelReason : condition .Reason ,
158
180
})
159
181
}
160
- if o .GetDeletionTimestamp () != nil {
161
- c .setGaugeMetric (c .TerminationCurrentTimeSeconds , TerminationCurrentTimeSeconds , time .Since (o .GetDeletionTimestamp ().Time ).Seconds (), map [string ]string {
162
- MetricLabelNamespace : req .Namespace ,
163
- MetricLabelName : req .Name ,
164
- })
165
- c .terminatingObjects .Store (req , o .GetDeletionTimestamp ())
166
- }
182
+
167
183
for _ , observedCondition := range observedConditions .List () {
168
184
if currentCondition := currentConditions .Get (observedCondition .Type ); currentCondition == nil || currentCondition .Status != observedCondition .Status {
169
185
c .deleteGaugeMetric (c .ConditionCount , ConditionCount , map [string ]string {
0 commit comments