Skip to content

Commit f59ee37

Browse files
committed
Add more BucketReconciler tests
Signed-off-by: Hidde Beydals <[email protected]>
1 parent 7b7e119 commit f59ee37

File tree

7 files changed

+516
-40
lines changed

7 files changed

+516
-40
lines changed

controllers/bucket_controller.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,15 @@ func (r *BucketReconciler) reconcileStorage(ctx context.Context, obj *sourcev1.B
255255
// set to a new artifact.
256256
func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bucket, artifact *sourcev1.Artifact, dir string) (ctrl.Result, error) {
257257
// Attempt to retrieve secret if one is configured
258-
secret := &corev1.Secret{}
258+
var secret *corev1.Secret
259259
if obj.Spec.SecretRef != nil {
260+
secret = &corev1.Secret{}
260261
name := types.NamespacedName{
261262
Namespace: obj.GetNamespace(),
262263
Name: obj.Spec.SecretRef.Name,
263264
}
264265
if err := r.Client.Get(ctx, name, secret); err != nil {
265-
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.AuthenticationFailedReason, "Failed to get secret %s: %s", name.String(), err.Error())
266+
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.AuthenticationFailedReason, "Failed to get secret '%s': %s", name.String(), err.Error())
266267
r.Events.Event(ctx, obj, events.EventSeverityError, sourcev1.AuthenticationFailedReason, conditions.Get(obj, sourcev1.SourceAvailableCondition).Message)
267268
// Return transient errors but wait for next interval on not found
268269
return ctrl.Result{RequeueAfter: obj.Spec.Interval.Duration}, client.IgnoreNotFound(err)
@@ -272,7 +273,7 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bu
272273
// Build the client with the configuration from the object and secret
273274
s3Client, err := r.buildClient(obj, secret)
274275
if err != nil {
275-
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Client construction failed: %s", err.Error())
276+
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Failed to construct S3 client: %s", err.Error())
276277
// Recovering from this without a change to the secret or object
277278
// is impossible
278279
return ctrl.Result{RequeueAfter: obj.Spec.Interval.Duration}, nil
@@ -283,7 +284,8 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bu
283284
// Confirm bucket exists
284285
exists, err := s3Client.BucketExists(ctxTimeout, obj.Spec.BucketName)
285286
if err != nil {
286-
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Verification of bucket %q existence failed: %s", obj.Spec.BucketName, err.Error())
287+
// Error may be transient
288+
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Failed to verify existence of bucket %q: %s", obj.Spec.BucketName, err.Error())
287289
return ctrl.Result{}, err
288290
}
289291
if !exists {
@@ -297,13 +299,13 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bu
297299
path := filepath.Join(dir, sourceignore.IgnoreFile)
298300
if err := s3Client.FGetObject(ctxTimeout, obj.Spec.BucketName, sourceignore.IgnoreFile, path, minio.GetObjectOptions{}); err != nil {
299301
if resp, ok := err.(minio.ErrorResponse); ok && resp.Code != "NoSuchKey" {
300-
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Downloading %s file failed: %s", sourceignore.IgnoreFile, err.Error())
302+
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Failed to download '%s' file: %s", sourceignore.IgnoreFile, err.Error())
301303
return ctrl.Result{}, err
302304
}
303305
}
304306
ps, err := sourceignore.ReadIgnoreFile(path, nil)
305307
if err != nil {
306-
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Reading %s file failed: %s", sourceignore.IgnoreFile, err.Error())
308+
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Failed to read '%s' file: %s", sourceignore.IgnoreFile, err.Error())
307309
return ctrl.Result{}, err
308310
}
309311
// In-spec patterns take precedence
@@ -319,7 +321,7 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bu
319321
UseV1: s3utils.IsGoogleEndpoint(*s3Client.EndpointURL()),
320322
}) {
321323
if err = object.Err; err != nil {
322-
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Listing objects from bucket %q failed: %s", obj.Spec.BucketName, err.Error())
324+
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Failed to list objects from bucket %q: %s", obj.Spec.BucketName, err.Error())
323325
return ctrl.Result{}, err
324326
}
325327

@@ -333,23 +335,23 @@ func (r *BucketReconciler) reconcileSource(ctx context.Context, obj *sourcev1.Bu
333335

334336
localPath := filepath.Join(dir, object.Key)
335337
if err = s3Client.FGetObject(ctx, obj.Spec.BucketName, object.Key, localPath, minio.GetObjectOptions{}); err != nil {
336-
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Downloading object %q from bucket %q failed: %s", object.Key, obj.Spec.BucketName, err.Error())
338+
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Failed to download object %q from bucket %q: %s", object.Key, obj.Spec.BucketName, err.Error())
337339
return ctrl.Result{}, err
338340
}
339341

340342
objCount++
341343
}
342344

343-
// Compute the checksum
344-
revision, err := r.checksum(dir)
345+
// Compute the checksum of the downloaded file contents, which is used as the revision
346+
checksum, err := r.checksum(dir)
345347
if err != nil {
346-
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Failed to compute revision: %s", err.Error())
348+
conditions.MarkFalse(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationFailedReason, "Failed to compute checksum for downloaded objects: %s", err.Error())
347349
return ctrl.Result{}, err
348350
}
349351

350352
// Create potential new artifact
351-
*artifact = r.Storage.NewArtifactFor(obj.Kind, obj, revision, fmt.Sprintf("%s.tar.gz", revision))
352-
conditions.MarkTrue(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationSucceedReason, "Downloaded %d bucket objects from %q", objCount, obj.Spec.BucketName)
353+
*artifact = r.Storage.NewArtifactFor(obj.Kind, obj, checksum, fmt.Sprintf("%s.tar.gz", checksum))
354+
conditions.MarkTrue(obj, sourcev1.SourceAvailableCondition, sourcev1.BucketOperationSucceedReason, "Downloaded %d objects from bucket %q", objCount, obj.Spec.BucketName)
353355

354356
return ctrl.Result{RequeueAfter: obj.Spec.Interval.Duration}, nil
355357
}
@@ -363,7 +365,7 @@ func (r *BucketReconciler) reconcileArtifact(ctx context.Context, obj *sourcev1.
363365
// The artifact is up-to-date
364366
if obj.GetArtifact().HasRevision(artifact.Revision) {
365367
logr.FromContext(ctx).Info("Artifact is up-to-date")
366-
conditions.MarkTrue(obj, sourcev1.ArtifactAvailableCondition, "ArchivedArtifact", "Compressed source to artifact with revision %s", artifact.Revision)
368+
conditions.MarkTrue(obj, sourcev1.ArtifactAvailableCondition, meta.SucceededReason, "Compressed source to artifact with revision '%s'", artifact.Revision)
367369
return ctrl.Result{RequeueAfter: obj.GetInterval().Duration}, nil
368370
}
369371

@@ -390,17 +392,17 @@ func (r *BucketReconciler) reconcileArtifact(ctx context.Context, obj *sourcev1.
390392
defer unlock()
391393

392394
// Archive directory to storage
393-
if err := r.Storage.Archive(&artifact, dir, nil); err != nil {
395+
if err = r.Storage.Archive(&artifact, dir, nil); err != nil {
394396
conditions.MarkFalse(obj, sourcev1.ArtifactAvailableCondition, sourcev1.StorageOperationFailedReason, "Unable to archive artifact to storage: %s", err)
395397
return ctrl.Result{}, err
396398
}
397399

398400
// Record it on the object
399401
obj.Status.Artifact = artifact.DeepCopy()
400-
conditions.MarkTrue(obj, sourcev1.ArtifactAvailableCondition, "ArchivedArtifact", "Compressed source to artifact with revision %s", artifact.Revision)
402+
conditions.MarkTrue(obj, sourcev1.ArtifactAvailableCondition, meta.SucceededReason, "Compressed source to artifact with revision '%s'", artifact.Revision)
401403
r.Events.EventWithMetaf(ctx, obj, map[string]string{
402404
"revision": obj.GetArtifact().Revision,
403-
}, events.EventSeverityInfo, sourcev1.GitOperationSucceedReason, conditions.Get(obj, sourcev1.ArtifactAvailableCondition).Message)
405+
}, events.EventSeverityInfo, meta.SucceededReason, conditions.Get(obj, sourcev1.ArtifactAvailableCondition).Message)
404406

405407
// Update symlink on a "best effort" basis
406408
url, err := r.Storage.Symlink(artifact, "latest.tar.gz")
@@ -458,10 +460,6 @@ func (r *BucketReconciler) buildClient(obj *sourcev1.Bucket, secret *corev1.Secr
458460
opts.Creds = credentials.NewIAM("")
459461
}
460462

461-
if opts.Creds == nil {
462-
return nil, fmt.Errorf("no bucket credentials configured")
463-
}
464-
465463
return minio.New(obj.Spec.Endpoint, &opts)
466464
}
467465

0 commit comments

Comments
 (0)