Code refactoring for bpa operator
[icn.git] / cmd / bpa-operator / vendor / sigs.k8s.io / controller-runtime / pkg / predicate / predicate.go
1 /*
2 Copyright 2018 The Kubernetes Authors.
3
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
7
8     http://www.apache.org/licenses/LICENSE-2.0
9
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.
15 */
16
17 package predicate
18
19 import (
20         "sigs.k8s.io/controller-runtime/pkg/event"
21         logf "sigs.k8s.io/controller-runtime/pkg/runtime/log"
22 )
23
24 var log = logf.KBLog.WithName("predicate").WithName("eventFilters")
25
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
30
31         // Delete returns true if the Delete event should be processed
32         Delete(event.DeleteEvent) bool
33
34         // Update returns true if the Update event should be processed
35         Update(event.UpdateEvent) bool
36
37         // Generic returns true if the Generic event should be processed
38         Generic(event.GenericEvent) bool
39 }
40
41 var _ Predicate = Funcs{}
42 var _ Predicate = ResourceVersionChangedPredicate{}
43
44 // Funcs is a function that implements Predicate.
45 type Funcs struct {
46         // Create returns true if the Create event should be processed
47         CreateFunc func(event.CreateEvent) bool
48
49         // Delete returns true if the Delete event should be processed
50         DeleteFunc func(event.DeleteEvent) bool
51
52         // Update returns true if the Update event should be processed
53         UpdateFunc func(event.UpdateEvent) bool
54
55         // Generic returns true if the Generic event should be processed
56         GenericFunc func(event.GenericEvent) bool
57 }
58
59 // Create implements Predicate
60 func (p Funcs) Create(e event.CreateEvent) bool {
61         if p.CreateFunc != nil {
62                 return p.CreateFunc(e)
63         }
64         return true
65 }
66
67 // Delete implements Predicate
68 func (p Funcs) Delete(e event.DeleteEvent) bool {
69         if p.DeleteFunc != nil {
70                 return p.DeleteFunc(e)
71         }
72         return true
73 }
74
75 // Update implements Predicate
76 func (p Funcs) Update(e event.UpdateEvent) bool {
77         if p.UpdateFunc != nil {
78                 return p.UpdateFunc(e)
79         }
80         return true
81 }
82
83 // Generic implements Predicate
84 func (p Funcs) Generic(e event.GenericEvent) bool {
85         if p.GenericFunc != nil {
86                 return p.GenericFunc(e)
87         }
88         return true
89 }
90
91 // ResourceVersionChangedPredicate implements a default update predicate function on resource version change
92 type ResourceVersionChangedPredicate struct {
93         Funcs
94 }
95
96 // Update implements default UpdateEvent filter for validating resource version change
97 func (ResourceVersionChangedPredicate) Update(e event.UpdateEvent) bool {
98         if e.MetaOld == nil {
99                 log.Error(nil, "UpdateEvent has no old metadata", "event", e)
100                 return false
101         }
102         if e.ObjectOld == nil {
103                 log.Error(nil, "GenericEvent has no old runtime object to update", "event", e)
104                 return false
105         }
106         if e.ObjectNew == nil {
107                 log.Error(nil, "GenericEvent has no new runtime object for update", "event", e)
108                 return false
109         }
110         if e.MetaNew == nil {
111                 log.Error(nil, "UpdateEvent has no new metadata", "event", e)
112                 return false
113         }
114         if e.MetaNew.GetResourceVersion() == e.MetaOld.GetResourceVersion() {
115                 return false
116         }
117         return true
118 }