Skip to content

Commit

Permalink
fix: expired cachedimages with pull errors were not deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
paullaffitte committed Aug 7, 2024
1 parent 3188ced commit 5fe3ef2
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions internal/controller/kuik/cachedimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
repository.Spec.Name = repositoryName
return nil
})

if err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -219,6 +218,23 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, err
}

// Delete expired CachedImage and schedule deletion for expiring ones
if !expiresAt.IsZero() {
if time.Now().After(expiresAt.Time) {
log.Info("cachedimage expired, deleting it", "now", time.Now(), "expiresAt", expiresAt)
r.Recorder.Eventf(&cachedImage, "Normal", "Expiring", "Image %s has expired, deleting it", cachedImage.Spec.SourceImage)
err := r.Delete(ctx, &cachedImage)
if err != nil {
r.Recorder.Eventf(&cachedImage, "Warning", "ExpiringFailed", "Image %s could not expire: %s", cachedImage.Spec.SourceImage, err)
return ctrl.Result{}, err
}
r.Recorder.Eventf(&cachedImage, "Normal", "Expired", "Image %s successfully expired", cachedImage.Spec.SourceImage)
return ctrl.Result{}, nil
} else {
return ctrl.Result{RequeueAfter: time.Until(expiresAt.Time)}, nil
}
}

// Adding image to registry
putImageInCache := true
if isCached && !forceUpdate {
Expand Down Expand Up @@ -247,23 +263,6 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
return ctrl.Result{}, err
}

// Delete expired CachedImage and schedule deletion for expiring ones
if !expiresAt.IsZero() {
if time.Now().After(expiresAt.Time) {
log.Info("cachedimage expired, deleting it", "now", time.Now(), "expiresAt", expiresAt)
r.Recorder.Eventf(&cachedImage, "Normal", "Expiring", "Image %s has expired, deleting it", cachedImage.Spec.SourceImage)
err := r.Delete(ctx, &cachedImage)
if err != nil {
r.Recorder.Eventf(&cachedImage, "Warning", "ExpiringFailed", "Image %s could not expire: %s", cachedImage.Spec.SourceImage, err)
return ctrl.Result{}, err
}
r.Recorder.Eventf(&cachedImage, "Normal", "Expired", "Image %s successfully expired", cachedImage.Spec.SourceImage)
return ctrl.Result{}, nil
} else {
return ctrl.Result{RequeueAfter: time.Until(expiresAt.Time)}, nil
}
}

log.Info("cachedimage reconciled")
return ctrl.Result{}, nil
}
Expand Down

0 comments on commit 5fe3ef2

Please sign in to comment.