diff --git a/apis/core/v1beta1/openstackcontrolplane_webhook.go b/apis/core/v1beta1/openstackcontrolplane_webhook.go index 22105854e..0e9d9f430 100644 --- a/apis/core/v1beta1/openstackcontrolplane_webhook.go +++ b/apis/core/v1beta1/openstackcontrolplane_webhook.go @@ -324,6 +324,16 @@ func (r *OpenStackControlPlane) ValidateCreateServices(basePath *field.Path) (ad } } + if r.Spec.Rabbitmq.Enabled { + if r.Spec.Rabbitmq.Templates != nil { + err := common_webhook.ValidateDNS1123Label( + basePath.Child("rabbitmq").Child("template"), + maps.Keys(*r.Spec.Rabbitmq.Templates), + memcachedv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "-" + errors = append(errors, err...) + } + } + return warnings, errors } @@ -435,6 +445,16 @@ func (r *OpenStackControlPlane) ValidateUpdateServices(old OpenStackControlPlane } } + if r.Spec.Rabbitmq.Enabled { + if r.Spec.Rabbitmq.Templates != nil { + err := common_webhook.ValidateDNS1123Label( + basePath.Child("rabbitmq").Child("template"), + maps.Keys(*r.Spec.Rabbitmq.Templates), + memcachedv1.CrMaxLengthCorrection) // omit issue with statefulset pod label "controller-revision-hash": "-" + errors = append(errors, err...) + } + } + return errors } diff --git a/tests/functional/ctlplane/openstackoperator_controller_test.go b/tests/functional/ctlplane/openstackoperator_controller_test.go index 4725481cf..5af882b9e 100644 --- a/tests/functional/ctlplane/openstackoperator_controller_test.go +++ b/tests/functional/ctlplane/openstackoperator_controller_test.go @@ -1937,4 +1937,78 @@ var _ = Describe("OpenStackOperator Webhook", func() { "Invalid value: \"foo_bar\": a lowercase RFC 1123 label must consist"), ) }) + + It("Blocks creating ctlplane CRs with to long rabbitmq keys/names", func() { + spec := GetDefaultOpenStackControlPlaneSpec() + + rabbitmqTemplate := map[string]interface{}{ + "foo-1234567890-1234567890-1234567890-1234567890-1234567890": map[string]interface{}{ + "replicas": 1, + }, + } + + spec["rabbitmq"] = map[string]interface{}{ + "enabled": true, + "templates": rabbitmqTemplate, + } + + raw := map[string]interface{}{ + "apiVersion": "core.openstack.org/v1beta1", + "kind": "OpenStackControlPlane", + "metadata": map[string]interface{}{ + "name": "foo", + "namespace": namespace, + }, + "spec": spec, + } + + unstructuredObj := &unstructured.Unstructured{Object: raw} + _, err := controllerutil.CreateOrPatch( + th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil }) + Expect(err).Should(HaveOccurred()) + var statusError *k8s_errors.StatusError + Expect(errors.As(err, &statusError)).To(BeTrue()) + Expect(statusError.ErrStatus.Details.Kind).To(Equal("OpenStackControlPlane")) + Expect(statusError.ErrStatus.Message).To( + ContainSubstring( + "Invalid value: \"foo-1234567890-1234567890-1234567890-1234567890-1234567890\": must be no more than 52 characters"), + ) + }) + + It("Blocks creating ctlplane CRs with wrong rabbitmq keys/names", func() { + spec := GetDefaultOpenStackControlPlaneSpec() + + rabbitmqTemplate := map[string]interface{}{ + "foo_bar": map[string]interface{}{ + "replicas": 1, + }, + } + + spec["rabbitmq"] = map[string]interface{}{ + "enabled": true, + "templates": rabbitmqTemplate, + } + + raw := map[string]interface{}{ + "apiVersion": "core.openstack.org/v1beta1", + "kind": "OpenStackControlPlane", + "metadata": map[string]interface{}{ + "name": "foo", + "namespace": namespace, + }, + "spec": spec, + } + + unstructuredObj := &unstructured.Unstructured{Object: raw} + _, err := controllerutil.CreateOrPatch( + th.Ctx, th.K8sClient, unstructuredObj, func() error { return nil }) + Expect(err).Should(HaveOccurred()) + var statusError *k8s_errors.StatusError + Expect(errors.As(err, &statusError)).To(BeTrue()) + Expect(statusError.ErrStatus.Details.Kind).To(Equal("OpenStackControlPlane")) + Expect(statusError.ErrStatus.Message).To( + ContainSubstring( + "Invalid value: \"foo_bar\": a lowercase RFC 1123 label must consist"), + ) + }) })