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
20 changes: 11 additions & 9 deletions internal/controller/bucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
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/client"
Expand All @@ -42,14 +41,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
eventv1 "github.com/fluxcd/pkg/apis/event/v1"
"github.com/fluxcd/pkg/apis/meta"
intdigest "github.com/fluxcd/pkg/artifact/digest"
"github.com/fluxcd/pkg/artifact/storage"
"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/jitter"
"github.com/fluxcd/pkg/runtime/patch"
"github.com/fluxcd/pkg/runtime/predicates"
Expand Down Expand Up @@ -125,7 +125,7 @@ var bucketFailConditions = []string{
// BucketReconciler reconciles a v1.Bucket object.
type BucketReconciler struct {
client.Client
kuberecorder.EventRecorder
events.EventRecorder
helper.Metrics

Storage *storage.Storage
Expand Down Expand Up @@ -347,13 +347,13 @@ func (r *BucketReconciler) notify(ctx context.Context, oldObj, newObj *sourcev1.

// Notify on new artifact and failure recovery.
if !oldObj.GetArtifact().HasDigest(newObj.GetArtifact().Digest) {
r.AnnotatedEventf(newObj, annotations, corev1.EventTypeNormal,
"NewArtifact", message)
r.AnnotatedEventf(newObj, nil, annotations, corev1.EventTypeNormal,
"NewArtifact", eventv1.ActionApplied, message)
ctrl.LoggerFrom(ctx).Info(message)
} else {
if sreconcile.FailureRecovery(oldObj, newObj, bucketFailConditions) {
r.AnnotatedEventf(newObj, annotations, corev1.EventTypeNormal,
meta.SucceededReason, message)
r.AnnotatedEventf(newObj, nil, annotations, corev1.EventTypeNormal,
meta.SucceededReason, eventv1.ActionReconciled, message)
ctrl.LoggerFrom(ctx).Info(message)
}
}
Expand Down Expand Up @@ -387,7 +387,7 @@ func (r *BucketReconciler) reconcileStorage(ctx context.Context, sp *patch.Seria
// matches the actual artifact
if !artifactMissing {
if err := r.Storage.VerifyArtifact(*artifact); err != nil {
r.Eventf(obj, corev1.EventTypeWarning, "ArtifactVerificationFailed", "failed to verify integrity of artifact: %s", err.Error())
r.Eventf(obj, nil, corev1.EventTypeWarning, "ArtifactVerificationFailed", eventv1.ActionFailed, "failed to verify integrity of artifact: %s", err.Error())

if err = r.Storage.Remove(*artifact); err != nil {
return sreconcile.ResultEmpty, fmt.Errorf("failed to remove artifact after digest mismatch: %w", err)
Expand Down Expand Up @@ -665,13 +665,15 @@ func (r *BucketReconciler) eventLogf(ctx context.Context, obj runtime.Object, ev
func (r *BucketReconciler) annotatedEventLogf(ctx context.Context,
obj runtime.Object, annotations map[string]string, eventType string, reason string, messageFmt string, args ...interface{}) {
msg := fmt.Sprintf(messageFmt, args...)
action := eventv1.ActionReconciled
// Log and emit event.
if eventType == corev1.EventTypeWarning {
action = eventv1.ActionFailed
ctrl.LoggerFrom(ctx).Error(errors.New(reason), msg)
} else {
ctrl.LoggerFrom(ctx).Info(msg)
}
r.AnnotatedEventf(obj, annotations, eventType, reason, msg)
r.AnnotatedEventf(obj, nil, annotations, eventType, reason, action, messageFmt, args...)
}

// fetchEtagIndex fetches the current etagIndex for the in the obj specified
Expand Down
53 changes: 37 additions & 16 deletions internal/controller/bucket_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,22 @@ import (

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
eventsv1 "k8s.io/api/events/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"
fakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"

kstatus "github.com/fluxcd/cli-utils/pkg/kstatus/status"

eventv1 "github.com/fluxcd/pkg/apis/event/v1"
"github.com/fluxcd/pkg/apis/meta"
intdigest "github.com/fluxcd/pkg/artifact/digest"
"github.com/fluxcd/pkg/artifact/storage"
"github.com/fluxcd/pkg/auth"
"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/jitter"
"github.com/fluxcd/pkg/runtime/patch"

Expand Down Expand Up @@ -85,7 +88,7 @@ func TestBucketReconciler_deleteBeforeFinalizer(t *testing.T) {

r := &BucketReconciler{
Client: k8sClient,
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Storage: testStorage,
}
// NOTE: Only a real API server responds with an error in this scenario.
Expand Down Expand Up @@ -383,7 +386,7 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
WithScheme(testEnv.GetScheme()).
WithStatusSubresource(&sourcev1.Bucket{}).
Build(),
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Storage: testStorage,
patchOptions: getPatchOptions(bucketReadyCondition.Owned, "sc"),
}
Expand Down Expand Up @@ -918,7 +921,7 @@ func TestBucketReconciler_reconcileSource_generic(t *testing.T) {
}

r := &BucketReconciler{
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Client: clientBuilder.Build(),
Storage: testStorage,
patchOptions: getPatchOptions(bucketReadyCondition.Owned, "sc"),
Expand Down Expand Up @@ -1385,7 +1388,7 @@ func TestBucketReconciler_reconcileSource_gcs(t *testing.T) {
}

r := &BucketReconciler{
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Client: clientBuilder.Build(),
Storage: testStorage,
patchOptions: getPatchOptions(bucketReadyCondition.Owned, "sc"),
Expand Down Expand Up @@ -1589,7 +1592,7 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {

r := &BucketReconciler{
Client: clientBuilder.Build(),
EventRecorder: record.NewFakeRecorder(32),
EventRecorder: events.NewFakeRecorder(32, false),
Storage: testStorage,
patchOptions: getPatchOptions(bucketReadyCondition.Owned, "sc"),
}
Expand Down Expand Up @@ -1712,7 +1715,7 @@ func TestBucketReconciler_statusConditions(t *testing.T) {
}

ctx := context.TODO()
summarizeHelper := summarize.NewHelper(record.NewFakeRecorder(32), serialPatcher)
summarizeHelper := summarize.NewHelper(events.NewFakeRecorder(32, false), serialPatcher)
summarizeOpts := []summarize.Option{
summarize.WithConditions(bucketReadyCondition),
summarize.WithReconcileResult(sreconcile.ResultSuccess),
Expand All @@ -1739,7 +1742,7 @@ func TestBucketReconciler_notify(t *testing.T) {
resErr error
oldObjBeforeFunc func(obj *sourcev1.Bucket)
newObjBeforeFunc func(obj *sourcev1.Bucket)
wantEvent string
wantEvent *eventsv1.Event
}{
{
name: "error - no event",
Expand All @@ -1753,7 +1756,12 @@ func TestBucketReconciler_notify(t *testing.T) {
newObjBeforeFunc: func(obj *sourcev1.Bucket) {
obj.Status.Artifact = &meta.Artifact{Revision: "xxx", Digest: "yyy"}
},
wantEvent: "Normal NewArtifact stored artifact with 2 fetched files from",
wantEvent: &eventsv1.Event{
Type: "Normal",
Reason: "NewArtifact",
Action: eventv1.ActionApplied,
Note: "stored artifact with 2 fetched files from",
},
},
{
name: "recovery from failure",
Expand All @@ -1768,7 +1776,12 @@ func TestBucketReconciler_notify(t *testing.T) {
obj.Status.Artifact = &meta.Artifact{Revision: "xxx", Digest: "yyy"}
conditions.MarkTrue(obj, meta.ReadyCondition, meta.SucceededReason, "ready")
},
wantEvent: "Normal Succeeded stored artifact with 2 fetched files from",
wantEvent: &eventsv1.Event{
Type: "Normal",
Reason: "Succeeded",
Action: eventv1.ActionReconciled,
Note: "stored artifact with 2 fetched files from",
},
},
{
name: "recovery and new artifact",
Expand All @@ -1783,7 +1796,12 @@ func TestBucketReconciler_notify(t *testing.T) {
obj.Status.Artifact = &meta.Artifact{Revision: "aaa", Digest: "bbb"}
conditions.MarkTrue(obj, meta.ReadyCondition, meta.SucceededReason, "ready")
},
wantEvent: "Normal NewArtifact stored artifact with 2 fetched files from",
wantEvent: &eventsv1.Event{
Type: "Normal",
Reason: "NewArtifact",
Action: eventv1.ActionApplied,
Note: "stored artifact with 2 fetched files from",
},
},
{
name: "no updates",
Expand All @@ -1804,7 +1822,7 @@ func TestBucketReconciler_notify(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

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

oldObj := &sourcev1.Bucket{
Spec: sourcev1.BucketSpec{
Expand Down Expand Up @@ -1832,12 +1850,15 @@ func TestBucketReconciler_notify(t *testing.T) {

select {
case x, ok := <-recorder.Events:
g.Expect(ok).To(Equal(tt.wantEvent != ""), "unexpected event received")
if tt.wantEvent != "" {
g.Expect(x).To(ContainSubstring(tt.wantEvent))
g.Expect(ok).To(Equal(tt.wantEvent != nil), "unexpected event received")
if tt.wantEvent != nil {
g.Expect(x.Type).To(Equal(tt.wantEvent.Type))
g.Expect(x.Reason).To(Equal(tt.wantEvent.Reason))
g.Expect(x.Action).To(Equal(tt.wantEvent.Action))
g.Expect(x.Note).To(ContainSubstring(tt.wantEvent.Note))
}
default:
if tt.wantEvent != "" {
if tt.wantEvent != nil {
t.Errorf("expected some event to be emitted")
}
}
Expand Down
20 changes: 11 additions & 9 deletions internal/controller/gitrepository_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import (
"github.com/fluxcd/pkg/auth"
"github.com/fluxcd/pkg/auth/githubapp"
authutils "github.com/fluxcd/pkg/auth/utils"
"github.com/fluxcd/pkg/runtime/events"
"github.com/fluxcd/pkg/runtime/logger"
"github.com/fluxcd/pkg/runtime/secrets"
"github.com/go-git/go-git/v5/plumbing/transport"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
kuberecorder "k8s.io/client-go/tools/record"
"k8s.io/client-go/util/workqueue"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -47,7 +47,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
eventv1 "github.com/fluxcd/pkg/apis/event/v1"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/artifact/storage"
"github.com/fluxcd/pkg/cache"
Expand Down Expand Up @@ -129,7 +129,7 @@ func getPatchOptions(ownedConditions []string, controllerName string) []patch.Op
// GitRepositoryReconciler reconciles a v1.GitRepository object.
type GitRepositoryReconciler struct {
client.Client
kuberecorder.EventRecorder
events.EventRecorder
helper.Metrics

Storage *storage.Storage
Expand Down Expand Up @@ -341,13 +341,13 @@ func (r *GitRepositoryReconciler) notify(ctx context.Context, oldObj, newObj *so

// Notify on new artifact and failure recovery.
if !oldObj.GetArtifact().HasDigest(newObj.GetArtifact().Digest) {
r.AnnotatedEventf(newObj, annotations, corev1.EventTypeNormal,
"NewArtifact", message)
r.AnnotatedEventf(newObj, nil, annotations, corev1.EventTypeNormal,
"NewArtifact", eventv1.ActionApplied, message)
ctrl.LoggerFrom(ctx).Info(message)
} else {
if sreconcile.FailureRecovery(oldObj, newObj, gitRepositoryFailConditions) {
r.AnnotatedEventf(newObj, annotations, corev1.EventTypeNormal,
meta.SucceededReason, message)
r.AnnotatedEventf(newObj, nil, annotations, corev1.EventTypeNormal,
meta.SucceededReason, eventv1.ActionReconciled, message)
ctrl.LoggerFrom(ctx).Info(message)
}
}
Expand Down Expand Up @@ -401,7 +401,7 @@ func (r *GitRepositoryReconciler) reconcileStorage(ctx context.Context, sp *patc
// matches the actual artifact
if !artifactMissing {
if err := r.Storage.VerifyArtifact(*artifact); err != nil {
r.Eventf(obj, corev1.EventTypeWarning, "ArtifactVerificationFailed", "failed to verify integrity of artifact: %s", err.Error())
r.Eventf(obj, nil, corev1.EventTypeWarning, "ArtifactVerificationFailed", eventv1.ActionFailed, "failed to verify integrity of artifact: %s", err.Error())

if err = r.Storage.Remove(*artifact); err != nil {
return sreconcile.ResultEmpty, fmt.Errorf("failed to remove artifact after digest mismatch: %w", err)
Expand Down Expand Up @@ -1229,13 +1229,15 @@ func (r *GitRepositoryReconciler) garbageCollect(ctx context.Context, obj *sourc
// about the event.
func (r *GitRepositoryReconciler) eventLogf(ctx context.Context, obj runtime.Object, eventType string, reason string, messageFmt string, args ...interface{}) {
msg := fmt.Sprintf(messageFmt, args...)
action := eventv1.ActionReconciled
// Log and emit event.
if eventType == corev1.EventTypeWarning {
action = eventv1.ActionFailed
ctrl.LoggerFrom(ctx).Error(errors.New(reason), msg)
} else {
ctrl.LoggerFrom(ctx).Info(msg)
}
r.Eventf(obj, eventType, reason, msg)
r.Eventf(obj, nil, eventType, reason, action, msg)
}

// gitContentConfigChanged evaluates the current spec with the observations of
Expand Down
Loading