Skip to content

Commit 3a3a53b

Browse files
committed
add apivalidations tests to ensure that identityRef field is immutable and can't be set on created machine
Signed-off-by: MatthieuFin <[email protected]>
1 parent 5bf5f01 commit 3a3a53b

8 files changed

+62
-25
lines changed

api/v1beta1/identity_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta1
1818

1919
// OpenStackIdentityReference is a reference to an infrastructure
2020
// provider identity to be used to provision cluster resources.
21+
// +kubebuilder:validation:XValidation:rule="(!has(self.region) && !has(oldSelf.region)) || self.region == oldSelf.region",message="region is immutable"
2122
type OpenStackIdentityReference struct {
2223
// Name is the name of a secret in the same namespace as the resource being provisioned.
2324
// The secret must contain a key named `clouds.yaml` which contains an OpenStack clouds.yaml file.
@@ -32,7 +33,6 @@ type OpenStackIdentityReference struct {
3233
// Region specifies an OpenStack region to use. If specified, it overrides
3334
// any value in clouds.yaml. If specified for an OpenStackMachine, its
3435
// value will be included in providerID.
35-
// +kubebuilder:validation:XValidation:rule="self == oldSelf",message="region is immutable"
3636
// +optional
3737
Region string `json:"region,omitempty"`
3838
}

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclusters.yaml

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackclustertemplates.yaml

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackfloatingippools.yaml

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachines.yaml

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackmachinetemplates.yaml

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/infrastructure.cluster.x-k8s.io_openstackservers.yaml

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/e2e/suites/apivalidations/openstackmachine_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,35 @@ var _ = Describe("OpenStackMachine API validations", func() {
133133
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with max metadata key and value should succeed")
134134
})
135135

136+
It("should allow server identityRef Region field or unset on creation", func() {
137+
machine := defaultMachine()
138+
139+
By("Creating a machine with identityRef Region field set on creation")
140+
machine.Spec.IdentityRef = &infrav1.OpenStackIdentityReference{
141+
Name: "secretName",
142+
CloudName: "cloudName",
143+
Region: "regionName",
144+
}
145+
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with a spec.identityRef.Region field should be allowed")
146+
147+
By("Updating the identityRef Region field")
148+
machine.Spec.IdentityRef.Region = "anotherRegionName"
149+
Expect(k8sClient.Update(ctx, machine)).NotTo(Succeed(), "Updating spec.identityRef.Region field should fail")
150+
151+
machine = defaultMachine()
152+
By("Creating a machine with identityRef Region field not set on creation")
153+
machine.Spec.IdentityRef = &infrav1.OpenStackIdentityReference{
154+
Name: "secretName",
155+
CloudName: "cloudName",
156+
}
157+
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine without a spec.identityRef.Region field should be allowed")
158+
159+
By("Setting the identityRef Region field")
160+
machine.Spec.IdentityRef.Region = "regionName"
161+
Expect(k8sClient.Update(ctx, machine)).NotTo(Succeed(), "Setting spec.identityRef.Region field should fail")
162+
163+
})
164+
136165
Context("flavors", func() {
137166
It("should require either a flavor or flavorID", func() {
138167
machine := defaultMachine()

0 commit comments

Comments
 (0)