From 2624ba93a3f55b4880b10dcd27e2e88119818153 Mon Sep 17 00:00:00 2001 From: Somtochi Onyekwere Date: Tue, 16 Mar 2021 23:29:20 +0100 Subject: [PATCH] Record suspension metric Signed-off-by: Somtochi Onyekwere --- controllers/bucket_controller.go | 22 ++++++++++++++++++++++ controllers/gitrepository_controller.go | 22 ++++++++++++++++++++++ controllers/helmchart_controller.go | 22 ++++++++++++++++++++++ controllers/helmrepository_controller.go | 19 +++++++++++++++++++ go.mod | 2 +- go.sum | 6 ++---- 6 files changed, 88 insertions(+), 5 deletions(-) diff --git a/controllers/bucket_controller.go b/controllers/bucket_controller.go index 3d1fabead..78ff38933 100644 --- a/controllers/bucket_controller.go +++ b/controllers/bucket_controller.go @@ -91,6 +91,9 @@ func (r *BucketReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr return ctrl.Result{}, client.IgnoreNotFound(err) } + // Record suspended status metric + defer r.recordSuspension(ctx, bucket) + // Add our finalizer if it does not exist if !controllerutil.ContainsFinalizer(&bucket, sourcev1.SourceFinalizer) { controllerutil.AddFinalizer(&bucket, sourcev1.SourceFinalizer) @@ -421,6 +424,25 @@ func (r *BucketReconciler) recordReadiness(ctx context.Context, bucket sourcev1. } } +func (r *BucketReconciler) recordSuspension(ctx context.Context, bucket sourcev1.Bucket) { + if r.MetricsRecorder == nil { + return + } + log := logr.FromContext(ctx) + + objRef, err := reference.GetReference(r.Scheme, &bucket) + if err != nil { + log.Error(err, "unable to record suspended metric") + return + } + + if !bucket.DeletionTimestamp.IsZero() { + r.MetricsRecorder.RecordSuspend(*objRef, false) + } else { + r.MetricsRecorder.RecordSuspend(*objRef, bucket.Spec.Suspend) + } +} + func (r *BucketReconciler) updateStatus(ctx context.Context, req ctrl.Request, newStatus sourcev1.BucketStatus) error { var bucket sourcev1.Bucket if err := r.Get(ctx, req.NamespacedName, &bucket); err != nil { diff --git a/controllers/gitrepository_controller.go b/controllers/gitrepository_controller.go index 70e5144c2..8aea8f77b 100644 --- a/controllers/gitrepository_controller.go +++ b/controllers/gitrepository_controller.go @@ -87,6 +87,9 @@ func (r *GitRepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Reques return ctrl.Result{}, client.IgnoreNotFound(err) } + // Record suspended status metric + defer r.recordSuspension(ctx, repository) + // Add our finalizer if it does not exist if !controllerutil.ContainsFinalizer(&repository, sourcev1.SourceFinalizer) { controllerutil.AddFinalizer(&repository, sourcev1.SourceFinalizer) @@ -364,6 +367,25 @@ func (r *GitRepositoryReconciler) recordReadiness(ctx context.Context, repositor } } +func (r *GitRepositoryReconciler) recordSuspension(ctx context.Context, gitrepository sourcev1.GitRepository) { + if r.MetricsRecorder == nil { + return + } + log := logr.FromContext(ctx) + + objRef, err := reference.GetReference(r.Scheme, &gitrepository) + if err != nil { + log.Error(err, "unable to record suspended metric") + return + } + + if !gitrepository.DeletionTimestamp.IsZero() { + r.MetricsRecorder.RecordSuspend(*objRef, false) + } else { + r.MetricsRecorder.RecordSuspend(*objRef, gitrepository.Spec.Suspend) + } +} + func (r *GitRepositoryReconciler) updateStatus(ctx context.Context, req ctrl.Request, newStatus sourcev1.GitRepositoryStatus) error { var repository sourcev1.GitRepository if err := r.Get(ctx, req.NamespacedName, &repository); err != nil { diff --git a/controllers/helmchart_controller.go b/controllers/helmchart_controller.go index 5e57bfae5..3d153b3d8 100644 --- a/controllers/helmchart_controller.go +++ b/controllers/helmchart_controller.go @@ -123,6 +123,9 @@ func (r *HelmChartReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( return ctrl.Result{Requeue: true}, client.IgnoreNotFound(err) } + // Record suspended status metric + defer r.recordSuspension(ctx, chart) + // Add our finalizer if it does not exist if !controllerutil.ContainsFinalizer(&chart, sourcev1.SourceFinalizer) { controllerutil.AddFinalizer(&chart, sourcev1.SourceFinalizer) @@ -939,3 +942,22 @@ func validHelmChartName(s string) error { } return nil } + +func (r *HelmChartReconciler) recordSuspension(ctx context.Context, chart sourcev1.HelmChart) { + if r.MetricsRecorder == nil { + return + } + log := logr.FromContext(ctx) + + objRef, err := reference.GetReference(r.Scheme, &chart) + if err != nil { + log.Error(err, "unable to record suspended metric") + return + } + + if !chart.DeletionTimestamp.IsZero() { + r.MetricsRecorder.RecordSuspend(*objRef, false) + } else { + r.MetricsRecorder.RecordSuspend(*objRef, chart.Spec.Suspend) + } +} diff --git a/controllers/helmrepository_controller.go b/controllers/helmrepository_controller.go index 613ec72c8..d7f3bdf1a 100644 --- a/controllers/helmrepository_controller.go +++ b/controllers/helmrepository_controller.go @@ -361,3 +361,22 @@ func (r *HelmRepositoryReconciler) updateStatus(ctx context.Context, req ctrl.Re return r.Status().Patch(ctx, &repository, patch) } + +func (r *HelmRepositoryReconciler) recordSuspension(ctx context.Context, hr sourcev1.HelmRepository) { + if r.MetricsRecorder == nil { + return + } + log := logr.FromContext(ctx) + + objRef, err := reference.GetReference(r.Scheme, &hr) + if err != nil { + log.Error(err, "unable to record suspended metric") + return + } + + if !hr.DeletionTimestamp.IsZero() { + r.MetricsRecorder.RecordSuspend(*objRef, false) + } else { + r.MetricsRecorder.RecordSuspend(*objRef, hr.Spec.Suspend) + } +} diff --git a/go.mod b/go.mod index 7efe2501f..835ed95ee 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( github.com/fluxcd/pkg/gitutil v0.0.1 github.com/fluxcd/pkg/helmtestserver v0.1.0 github.com/fluxcd/pkg/lockedfile v0.0.5 - github.com/fluxcd/pkg/runtime v0.8.4 + github.com/fluxcd/pkg/runtime v0.8.5 github.com/fluxcd/pkg/ssh v0.0.5 github.com/fluxcd/pkg/untar v0.0.5 github.com/fluxcd/pkg/version v0.0.1 diff --git a/go.sum b/go.sum index 575207825..199ea24eb 100644 --- a/go.sum +++ b/go.sum @@ -262,8 +262,8 @@ github.com/fluxcd/pkg/helmtestserver v0.1.0 h1:RiVVxIHD6PJdKinW46feFIYf1LUj6xXSp github.com/fluxcd/pkg/helmtestserver v0.1.0/go.mod h1:3L+tbPn74PsHwHsyhbfk/kZAosrwMFTTA92XEFiwVAE= github.com/fluxcd/pkg/lockedfile v0.0.5 h1:C3T8wfdff1UY1bvplmCkGOLrdMWJHO8Q8+tdlEXJYzQ= github.com/fluxcd/pkg/lockedfile v0.0.5/go.mod h1:uAtPUBId6a2RqO84MTH5HKGX0SbM1kNW3Wr/FhYyDVA= -github.com/fluxcd/pkg/runtime v0.8.4 h1:amuhfoHGCUfFCPXg3Zrcyy7f9J+fho+/+FbQDDyewko= -github.com/fluxcd/pkg/runtime v0.8.4/go.mod h1:JD0eZIn5xkTeHHQUWXSqJPIh/ecO0d0qrUKbSVHnpnw= +github.com/fluxcd/pkg/runtime v0.8.5 h1:ynh8fszbLQ3QSisQBNOABEUTnvt+/QfCdaL6gOJQcoQ= +github.com/fluxcd/pkg/runtime v0.8.5/go.mod h1:JD0eZIn5xkTeHHQUWXSqJPIh/ecO0d0qrUKbSVHnpnw= github.com/fluxcd/pkg/ssh v0.0.5 h1:rnbFZ7voy2JBlUfMbfyqArX2FYaLNpDhccGFC3qW83A= github.com/fluxcd/pkg/ssh v0.0.5/go.mod h1:7jXPdXZpc0ttMNz2kD9QuMi3RNn/e0DOFbj0Tij/+Hs= github.com/fluxcd/pkg/testserver v0.0.2 h1:SoaMtO9cE5p/wl2zkGudzflnEHd9mk68CGjZOo7w0Uk= @@ -603,8 +603,6 @@ github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182aff github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= github.com/minio/md5-simd v1.1.0 h1:QPfiOqlZH+Cj9teu0t9b1nTBfPbyTl16Of5MeuShdK4= github.com/minio/md5-simd v1.1.0/go.mod h1:XpBqgZULrMYD3R+M28PcmP0CkI7PEMzB3U77ZrKZ0Gw= -github.com/minio/minio-go/v7 v7.0.5 h1:I2NIJ2ojwJqD/YByemC1M59e1b4FW9kS7NlOar7HPV4= -github.com/minio/minio-go/v7 v7.0.5/go.mod h1:TA0CQCjJZHM5SJj9IjqR0NmpmQJ6bCbXifAJ3mUU6Hw= github.com/minio/minio-go/v7 v7.0.10 h1:1oUKe4EOPUEhw2qnPQaPsJ0lmVTYLFu03SiItauXs94= github.com/minio/minio-go/v7 v7.0.10/go.mod h1:td4gW1ldOsj1PbSNS+WYK43j+P1XVhX/8W8awaYlBFo= github.com/minio/sha256-simd v0.1.1 h1:5QHSlgo3nt5yKOJrC7W8w7X+NFl8cMPZm96iu8kKUJU=