From 59b8539a44a0181a5f50f6169a58afca982d26b4 Mon Sep 17 00:00:00 2001 From: Martin Schuppert Date: Wed, 8 May 2024 15:11:06 +0200 Subject: [PATCH] Add webhook to validate service override endpoint type Can also be called from the openstack-operator webhook on the HeatSpecCore, like for other operators in [1]. Depends-On: https://github.com/openstack-k8s-operators/lib-common/pull/505 [1] https://github.com/openstack-k8s-operators/openstack-operator/blob/d2703d3a321c979dacaca95b5d4a634bf116e0db/apis/core/v1beta1/openstackcontrolplane_webhook.go#L181 --- api/go.mod | 2 +- api/go.sum | 4 +- api/v1beta1/manila_webhook.go | 88 ++++++++++++++++++++++- go.mod | 2 +- go.sum | 4 +- test/functional/manila_controller_test.go | 43 +++++++++++ 6 files changed, 135 insertions(+), 8 deletions(-) diff --git a/api/go.mod b/api/go.mod index 6df979a9..2b155efe 100644 --- a/api/go.mod +++ b/api/go.mod @@ -3,7 +3,7 @@ module github.com/openstack-k8s-operators/manila-operator/api go 1.20 require ( - github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6 + github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240522141801-d6e03083e82a github.com/openstack-k8s-operators/lib-common/modules/storage v0.3.1-0.20240429052447-09a614506ca6 k8s.io/api v0.28.9 k8s.io/apimachinery v0.28.9 diff --git a/api/go.sum b/api/go.sum index ad124bd1..4c232cf2 100644 --- a/api/go.sum +++ b/api/go.sum @@ -65,8 +65,8 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/onsi/ginkgo/v2 v2.17.2 h1:7eMhcy3GimbsA3hEnVKdw/PQM9XN9krpKVXsZdph0/g= github.com/onsi/gomega v1.33.0 h1:snPCflnZrpMsy94p4lXVEkHo12lmPnc3vY5XBbreexE= -github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6 h1:WLsG3Ko+phW5xZJjncypLWGASoLqKrt05qN9Zxsad6g= -github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6/go.mod h1:lYhFzul37AR/6gAhTAA1KKWbOlzB3F/7014lejn883c= +github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240522141801-d6e03083e82a h1:kcASVA9sZg9DtggyJlN6JZE6pIenJgXivFK6ry7WUVM= +github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240522141801-d6e03083e82a/go.mod h1:lYhFzul37AR/6gAhTAA1KKWbOlzB3F/7014lejn883c= github.com/openstack-k8s-operators/lib-common/modules/storage v0.3.1-0.20240429052447-09a614506ca6 h1:NNSOEpTZCa9RL5sZiF4ZOlB+agBrL7q7FB9pC58d4S8= github.com/openstack-k8s-operators/lib-common/modules/storage v0.3.1-0.20240429052447-09a614506ca6/go.mod h1:C/qUWW4lW3687riZxYd+YRCtOyHZKURu3Imv6S9OP7U= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= diff --git a/api/v1beta1/manila_webhook.go b/api/v1beta1/manila_webhook.go index 5171e6b7..459d54fd 100644 --- a/api/v1beta1/manila_webhook.go +++ b/api/v1beta1/manila_webhook.go @@ -23,8 +23,14 @@ limitations under the License. package v1beta1 import ( + "fmt" + + "github.com/openstack-k8s-operators/lib-common/modules/common/service" "github.com/openstack-k8s-operators/lib-common/modules/common/util" + apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apimachinery/pkg/util/validation/field" ctrl "sigs.k8s.io/controller-runtime" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/webhook" @@ -127,18 +133,96 @@ var _ webhook.Validator = &Manila{} func (r *Manila) ValidateCreate() (admission.Warnings, error) { manilalog.Info("validate create", "name", r.Name) - // TODO(user): fill in your validation logic upon object creation. + var allErrs field.ErrorList + basePath := field.NewPath("spec") + if err := r.Spec.ValidateCreate(basePath); err != nil { + allErrs = append(allErrs, err...) + } + + if len(allErrs) != 0 { + return nil, apierrors.NewInvalid( + schema.GroupKind{Group: "manila.openstack.org", Kind: "Manila"}, + r.Name, allErrs) + } + return nil, nil } +// ValidateCreate - Exported function wrapping non-exported validate functions, +// this function can be called externally to validate an manila spec. +func (spec *ManilaSpec) ValidateCreate(basePath *field.Path) field.ErrorList { + var allErrs field.ErrorList + + // validate the service override key is valid + allErrs = append(allErrs, service.ValidateRoutedOverrides( + basePath.Child("manilaAPI").Child("override").Child("service"), + spec.ManilaAPI.Override.Service)...) + + return allErrs +} + +// ValidateCreate - +func (spec *ManilaSpecCore) ValidateCreate(basePath *field.Path) field.ErrorList { + var allErrs field.ErrorList + + // validate the service override key is valid + allErrs = append(allErrs, service.ValidateRoutedOverrides( + basePath.Child("manilaAPI").Child("override").Child("service"), + spec.ManilaAPI.Override.Service)...) + + return allErrs +} + // ValidateUpdate implements webhook.Validator so a webhook will be registered for the type func (r *Manila) ValidateUpdate(old runtime.Object) (admission.Warnings, error) { manilalog.Info("validate update", "name", r.Name) - // TODO(user): fill in your validation logic upon object update. + oldManila, ok := old.(*Manila) + if !ok || oldManila == nil { + return nil, apierrors.NewInternalError(fmt.Errorf("unable to convert existing object")) + } + + var allErrs field.ErrorList + basePath := field.NewPath("spec") + + if err := r.Spec.ValidateUpdate(oldManila.Spec, basePath); err != nil { + allErrs = append(allErrs, err...) + } + + if len(allErrs) != 0 { + return nil, apierrors.NewInvalid( + schema.GroupKind{Group: "manila.openstack.org", Kind: "Manila"}, + r.Name, allErrs) + } + return nil, nil } +// ValidateUpdate - Exported function wrapping non-exported validate functions, +// this function can be called externally to validate an manila spec. +func (spec *ManilaSpec) ValidateUpdate(old ManilaSpec, basePath *field.Path) field.ErrorList { + var allErrs field.ErrorList + + // validate the service override key is valid + allErrs = append(allErrs, service.ValidateRoutedOverrides( + basePath.Child("manilaAPI").Child("override").Child("service"), + spec.ManilaAPI.Override.Service)...) + + return allErrs +} + +// ValidateUpdate - +func (spec *ManilaSpecCore) ValidateUpdate(old ManilaSpecCore, basePath *field.Path) field.ErrorList { + var allErrs field.ErrorList + + // validate the service override key is valid + allErrs = append(allErrs, service.ValidateRoutedOverrides( + basePath.Child("manilaAPI").Child("override").Child("service"), + spec.ManilaAPI.Override.Service)...) + + return allErrs +} + // ValidateDelete implements webhook.Validator so a webhook will be registered for the type func (r *Manila) ValidateDelete() (admission.Warnings, error) { manilalog.Info("validate delete", "name", r.Name) diff --git a/go.mod b/go.mod index ea75e29e..6c72445d 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/onsi/gomega v1.33.0 github.com/openstack-k8s-operators/infra-operator/apis v0.3.1-0.20240429104248-25176c735750 github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20240429164853-7e1e3b111ee9 - github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6 + github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240522141801-d6e03083e82a github.com/openstack-k8s-operators/lib-common/modules/storage v0.3.1-0.20240429052447-09a614506ca6 github.com/openstack-k8s-operators/lib-common/modules/test v0.3.1-0.20240429052447-09a614506ca6 github.com/openstack-k8s-operators/manila-operator/api v0.0.0-00010101000000-000000000000 diff --git a/go.sum b/go.sum index 6e48897e..fbd50717 100644 --- a/go.sum +++ b/go.sum @@ -78,8 +78,8 @@ github.com/openstack-k8s-operators/infra-operator/apis v0.3.1-0.20240429104248-2 github.com/openstack-k8s-operators/infra-operator/apis v0.3.1-0.20240429104248-25176c735750/go.mod h1:QN2DJpfEc+mbvvfhoCuJ/UhQzvw12Mf+8nS0QX1HGIg= github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20240429164853-7e1e3b111ee9 h1:aS7xUxC/uOXfw0T4ARTu0G1qb6eJ0WnB2JRv9donPOE= github.com/openstack-k8s-operators/keystone-operator/api v0.3.1-0.20240429164853-7e1e3b111ee9/go.mod h1:Y/ge/l24phVaJb9S8mYRjtnDkohFkX/KEOUXLArcyvQ= -github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6 h1:WLsG3Ko+phW5xZJjncypLWGASoLqKrt05qN9Zxsad6g= -github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240429052447-09a614506ca6/go.mod h1:lYhFzul37AR/6gAhTAA1KKWbOlzB3F/7014lejn883c= +github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240522141801-d6e03083e82a h1:kcASVA9sZg9DtggyJlN6JZE6pIenJgXivFK6ry7WUVM= +github.com/openstack-k8s-operators/lib-common/modules/common v0.3.1-0.20240522141801-d6e03083e82a/go.mod h1:lYhFzul37AR/6gAhTAA1KKWbOlzB3F/7014lejn883c= github.com/openstack-k8s-operators/lib-common/modules/openstack v0.3.1-0.20240429052447-09a614506ca6 h1:/mhzQQ9FF70z00zZD7dpgOoNXvEu9q68oob3oAiJW08= github.com/openstack-k8s-operators/lib-common/modules/openstack v0.3.1-0.20240429052447-09a614506ca6/go.mod h1:mrRNYeg8jb1zgGsufpN1/IB3sdbaST8btTBLwQ+taaA= github.com/openstack-k8s-operators/lib-common/modules/storage v0.3.1-0.20240429052447-09a614506ca6 h1:NNSOEpTZCa9RL5sZiF4ZOlB+agBrL7q7FB9pC58d4S8= diff --git a/test/functional/manila_controller_test.go b/test/functional/manila_controller_test.go index 8c9d3063..b0826644 100644 --- a/test/functional/manila_controller_test.go +++ b/test/functional/manila_controller_test.go @@ -17,13 +17,16 @@ package functional import ( "fmt" + "os" . "github.com/onsi/ginkgo/v2" //revive:disable:dot-imports . "github.com/onsi/gomega" //revive:disable:dot-imports + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" //revive:disable-next-line:dot-imports . "github.com/openstack-k8s-operators/lib-common/modules/common/test/helpers" mariadb_test "github.com/openstack-k8s-operators/mariadb-operator/api/test/helpers" corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" "k8s.io/utils/ptr" @@ -729,3 +732,43 @@ var _ = Describe("Manila controller", func() { }) }) + +var _ = Describe("Manila Webhook", func() { + + BeforeEach(func() { + err := os.Setenv("OPERATOR_TEMPLATES", "../../templates") + Expect(err).NotTo(HaveOccurred()) + }) + + It("rejects with wrong ManilaAPI service override endpoint type", func() { + spec := GetDefaultManilaSpec() + apiSpec := GetDefaultManilaAPISpec() + apiSpec["override"] = map[string]interface{}{ + "service": map[string]interface{}{ + "internal": map[string]interface{}{}, + "wrooong": map[string]interface{}{}, + }, + } + spec["manilaAPI"] = apiSpec + + raw := map[string]interface{}{ + "apiVersion": "manila.openstack.org/v1beta1", + "kind": "Manila", + "metadata": map[string]interface{}{ + "name": manilaTest.Instance.Name, + "namespace": manilaTest.Instance.Namespace, + }, + "spec": spec, + } + + unstructuredObj := &unstructured.Unstructured{Object: raw} + _, err := controllerutil.CreateOrPatch( + th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil }) + Expect(err).To(HaveOccurred()) + Expect(err.Error()).To( + ContainSubstring( + "invalid: spec.manilaAPI.override.service[wrooong]: " + + "Invalid value: \"wrooong\": invalid endpoint type: wrooong"), + ) + }) +})