Skip to content

Commit

Permalink
Fix server metadata length validation
Browse files Browse the repository at this point in the history
  • Loading branch information
mdbooth committed Mar 20, 2024
1 parent 66d93db commit ad3fd18
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 30 deletions.
6 changes: 4 additions & 2 deletions api/v1beta1/openstackmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions docs/book/src/api/v1beta1/api.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions test/e2e/suites/apivalidations/openstackmachine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
})
})

0 comments on commit ad3fd18

Please sign in to comment.