From ad3fd18ad26511faf17dcb231590d41a73064049 Mon Sep 17 00:00:00 2001 From: Matthew Booth Date: Wed, 20 Mar 2024 16:52:29 +0000 Subject: [PATCH] Fix server metadata length validation --- api/v1beta1/openstackmachine_types.go | 6 ++-- ...re.cluster.x-k8s.io_openstackclusters.yaml | 10 +++--- ...er.x-k8s.io_openstackclustertemplates.yaml | 10 +++--- ...re.cluster.x-k8s.io_openstackmachines.yaml | 10 +++--- ...er.x-k8s.io_openstackmachinetemplates.yaml | 10 +++--- docs/book/src/api/v1beta1/api.md | 6 ++-- .../apivalidations/openstackmachine_test.go | 31 +++++++++++++++++++ 7 files changed, 53 insertions(+), 30 deletions(-) diff --git a/api/v1beta1/openstackmachine_types.go b/api/v1beta1/openstackmachine_types.go index 52caccbaf1..17249383d4 100644 --- a/api/v1beta1/openstackmachine_types.go +++ b/api/v1beta1/openstackmachine_types.go @@ -101,11 +101,13 @@ type OpenStackMachineSpec struct { type ServerMetadata struct { // Key is the server metadata key - // kubebuilder:validation:MaxLength:=255 + // +kubebuilder:validation:MaxLength:=255 + // +kubebuilder:validation:Required Key string `json:"key"` // Value is the server metadata value - // kubebuilder:validation:MaxLength:=255 + // +kubebuilder:validation:MaxLength:=255 + // +kubebuilder:validation:Required Value string `json:"value"` } diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml index 30ae084647..88d9ebb896 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml @@ -5536,14 +5536,12 @@ spec: items: properties: key: - description: |- - Key is the server metadata key - kubebuilder:validation:MaxLength:=255 + description: Key is the server metadata key + maxLength: 255 type: string value: - description: |- - Value is the server metadata value - kubebuilder:validation:MaxLength:=255 + description: Value is the server metadata value + maxLength: 255 type: string required: - key diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml index f577f0eb3b..85ff200d31 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml @@ -2970,14 +2970,12 @@ spec: items: properties: key: - description: |- - Key is the server metadata key - kubebuilder:validation:MaxLength:=255 + description: Key is the server metadata key + maxLength: 255 type: string value: - description: |- - Value is the server metadata value - kubebuilder:validation:MaxLength:=255 + description: Value is the server metadata value + maxLength: 255 type: string required: - key diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml index 26e74f69e6..2067fad50e 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml @@ -2318,14 +2318,12 @@ spec: items: properties: key: - description: |- - Key is the server metadata key - kubebuilder:validation:MaxLength:=255 + description: Key is the server metadata key + maxLength: 255 type: string value: - description: |- - Value is the server metadata value - kubebuilder:validation:MaxLength:=255 + description: Value is the server metadata value + maxLength: 255 type: string required: - key diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml index 0780b5e09f..b71228fa4a 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml @@ -1997,14 +1997,12 @@ spec: items: properties: key: - description: |- - Key is the server metadata key - kubebuilder:validation:MaxLength:=255 + description: Key is the server metadata key + maxLength: 255 type: string value: - description: |- - Value is the server metadata value - kubebuilder:validation:MaxLength:=255 + description: Value is the server metadata value + maxLength: 255 type: string required: - key diff --git a/docs/book/src/api/v1beta1/api.md b/docs/book/src/api/v1beta1/api.md index 2b12258bef..58b54fa8ae 100644 --- a/docs/book/src/api/v1beta1/api.md +++ b/docs/book/src/api/v1beta1/api.md @@ -4469,8 +4469,7 @@ string -

Key is the server metadata key -kubebuilder:validation:MaxLength:=255

+

Key is the server metadata key

@@ -4481,8 +4480,7 @@ string -

Value is the server metadata value -kubebuilder:validation:MaxLength:=255

+

Value is the server metadata value

diff --git a/test/e2e/suites/apivalidations/openstackmachine_test.go b/test/e2e/suites/apivalidations/openstackmachine_test.go index 00f262cc0b..c984ab1e61 100644 --- a/test/e2e/suites/apivalidations/openstackmachine_test.go +++ b/test/e2e/suites/apivalidations/openstackmachine_test.go @@ -17,6 +17,8 @@ limitations under the License. package apivalidations import ( + "strings" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" @@ -54,4 +56,33 @@ var _ = Describe("OpenStackMachine API validations", func() { machine.Spec.ProviderID = pointer.String("bar") Expect(k8sClient.Update(ctx, machine)).NotTo(Succeed(), "Updating providerID should fail") }) + + It("should not allow server metadata to exceed 255 characters", func() { + By("Creating a machine with a metadata key that is too long") + machine.Spec.ServerMetadata = []infrav1.ServerMetadata{ + { + Key: strings.Repeat("a", 256), + Value: "value", + }, + } + Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a long metadata key should fail") + + By("Creating a machine with a metadata value that is too long") + machine.Spec.ServerMetadata = []infrav1.ServerMetadata{ + { + Key: "key", + Value: strings.Repeat("a", 256), + }, + } + Expect(k8sClient.Create(ctx, machine)).NotTo(Succeed(), "Creating a machine with a long metadata value should fail") + + By("Creating a machine with a metadata key and value of 255 characters should succeed") + machine.Spec.ServerMetadata = []infrav1.ServerMetadata{ + { + Key: strings.Repeat("a", 255), + Value: strings.Repeat("b", 255), + }, + } + Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with max metadata key and value should succeed") + }) })