@@ -35,12 +35,9 @@ import (
3535 "github.com/go-logr/logr"
3636 libgit2 "github.com/libgit2/git2go/v33"
3737 corev1 "k8s.io/api/core/v1"
38- apimeta "k8s.io/apimachinery/pkg/api/meta"
3938 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
40- "k8s.io/apimachinery/pkg/runtime"
4139 "k8s.io/apimachinery/pkg/types"
4240 kuberecorder "k8s.io/client-go/tools/record"
43- "k8s.io/client-go/tools/reference"
4441 ctrl "sigs.k8s.io/controller-runtime"
4542 "sigs.k8s.io/controller-runtime/pkg/builder"
4643 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -56,9 +53,9 @@ import (
5653 apiacl "github.com/fluxcd/pkg/apis/acl"
5754 "github.com/fluxcd/pkg/apis/meta"
5855 "github.com/fluxcd/pkg/runtime/acl"
56+ helper "github.com/fluxcd/pkg/runtime/controller"
5957 "github.com/fluxcd/pkg/runtime/events"
6058 "github.com/fluxcd/pkg/runtime/logger"
61- "github.com/fluxcd/pkg/runtime/metrics"
6259 "github.com/fluxcd/pkg/runtime/predicates"
6360 sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
6461 "github.com/fluxcd/source-controller/pkg/git"
@@ -87,9 +84,9 @@ type TemplateData struct {
8784// ImageUpdateAutomationReconciler reconciles a ImageUpdateAutomation object
8885type ImageUpdateAutomationReconciler struct {
8986 client.Client
90- Scheme * runtime. Scheme
91- EventRecorder kuberecorder. EventRecorder
92- MetricsRecorder * metrics. Recorder
87+ EventRecorder kuberecorder. EventRecorder
88+ helper. Metrics
89+
9390 NoCrossNamespaceRef bool
9491}
9592
@@ -107,7 +104,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
107104 log := ctrl .LoggerFrom (ctx )
108105 debuglog := log .V (logger .DebugLevel )
109106 tracelog := log .V (logger .TraceLevel )
110- now := time .Now ()
107+ start := time .Now ()
111108 var templateValues TemplateData
112109
113110 var auto imagev1.ImageUpdateAutomation
@@ -127,7 +124,6 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
127124
128125 // If the object is under deletion, record the readiness, and remove our finalizer.
129126 if ! auto .ObjectMeta .DeletionTimestamp .IsZero () {
130- r .recordReadinessMetric (ctx , & auto )
131127 controllerutil .RemoveFinalizer (& auto , imagev1 .ImageUpdateAutomationFinalizer )
132128 if err := r .Update (ctx , & auto ); err != nil {
133129 return ctrl.Result {}, err
@@ -136,7 +132,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
136132 }
137133
138134 // record suspension metrics
139- defer r . recordSuspension (ctx , auto )
135+ r . RecordSuspend (ctx , & auto , auto . Spec . Suspend )
140136
141137 if auto .Spec .Suspend {
142138 log .Info ("ImageUpdateAutomation is suspended, skipping automation run" )
@@ -145,18 +141,11 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
145141
146142 templateValues .AutomationObject = req .NamespacedName
147143
148- // Record readiness metric when exiting; if there's any points at
149- // which the readiness is updated _without also exiting_, they
150- // should also record the readiness.
151- defer r .recordReadinessMetric (ctx , & auto )
152- // Record reconciliation duration when exiting
153- if r .MetricsRecorder != nil {
154- objRef , err := reference .GetReference (r .Scheme , & auto )
155- if err != nil {
156- return ctrl.Result {}, err
157- }
158- defer r .MetricsRecorder .RecordDuration (* objRef , now )
159- }
144+ defer func () {
145+ // Always record readiness and duration metrics
146+ r .Metrics .RecordReadiness (ctx , & auto )
147+ r .Metrics .RecordDuration (ctx , & auto , start )
148+ }()
160149
161150 // whatever else happens, we've now "seen" the reconcile
162151 // annotation if it's there
@@ -405,12 +394,12 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
405394 r .event (ctx , auto , events .EventSeverityInfo , fmt .Sprintf ("Committed and pushed change %s to %s\n %s" , rev , pushBranch , message ))
406395 log .Info ("pushed commit to origin" , "revision" , rev , "branch" , pushBranch )
407396 auto .Status .LastPushCommit = rev
408- auto .Status .LastPushTime = & metav1.Time {Time : now }
397+ auto .Status .LastPushTime = & metav1.Time {Time : start }
409398 statusMessage = "committed and pushed " + rev + " to " + pushBranch
410399 }
411400
412401 // Getting to here is a successful run.
413- auto .Status .LastAutomationRunTime = & metav1.Time {Time : now }
402+ auto .Status .LastAutomationRunTime = & metav1.Time {Time : start }
414403 imagev1 .SetImageUpdateAutomationReadiness (& auto , metav1 .ConditionTrue , imagev1 .ReconciliationSucceededReason , statusMessage )
415404 if err := r .patchStatus (ctx , req , auto .Status ); err != nil {
416405 return ctrl.Result {Requeue : true }, err
@@ -923,26 +912,6 @@ func (r *ImageUpdateAutomationReconciler) event(ctx context.Context, auto imagev
923912 r .EventRecorder .Eventf (& auto , eventtype , severity , msg )
924913}
925914
926- func (r * ImageUpdateAutomationReconciler ) recordReadinessMetric (ctx context.Context , auto * imagev1.ImageUpdateAutomation ) {
927- if r .MetricsRecorder == nil {
928- return
929- }
930-
931- objRef , err := reference .GetReference (r .Scheme , auto )
932- if err != nil {
933- ctrl .LoggerFrom (ctx ).Error (err , "unable to record readiness metric" )
934- return
935- }
936- if rc := apimeta .FindStatusCondition (auto .Status .Conditions , meta .ReadyCondition ); rc != nil {
937- r .MetricsRecorder .RecordCondition (* objRef , * rc , ! auto .DeletionTimestamp .IsZero ())
938- } else {
939- r .MetricsRecorder .RecordCondition (* objRef , metav1.Condition {
940- Type : meta .ReadyCondition ,
941- Status : metav1 .ConditionUnknown ,
942- }, ! auto .DeletionTimestamp .IsZero ())
943- }
944- }
945-
946915// --- updates
947916
948917// updateAccordingToSetters updates files under the root by treating
@@ -951,25 +920,6 @@ func updateAccordingToSetters(ctx context.Context, tracelog logr.Logger, path st
951920 return update .UpdateWithSetters (tracelog , path , path , policies )
952921}
953922
954- func (r * ImageUpdateAutomationReconciler ) recordSuspension (ctx context.Context , auto imagev1.ImageUpdateAutomation ) {
955- if r .MetricsRecorder == nil {
956- return
957- }
958- log := ctrl .LoggerFrom (ctx )
959-
960- objRef , err := reference .GetReference (r .Scheme , & auto )
961- if err != nil {
962- log .Error (err , "unable to record suspended metric" )
963- return
964- }
965-
966- if ! auto .DeletionTimestamp .IsZero () {
967- r .MetricsRecorder .RecordSuspend (* objRef , false )
968- } else {
969- r .MetricsRecorder .RecordSuspend (* objRef , auto .Spec .Suspend )
970- }
971- }
972-
973923// templateMsg renders a msg template, returning the message or an error.
974924func templateMsg (messageTemplate string , templateValues * TemplateData ) (string , error ) {
975925 if messageTemplate == "" {
0 commit comments