Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/controller/imagepolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"k8s.io/apimachinery/pkg/types"
kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/wait"
kuberecorder "k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -49,6 +48,7 @@ import (
"github.com/fluxcd/pkg/runtime/acl"
"github.com/fluxcd/pkg/runtime/conditions"
helper "github.com/fluxcd/pkg/runtime/controller"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/patch"
"github.com/fluxcd/pkg/runtime/predicates"
pkgreconcile "github.com/fluxcd/pkg/runtime/reconcile"
Expand Down Expand Up @@ -104,7 +104,7 @@ const imageRepoKey = ".spec.imageRepository"
// ImagePolicyReconciler reconciles a ImagePolicy object
type ImagePolicyReconciler struct {
client.Client
kuberecorder.EventRecorder
events.EventRecorder
helper.Metrics

ControllerName string
Expand Down Expand Up @@ -290,7 +290,7 @@ func (r *ImagePolicyReconciler) reconcile(ctx context.Context, sp *patch.SerialP
conditions.Set(obj, reconciling)
}

notify(ctx, r.EventRecorder, oldObj, obj, readyMsg)
notify(r.EventRecorder, oldObj, obj, readyMsg)
}()

// Validate errors in the spec before proceeding.
Expand Down
12 changes: 6 additions & 6 deletions internal/controller/imagepolicy_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
Expand All @@ -41,6 +40,7 @@ import (
"github.com/fluxcd/pkg/auth"
"github.com/fluxcd/pkg/runtime/acl"
"github.com/fluxcd/pkg/runtime/conditions"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/patch"

imagev1 "github.com/fluxcd/image-reflector-controller/api/v1"
Expand Down Expand Up @@ -540,7 +540,7 @@ func TestImagePolicyReconciler_intervalNotConfigured(t *testing.T) {

r := &ImagePolicyReconciler{
Client: k8sClient,
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
}

obj := &imagev1.ImagePolicy{
Expand Down Expand Up @@ -632,7 +632,7 @@ func TestImagePolicyReconciler_deleteBeforeFinalizer(t *testing.T) {

r := &ImagePolicyReconciler{
Client: k8sClient,
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
}
// NOTE: Only a real API server responds with an error in this scenario.
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: client.ObjectKeyFromObject(imagePolicy)})
Expand Down Expand Up @@ -805,7 +805,7 @@ func TestImagePolicyReconciler_getImageRepository(t *testing.T) {
clientBuilder.WithObjects(imagePolicyNS, imageRepoNS, imageRepo)

r := &ImagePolicyReconciler{
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Client: clientBuilder.Build(),
ACLOptions: tt.aclOpts,
patchOptions: getPatchOptions(imagePolicyOwnedConditions, "irc"),
Expand Down Expand Up @@ -1104,7 +1104,7 @@ func TestImagePolicyReconciler_digestReflection(t *testing.T) {
).To(Succeed(), "failed getting image repo")

r := &ImagePolicyReconciler{
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Client: c,
Database: &mockDatabase{TagData: imageRepo.Status.LastScanResult.LatestTags},
AuthOptionsGetter: &registry.AuthOptionsGetter{Client: c},
Expand Down Expand Up @@ -1260,7 +1260,7 @@ func TestImagePolicyReconciler_applyPolicy(t *testing.T) {
g := NewWithT(t)

r := &ImagePolicyReconciler{
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Database: tt.db,
patchOptions: getPatchOptions(imagePolicyOwnedConditions, "irc"),
}
Expand Down
37 changes: 9 additions & 28 deletions internal/controller/imagerepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import (
"context"
"errors"
"fmt"
"regexp"
"slices"
Expand All @@ -30,9 +29,7 @@
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
kerrors "k8s.io/apimachinery/pkg/util/errors"
kuberecorder "k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
Expand All @@ -42,12 +39,13 @@
"sigs.k8s.io/controller-runtime/pkg/predicate"
ctrreconcile "sigs.k8s.io/controller-runtime/pkg/reconcile"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
eventv1 "github.com/fluxcd/pkg/apis/event/v1"

Check failure on line 42 in internal/controller/imagerepository_controller.go

View workflow job for this annotation

GitHub Actions / analyze / codeql

no required module provides package github.com/fluxcd/pkg/apis/event/v1; to add it:
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/auth"
"github.com/fluxcd/pkg/cache"
"github.com/fluxcd/pkg/runtime/conditions"
helper "github.com/fluxcd/pkg/runtime/controller"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/patch"
"github.com/fluxcd/pkg/runtime/predicates"
"github.com/fluxcd/pkg/runtime/reconcile"
Expand Down Expand Up @@ -104,7 +102,7 @@
// ImageRepositoryReconciler reconciles a ImageRepository object
type ImageRepositoryReconciler struct {
client.Client
kuberecorder.EventRecorder
events.EventRecorder
helper.Metrics

ControllerName string
Expand Down Expand Up @@ -229,7 +227,7 @@
conditions.Set(obj, reconciling)
}

notify(ctx, r.EventRecorder, oldObj, obj, nextScanMsg)
notify(r.EventRecorder, oldObj, obj, nextScanMsg)
}()

// Check object-level workload identity feature gate.
Expand Down Expand Up @@ -470,22 +468,6 @@
return ctrl.Result{}, nil
}

// eventLogf records events, and logs at the same time.
//
// This log is different from the debug log in the EventRecorder, in the sense
// that this is a simple log. While the debug log contains complete details
// about the event.
func eventLogf(ctx context.Context, r kuberecorder.EventRecorder, obj runtime.Object, eventType string, reason string, messageFmt string, args ...interface{}) {
msg := fmt.Sprintf(messageFmt, args...)
// Log and emit event.
if eventType == corev1.EventTypeWarning {
ctrl.LoggerFrom(ctx).Error(errors.New(reason), msg)
} else {
ctrl.LoggerFrom(ctx).Info(msg)
}
r.Eventf(obj, eventType, reason, "%s", msg)
}

// filterOutTags filters the given tags through the given regular expression
// patterns and returns a list of tags that don't match with the pattern.
func filterOutTags(tags []string, patterns []string) ([]string, error) {
Expand Down Expand Up @@ -550,28 +532,27 @@

// notify emits events, logs and notification based on the resulting objects
// before and after the reconciliation.
func notify(ctx context.Context, r kuberecorder.EventRecorder, oldObj, newObj conditions.Setter, nextScanMsg string) {
func notify(r events.EventRecorder, oldObj, newObj conditions.Setter, nextScanMsg string) {
ready := conditions.Get(newObj, meta.ReadyCondition)

// Was ready before and is ready now, but the scan results have changed.
if conditions.IsReady(oldObj) && conditions.IsReady(newObj) &&
(conditions.GetMessage(oldObj, meta.ReadyCondition)) != ready.Message {
eventLogf(ctx, r, newObj, corev1.EventTypeNormal, ready.Reason, "%s", ready.Message)
r.Eventf(newObj, nil, corev1.EventTypeNormal, ready.Reason, "", "%s", ready.Message)
return
}

// Emit events when reconciliation fails or recovers from failure.

// Became ready from not ready.
if !conditions.IsReady(oldObj) && conditions.IsReady(newObj) {
eventLogf(ctx, r, newObj, corev1.EventTypeNormal, ready.Reason, "%s", ready.Message)
r.Eventf(newObj, nil, corev1.EventTypeNormal, ready.Reason, "", "%s", ready.Message)
return
}
// Not ready, failed.
if !conditions.IsReady(newObj) {
eventLogf(ctx, r, newObj, corev1.EventTypeWarning, ready.Reason, "%s", ready.Message)
r.Eventf(newObj, nil, corev1.EventTypeWarning, ready.Reason, "", "%s", ready.Message)
return
}

eventLogf(ctx, r, newObj, eventv1.EventTypeTrace, meta.SucceededReason, "%s", nextScanMsg)
r.Eventf(newObj, nil, eventv1.EventTypeTrace, meta.SucceededReason, "", "%s", nextScanMsg)
}
14 changes: 7 additions & 7 deletions internal/controller/imagerepository_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ import (
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/patch"
"github.com/fluxcd/pkg/runtime/secrets"

Expand Down Expand Up @@ -101,7 +101,7 @@ func TestImageRepositoryReconciler_deleteBeforeFinalizer(t *testing.T) {

r := &ImageRepositoryReconciler{
Client: k8sClient,
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
}
// NOTE: Only a real API server responds with an error in this scenario.
_, err := r.Reconcile(ctx, ctrl.Request{NamespacedName: client.ObjectKeyFromObject(imagerepo)})
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestImageRepositoryReconciler_shouldScan(t *testing.T) {
g := NewWithT(t)

r := &ImageRepositoryReconciler{
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Database: tt.db,
patchOptions: getPatchOptions(imageRepositoryOwnedConditions, "irc"),
}
Expand Down Expand Up @@ -369,7 +369,7 @@ func TestImageRepositoryReconciler_scan(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())

r := ImageRepositoryReconciler{
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Database: tt.db,
patchOptions: getPatchOptions(imageRepositoryOwnedConditions, "irc"),
}
Expand Down Expand Up @@ -605,7 +605,7 @@ func TestNotify(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

recorder := record.NewFakeRecorder(32)
recorder := events.NewFakeRecorder(32, false)

oldObj := &imagev1.ImageRepository{}
newObj := oldObj.DeepCopy()
Expand All @@ -614,7 +614,7 @@ func TestNotify(t *testing.T) {
tt.beforeFunc(oldObj, newObj)
}

notify(context.TODO(), recorder, oldObj, newObj, nextScanMsg)
notify(recorder, oldObj, newObj, nextScanMsg)

select {
case x, ok := <-recorder.Events:
Expand Down Expand Up @@ -699,7 +699,7 @@ func TestImageRepositoryReconciler_TLS(t *testing.T) {
Build()

r := &ImageRepositoryReconciler{
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Client: client,
patchOptions: getPatchOptions(imageRepositoryOwnedConditions, "irc"),
Database: &mockDatabase{},
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import (
apimeta "k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"

aclapi "github.com/fluxcd/pkg/apis/acl"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/acl"
"github.com/fluxcd/pkg/runtime/conditions"
conditionscheck "github.com/fluxcd/pkg/runtime/conditions/check"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/patch"

imagev1 "github.com/fluxcd/image-reflector-controller/api/v1"
Expand Down Expand Up @@ -119,7 +119,7 @@ func TestImagePolicyReconciler_crossNamespaceRefsDisallowed(t *testing.T) {
r := &ImagePolicyReconciler{
Client: builder.Build(),
Database: database.NewBadgerDatabase(testBadgerDB),
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
ACLOptions: acl.Options{
NoCrossNamespaceRefs: true,
},
Expand Down
6 changes: 3 additions & 3 deletions internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ import (
"github.com/dgraph-io/badger/v4"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/yaml"

"github.com/fluxcd/pkg/runtime/controller"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/testenv"

imagev1 "github.com/fluxcd/image-reflector-controller/api/v1"
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestMain(m *testing.M) {
if err = (&ImageRepositoryReconciler{
Client: testEnv,
Database: database.NewBadgerDatabase(testBadgerDB),
EventRecorder: record.NewFakeRecorder(256),
EventRecorder: events.NewFakeRecorder(256, false),
AuthOptionsGetter: optGetter,
}).SetupWithManager(testEnv, ImageRepositoryReconcilerOptions{
RateLimiter: controller.GetDefaultRateLimiter(),
Expand All @@ -103,7 +103,7 @@ func TestMain(m *testing.M) {
if err = (&ImagePolicyReconciler{
Client: testEnv,
Database: database.NewBadgerDatabase(testBadgerDB),
EventRecorder: record.NewFakeRecorder(256),
EventRecorder: events.NewFakeRecorder(256, false),
AuthOptionsGetter: optGetter,
DependencyRequeueInterval: 30 * time.Second,
}).SetupWithManager(testEnv, ImagePolicyReconcilerOptions{
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ func main() {

probes.SetupChecks(mgr, setupLog)

var eventRecorder *events.Recorder
if eventRecorder, err = events.NewRecorder(mgr, ctrl.Log, eventsAddr, controllerName); err != nil {
eventRecorder, err := events.NewRecorder(mgr, ctrl.Log, eventsAddr, controllerName)
if err != nil {
setupLog.Error(err, "unable to create event recorder")
os.Exit(1)
}
Expand Down
Loading