...
1
16
17 package controllers
18
19 import (
20 "reflect"
21
22 cmutil "github.com/cert-manager/cert-manager/pkg/api/util"
23 cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
24 certificatesv1 "k8s.io/api/certificates/v1"
25 "sigs.k8s.io/controller-runtime/pkg/event"
26 "sigs.k8s.io/controller-runtime/pkg/predicate"
27
28 "github.com/cert-manager/issuer-lib/api/v1alpha1"
29 "github.com/cert-manager/issuer-lib/conditions"
30 )
31
32
33
34
35
36
37
38
39 type CertificateRequestPredicate struct {
40 predicate.Funcs
41 }
42
43 func (CertificateRequestPredicate) Update(e event.UpdateEvent) bool {
44 if e.ObjectOld == nil || e.ObjectNew == nil {
45
46 return true
47 }
48
49 oldCr, oldOk := e.ObjectOld.(*cmapi.CertificateRequest)
50 newCr, newOk := e.ObjectNew.(*cmapi.CertificateRequest)
51 if !oldOk || !newOk {
52
53 return true
54 }
55
56 if len(oldCr.Status.Conditions) != len(newCr.Status.Conditions) {
57
58 return true
59 }
60
61 for _, oldCond := range oldCr.Status.Conditions {
62 if oldCond.Type == cmapi.CertificateRequestConditionReady {
63
64 continue
65 }
66
67 newCond := cmutil.GetCertificateRequestCondition(newCr, oldCond.Type)
68 if (newCond == nil) || (oldCond.Status != newCond.Status) {
69
70 return true
71 }
72 }
73
74
75 return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
76 }
77
78
79
80
81
82
83
84
85 type CertificateSigningRequestPredicate struct {
86 predicate.Funcs
87 }
88
89 func (CertificateSigningRequestPredicate) Update(e event.UpdateEvent) bool {
90 if e.ObjectOld == nil || e.ObjectNew == nil {
91
92 return true
93 }
94
95 oldCr, oldOk := e.ObjectOld.(*certificatesv1.CertificateSigningRequest)
96 newCr, newOk := e.ObjectNew.(*certificatesv1.CertificateSigningRequest)
97 if !oldOk || !newOk {
98
99 return true
100 }
101
102 if len(oldCr.Status.Conditions) != len(newCr.Status.Conditions) {
103
104 return true
105 }
106
107 for _, oldCond := range oldCr.Status.Conditions {
108 newCond := conditions.GetCertificateSigningRequestStatusCondition(newCr.Status.Conditions, oldCond.Type)
109 if (newCond == nil) || (oldCond.Status != newCond.Status) {
110
111 return true
112 }
113 }
114
115
116 return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
117 }
118
119
120
121
122
123
124
125 type LinkedIssuerPredicate struct {
126 predicate.Funcs
127 }
128
129
130 func (LinkedIssuerPredicate) Update(e event.UpdateEvent) bool {
131 if e.ObjectOld == nil || e.ObjectNew == nil {
132
133 return true
134 }
135
136 issuerOld, okOld := e.ObjectOld.(v1alpha1.Issuer)
137 issuerNew, okNew := e.ObjectNew.(v1alpha1.Issuer)
138 if (!okOld || !okNew) ||
139 (issuerOld.GetStatus() == nil || issuerNew.GetStatus() == nil) {
140
141 return true
142 }
143
144 readyOld := conditions.GetIssuerStatusCondition(
145 issuerOld.GetStatus().Conditions,
146 cmapi.IssuerConditionReady,
147 )
148
149 readyNew := conditions.GetIssuerStatusCondition(
150 issuerNew.GetStatus().Conditions,
151 cmapi.IssuerConditionReady,
152 )
153
154 if readyOld == nil || readyNew == nil {
155
156
157 return readyOld != nil || readyNew != nil
158 }
159
160 return readyNew.Status != readyOld.Status || readyNew.ObservedGeneration != readyOld.ObservedGeneration
161 }
162
163
164
165
166
167
168
169 type IssuerPredicate struct {
170 predicate.Funcs
171 }
172
173
174 func (IssuerPredicate) Update(e event.UpdateEvent) bool {
175 if e.ObjectOld == nil || e.ObjectNew == nil {
176
177 return true
178 }
179
180 if e.ObjectNew.GetGeneration() != e.ObjectOld.GetGeneration() {
181
182 return true
183 }
184
185 issuerOld, okOld := e.ObjectOld.(v1alpha1.Issuer)
186 issuerNew, okNew := e.ObjectNew.(v1alpha1.Issuer)
187 if (!okOld || !okNew) ||
188 (issuerOld.GetStatus() == nil || issuerNew.GetStatus() == nil) {
189
190 return true
191 }
192
193 readyOld := conditions.GetIssuerStatusCondition(
194 issuerOld.GetStatus().Conditions,
195 cmapi.IssuerConditionReady,
196 )
197
198 readyNew := conditions.GetIssuerStatusCondition(
199 issuerNew.GetStatus().Conditions,
200 cmapi.IssuerConditionReady,
201 )
202
203 if (readyOld == nil && readyNew != nil) ||
204 (readyOld != nil && readyNew == nil) {
205
206
207 return true
208 }
209
210
211 return !reflect.DeepEqual(e.ObjectNew.GetAnnotations(), e.ObjectOld.GetAnnotations())
212 }
213
View as plain text