Skip to content

Commit a632588

Browse files
committed
Fix FailoverIP validation and immutability tests
- Update FailoverIP validation rule to handle optional fields correctly - Add !has(self) check for optional field handling - Improve IPv4 regex pattern for more accurate validation - Fixes validation for 'Auto' (mixed case) and incomplete IPs - Fix FailoverIP immutability test - Add Get calls before Update attempts to ensure CEL validation can properly compare self with oldSelf - Same fix pattern as DatacenterID immutability test
1 parent 089ffda commit a632588

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

api/v1alpha1/ionoscloudmachine_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ type IonosCloudMachineSpec struct {
164164
//
165165
// If the machine is a control plane machine, this field will not be taken into account.
166166
//+kubebuilder:validation:XValidation:rule="self == oldSelf",message="failoverIP is immutable"
167-
//+kubebuilder:validation:XValidation:rule=`self == "AUTO" || self.matches("((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$")`,message="failoverIP must be either 'AUTO' or a valid IPv4 address"
167+
//+kubebuilder:validation:XValidation:rule=`!has(self) || self == "AUTO" || self.matches("^((25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])$")`,message="failoverIP must be either 'AUTO' or a valid IPv4 address"
168168
//+optional
169169
FailoverIP *string `json:"failoverIP,omitempty"`
170170

api/v1alpha1/ionoscloudmachine_types_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,8 +463,12 @@ var _ = Describe("IonosCloudMachine Tests", func() {
463463
m.Spec.FailoverIP = ptr.To(CloudResourceConfigAuto)
464464
Expect(k8sClient.Create(context.Background(), m)).To(Succeed())
465465
Expect(m.Spec.FailoverIP).To(Equal(ptr.To(CloudResourceConfigAuto)))
466+
// Get the object from the server to ensure we have the latest version
467+
Expect(k8sClient.Get(context.Background(), client.ObjectKey{Name: m.Name, Namespace: m.Namespace}, m)).To(Succeed())
466468
m.Spec.FailoverIP = ptr.To("127.0.0.1")
467469
Expect(k8sClient.Update(context.Background(), m)).ToNot(Succeed())
470+
// Get the object again before the next update attempt
471+
Expect(k8sClient.Get(context.Background(), client.ObjectKey{Name: m.Name, Namespace: m.Namespace}, m)).To(Succeed())
468472
m.Spec.FailoverIP = ptr.To("")
469473
Expect(k8sClient.Update(context.Background(), m)).ToNot(Succeed())
470474
})

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ spec:
260260
- message: failoverIP is immutable
261261
rule: self == oldSelf
262262
- message: failoverIP must be either 'AUTO' or a valid IPv4 address
263-
rule: self == "AUTO" || self.matches("((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$")
263+
rule: '!has(self) || self == "AUTO" || self.matches("^((25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])\\.){3}(25[0-5]|(2[0-4]|1[0-9]|[1-9])?[0-9])$")'
264264
ipv4PoolRef:
265265
description: |-
266266
IPv4PoolRef is a reference to an IPAMConfig Pool resource, which exposes IPv4 addresses.

0 commit comments

Comments
 (0)