mirror of https://github.com/knative/pkg.git
Add possibility to implement a deletion handler in a reconciler. (#2089)
* Add possibility to implement a deletion handler in a reconciler. ObserveFinalizeKind is not guaranteed to be called for every resource that gets deleted, even if they do have a finalizer. The leader can race the observers in removing the finalizer so the observers would not even see the deletion. Using the OnDelete handler on the informer is equally racy in that it can trigger the deletion handler while a potential "normal" reconcile is still in-flight. This adds an ObserveDeletedKind handler that can be implemented by reconcilers. The events still go via the workQueue, so proper order and deduplication is guaranteed. Observation is guaranteed as well. In most if not all cases, this handler should replace the ObserveFinalizeKind handler. * Move interface to its own file * Fix license * Fix comment
This commit is contained in:
parent
b80a192625
commit
2e62ba6a9d
|
|
@ -212,8 +212,15 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error {
|
|||
original, err := getter.Get(s.name)
|
||||
|
||||
if errors.IsNotFound(err) {
|
||||
// The resource may no longer exist, in which case we stop processing.
|
||||
// The resource may no longer exist, in which case we stop processing and call
|
||||
// the ObserveDeletion handler if appropriate.
|
||||
logger.Debugf("Resource %q no longer exists", key)
|
||||
if del, ok := r.reconciler.(reconciler.OnDeletionInterface); ok {
|
||||
return del.ObserveDeletion(ctx, types.NamespacedName{
|
||||
Namespace: s.namespace,
|
||||
Name: s.name,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -212,8 +212,15 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error {
|
|||
original, err := getter.Get(s.name)
|
||||
|
||||
if errors.IsNotFound(err) {
|
||||
// The resource may no longer exist, in which case we stop processing.
|
||||
// The resource may no longer exist, in which case we stop processing and call
|
||||
// the ObserveDeletion handler if appropriate.
|
||||
logger.Debugf("Resource %q no longer exists", key)
|
||||
if del, ok := r.reconciler.(reconciler.OnDeletionInterface); ok {
|
||||
return del.ObserveDeletion(ctx, types.NamespacedName{
|
||||
Namespace: s.namespace,
|
||||
Name: s.name,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -212,8 +212,15 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error {
|
|||
original, err := getter.Get(s.name)
|
||||
|
||||
if errors.IsNotFound(err) {
|
||||
// The resource may no longer exist, in which case we stop processing.
|
||||
// The resource may no longer exist, in which case we stop processing and call
|
||||
// the ObserveDeletion handler if appropriate.
|
||||
logger.Debugf("Resource %q no longer exists", key)
|
||||
if del, ok := r.reconciler.(reconciler.OnDeletionInterface); ok {
|
||||
return del.ObserveDeletion(ctx, types.NamespacedName{
|
||||
Namespace: s.namespace,
|
||||
Name: s.name,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -211,8 +211,15 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error {
|
|||
original, err := getter.Get(s.name)
|
||||
|
||||
if errors.IsNotFound(err) {
|
||||
// The resource may no longer exist, in which case we stop processing.
|
||||
// The resource may no longer exist, in which case we stop processing and call
|
||||
// the ObserveDeletion handler if appropriate.
|
||||
logger.Debugf("Resource %q no longer exists", key)
|
||||
if del, ok := r.reconciler.(reconciler.OnDeletionInterface); ok {
|
||||
return del.ObserveDeletion(ctx, types.NamespacedName{
|
||||
Namespace: s.namespace,
|
||||
Name: s.name,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -201,8 +201,15 @@ func (r *reconcilerImpl) Reconcile(ctx context.Context, key string) error {
|
|||
original, err := getter.Get(s.name)
|
||||
|
||||
if errors.IsNotFound(err) {
|
||||
// The resource may no longer exist, in which case we stop processing.
|
||||
// The resource may no longer exist, in which case we stop processing and call
|
||||
// the ObserveDeletion handler if appropriate.
|
||||
logger.Debugf("Resource %q no longer exists", key)
|
||||
if del, ok := r.reconciler.(reconciler.OnDeletionInterface); ok {
|
||||
return del.ObserveDeletion(ctx, types.NamespacedName{
|
||||
Namespace: s.namespace,
|
||||
Name: s.name,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -111,6 +111,7 @@ func (g *reconcilerReconcilerGenerator) GenerateType(c *generator.Context, t *ty
|
|||
"reconcilerReconcilerEvent": c.Universe.Type(types.Name{Package: "knative.dev/pkg/reconciler", Name: "ReconcilerEvent"}),
|
||||
"reconcilerRetryUpdateConflicts": c.Universe.Function(types.Name{Package: "knative.dev/pkg/reconciler", Name: "RetryUpdateConflicts"}),
|
||||
"reconcilerConfigStore": c.Universe.Type(types.Name{Name: "ConfigStore", Package: "knative.dev/pkg/reconciler"}),
|
||||
"reconcilerOnDeletionInterface": c.Universe.Type(types.Name{Package: "knative.dev/pkg/reconciler", Name: "OnDeletionInterface"}),
|
||||
// Deps
|
||||
"clientsetInterface": c.Universe.Type(types.Name{Name: "Interface", Package: g.clientsetPkg}),
|
||||
"resourceLister": c.Universe.Type(types.Name{Name: g.listerName, Package: g.listerPkg}),
|
||||
|
|
@ -410,8 +411,15 @@ func (r *reconcilerImpl) Reconcile(ctx {{.contextContext|raw}}, key string) erro
|
|||
original, err := getter.Get(s.name)
|
||||
|
||||
if {{.apierrsIsNotFound|raw}}(err) {
|
||||
// The resource may no longer exist, in which case we stop processing.
|
||||
// The resource may no longer exist, in which case we stop processing and call
|
||||
// the ObserveDeletion handler if appropriate.
|
||||
logger.Debugf("Resource %q no longer exists", key)
|
||||
if del, ok := r.reconciler.({{.reconcilerOnDeletionInterface|raw}}); ok {
|
||||
return del.ObserveDeletion(ctx, {{.typesNamespacedName|raw}}{
|
||||
Namespace: s.namespace,
|
||||
Name: s.name,
|
||||
})
|
||||
}
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
/*
|
||||
Copyright 2020 The Knative Authors
|
||||
|
||||
Licensed under the Apache License, Veroute.on 2.0 (the "License");
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
Copyright 2021 The Knative Authors
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package reconciler
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"k8s.io/apimachinery/pkg/types"
|
||||
)
|
||||
|
||||
// OnDeletionInterface defines the strongly typed interface to be implemented by a
|
||||
// controller observing a deletion of an object. Every controller that was active
|
||||
// during the deletion of the respective resource is guaranteed to observe this event,
|
||||
// leader or not. It's usually used to clear up in-memory state regarding the respective
|
||||
// resource. Finalizers should be used to ensure external resources are properly cleaned
|
||||
// up.
|
||||
type OnDeletionInterface interface {
|
||||
// ObserveDeletion implements custom logic to observe deletion of the respective resource
|
||||
// with the given key.
|
||||
ObserveDeletion(ctx context.Context, key types.NamespacedName) error
|
||||
}
|
||||
Loading…
Reference in New Issue