Skip to content

Commit 36201be

Browse files
amoralejopenshift-merge-bot[bot]
authored andcommitted
Refactor Watcher validation webhooks
After introducing WatcherSpecCore struct, this patch is modifying the validation webhooks to make it a callable method on the WatcherSpecCore so that they can be easily invoked from the openstack-operator. Jira: https://issues.redhat.com/browse/OSPRH-16236
1 parent 9f78d85 commit 36201be

1 file changed

Lines changed: 53 additions & 20 deletions

File tree

api/v1beta1/watcher_webhook.go

Lines changed: 53 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
"fmt"
21+
2022
topologyv1 "github.com/openstack-k8s-operators/infra-operator/apis/topology/v1beta1"
2123
apierrors "k8s.io/apimachinery/pkg/api/errors"
2224
"k8s.io/apimachinery/pkg/runtime"
@@ -68,26 +70,59 @@ var _ webhook.Validator = &Watcher{}
6870
func (r *Watcher) ValidateCreate() (admission.Warnings, error) {
6971
watcherlog.Info("validate create", "name", r.Name)
7072

73+
allErrs := r.Spec.ValidateCreate(field.NewPath("spec"), r.Namespace)
74+
75+
if len(allErrs) != 0 {
76+
return nil, apierrors.NewInvalid(
77+
schema.GroupKind{Group: "watcher.openstack.org", Kind: "Watcher"},
78+
r.Name, allErrs)
79+
}
80+
81+
return nil, nil
82+
}
83+
84+
// ValidateCreate validates the WatcherSpec during the webhook invocation.
85+
func (r *WatcherSpec) ValidateCreate(basePath *field.Path, namespace string) field.ErrorList {
86+
return r.WatcherSpecCore.ValidateCreate(basePath, namespace)
87+
}
88+
89+
// ValidateCreate validates the WatcherSpecCore during the webhook invocation. It is
90+
// expected to be called by the validation webhook in the higher level meta
91+
// operator
92+
func (r *WatcherSpecCore) ValidateCreate(basePath *field.Path, namespace string) field.ErrorList {
7193
var allErrs field.ErrorList
72-
basePath := field.NewPath("spec")
7394

74-
if *r.Spec.DatabaseInstance == "" || r.Spec.DatabaseInstance == nil {
95+
if *r.DatabaseInstance == "" || r.DatabaseInstance == nil {
7596
allErrs = append(
7697
allErrs,
7798
field.Invalid(
7899
basePath.Child("databaseInstance"), "", "databaseInstance field should not be empty"),
79100
)
80101
}
81102

82-
if *r.Spec.RabbitMqClusterName == "" || r.Spec.RabbitMqClusterName == nil {
103+
if *r.RabbitMqClusterName == "" || r.RabbitMqClusterName == nil {
83104
allErrs = append(
84105
allErrs,
85106
field.Invalid(
86107
basePath.Child("rabbitMqClusterName"), "", "rabbitMqClusterName field should not be empty"),
87108
)
88109
}
89110

90-
allErrs = append(allErrs, r.Spec.ValidateWatcherTopology(basePath, r.Namespace)...)
111+
allErrs = append(allErrs, r.ValidateWatcherTopology(basePath, namespace)...)
112+
113+
return allErrs
114+
}
115+
116+
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
117+
func (r *Watcher) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
118+
119+
watcherlog.Info("validate update", "name", r.Name)
120+
oldWatcher, ok := old.(*Watcher)
121+
if !ok || oldWatcher == nil {
122+
return nil, apierrors.NewInternalError(fmt.Errorf("unable to convert existing object"))
123+
}
124+
125+
allErrs := r.Spec.ValidateUpdate(oldWatcher.Spec, field.NewPath("spec"), r.Namespace)
91126

92127
if len(allErrs) != 0 {
93128
return nil, apierrors.NewInvalid(
@@ -96,41 +131,39 @@ func (r *Watcher) ValidateCreate() (admission.Warnings, error) {
96131
}
97132

98133
return nil, nil
134+
99135
}
100136

101-
// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
102-
func (r *Watcher) ValidateUpdate(runtime.Object) (admission.Warnings, error) {
103-
watcherlog.Info("validate update", "name", r.Name)
137+
// ValidateCreate validates the WatcherSpec during the webhook invocation.
138+
func (r *WatcherSpec) ValidateUpdate(old WatcherSpec, basePath *field.Path, namespace string) field.ErrorList {
139+
return r.WatcherSpecCore.ValidateUpdate(old.WatcherSpecCore, basePath, namespace)
140+
}
104141

142+
// ValidateUpdate validates the WatcherSpecCore during the webhook invocation. It is
143+
// expected to be called by the validation webhook in the higher level meta
144+
// operator
145+
func (r *WatcherSpecCore) ValidateUpdate(old WatcherSpecCore, basePath *field.Path, namespace string) field.ErrorList {
105146
var allErrs field.ErrorList
106-
basePath := field.NewPath("spec")
107147

108-
if *r.Spec.DatabaseInstance == "" || r.Spec.DatabaseInstance == nil {
148+
if *r.DatabaseInstance == "" || r.DatabaseInstance == nil {
109149
allErrs = append(
110150
allErrs,
111151
field.Invalid(
112152
basePath.Child("databaseInstance"), "", "databaseInstance field should not be empty"),
113153
)
114154
}
115155

116-
if *r.Spec.RabbitMqClusterName == "" || r.Spec.RabbitMqClusterName == nil {
156+
if *r.RabbitMqClusterName == "" || r.RabbitMqClusterName == nil {
117157
allErrs = append(
118158
allErrs,
119159
field.Invalid(
120160
basePath.Child("rabbitMqClusterName"), "", "rabbitMqClusterName field should not be empty"),
121161
)
122162
}
123163

124-
allErrs = append(allErrs, r.Spec.ValidateWatcherTopology(basePath, r.Namespace)...)
125-
126-
if len(allErrs) != 0 {
127-
return nil, apierrors.NewInvalid(
128-
schema.GroupKind{Group: "watcher.openstack.org", Kind: "Watcher"},
129-
r.Name, allErrs)
130-
}
131-
132-
return nil, nil
164+
allErrs = append(allErrs, r.ValidateWatcherTopology(basePath, namespace)...)
133165

166+
return allErrs
134167
}
135168

136169
// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
@@ -142,7 +175,7 @@ func (r *Watcher) ValidateDelete() (admission.Warnings, error) {
142175

143176
// ValidateWatcherTopology - Returns an ErrorList if the Topology is referenced
144177
// on a different namespace
145-
func (spec *WatcherSpec) ValidateWatcherTopology(basePath *field.Path, namespace string) field.ErrorList {
178+
func (spec *WatcherSpecCore) ValidateWatcherTopology(basePath *field.Path, namespace string) field.ErrorList {
146179
watcherlog.Info("validate topology")
147180
var allErrs field.ErrorList
148181

0 commit comments

Comments
 (0)