Skip to content

Commit dcdfecb

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 ae527bb commit dcdfecb

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
@@ -132,6 +132,35 @@ var _ = Describe("OpenStackMachine API validations", func() {
132132
Expect(k8sClient.Create(ctx, machine)).To(Succeed(), "Creating a machine with max metadata key and value should succeed")
133133
})
134134

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

0 commit comments

Comments
 (0)