Skip to content

Commit 48c14fa

Browse files
authored
Fix pod metrics that don't get deleted when the pod is deleted (#1796)
1 parent fcaacf9 commit 48c14fa

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

pkg/controllers/metrics/pod/controller.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,17 @@ func (c *Controller) Reconcile(ctx context.Context, req reconcile.Request) (reco
147147
if err := c.kubeClient.Get(ctx, req.NamespacedName, pod); err != nil {
148148
if errors.IsNotFound(err) {
149149
c.pendingPods.Delete(req.NamespacedName.String())
150+
// Delete the unstarted metric since the pod is deleted
151+
podUnstartedTimeSeconds.Delete(map[string]string{
152+
podName: req.Name,
153+
podNamespace: req.Namespace,
154+
})
150155
c.unscheduledPods.Delete(req.NamespacedName.String())
156+
// Delete the unbound metric since the pod is deleted
157+
podCurrentUnboundTimeSeconds.Delete(map[string]string{
158+
podName: req.Name,
159+
podNamespace: req.Namespace,
160+
})
151161
c.metricStore.Delete(req.NamespacedName.String())
152162
}
153163
return reconcile.Result{}, client.IgnoreNotFound(err)

pkg/controllers/metrics/pod/suite_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,35 @@ var _ = Describe("Pod Metrics", func() {
177177
_, found = FindMetricWithLabelValues("karpenter_pods_startup_duration_seconds", nil)
178178
Expect(found).To(BeTrue())
179179
})
180+
It("should delete pod unstarted time and pod unbound duration metric on pod delete", func() {
181+
p := test.Pod()
182+
p.Status.Phase = corev1.PodPending
183+
ExpectApplied(ctx, env.Client, p)
184+
ExpectReconcileSucceeded(ctx, podController, client.ObjectKeyFromObject(p))
185+
_, found := FindMetricWithLabelValues("karpenter_pods_current_unbound_time_seconds", map[string]string{
186+
"name": p.GetName(),
187+
"namespace": p.GetNamespace(),
188+
})
189+
Expect(found).To(BeTrue())
190+
_, found = FindMetricWithLabelValues("karpenter_pods_unstarted_time_seconds", map[string]string{
191+
"name": p.GetName(),
192+
"namespace": p.GetNamespace(),
193+
})
194+
Expect(found).To(BeTrue())
195+
196+
ExpectDeleted(ctx, env.Client, p)
197+
ExpectReconcileSucceeded(ctx, podController, client.ObjectKeyFromObject(p))
198+
_, found = FindMetricWithLabelValues("karpenter_pods_current_unbound_time_seconds", map[string]string{
199+
"name": p.GetName(),
200+
"namespace": p.GetNamespace(),
201+
})
202+
Expect(found).To(BeFalse())
203+
_, found = FindMetricWithLabelValues("karpenter_pods_unstarted_time_seconds", map[string]string{
204+
"name": p.GetName(),
205+
"namespace": p.GetNamespace(),
206+
})
207+
Expect(found).To(BeFalse())
208+
})
180209
It("should delete the pod state metric on pod delete", func() {
181210
p := test.Pod()
182211
ExpectApplied(ctx, env.Client, p)

0 commit comments

Comments
 (0)