Skip to content

Commit 5f717fe

Browse files
committed
Update SpecCore webhook interface
This patch updates the SpecCore webhook interface to accept the Namespace as parameter. This change ensures that we properly validate the referenced topology from openstack-operator, rejecting requests with a different namespace. Signed-off-by: Francesco Pantano <[email protected]>
1 parent b9dd679 commit 5f717fe

File tree

1 file changed

+38
-20
lines changed

1 file changed

+38
-20
lines changed

api/v1beta1/galera_webhook.go

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,7 @@ func (r *Galera) ValidateCreate() (admission.Warnings, error) {
9090
[]string{r.Name},
9191
CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "<statefulset_name>-<hash>"
9292

93-
// When a TopologyRef CR is referenced, fail if a different Namespace is
94-
// referenced because is not supported
95-
if r.Spec.TopologyRef != nil {
96-
if err := topologyv1.ValidateTopologyNamespace(r.Spec.TopologyRef.Namespace, *basePath, r.Namespace); err != nil {
97-
allErrs = append(allErrs, err)
98-
}
99-
}
100-
101-
warn, err := r.Spec.ValidateCreate(basePath)
93+
warn, err := r.Spec.ValidateCreate(basePath, r.Namespace)
10294

10395
if err != nil {
10496
allErrs = append(allErrs, err...)
@@ -117,12 +109,12 @@ func (r *Galera) ValidateCreate() (admission.Warnings, error) {
117109

118110
// ValidateCreate - Exported function wrapping non-exported validate functions,
119111
// this function can be called externally to validate an KeystoneAPI spec.
120-
func (spec *GaleraSpec) ValidateCreate(basePath *field.Path) (admission.Warnings, field.ErrorList) {
121-
return spec.GaleraSpecCore.ValidateCreate(basePath)
112+
func (spec *GaleraSpec) ValidateCreate(basePath *field.Path, namespace string) (admission.Warnings, field.ErrorList) {
113+
return spec.GaleraSpecCore.ValidateCreate(basePath, namespace)
122114
}
123115

124116
// ValidateCreate -
125-
func (spec *GaleraSpecCore) ValidateCreate(basePath *field.Path) (admission.Warnings, field.ErrorList) {
117+
func (spec *GaleraSpecCore) ValidateCreate(basePath *field.Path, namespace string) (admission.Warnings, field.ErrorList) {
126118
var allErrs field.ErrorList
127119
allWarn := []string{}
128120

@@ -132,6 +124,14 @@ func (spec *GaleraSpecCore) ValidateCreate(basePath *field.Path) (admission.Warn
132124
warn = spec.ValidateGaleraReplicas(basePath)
133125
allWarn = append(allWarn, warn...)
134126

127+
// When a TopologyRef CR is referenced, fail if a different Namespace is
128+
// referenced because is not supported
129+
if spec.TopologyRef != nil {
130+
if err := topologyv1.ValidateTopologyNamespace(spec.TopologyRef.Namespace, *basePath, namespace); err != nil {
131+
allErrs = append(allErrs, err)
132+
}
133+
}
134+
135135
return allWarn, allErrs
136136
}
137137

@@ -145,16 +145,9 @@ func (r *Galera) ValidateUpdate(old runtime.Object) (admission.Warnings, error)
145145
if !ok || oldGalera == nil {
146146
return nil, apierrors.NewInternalError(fmt.Errorf("unable to convert existing object"))
147147
}
148-
149148
basePath := field.NewPath("spec")
149+
allErrs = append(allErrs, r.Spec.ValidateUpdate(oldGalera.Spec, basePath, r.Namespace)...)
150150

151-
// When a TopologyRef CR is referenced, fail if a different Namespace is
152-
// referenced because is not supported
153-
if r.Spec.TopologyRef != nil {
154-
if err := topologyv1.ValidateTopologyNamespace(r.Spec.TopologyRef.Namespace, *basePath, r.Namespace); err != nil {
155-
allErrs = append(allErrs, err)
156-
}
157-
}
158151
warn := r.Spec.ValidateGaleraReplicas(basePath)
159152
allWarn = append(allWarn, warn...)
160153

@@ -163,6 +156,31 @@ func (r *Galera) ValidateUpdate(old runtime.Object) (admission.Warnings, error)
163156
}
164157
return allWarn, nil
165158
}
159+
func (r *GaleraSpec) ValidateUpdate(old GaleraSpec, basePath *field.Path, namespace string) field.ErrorList {
160+
allErrs := field.ErrorList{}
161+
162+
// When a TopologyRef CR is referenced, fail if a different Namespace is
163+
// referenced because is not supported
164+
if r.TopologyRef != nil {
165+
if err := topologyv1.ValidateTopologyNamespace(r.TopologyRef.Namespace, *basePath, namespace); err != nil {
166+
allErrs = append(allErrs, err)
167+
}
168+
}
169+
return allErrs
170+
}
171+
172+
func (r *GaleraSpecCore) ValidateUpdate(old GaleraSpecCore, basePath *field.Path, namespace string) field.ErrorList {
173+
allErrs := field.ErrorList{}
174+
175+
// When a TopologyRef CR is referenced, fail if a different Namespace is
176+
// referenced because is not supported
177+
if r.TopologyRef != nil {
178+
if err := topologyv1.ValidateTopologyNamespace(r.TopologyRef.Namespace, *basePath, namespace); err != nil {
179+
allErrs = append(allErrs, err)
180+
}
181+
}
182+
return allErrs
183+
}
166184

167185
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
168186
func (r *Galera) ValidateDelete() (admission.Warnings, error) {

0 commit comments

Comments
 (0)