/
predicates.go
212 lines (177 loc) · 6.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
Copyright 2023 The cert-manager 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 controllers
import (
"reflect"
cmutil "github.com/cert-manager/cert-manager/pkg/api/util"
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
certificatesv1 "k8s.io/api/certificates/v1"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"github.com/cert-manager/issuer-lib/api/v1alpha1"
"github.com/cert-manager/issuer-lib/conditions"
)
// This predicate is used to indicate when a CertificateRequest event should
// trigger a reconciliation of itself.
//
// In these cases we want to trigger:
// - an annotation changed/ was added or removed
// - a status condition was added or removed
// - a status condition that does not have type == Ready was changed (aka. other Status value)
type CertificateRequestPredicate struct {
predicate.Funcs
}
func (CertificateRequestPredicate) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
// a reference object is missing, just reconcile to be safe
return true
}
oldCr, oldOk := e.ObjectOld.(*cmapi.CertificateRequest)
newCr, newOk := e.ObjectNew.(*cmapi.CertificateRequest)
if !oldOk || !newOk {
// a reference object is invalid, just reconcile to be safe
return true
}
if len(oldCr.Status.Conditions) != len(newCr.Status.Conditions) {
// Fail fast in case we are certain a non-ready condition was added or removed.
return true
}
for _, oldCond := range oldCr.Status.Conditions {
if oldCond.Type == cmapi.CertificateRequestConditionReady {
// we can skip the Ready conditions
continue
}
newCond := cmutil.GetCertificateRequestCondition(newCr, oldCond.Type)
if (newCond == nil) || (oldCond.Status != newCond.Status) {
// we found a missing or changed condition
return true
}
}
// check if any of the annotations changed
return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
}
// This predicate is used to indicate when a CertificateSigningRequest event should
// trigger a reconciliation of itself.
//
// In these cases we want to trigger:
// - an annotation changed/ was added or removed
// - a status condition was added or removed
// - a status condition was changed
type CertificateSigningRequestPredicate struct {
predicate.Funcs
}
func (CertificateSigningRequestPredicate) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
// a reference object is missing, just reconcile to be safe
return true
}
oldCr, oldOk := e.ObjectOld.(*certificatesv1.CertificateSigningRequest)
newCr, newOk := e.ObjectNew.(*certificatesv1.CertificateSigningRequest)
if !oldOk || !newOk {
// a reference object is invalid, just reconcile to be safe
return true
}
if len(oldCr.Status.Conditions) != len(newCr.Status.Conditions) {
// Fail fast in case we are certain a non-ready condition was added or removed.
return true
}
for _, oldCond := range oldCr.Status.Conditions {
newCond := conditions.GetCertificateSigningRequestStatusCondition(newCr.Status.Conditions, oldCond.Type)
if (newCond == nil) || (oldCond.Status != newCond.Status) {
// we found a missing or changed condition
return true
}
}
// check if any of the annotations changed
return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
}
// Predicate for Issuer events that should trigger the CertificateRequest reconciler
//
// In these cases we want to trigger:
// - the Ready condition was added/ removed
// - the Ready condition's Status property changed
// - the Ready condition's observed generation changed
type LinkedIssuerPredicate struct {
predicate.Funcs
}
// Update implements default UpdateEvent filter for validating resource version change.
func (LinkedIssuerPredicate) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
// a reference object is missing, just reconcile to be safe
return true
}
issuerOld, okOld := e.ObjectOld.(v1alpha1.Issuer)
issuerNew, okNew := e.ObjectNew.(v1alpha1.Issuer)
if (!okOld || !okNew) ||
(issuerOld.GetStatus() == nil || issuerNew.GetStatus() == nil) {
// a reference object is invalid, just reconcile to be safe
return true
}
readyOld := conditions.GetIssuerStatusCondition(
issuerOld.GetStatus().Conditions,
cmapi.IssuerConditionReady,
)
readyNew := conditions.GetIssuerStatusCondition(
issuerNew.GetStatus().Conditions,
cmapi.IssuerConditionReady,
)
if readyOld == nil || readyNew == nil {
// the ready condition is not present in the old and/or new version
// we only want to reconcile if one of the two conditions is not nil
return readyOld != nil || readyNew != nil
}
return readyNew.Status != readyOld.Status || readyNew.ObservedGeneration != readyOld.ObservedGeneration
}
// Predicate for Issuer events that should trigger the Issuer reconciler
//
// In these cases we want to trigger:
// - an annotation changed/ was added or removed
// - the generation changed
// - the Ready condition was added/ removed
type IssuerPredicate struct {
predicate.Funcs
}
// Update implements default UpdateEvent filter for validating generation change.
func (IssuerPredicate) Update(e event.UpdateEvent) bool {
if e.ObjectOld == nil || e.ObjectNew == nil {
// a reference object is missing, just reconcile to be safe
return true
}
if e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration() {
// we noticed a generation change
return true
}
issuerOld, okOld := e.ObjectOld.(v1alpha1.Issuer)
issuerNew, okNew := e.ObjectNew.(v1alpha1.Issuer)
if (!okOld || !okNew) ||
(issuerOld.GetStatus() == nil || issuerNew.GetStatus() == nil) {
// a reference object is invalid, just reconcile to be safe
return true
}
readyOld := conditions.GetIssuerStatusCondition(
issuerOld.GetStatus().Conditions,
cmapi.IssuerConditionReady,
)
readyNew := conditions.GetIssuerStatusCondition(
issuerNew.GetStatus().Conditions,
cmapi.IssuerConditionReady,
)
if (readyOld == nil && readyNew != nil) ||
(readyOld != nil && readyNew == nil) {
// the ready condition is not present in the old or new version but
// is present in the new or old version
return true
}
// check if any of the annotations changed
return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
}