@@ -17,7 +17,13 @@ limitations under the License.
1717package v1beta1
1818
1919import (
20+ "fmt"
21+
22+ "github.com/openstack-k8s-operators/lib-common/modules/common/service"
23+ apierrors "k8s.io/apimachinery/pkg/api/errors"
2024 "k8s.io/apimachinery/pkg/runtime"
25+ "k8s.io/apimachinery/pkg/runtime/schema"
26+ "k8s.io/apimachinery/pkg/util/validation/field"
2127 ctrl "sigs.k8s.io/controller-runtime"
2228 logf "sigs.k8s.io/controller-runtime/pkg/log"
2329 "sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -95,18 +101,94 @@ var _ webhook.Validator = &Octavia{}
95101func (r * Octavia ) ValidateCreate () (admission.Warnings , error ) {
96102 octavialog .Info ("validate create" , "name" , r .Name )
97103
98- // TODO(user): fill in your validation logic upon object creation.
104+ var allErrs field.ErrorList
105+ basePath := field .NewPath ("spec" )
106+ if err := r .Spec .ValidateCreate (basePath ); err != nil {
107+ allErrs = append (allErrs , err ... )
108+ }
109+
110+ if len (allErrs ) != 0 {
111+ return nil , apierrors .NewInvalid (
112+ schema.GroupKind {Group : "octavia.openstack.org" , Kind : "Octavia" },
113+ r .Name , allErrs )
114+ }
115+
99116 return nil , nil
100117}
101118
119+ // ValidateCreate - Exported function wrapping non-exported validate functions,
120+ // this function can be called externally to validate an octavia spec.
121+ func (r * OctaviaSpec ) ValidateCreate (basePath * field.Path ) field.ErrorList {
122+ var allErrs field.ErrorList
123+
124+ // validate the service override key is valid
125+ allErrs = append (allErrs , service .ValidateRoutedOverrides (
126+ basePath .Child ("octaviaAPI" ).Child ("override" ).Child ("service" ),
127+ r .OctaviaAPI .Override .Service )... )
128+
129+ return allErrs
130+ }
131+
132+ func (r * OctaviaSpecCore ) ValidateCreate (basePath * field.Path ) field.ErrorList {
133+ var allErrs field.ErrorList
134+
135+ // validate the service override key is valid
136+ allErrs = append (allErrs , service .ValidateRoutedOverrides (
137+ basePath .Child ("octaviaAPI" ).Child ("override" ).Child ("service" ),
138+ r .OctaviaAPI .Override .Service )... )
139+
140+ return allErrs
141+ }
142+
102143// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
103144func (r * Octavia ) ValidateUpdate (old runtime.Object ) (admission.Warnings , error ) {
104145 octavialog .Info ("validate update" , "name" , r .Name )
105146
106- // TODO(user): fill in your validation logic upon object update.
147+ oldOctavia , ok := old .(* Octavia )
148+ if ! ok || oldOctavia == nil {
149+ return nil , apierrors .NewInternalError (fmt .Errorf ("unable to convert existing object" ))
150+ }
151+
152+ var allErrs field.ErrorList
153+ basePath := field .NewPath ("spec" )
154+
155+ if err := r .Spec .ValidateUpdate (oldOctavia .Spec , basePath ); err != nil {
156+ allErrs = append (allErrs , err ... )
157+ }
158+
159+ if len (allErrs ) != 0 {
160+ return nil , apierrors .NewInvalid (
161+ schema.GroupKind {Group : "octavia.openstack.org" , Kind : "Octavia" },
162+ r .Name , allErrs )
163+ }
164+
107165 return nil , nil
108166}
109167
168+ // ValidateUpdate - Exported function wrapping non-exported validate functions,
169+ // this function can be called externally to validate an barbican spec.
170+ func (r * OctaviaSpec ) ValidateUpdate (old OctaviaSpec , basePath * field.Path ) field.ErrorList {
171+ var allErrs field.ErrorList
172+
173+ // validate the service override key is valid
174+ allErrs = append (allErrs , service .ValidateRoutedOverrides (
175+ basePath .Child ("octaviaAPI" ).Child ("override" ).Child ("service" ),
176+ r .OctaviaAPI .Override .Service )... )
177+
178+ return allErrs
179+ }
180+
181+ func (r * OctaviaSpecCore ) ValidateUpdate (old OctaviaSpecCore , basePath * field.Path ) field.ErrorList {
182+ var allErrs field.ErrorList
183+
184+ // validate the service override key is valid
185+ allErrs = append (allErrs , service .ValidateRoutedOverrides (
186+ basePath .Child ("octaviaAPI" ).Child ("override" ).Child ("service" ),
187+ r .OctaviaAPI .Override .Service )... )
188+
189+ return allErrs
190+ }
191+
110192// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
111193func (r * Octavia ) ValidateDelete () (admission.Warnings , error ) {
112194 octavialog .Info ("validate delete" , "name" , r .Name )
0 commit comments