|
| 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