-
Notifications
You must be signed in to change notification settings - Fork 24
/
predicates.go
78 lines (68 loc) · 2.72 KB
/
predicates.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package controllers
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
)
// genericPredicates return predicates that are common for all controllers.
// Controller specific predicates should be named <CONTROLLER-NAME>Predicates.
// For example: workspacePredicates().
func genericPredicates() predicate.Predicate {
return predicate.Funcs{
CreateFunc: func(e event.CreateEvent) bool {
return true
},
UpdateFunc: func(e event.UpdateEvent) bool {
// Update event has no old object to update.
if e.ObjectOld == nil {
return false
}
// Update event has no new object to update.
if e.ObjectNew == nil {
return false
}
// Do not trigger reconciliation if an object was deleted with the `foreground` option.
// Let Kubernetes first delete all dependent objects and only then the operator proceeds with the deletion of the parent object.
// This helps to avoid the situation when the operator triggers deletion twice.
// The second deletion get triggered when Kubernetes GC removes the `foregroundDeletion` finalizer.
if e.ObjectNew.GetDeletionTimestamp() != nil {
return !controllerutil.ContainsFinalizer(e.ObjectNew, metav1.FinalizerDeleteDependents)
}
// Generation of an object changes when .spec has been updated.
// If Generations of new and old objects are not equal the object has be to reconcile.
if e.ObjectOld.GetGeneration() != e.ObjectNew.GetGeneration() {
return true
}
// ResourceVersion of an object changes when any part of the object has been updated.
// If ResourceVersions of new and old objects are equal this is a periodic reconciliation.
if e.ObjectOld.GetResourceVersion() == e.ObjectNew.GetResourceVersion() {
return true
}
// Do not call reconciliation in all other cases
return false
},
DeleteFunc: func(e event.DeleteEvent) bool {
return true
},
GenericFunc: func(e event.GenericEvent) bool {
return true
},
}
}
// workspacePredicates return predicates that are specific for the workspace controllers.
func workspacePredicates() predicate.Predicate {
return predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
// Validate if a certain annotation persists in a new object and does not match the old one.
// In that case, it is a new or updated annotation and we need to trigger a reconciliation cycle.
if a, ok := e.ObjectNew.GetAnnotations()[workspaceAnnotationRunNew]; ok && a == annotationTrue {
return true
}
// Do not call reconciliation in all other cases
return false
},
}
}