Skip to content

Commit a67d9e0

Browse files
authored
Merge pull request #1812 from fluxcd/backport-1799-to-release/v1.10.x
[release/v1.10.x] Delete duration metric when object is gone
2 parents 49852d7 + da716eb commit a67d9e0

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

‎internal/controller/kustomization_controller.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ func (r *KustomizationReconciler) Reconcile(ctx context.Context, req ctrl.Reques
142142

143143
obj := &kustomizev1.Kustomization{}
144144
if err := r.Get(ctx, req.NamespacedName, obj); err != nil {
145+
if apierrors.IsNotFound(err) && r.Metrics.MetricsRecorder != nil {
146+
// The deferred recording below is not registered when the object is
147+
// already gone, so delete any existing duration series for it.
148+
r.Metrics.MetricsRecorder.DeleteDuration(corev1.ObjectReference{
149+
Kind: kustomizev1.KustomizationKind,
150+
Name: req.Name,
151+
Namespace: req.Namespace,
152+
})
153+
}
145154
return ctrl.Result{}, client.IgnoreNotFound(err)
146155
}
147156

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2026 The Flux authors
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package controller
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
. "github.com/onsi/gomega"
24+
corev1 "k8s.io/api/core/v1"
25+
ctrl "sigs.k8s.io/controller-runtime"
26+
crtlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
27+
28+
kustomizev1 "github.com/fluxcd/kustomize-controller/api/v1"
29+
)
30+
31+
func TestKustomizationReconciler_DeletesDurationMetricOnNotFound(t *testing.T) {
32+
g := NewWithT(t)
33+
34+
name := "metrics-" + randStringRunes(5)
35+
namespace := "default"
36+
ref := corev1.ObjectReference{
37+
Kind: kustomizev1.KustomizationKind,
38+
Name: name,
39+
Namespace: namespace,
40+
}
41+
42+
// Simulate a series left behind by a previous reconcile of an object that
43+
// was force-deleted before the controller observed its finalizer removal.
44+
g.Expect(testMetricsH.MetricsRecorder).NotTo(BeNil())
45+
testMetricsH.MetricsRecorder.RecordDuration(ref, time.Now())
46+
g.Expect(countDurationSeries(g, name, namespace)).To(Equal(1))
47+
48+
// The object does not exist, so Get returns NotFound and the deferred
49+
// recording is never registered.
50+
_, err := reconciler.Reconcile(ctx, ctrl.Request{
51+
Name: name, Namespace: namespace,
52+
})
53+
g.Expect(err).NotTo(HaveOccurred())
54+
g.Expect(countDurationSeries(g, name, namespace)).To(Equal(0))
55+
}
56+
57+
// countDurationSeries returns the number of gotk_reconcile_duration_seconds
58+
// series matching the given object name and namespace.
59+
func countDurationSeries(g *WithT, name, namespace string) int {
60+
families, err := crtlmetrics.Registry.Gather()
61+
g.Expect(err).NotTo(HaveOccurred())
62+
63+
count := 0
64+
for _, family := range families {
65+
if family.GetName() != "gotk_reconcile_duration_seconds" {
66+
continue
67+
}
68+
for _, metric := range family.GetMetric() {
69+
labels := make(map[string]string, len(metric.GetLabel()))
70+
for _, label := range metric.GetLabel() {
71+
labels[label.GetName()] = label.GetValue()
72+
}
73+
if labels["name"] == name && labels["namespace"] == namespace {
74+
count++
75+
}
76+
}
77+
}
78+
return count
79+
}

0 commit comments

Comments
 (0)