Skip to content

Commit a7435ed

Browse files
author
Paulo Gomes
authored
Merge pull request #431 from pjbgf/align-output
Aligns output with source-controller on no-ops
2 parents 4978ee0 + 06f4acd commit a7435ed

File tree

5 files changed

+17
-76
lines changed

5 files changed

+17
-76
lines changed

controllers/imageupdateautomation_controller.go

Lines changed: 14 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -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
8885
type 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
@@ -388,7 +377,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
388377
return failWithError(err)
389378
}
390379

391-
debuglog.Info("no changes made in working directory; no commit")
380+
log.Info("no changes made in working directory; no commit")
392381
statusMessage = "no updates made"
393382

394383
if auto.Status.LastPushTime != nil && len(auto.Status.LastPushCommit) >= 7 {
@@ -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.
974924
func templateMsg(messageTemplate string, templateValues *TemplateData) (string, error) {
975925
if messageTemplate == "" {

controllers/suite_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func TestMain(m *testing.M) {
7070
controllerName := "image-automation-controller"
7171
if err := (&ImageUpdateAutomationReconciler{
7272
Client: testEnv,
73-
Scheme: scheme.Scheme,
7473
EventRecorder: testEnv.GetEventRecorderFor(controllerName),
7574
}).SetupWithManager(testEnv, ImageUpdateAutomationReconcilerOptions{}); err != nil {
7675
panic(fmt.Sprintf("Failed to start ImageUpdateAutomationReconciler: %v", err))

controllers/update_test.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
apimeta "k8s.io/apimachinery/pkg/api/meta"
4444
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4545
"k8s.io/apimachinery/pkg/types"
46-
"k8s.io/client-go/kubernetes/scheme"
4746
ctrl "sigs.k8s.io/controller-runtime"
4847
"sigs.k8s.io/controller-runtime/pkg/client"
4948
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
@@ -250,7 +249,6 @@ func TestImageAutomationReconciler_crossNamespaceRef(t *testing.T) {
250249
builder := fakeclient.NewClientBuilder().WithScheme(testEnv.Scheme())
251250
r := &ImageUpdateAutomationReconciler{
252251
Client: builder.Build(),
253-
Scheme: scheme.Scheme,
254252
EventRecorder: testEnv.GetEventRecorderFor("image-automation-controller"),
255253
NoCrossNamespaceRef: true,
256254
}
@@ -731,7 +729,6 @@ func TestImageAutomationReconciler_e2e(t *testing.T) {
731729
// explicitly.
732730
imageAutoReconciler := &ImageUpdateAutomationReconciler{
733731
Client: testEnv,
734-
Scheme: scheme.Scheme,
735732
}
736733

737734
// Wait for the suspension to reach the cache

main.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2727
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
2828
ctrl "sigs.k8s.io/controller-runtime"
29-
ctrlmetrics "sigs.k8s.io/controller-runtime/pkg/metrics"
3029

3130
imagev1_reflect "github.com/fluxcd/image-reflector-controller/api/v1beta1"
3231
"github.com/fluxcd/pkg/runtime/acl"
@@ -36,7 +35,6 @@ import (
3635
feathelper "github.com/fluxcd/pkg/runtime/features"
3736
"github.com/fluxcd/pkg/runtime/leaderelection"
3837
"github.com/fluxcd/pkg/runtime/logger"
39-
"github.com/fluxcd/pkg/runtime/metrics"
4038
"github.com/fluxcd/pkg/runtime/pprof"
4139
"github.com/fluxcd/pkg/runtime/probes"
4240
sourcev1 "github.com/fluxcd/source-controller/api/v1beta2"
@@ -115,9 +113,6 @@ func main() {
115113
os.Exit(1)
116114
}
117115

118-
metricsRecorder := metrics.NewRecorder()
119-
ctrlmetrics.Registry.MustRegister(metricsRecorder.Collectors()...)
120-
121116
watchNamespace := ""
122117
if !watchAllNamespaces {
123118
watchNamespace = os.Getenv("RUNTIME_NAMESPACE")
@@ -151,11 +146,12 @@ func main() {
151146
os.Exit(1)
152147
}
153148

149+
metricsH := helper.MustMakeMetrics(mgr)
150+
154151
if err = (&controllers.ImageUpdateAutomationReconciler{
155152
Client: mgr.GetClient(),
156-
Scheme: mgr.GetScheme(),
157153
EventRecorder: eventRecorder,
158-
MetricsRecorder: metricsRecorder,
154+
Metrics: metricsH,
159155
NoCrossNamespaceRef: aclOptions.NoCrossNamespaceRefs,
160156
}).SetupWithManager(mgr, controllers.ImageUpdateAutomationReconcilerOptions{
161157
MaxConcurrentReconciles: concurrent,

tests/fuzz/image_update_fuzzer.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,6 @@ func FuzzImageUpdateReconciler(data []byte) int {
178178
utilruntime.Must(ensureDependencies(func(m manager.Manager) {
179179
utilruntime.Must((&controllers.ImageUpdateAutomationReconciler{
180180
Client: m.GetClient(),
181-
Scheme: scheme.Scheme,
182181
}).SetupWithManager(m, controllers.ImageUpdateAutomationReconcilerOptions{MaxConcurrentReconciles: 4}))
183182
}))
184183
})

0 commit comments

Comments
 (0)