@@ -23,15 +23,14 @@ import (
2323 "github.com/google/go-cmp/cmp"
2424 topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
2525 "github.com/openstack-k8s-operators/lib-common/modules/common/service"
26+ common_webhook "github.com/openstack-k8s-operators/lib-common/modules/common/webhook"
2627 apierrors "k8s.io/apimachinery/pkg/api/errors"
2728 "k8s.io/apimachinery/pkg/runtime"
2829 "k8s.io/apimachinery/pkg/runtime/schema"
2930 "k8s.io/apimachinery/pkg/util/validation/field"
3031 logf "sigs.k8s.io/controller-runtime/pkg/log"
3132 "sigs.k8s.io/controller-runtime/pkg/webhook"
3233 "sigs.k8s.io/controller-runtime/pkg/webhook/admission"
33-
34- common_webhook "github.com/openstack-k8s-operators/lib-common/modules/common/webhook"
3534)
3635
3736// GlanceDefaults -
@@ -114,6 +113,10 @@ func (r *GlanceSpecCore) Default() {
114113 if r .DBPurge .Schedule == "" {
115114 r .DBPurge .Schedule = glanceDefaults .DBPurgeSchedule
116115 }
116+
117+ // NotificationsBus.Cluster is not defaulted - it must be explicitly set if NotificationsBus is configured
118+ // Migration from deprecated fields is handled by openstack-operator
119+ // This ensures users make a conscious choice about which cluster to use for notifications
117120 // When no glanceAPI(s) are specified in the top-level CR
118121 // we build one by default, but we set replicas=0 and we
119122 // build a "CustomServiceConfig" template that should be
@@ -202,12 +205,64 @@ func (r *GlanceSpecCore) isInvalidBackend(glanceAPI GlanceAPITemplate, topLevel
202205 return false , ""
203206}
204207
208+ // getDeprecatedFields returns the centralized list of deprecated fields for GlanceSpecCore
209+ func (spec * GlanceSpecCore ) getDeprecatedFields (old * GlanceSpecCore ) []common_webhook.DeprecatedFieldUpdate {
210+ // Get new field value (handle nil NotificationsBus)
211+ var newNotifBusCluster * string
212+ if spec .NotificationsBus != nil {
213+ newNotifBusCluster = & spec .NotificationsBus .Cluster
214+ }
215+
216+ deprecatedFields := []common_webhook.DeprecatedFieldUpdate {
217+ {
218+ DeprecatedFieldName : "notificationBusInstance" ,
219+ NewFieldPath : []string {"notificationsBus" , "cluster" },
220+ NewDeprecatedValue : spec .NotificationBusInstance ,
221+ NewValue : newNotifBusCluster ,
222+ },
223+ }
224+
225+ // If old spec is provided (UPDATE operation), add old values
226+ if old != nil {
227+ deprecatedFields [0 ].OldDeprecatedValue = old .NotificationBusInstance
228+ }
229+
230+ return deprecatedFields
231+ }
232+
233+ // validateDeprecatedFieldsCreate validates deprecated fields during CREATE operations
234+ func (spec * GlanceSpecCore ) validateDeprecatedFieldsCreate (basePath * field.Path ) ([]string , field.ErrorList ) {
235+ // Get deprecated fields list (without old values for CREATE)
236+ deprecatedFieldsUpdate := spec .getDeprecatedFields (nil )
237+
238+ // Convert to DeprecatedField list for CREATE validation
239+ deprecatedFields := make ([]common_webhook.DeprecatedField , len (deprecatedFieldsUpdate ))
240+ for i , df := range deprecatedFieldsUpdate {
241+ deprecatedFields [i ] = common_webhook.DeprecatedField {
242+ DeprecatedFieldName : df .DeprecatedFieldName ,
243+ NewFieldPath : df .NewFieldPath ,
244+ DeprecatedValue : df .NewDeprecatedValue ,
245+ NewValue : df .NewValue ,
246+ }
247+ }
248+
249+ return common_webhook .ValidateDeprecatedFieldsCreate (deprecatedFields , basePath ), nil
250+ }
251+
252+ // validateDeprecatedFieldsUpdate validates deprecated fields during UPDATE operations
253+ func (spec * GlanceSpecCore ) validateDeprecatedFieldsUpdate (old GlanceSpecCore , basePath * field.Path ) ([]string , field.ErrorList ) {
254+ // Get deprecated fields list with old values
255+ deprecatedFields := spec .getDeprecatedFields (& old )
256+ return common_webhook .ValidateDeprecatedFieldsUpdate (deprecatedFields , basePath )
257+ }
258+
205259var _ webhook.Validator = & Glance {}
206260
207261// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
208262func (r * Glance ) ValidateCreate () (admission.Warnings , error ) {
209263 glancelog .Info ("validate create" , "name" , r .Name )
210264 var allErrs field.ErrorList
265+ var allWarns []string
211266 basePath := field .NewPath ("spec" )
212267
213268 for key , glanceAPI := range r .Spec .GlanceAPIs {
@@ -229,28 +284,34 @@ func (r *Glance) ValidateCreate() (admission.Warnings, error) {
229284 allErrs = append (allErrs , err ... )
230285 }
231286
232- if err := r .Spec .ValidateCreate (basePath , r .Namespace ); err != nil {
233- allErrs = append (allErrs , err ... )
234- }
287+ warns , errs := r .Spec .ValidateCreate (basePath , r .Namespace )
288+ allWarns = append (allWarns , warns ... )
289+ allErrs = append ( allErrs , errs ... )
235290
236291 if len (allErrs ) != 0 {
237- return nil , apierrors .NewInvalid (
292+ return allWarns , apierrors .NewInvalid (
238293 schema.GroupKind {Group : "glance.openstack.org" , Kind : "Glance" },
239294 r .Name , allErrs )
240295 }
241296
242- return nil , nil
297+ return allWarns , nil
243298}
244299
245300// ValidateCreate - Exported function wrapping non-exported validate functions,
246301// this function can be called externally to validate an ironic spec.
247- func (r * GlanceSpec ) ValidateCreate (basePath * field.Path , namespace string ) field.ErrorList {
302+ func (r * GlanceSpec ) ValidateCreate (basePath * field.Path , namespace string ) ([] string , field.ErrorList ) {
248303 return r .GlanceSpecCore .ValidateCreate (basePath , namespace )
249304}
250305
251306// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
252- func (r * GlanceSpecCore ) ValidateCreate (basePath * field.Path , namespace string ) field.ErrorList {
307+ func (r * GlanceSpecCore ) ValidateCreate (basePath * field.Path , namespace string ) ([] string , field.ErrorList ) {
253308 var allErrs field.ErrorList
309+ var allWarns []string
310+
311+ // Validate deprecated fields using shared helper
312+ warns , errs := r .validateDeprecatedFieldsCreate (basePath )
313+ allWarns = append (allWarns , warns ... )
314+ allErrs = append (allErrs , errs ... )
254315
255316 // Check if the top-level CR has a "customServiceConfig" with an explicit
256317 // "backend:file || empty string" and save the result into topLevel var.
@@ -293,7 +354,7 @@ func (r *GlanceSpecCore) ValidateCreate(basePath *field.Path, namespace string)
293354 path , r .KeystoneEndpoint , KeystoneEndpointErrorMessage ))
294355 }
295356
296- return allErrs
357+ return allWarns , allErrs
297358}
298359
299360// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
@@ -305,6 +366,7 @@ func (r *Glance) ValidateUpdate(old runtime.Object) (admission.Warnings, error)
305366 glancelog .Info ("validate update" , "diff" , cmp .Diff (old , r ))
306367
307368 var allErrs field.ErrorList
369+ var allWarns []string
308370 basePath := field .NewPath ("spec" )
309371
310372 for key , glanceAPI := range r .Spec .GlanceAPIs {
@@ -326,28 +388,34 @@ func (r *Glance) ValidateUpdate(old runtime.Object) (admission.Warnings, error)
326388 allErrs = append (allErrs , err ... )
327389 }
328390
329- if err := r .Spec .ValidateUpdate (o .Spec , basePath , r .Namespace ); err != nil {
330- allErrs = append (allErrs , err ... )
331- }
391+ warns , errs := r .Spec .ValidateUpdate (o .Spec , basePath , r .Namespace )
392+ allWarns = append (allWarns , warns ... )
393+ allErrs = append ( allErrs , errs ... )
332394
333395 if len (allErrs ) != 0 {
334- return nil , apierrors .NewInvalid (
396+ return allWarns , apierrors .NewInvalid (
335397 schema.GroupKind {Group : "glance.openstack.org" , Kind : "Glance" },
336398 r .Name , allErrs )
337399 }
338400
339- return nil , nil
401+ return allWarns , nil
340402}
341403
342404// ValidateUpdate - Exported function wrapping non-exported validate functions,
343405// this function can be called externally to validate an glance spec.
344- func (r * GlanceSpec ) ValidateUpdate (old GlanceSpec , basePath * field.Path , namespace string ) field.ErrorList {
406+ func (r * GlanceSpec ) ValidateUpdate (old GlanceSpec , basePath * field.Path , namespace string ) ([] string , field.ErrorList ) {
345407 return r .GlanceSpecCore .ValidateUpdate (old .GlanceSpecCore , basePath , namespace )
346408}
347409
348410// ValidateUpdate -
349- func (r * GlanceSpecCore ) ValidateUpdate (old GlanceSpecCore , basePath * field.Path , namespace string ) field.ErrorList {
411+ func (r * GlanceSpecCore ) ValidateUpdate (old GlanceSpecCore , basePath * field.Path , namespace string ) ([] string , field.ErrorList ) {
350412 var allErrs field.ErrorList
413+ var allWarns []string
414+
415+ // Validate deprecated fields using shared helper
416+ warns , errs := r .validateDeprecatedFieldsUpdate (old , basePath )
417+ allWarns = append (allWarns , warns ... )
418+ allErrs = append (allErrs , errs ... )
351419
352420 // fail if a wrong topology is referenced
353421 allErrs = append (allErrs , topologyv1 .ValidateTopologyRef (
@@ -399,7 +467,7 @@ func (r *GlanceSpecCore) ValidateUpdate(old GlanceSpecCore, basePath *field.Path
399467 path , r .KeystoneEndpoint , KeystoneEndpointErrorMessage ))
400468 }
401469
402- return allErrs
470+ return allWarns , allErrs
403471}
404472
405473// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
0 commit comments