Skip to content

Commit 47d0958

Browse files
darkowlzzhiddeco
authored andcommitted
bucket: Make NewArtifact event more informative
Use the etagIndex to provide more information about the artifact in NewArtifact events and remove the revision from the event message. The revision is still kept in the event annotations. Signed-off-by: Sunny <[email protected]>
1 parent acda998 commit 47d0958

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

controllers/bucket_controller.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ type BucketReconcilerOptions struct {
104104

105105
// bucketReconcilerFunc is the function type for all the bucket reconciler
106106
// functions.
107-
type bucketReconcilerFunc func(ctx context.Context, obj *sourcev1.Bucket, artifact *sourcev1.Artifact, dir string) (sreconcile.Result, error)
107+
type bucketReconcilerFunc func(ctx context.Context, obj *sourcev1.Bucket, index etagIndex, artifact *sourcev1.Artifact, dir string) (sreconcile.Result, error)
108108

109109
func (r *BucketReconciler) SetupWithManager(mgr ctrl.Manager) error {
110110
return r.SetupWithManagerAndOptions(mgr, BucketReconcilerOptions{})
@@ -199,6 +199,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, obj *sourcev1.Bucket,
199199
conditions.MarkReconciling(obj, "NewGeneration", "reconciling new object generation (%d)", obj.Generation)
200200
}
201201

202+
index := make(etagIndex)
202203
var artifact sourcev1.Artifact
203204

204205
// Create temp working dir
@@ -215,7 +216,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, obj *sourcev1.Bucket,
215216
var res sreconcile.Result
216217
var resErr error
217218
for _, rec := range reconcilers {
218-
recResult, err := rec(ctx, obj, &artifact, tmpDir)
219+
recResult, err := rec(ctx, obj, index, &artifact, tmpDir)
219220
// Exit immediately on ResultRequeue.
220221
if recResult == sreconcile.ResultRequeue {
221222
return sreconcile.ResultRequeue, nil
@@ -238,7 +239,8 @@ func (r *BucketReconciler) reconcile(ctx context.Context, obj *sourcev1.Bucket,
238239
// All artifacts for the resource except for the current one are garbage collected from the storage.
239240
// If the artifact in the Status object of the resource disappeared from storage, it is removed from the object.
240241
// If the hostname of the URLs on the object do not match the current storage server hostname, they are updated.
241-
func (r *BucketReconciler) reconcileStorage(ctx context.Context, obj *sourcev1.Bucket, artifact *sourcev1.Artifact, dir string) (sreconcile.Result, error) {
242+
func (r *BucketReconciler) reconcileStorage(ctx context.Context,
243+
obj *sourcev1.Bucket, _ etagIndex, artifact *sourcev1.Artifact, dir string) (sreconcile.Result, error) {
242244
// Garbage collect previous advertised artifact(s) from storage
243245
_ = r.garbageCollect(ctx, obj)
244246

@@ -266,7 +268,8 @@ func (r *BucketReconciler) reconcileStorage(ctx context.Context, obj *sourcev1.B
266268
// result.
267269
// If a SecretRef is defined, it attempts to fetch the Secret before calling the provider. If the fetch of the Secret
268270
// fails, it records v1beta1.FetchFailedCondition=True and returns early.
269-
func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bucket, artifact *sourcev1.Artifact, dir string) (sreconcile.Result, error) {
271+
func (r *BucketReconciler) reconcileSource(ctx context.Context,
272+
obj *sourcev1.Bucket, index etagIndex, artifact *sourcev1.Artifact, dir string) (sreconcile.Result, error) {
270273
var secret *corev1.Secret
271274
if obj.Spec.SecretRef != nil {
272275
secretName := types.NamespacedName{
@@ -287,9 +290,9 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bu
287290

288291
switch obj.Spec.Provider {
289292
case sourcev1.GoogleBucketProvider:
290-
return r.reconcileGCPSource(ctx, obj, artifact, secret, dir)
293+
return r.reconcileGCPSource(ctx, obj, index, artifact, secret, dir)
291294
default:
292-
return r.reconcileMinioSource(ctx, obj, artifact, secret, dir)
295+
return r.reconcileMinioSource(ctx, obj, index, artifact, secret, dir)
293296
}
294297
}
295298

@@ -302,8 +305,8 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bu
302305
// On a successful download, it removes v1beta1.FetchFailedCondition, and compares the current revision of HEAD to
303306
// the artifact on the object, and records v1beta1.ArtifactOutdatedCondition if they differ.
304307
// If the download was successful, the given artifact pointer is set to a new artifact with the available metadata.
305-
func (r *BucketReconciler) reconcileMinioSource(ctx context.Context, obj *sourcev1.Bucket, artifact *sourcev1.Artifact,
306-
secret *corev1.Secret, dir string) (sreconcile.Result, error) {
308+
func (r *BucketReconciler) reconcileMinioSource(ctx context.Context,
309+
obj *sourcev1.Bucket, index etagIndex, artifact *sourcev1.Artifact, secret *corev1.Secret, dir string) (sreconcile.Result, error) {
307310
// Build the client with the configuration from the object and secret
308311
s3Client, err := r.buildMinioClient(obj, secret)
309312
if err != nil {
@@ -367,7 +370,6 @@ func (r *BucketReconciler) reconcileMinioSource(ctx context.Context, obj *source
367370
// Build up an index of object keys and their etags
368371
// As the keys define the paths and the etags represent a change in file contents, this should be sufficient to
369372
// detect both structural and file changes
370-
var index = make(etagIndex)
371373
for object := range s3Client.ListObjects(ctxTimeout, obj.Spec.BucketName, minio.ListObjectsOptions{
372374
Recursive: true,
373375
UseV1: s3utils.IsGoogleEndpoint(*s3Client.EndpointURL()),
@@ -438,8 +440,6 @@ func (r *BucketReconciler) reconcileMinioSource(ctx context.Context, obj *source
438440
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, sourcev1.BucketOperationFailedReason, e.Err.Error())
439441
return sreconcile.ResultEmpty, e
440442
}
441-
r.eventLogf(ctx, obj, events.EventTypeTrace, sourcev1.BucketOperationSucceededReason,
442-
"fetched %d files with revision '%s' from '%s'", len(index), revision, obj.Spec.BucketName)
443443
}
444444
conditions.Delete(obj, sourcev1.FetchFailedCondition)
445445

@@ -457,8 +457,8 @@ func (r *BucketReconciler) reconcileMinioSource(ctx context.Context, obj *source
457457
// On a successful download, it removes v1beta1.DownloadFailedCondition, and compares the current revision of HEAD to
458458
// the artifact on the object, and records v1beta1.ArtifactOutdatedCondition if they differ.
459459
// If the download was successful, the given artifact pointer is set to a new artifact with the available metadata.
460-
func (r *BucketReconciler) reconcileGCPSource(ctx context.Context, obj *sourcev1.Bucket, artifact *sourcev1.Artifact,
461-
secret *corev1.Secret, dir string) (sreconcile.Result, error) {
460+
func (r *BucketReconciler) reconcileGCPSource(ctx context.Context,
461+
obj *sourcev1.Bucket, index etagIndex, artifact *sourcev1.Artifact, secret *corev1.Secret, dir string) (sreconcile.Result, error) {
462462
gcpClient, err := r.buildGCPClient(ctx, secret)
463463
if err != nil {
464464
e := &serror.Event{
@@ -522,7 +522,6 @@ func (r *BucketReconciler) reconcileGCPSource(ctx context.Context, obj *sourcev1
522522
// Build up an index of object keys and their etags
523523
// As the keys define the paths and the etags represent a change in file contents, this should be sufficient to
524524
// detect both structural and file changes
525-
var index = make(etagIndex)
526525
objects := gcpClient.ListObjects(ctxTimeout, obj.Spec.BucketName, nil)
527526
for {
528527
object, err := objects.Next()
@@ -593,8 +592,6 @@ func (r *BucketReconciler) reconcileGCPSource(ctx context.Context, obj *sourcev1
593592
conditions.MarkTrue(obj, sourcev1.FetchFailedCondition, sourcev1.BucketOperationFailedReason, e.Err.Error())
594593
return sreconcile.ResultEmpty, e
595594
}
596-
r.eventLogf(ctx, obj, events.EventTypeTrace, sourcev1.BucketOperationSucceededReason,
597-
"fetched %d files from bucket '%s'", len(index), obj.Spec.BucketName)
598595
}
599596
conditions.Delete(obj, sourcev1.FetchFailedCondition)
600597

@@ -610,7 +607,8 @@ func (r *BucketReconciler) reconcileGCPSource(ctx context.Context, obj *sourcev1
610607
// If the given artifact does not differ from the object's current, it returns early.
611608
// On a successful archive, the artifact in the status of the given object is set, and the symlink in the storage is
612609
// updated to its path.
613-
func (r *BucketReconciler) reconcileArtifact(ctx context.Context, obj *sourcev1.Bucket, artifact *sourcev1.Artifact, dir string) (sreconcile.Result, error) {
610+
func (r *BucketReconciler) reconcileArtifact(ctx context.Context,
611+
obj *sourcev1.Bucket, index etagIndex, artifact *sourcev1.Artifact, dir string) (sreconcile.Result, error) {
614612
// Always restore the Ready condition in case it got removed due to a transient error
615613
defer func() {
616614
if obj.GetArtifact().HasRevision(artifact.Revision) {
@@ -666,10 +664,10 @@ func (r *BucketReconciler) reconcileArtifact(ctx context.Context, obj *sourcev1.
666664
Reason: sourcev1.StorageOperationFailedReason,
667665
}
668666
}
669-
r.AnnotatedEventf(obj, map[string]string{
667+
r.annotatedEventLogf(ctx, obj, map[string]string{
670668
"revision": artifact.Revision,
671669
"checksum": artifact.Checksum,
672-
}, corev1.EventTypeNormal, "NewArtifact", "stored artifact for revision '%s'", artifact.Revision)
670+
}, corev1.EventTypeNormal, "NewArtifact", "fetched %d files from '%s'", len(index), obj.Spec.BucketName)
673671

674672
// Record it on the object
675673
obj.Status.Artifact = artifact.DeepCopy()
@@ -803,16 +801,23 @@ func (i etagIndex) Revision() (string, error) {
803801
return fmt.Sprintf("%x", sum.Sum(nil)), nil
804802
}
805803

806-
// eventLog records event and logs at the same time. This log is different from
807-
// the debug log in the event recorder in the sense that this is a simple log,
808-
// the event recorder debug log contains complete details about the event.
804+
// eventLogf records event and logs at the same time.
809805
func (r *BucketReconciler) eventLogf(ctx context.Context, obj runtime.Object, eventType string, reason string, messageFmt string, args ...interface{}) {
806+
r.annotatedEventLogf(ctx, obj, nil, eventType, reason, messageFmt, args...)
807+
}
808+
809+
// annotatedEventLogf records annotated event and logs at the same time. This
810+
// log is different from the debug log in the event recorder in the sense that
811+
// this is a simple log, the event recorder debug log contains complete details
812+
// about the event.
813+
func (r *BucketReconciler) annotatedEventLogf(ctx context.Context,
814+
obj runtime.Object, annotations map[string]string, eventType string, reason string, messageFmt string, args ...interface{}) {
810815
msg := fmt.Sprintf(messageFmt, args...)
811816
// Log and emit event.
812817
if eventType == corev1.EventTypeWarning {
813818
ctrl.LoggerFrom(ctx).Error(errors.New(reason), msg)
814819
} else {
815820
ctrl.LoggerFrom(ctx).Info(msg)
816821
}
817-
r.Eventf(obj, eventType, reason, msg)
822+
r.AnnotatedEventf(obj, annotations, eventType, reason, msg)
818823
}

controllers/bucket_controller_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,10 @@ func TestBucketReconciler_reconcileStorage(t *testing.T) {
264264
g.Expect(tt.beforeFunc(obj, testStorage)).To(Succeed())
265265
}
266266

267+
index := make(etagIndex)
267268
var artifact sourcev1.Artifact
268269

269-
got, err := r.reconcileStorage(context.TODO(), obj, &artifact, "")
270+
got, err := r.reconcileStorage(context.TODO(), obj, index, &artifact, "")
270271
g.Expect(err != nil).To(Equal(tt.wantErr))
271272
g.Expect(got).To(Equal(tt.want))
272273

@@ -549,7 +550,8 @@ func TestBucketReconciler_reconcileMinioSource(t *testing.T) {
549550
}
550551

551552
artifact := &sourcev1.Artifact{}
552-
got, err := r.reconcileSource(context.TODO(), obj, artifact, tmpDir)
553+
index := make(etagIndex)
554+
got, err := r.reconcileSource(context.TODO(), obj, index, artifact, tmpDir)
553555
g.Expect(err != nil).To(Equal(tt.wantErr))
554556
g.Expect(got).To(Equal(tt.want))
555557

@@ -828,7 +830,8 @@ func TestBucketReconciler_reconcileGCPSource(t *testing.T) {
828830
}()
829831

830832
artifact := &sourcev1.Artifact{}
831-
got, err := r.reconcileSource(context.TODO(), obj, artifact, tmpDir)
833+
index := make(etagIndex)
834+
got, err := r.reconcileSource(context.TODO(), obj, index, artifact, tmpDir)
832835
g.Expect(err != nil).To(Equal(tt.wantErr))
833836
g.Expect(got).To(Equal(tt.want))
834837

@@ -965,14 +968,15 @@ func TestBucketReconciler_reconcileArtifact(t *testing.T) {
965968
},
966969
}
967970

971+
index := make(etagIndex)
968972
artifact := testStorage.NewArtifactFor(obj.Kind, obj, "existing", "foo.tar.gz")
969973
artifact.Checksum = testChecksum
970974

971975
if tt.beforeFunc != nil {
972976
tt.beforeFunc(g, obj, artifact, tmpDir)
973977
}
974978

975-
got, err := r.reconcileArtifact(context.TODO(), obj, &artifact, tmpDir)
979+
got, err := r.reconcileArtifact(context.TODO(), obj, index, &artifact, tmpDir)
976980
g.Expect(err != nil).To(Equal(tt.wantErr))
977981
g.Expect(got).To(Equal(tt.want))
978982

0 commit comments

Comments
 (0)