Skip to content

Commit 5eaf92a

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 d23b7ab commit 5eaf92a

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
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+
subs := &v1alpha1.Subscription{}
49+
if err := scheme.Convert(event.Resource(), subs, nil); err != nil {
5050
return err
5151
}
5252

53-
s.recordMetrics(res)
53+
metrics.EmitSubsMetrics(subs)
5454

5555
logger := s.logger.WithFields(logrus.Fields{
56-
"reconciling": fmt.Sprintf("%T", res),
57-
"selflink": res.GetSelfLink(),
56+
"reconciling": fmt.Sprintf("%T", subs),
57+
"selflink": subs.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+
initialSubsState := NewSubscriptionState(subs.DeepCopy())
6666
switch event.Type() {
6767
case kubestate.ResourceAdded:
68-
initial = initial.Add()
68+
initialSubsState = initialSubsState.Add()
6969
case kubestate.ResourceUpdated:
70-
initial = initial.Update()
70+
initialSubsState = initialSubsState.Update()
7171
case kubestate.ResourceDeleted:
72-
initial = initial.Delete()
72+
{
73+
initialSubsState = initialSubsState.Delete()
74+
metrics.DeleteSubsMetric(subs)
75+
}
7376
}
7477

75-
reconciled, err := s.reconcilers.Reconcile(ctx, initial)
78+
reconciled, err := s.reconcilers.Reconcile(ctx, initialSubsState)
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: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,6 @@ func RegisterCatalog() {
185185
prometheus.MustRegister(SubscriptionSyncCount)
186186
}
187187

188-
func CounterForSubscription(name, installedCSV, channelName, packageName string) prometheus.Counter {
189-
return SubscriptionSyncCount.WithLabelValues(name, installedCSV, channelName, packageName)
190-
}
191-
192188
func EmitCSVMetric(oldCSV *olmv1alpha1.ClusterServiceVersion, newCSV *olmv1alpha1.ClusterServiceVersion) {
193189
if oldCSV == nil || newCSV == nil {
194190
return
@@ -212,3 +208,16 @@ func EmitCSVMetric(oldCSV *olmv1alpha1.ClusterServiceVersion, newCSV *olmv1alpha
212208
csvAbnormal.WithLabelValues(newCSV.Namespace, newCSV.Name, newCSV.Spec.Version.String(), string(newCSV.Status.Phase), string(newCSV.Status.Reason)).Set(1)
213209
}
214210
}
211+
212+
func EmitSubsMetrics(sub *olmv1alpha1.Subscription) {
213+
// sub.Spec is not a required field.
214+
if sub.Spec == nil {
215+
return
216+
}
217+
218+
SubscriptionSyncCount.WithLabelValues(sub.GetName(), sub.Status.InstalledCSV, sub.Spec.Channel, sub.Spec.Package).Inc()
219+
}
220+
221+
func DeleteSubsMetric(sub *olmv1alpha1.Subscription) {
222+
SubscriptionSyncCount.DeleteLabelValues(sub.GetName(), sub.Status.InstalledCSV, sub.Spec.Channel, sub.Spec.Package)
223+
}

0 commit comments

Comments
 (0)