2 Copyright 2018 The Kubernetes Authors.
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
8 http://www.apache.org/licenses/LICENSE-2.0
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
20 "sigs.k8s.io/controller-runtime/pkg/event"
21 logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
24 var log = logf.KBLog.WithName("predicate").WithName("eventFilters")
26 // Predicate filters events before enqueuing the keys.
27 type Predicate interface {
28 // Create returns true if the Create event should be processed
29 Create(event.CreateEvent) bool
31 // Delete returns true if the Delete event should be processed
32 Delete(event.DeleteEvent) bool
34 // Update returns true if the Update event should be processed
35 Update(event.UpdateEvent) bool
37 // Generic returns true if the Generic event should be processed
38 Generic(event.GenericEvent) bool
41 var _ Predicate = Funcs{}
42 var _ Predicate = ResourceVersionChangedPredicate{}
44 // Funcs is a function that implements Predicate.
46 // Create returns true if the Create event should be processed
47 CreateFunc func(event.CreateEvent) bool
49 // Delete returns true if the Delete event should be processed
50 DeleteFunc func(event.DeleteEvent) bool
52 // Update returns true if the Update event should be processed
53 UpdateFunc func(event.UpdateEvent) bool
55 // Generic returns true if the Generic event should be processed
56 GenericFunc func(event.GenericEvent) bool
59 // Create implements Predicate
60 func (p Funcs) Create(e event.CreateEvent) bool {
61 if p.CreateFunc != nil {
62 return p.CreateFunc(e)
67 // Delete implements Predicate
68 func (p Funcs) Delete(e event.DeleteEvent) bool {
69 if p.DeleteFunc != nil {
70 return p.DeleteFunc(e)
75 // Update implements Predicate
76 func (p Funcs) Update(e event.UpdateEvent) bool {
77 if p.UpdateFunc != nil {
78 return p.UpdateFunc(e)
83 // Generic implements Predicate
84 func (p Funcs) Generic(e event.GenericEvent) bool {
85 if p.GenericFunc != nil {
86 return p.GenericFunc(e)
91 // ResourceVersionChangedPredicate implements a default update predicate function on resource version change
92 type ResourceVersionChangedPredicate struct {
96 // Update implements default UpdateEvent filter for validating resource version change
97 func (ResourceVersionChangedPredicate) Update(e event.UpdateEvent) bool {
99 log.Error(nil, "UpdateEvent has no old metadata", "event", e)
102 if e.ObjectOld == nil {
103 log.Error(nil, "GenericEvent has no old runtime object to update", "event", e)
106 if e.ObjectNew == nil {
107 log.Error(nil, "GenericEvent has no new runtime object for update", "event", e)
110 if e.MetaNew == nil {
111 log.Error(nil, "UpdateEvent has no new metadata", "event", e)
114 if e.MetaNew.GetResourceVersion() == e.MetaOld.GetResourceVersion() {