77package v1
88
99import (
10+ "context"
1011 "fmt"
1112 "github.com/distribution/reference"
1213 "github.com/go-test/deep"
3132)
3233
3334func (in * Coherence ) SetupWebhookWithManager (mgr ctrl.Manager ) error {
35+ hook := & Coherence {}
3436 return ctrl .NewWebhookManagedBy (mgr ).
3537 For (in ).
38+ WithDefaulter (hook ).
39+ WithValidator (hook ).
3640 Complete ()
3741}
3842
@@ -44,22 +48,28 @@ const MutatingWebHookPath = "/mutate-coherence-oracle-com-v1-coherence"
4448
4549// An anonymous var to ensure that the Coherence struct implements webhook.Defaulter
4650// there will be a compile time error here if this fails.
47- var _ webhook.Defaulter = & Coherence {}
51+ var _ webhook.CustomDefaulter = & Coherence {}
4852
4953// Default implements webhook.Defaulter so a webhook will be registered for the type
50- func (in * Coherence ) Default () {
51- spec , _ := in .GetStatefulSetSpec ()
54+ func (in * Coherence ) Default (_ context.Context , obj runtime.Object ) error {
55+ coh , ok := obj .(* Coherence )
56+ if ! ok {
57+ return fmt .Errorf ("expected a Coherence instance but got a %T" , obj )
58+ }
59+
60+ spec , _ := coh .GetStatefulSetSpec ()
5261 // set the default replicas if not present
5362 if spec .Replicas == nil {
5463 spec .SetReplicas (spec .GetReplicas ())
5564 }
56- SetCommonDefaults (in )
65+ SetCommonDefaults (coh )
5766
5867 // apply a label with the hash of the spec - ths must be the last action here to make sure that
5968 // any modifications to the spec field are included in the hash
60- if hash , applied := EnsureCoherenceHashLabel (in ); applied {
69+ if hash , applied := EnsureCoherenceHashLabel (coh ); applied {
6170 webhookLogger .Info (fmt .Sprintf ("Applied %s label" , LabelCoherenceHash ), "hash" , hash )
6271 }
72+ return nil
6373}
6474
6575// SetCommonDefaults sets defaults common to both a Job and StatefulSet
@@ -135,31 +145,36 @@ const ValidatingWebHookPath = "/validate-coherence-oracle-com-v1-coherence"
135145
136146// An anonymous var to ensure that the Coherence struct implements webhook.Validator
137147// there will be a compile time error here if this fails.
138- var _ webhook.Validator = & Coherence {}
148+ var _ webhook.CustomValidator = & Coherence {}
139149var commonWebHook = CommonWebHook {}
140150
141151// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
142152// The optional warnings will be added to the response as warning messages.
143153// Return an error if the object is invalid.
144- func (in * Coherence ) ValidateCreate () (admission.Warnings , error ) {
145- logger := webhookLogger .WithValues ("namespace" , in .GetNamespace (), "name" , in .GetName ())
154+ func (in * Coherence ) ValidateCreate (_ context.Context , obj runtime.Object ) (admission.Warnings , error ) {
155+ coh , ok := obj .(* Coherence )
156+ if ! ok {
157+ return nil , fmt .Errorf ("expected a Coherence instance but got a %T" , obj )
158+ }
159+
160+ logger := webhookLogger .WithValues ("namespace" , coh .GetNamespace (), "name" , coh .GetName ())
146161 var warnings admission.Warnings
147162
148- dt := in .GetDeletionTimestamp ()
163+ dt := coh .GetDeletionTimestamp ()
149164 if dt != nil {
150165 // the deletion timestamp is set so do nothing
151166 logger .Info ("Skipping validation for deleted resource" , "deletionTimestamp" , * dt )
152167 return warnings , nil
153168 }
154169
155- webhookLogger .Info ("validate create" , "name" , in .Name )
156- if err := commonWebHook .validateReplicas (in ); err != nil {
170+ webhookLogger .Info ("validate create" , "name" , coh .Name )
171+ if err := commonWebHook .validateReplicas (coh ); err != nil {
157172 return warnings , err
158173 }
159- if err := commonWebHook .validateImages (in ); err != nil {
174+ if err := commonWebHook .validateImages (coh ); err != nil {
160175 return warnings , err
161176 }
162- if err := commonWebHook .validateNodePorts (in ); err != nil {
177+ if err := commonWebHook .validateNodePorts (coh ); err != nil {
163178 return warnings , err
164179 }
165180 return warnings , nil
@@ -168,39 +183,47 @@ func (in *Coherence) ValidateCreate() (admission.Warnings, error) {
168183// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
169184// The optional warnings will be added to the response as warning messages.
170185// Return an error if the object is invalid.
171- func (in * Coherence ) ValidateUpdate (previous runtime.Object ) (admission.Warnings , error ) {
172- webhookLogger .Info ("validate update" , "name" , in .Name )
173- logger := webhookLogger .WithValues ("namespace" , in .GetNamespace (), "name" , in .GetName ())
186+ func (in * Coherence ) ValidateUpdate (_ context.Context , oldObj , newObj runtime.Object ) (admission.Warnings , error ) {
187+ cohNew , ok := newObj .(* Coherence )
188+ if ! ok {
189+ return nil , fmt .Errorf ("expected a Coherence instance for new value but got a %T" , newObj )
190+ }
191+ cohPrev , ok := oldObj .(* Coherence )
192+ if ! ok {
193+ return nil , fmt .Errorf ("expected a Coherence instance for old value but got a %T" , newObj )
194+ }
195+
196+ webhookLogger .Info ("validate update" , "name" , cohNew .Name )
197+ logger := webhookLogger .WithValues ("namespace" , cohNew .GetNamespace (), "name" , cohNew .GetName ())
174198 var warnings admission.Warnings
175199
176- dt := in .GetDeletionTimestamp ()
200+ dt := cohNew .GetDeletionTimestamp ()
177201 if dt != nil {
178202 // the deletion timestamp is set so do nothing
179203 logger .Info ("Skipping validation for deleted resource" , "deletionTimestamp" , * dt )
180204 return warnings , nil
181205 }
182206
183- if err := commonWebHook .validateReplicas (in ); err != nil {
207+ if err := commonWebHook .validateReplicas (cohNew ); err != nil {
184208 return warnings , err
185209 }
186- if err := commonWebHook .validateImages (in ); err != nil {
210+ if err := commonWebHook .validateImages (cohNew ); err != nil {
187211 return warnings , err
188212 }
189- prev := previous .(* Coherence )
190213
191- if err := commonWebHook .validatePersistence (in , prev ); err != nil {
214+ if err := commonWebHook .validatePersistence (cohNew , cohPrev ); err != nil {
192215 return warnings , err
193216 }
194- if err := in .validateVolumeClaimTemplates (prev ); err != nil {
217+ if err := cohNew .validateVolumeClaimTemplates (cohNew , cohPrev ); err != nil {
195218 return warnings , err
196219 }
197- if err := commonWebHook .validateNodePorts (in ); err != nil {
220+ if err := commonWebHook .validateNodePorts (cohNew ); err != nil {
198221 return warnings , err
199222 }
200223
201224 var errorList field.ErrorList
202- sts := in .Spec .CreateStatefulSet (in )
203- stsOld := prev .Spec .CreateStatefulSet (prev )
225+ sts := cohNew .Spec .CreateStatefulSet (cohNew )
226+ stsOld := cohPrev .Spec .CreateStatefulSet (cohPrev )
204227 errorList = ValidateStatefulSetUpdate (& sts , & stsOld )
205228
206229 if len (errorList ) > 0 {
@@ -213,27 +236,27 @@ func (in *Coherence) ValidateUpdate(previous runtime.Object) (admission.Warnings
213236// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
214237// The optional warnings will be added to the response as warning messages.
215238// Return an error if the object is invalid.
216- func (in * Coherence ) ValidateDelete () (admission.Warnings , error ) {
239+ func (in * Coherence ) ValidateDelete (context. Context , runtime. Object ) (admission.Warnings , error ) {
217240 // we do not need to validate deletions
218241 return nil , nil
219242}
220243
221- func (in * Coherence ) validateVolumeClaimTemplates (previous * Coherence ) error {
222- if in .GetReplicas () == 0 || previous .GetReplicas () == 0 {
244+ func (in * Coherence ) validateVolumeClaimTemplates (cohNew , cohPrev * Coherence ) error {
245+ if cohNew .GetReplicas () == 0 || cohPrev .GetReplicas () == 0 {
223246 // changes are allowed if current or previous replicas == 0
224247 return nil
225248 }
226249
227- if len (in .Spec .VolumeClaimTemplates ) == 0 && len (previous .Spec .VolumeClaimTemplates ) == 0 {
250+ if len (cohNew .Spec .VolumeClaimTemplates ) == 0 && len (cohPrev .Spec .VolumeClaimTemplates ) == 0 {
228251 // no PVCs in either deployment
229252 return nil
230253 }
231254
232- diff := deep .Equal (previous .Spec .VolumeClaimTemplates , in .Spec .VolumeClaimTemplates )
255+ diff := deep .Equal (cohPrev .Spec .VolumeClaimTemplates , cohNew .Spec .VolumeClaimTemplates )
233256 if len (diff ) != 0 {
234257 return fmt .Errorf ("the Coherence resource \" %s\" is invalid: " +
235258 "changes cannot be made to spec.volumeclaimtemplates unless spec.replicas == 0 or the previous" +
236- " instance of the resource has spec.replicas == 0" , in .Name )
259+ " instance of the resource has spec.replicas == 0" , cohNew .Name )
237260 }
238261 return nil
239262}
0 commit comments