Skip to content

Commit cbeac17

Browse files
committed
fix(metrics): garbage collect deleted objects
Signed-off-by: Oliver Bähler <[email protected]>
1 parent cd92d7e commit cbeac17

File tree

4 files changed

+30
-2
lines changed

4 files changed

+30
-2
lines changed

internal/controllers/globalsopssecret_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ func (r *GlobalSopsSecretReconciler) Reconcile(ctx context.Context, req ctrl.Req
138138
log := r.Log.WithValues("Request.Name", req.Name)
139139
// Fetch the Tenant instance
140140
instance := &sopsv1alpha1.GlobalSopsSecret{}
141+
141142
if err := r.Get(ctx, req.NamespacedName, instance); err != nil {
142143
if apierrors.IsNotFound(err) {
143144
// Cleanup Metrics
144-
r.Metrics.DeleteGlobalSecretCondition(instance)
145+
r.Metrics.DeleteGlobalSecret(instance)
145146
log.V(5).Info("Request object not found, could have been deleted after reconcile request")
146147

147148
return reconcile.Result{}, nil

internal/controllers/sopsprovider_controller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ func (r *SopsProviderReconciler) Reconcile(ctx context.Context, req ctrl.Request
9090
log := r.Log.WithValues("Request.Name", req.Name)
9191
// Fetch the Tenant instance
9292
instance := &sopsv1alpha1.SopsProvider{}
93+
9394
if err := r.Get(ctx, req.NamespacedName, instance); err != nil {
9495
if apierrors.IsNotFound(err) {
96+
// Cleanup Metrics
97+
r.Metrics.DeleteProvider(instance)
98+
9599
log.V(5).Info("Request object not found, could have been deleted after reconcile request")
96100

97101
return reconcile.Result{}, nil

internal/controllers/sopssecret_controller.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ func (r *SopsSecretReconciler) Reconcile(ctx context.Context, req ctrl.Request)
106106
log := r.Log.WithValues("Request.Name", req.Name)
107107
// Fetch the Tenant instance
108108
instance := &sopsv1alpha1.SopsSecret{}
109+
109110
if err := r.Get(ctx, req.NamespacedName, instance); err != nil {
110111
if apierrors.IsNotFound(err) {
111112
// Cleanup Metrics
112-
r.Metrics.DeleteSecretCondition(instance)
113+
r.Metrics.DeleteSecret(instance)
113114
log.V(5).Info("Request object not found, could have been deleted after reconcile request")
114115

115116
return reconcile.Result{}, nil

internal/metrics/recorder.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,20 +98,42 @@ func (r *Recorder) RecordGlobalSecretCondition(secret *sopsv1alpha1.GlobalSopsSe
9898
}
9999
}
100100

101+
// DeleteCondition deletes the condition metrics for the ref.
102+
func (r *Recorder) DeleteProvider(provider *sopsv1alpha1.SopsProvider) {
103+
r.providerConditionGauge.DeletePartialMatch(map[string]string{
104+
"name": provider.Name,
105+
})
106+
}
107+
101108
// DeleteCondition deletes the condition metrics for the ref.
102109
func (r *Recorder) DeleteProviderCondition(provider *sopsv1alpha1.SopsProvider) {
103110
for _, status := range []string{meta.ReadyCondition, meta.NotReadyCondition} {
104111
r.providerConditionGauge.DeleteLabelValues(provider.Name, status)
105112
}
106113
}
107114

115+
// DeleteCondition deletes the condition metrics for the ref.
116+
func (r *Recorder) DeleteSecret(secret *sopsv1alpha1.SopsSecret) {
117+
r.secretConditionGauge.DeletePartialMatch(map[string]string{
118+
"name": secret.Name,
119+
"namespace": secret.Namespace,
120+
})
121+
}
122+
108123
// DeleteCondition deletes the condition metrics for the ref.
109124
func (r *Recorder) DeleteSecretCondition(secret *sopsv1alpha1.SopsSecret) {
110125
for _, status := range []string{meta.ReadyCondition, meta.NotReadyCondition} {
111126
r.secretConditionGauge.DeleteLabelValues(secret.Name, secret.Namespace, status)
112127
}
113128
}
114129

130+
// DeleteCondition deletes the condition metrics for the ref.
131+
func (r *Recorder) DeleteGlobalSecret(secret *sopsv1alpha1.GlobalSopsSecret) {
132+
r.globalSecretConditionGauge.DeletePartialMatch(map[string]string{
133+
"name": secret.Name,
134+
})
135+
}
136+
115137
// DeleteCondition deletes the condition metrics for the ref.
116138
func (r *Recorder) DeleteGlobalSecretCondition(secret *sopsv1alpha1.GlobalSopsSecret) {
117139
for _, status := range []string{meta.ReadyCondition, meta.NotReadyCondition} {

0 commit comments

Comments
 (0)