Skip to content
Open
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
29 changes: 23 additions & 6 deletions reconcilers/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type ResourceReconciler[Type client.Object] struct {
AfterReconcile func(ctx context.Context, req Request, res Result, err error) (Result, error)

// SkipResource shortcuts the reconciler for the specific request. While the context and logger
// are initialized, no work is preformed. The request is removed from the workqueue.
// are initialized, no work is performed. The request is removed from the workqueue.
//
// +optional
SkipRequest func(ctx context.Context, req Request) bool
Expand All @@ -131,6 +131,12 @@ type ResourceReconciler[Type client.Object] struct {
// +optional
SkipResource func(ctx context.Context, resource Type) bool

// IfNotFound is called if the resource doesn't exist yet. Return nil if the reconcile should
// be aborted.
//
// +optional
IfNotFound func(ctx context.Context, req Request) Type

Config Config

lazyInit sync.Once
Expand All @@ -142,6 +148,12 @@ func (r *ResourceReconciler[T]) init() {
var nilT T
r.Type = newEmpty(nilT).(T)
}
if r.IfNotFound == nil {
r.IfNotFound = func(ctx context.Context, req Request) T {
var nilT T
return nilT
}
}
if r.Name == "" {
r.Name = fmt.Sprintf("%sResourceReconciler", typeName(r.Type))
}
Expand Down Expand Up @@ -320,13 +332,18 @@ func (r *ResourceReconciler[T]) reconcileOuter(ctx context.Context, req Request)

originalResource := r.Type.DeepCopyObject().(T)

if err := c.Get(ctx, req.NamespacedName, originalResource); err != nil {
if apierrs.IsNotFound(err) {
// we'll ignore not-found errors, since they can't be fixed by an immediate
// requeue (we'll need to wait for a new notification), and we can get them
// on deleted requests.
err := c.Get(ctx, req.NamespacedName, originalResource)
switch {
case err == nil:
// all good, continue
case apierrs.IsNotFound(err):
// we'll ignore not-found errors, since they can't be fixed by an immediate
// requeue (we'll need to wait for a new notification), and we can get them
// on deleted requests – except if `IfNotFound` returns a new resource
if originalResource = r.IfNotFound(ctx, req); internal.IsNil(originalResource) {
return Result{}, nil
}
default:
if !errors.Is(err, ErrQuiet) {
log.Error(err, "unable to fetch resource")
}
Expand Down
Loading