-
Notifications
You must be signed in to change notification settings - Fork 72
/
predicate.go
48 lines (44 loc) · 1.54 KB
/
predicate.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package controllers
import (
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1"
)
func veleroPredicate(scheme *runtime.Scheme) predicate.Predicate {
return predicate.Funcs{
// Update returns true if the Update event should be processed
UpdateFunc: func(e event.UpdateEvent) bool {
if e.ObjectOld.GetGeneration() == e.ObjectNew.GetGeneration() {
return false
}
return isObjectOurs(scheme, e.ObjectOld)
},
// Create returns true if the Create event should be processed
CreateFunc: func(e event.CreateEvent) bool {
return isObjectOurs(scheme, e.Object)
},
// Delete returns true if the Delete event should be processed
DeleteFunc: func(e event.DeleteEvent) bool {
return !e.DeleteStateUnknown && isObjectOurs(scheme, e.Object)
},
}
}
// isObjectOurs returns true if the object is ours.
// it first checks if the object has our group, version, and kind
// else it will check for non empty OadpOperatorlabel labels
func isObjectOurs(scheme *runtime.Scheme, object client.Object) bool {
objGVKs, _, err := scheme.ObjectKinds(object)
if err != nil {
return false
}
if len(objGVKs) != 1 {
return false
}
gvk := objGVKs[0]
if gvk.Group == oadpv1alpha1.GroupVersion.Group && gvk.Version == oadpv1alpha1.GroupVersion.Version && gvk.Kind == oadpv1alpha1.Kind {
return true
}
return object.GetLabels()[oadpv1alpha1.OadpOperatorLabel] != ""
}