Skip to content

Commit b3025a7

Browse files
committed
Bug 1822396: Delete subscription metric when an operator is uninstalled
When an operator was subscribed to using a Subscription Object, the subscription_sync_total metric was emitted whenever the Subscription Object was created/updated/deleted. This PR updates that behaviour to emit the metric only when the Subscription object is created/updated, and deletes the time series for that particular subscription when the subscription object is deleted.
1 parent 8ad4341 commit b3025a7

File tree

2 files changed

+22
-19
lines changed

2 files changed

+22
-19
lines changed

pkg/controller/operators/catalog/subscription/syncer.go

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,37 @@ func (s *subscriptionSyncer) now() *metav1.Time {
4545
// Sync reconciles Subscription events by invoking a sequence of reconcilers, passing the result of each
4646
// successful reconciliation as an argument to its successor.
4747
func (s *subscriptionSyncer) Sync(ctx context.Context, event kubestate.ResourceEvent) error {
48-
res := &v1alpha1.Subscription{}
49-
if err := scheme.Convert(event.Resource(), res, nil); err != nil {
48+
sub := &v1alpha1.Subscription{}
49+
if err := scheme.Convert(event.Resource(), sub, nil); err != nil {
5050
return err
5151
}
5252

53-
s.recordMetrics(res)
53+
metrics.EmitSubMetric(sub)
5454

5555
logger := s.logger.WithFields(logrus.Fields{
56-
"reconciling": fmt.Sprintf("%T", res),
57-
"selflink": res.GetSelfLink(),
56+
"reconciling": fmt.Sprintf("%T", sub),
57+
"selflink": sub.GetSelfLink(),
5858
"event": event.Type(),
5959
})
6060
logger.Info("syncing")
6161

6262
// Enter initial state based on subscription and event type
6363
// TODO: Consider generalizing initial generic add, update, delete transitions in the kubestate package.
6464
// Possibly make a resource event aware bridge between Sync and reconciler.
65-
initial := NewSubscriptionState(res.DeepCopy())
65+
initialSubState := NewSubscriptionState(sub.DeepCopy())
6666
switch event.Type() {
6767
case kubestate.ResourceAdded:
68-
initial = initial.Add()
68+
initialSubState = initialSubState.Add()
6969
case kubestate.ResourceUpdated:
70-
initial = initial.Update()
70+
initialSubState = initialSubState.Update()
7171
case kubestate.ResourceDeleted:
72-
initial = initial.Delete()
72+
{
73+
initialSubState = initialSubState.Delete()
74+
metrics.DeleteSubsMetric(sub)
75+
}
7376
}
7477

75-
reconciled, err := s.reconcilers.Reconcile(ctx, initial)
78+
reconciled, err := s.reconcilers.Reconcile(ctx, initialSubState)
7679
if err != nil {
7780
logger.WithError(err).Warn("an error was encountered during reconciliation")
7881
return err
@@ -85,15 +88,6 @@ func (s *subscriptionSyncer) Sync(ctx context.Context, event kubestate.ResourceE
8588
return nil
8689
}
8790

88-
func (s *subscriptionSyncer) recordMetrics(sub *v1alpha1.Subscription) {
89-
// sub.Spec is not a required field.
90-
if sub.Spec == nil {
91-
return
92-
}
93-
94-
metrics.CounterForSubscription(sub.GetName(), sub.Status.InstalledCSV, sub.Spec.Channel, sub.Spec.Package).Inc()
95-
}
96-
9791
func (s *subscriptionSyncer) Notify(event kubestate.ResourceEvent) {
9892
s.notify(event)
9993
}

pkg/metrics/metrics.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package metrics
22

33
import (
44
"context"
5+
56
"github.com/prometheus/client_golang/prometheus"
67
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
78
"k8s.io/apimachinery/pkg/labels"
@@ -217,3 +218,11 @@ func EmitCSVMetric(oldCSV *olmv1alpha1.ClusterServiceVersion, newCSV *olmv1alpha
217218
csvAbnormal.WithLabelValues(newCSV.Namespace, newCSV.Name, newCSV.Spec.Version.String(), string(newCSV.Status.Phase), string(newCSV.Status.Reason)).Set(1)
218219
}
219220
}
221+
222+
func EmitSubMetric(sub *olmv1alpha1.Subscription) {
223+
SubscriptionSyncCount.WithLabelValues(sub.GetName(), sub.Status.InstalledCSV, sub.Spec.Channel, sub.Spec.Package).Inc()
224+
}
225+
226+
func DeleteSubsMetric(sub *olmv1alpha1.Subscription) {
227+
SubscriptionSyncCount.DeleteLabelValues(sub.GetName(), sub.Status.InstalledCSV, sub.Spec.Channel, sub.Spec.Package)
228+
}

0 commit comments

Comments
 (0)