Skip to content

Commit 2db68bd

Browse files
authored
Merge pull request #1194 from MaxRink/validate-ipaddr-a3
add checks for network validation on update in vSphereMachine #1192 of a3
2 parents 504148a + fbac1c1 commit 2db68bd

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

api/v1alpha3/vspheremachine_webhook.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ func (r *VSphereMachine) ValidateUpdate(old runtime.Object) error {
8383
delete(oldVSphereMachineNetwork, "devices")
8484
delete(newVSphereMachineNetwork, "devices")
8585

86+
// validate that IPAddrs in updaterequest are valid
87+
spec := r.Spec
88+
for i, device := range spec.Network.Devices {
89+
for j, ip := range device.IPAddrs {
90+
if _, _, err := net.ParseCIDR(ip); err != nil {
91+
allErrs = append(allErrs, field.Invalid(field.NewPath("spec", "network", fmt.Sprintf("devices[%d]", i), fmt.Sprintf("ipAddrs[%d]", j)), ip, "ip addresses should be in the CIDR format"))
92+
}
93+
}
94+
}
95+
8696
if !reflect.DeepEqual(oldVSphereMachineSpec, newVSphereMachineSpec) {
8797
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec"), "cannot be modified"))
8898
}

api/v1alpha3/vspheremachine_webhook_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ func TestVSphereMachine_ValidateCreate(t *testing.T) {
4545
vsphereMachine: createVSphereMachine("foo.com", nil, "", []string{"192.168.0.1/32", "192.168.0.3"}),
4646
wantErr: true,
4747
},
48+
{
49+
name: "IPs are not valid IPs in CIDR format",
50+
vsphereMachine: createVSphereMachine("foo.com", nil, "", []string{"<nil>/32", "192.168.0.644/33"}),
51+
wantErr: true,
52+
},
4853
{
4954
name: "successful VSphereMachine creation",
5055
vsphereMachine: createVSphereMachine("foo.com", nil, "", []string{"192.168.0.1/32", "192.168.0.3/32"}),
@@ -86,6 +91,18 @@ func TestVSphereMachine_ValidateUpdate(t *testing.T) {
8691
vsphereMachine: createVSphereMachine("foo.com", &someProviderID, "", []string{"192.168.0.1/32", "192.168.0.10/32"}),
8792
wantErr: false,
8893
},
94+
{
95+
name: "updating non-existing IP with invalid ips can not be done",
96+
oldVSphereMachine: createVSphereMachine("foo.com", nil, "", nil),
97+
vsphereMachine: createVSphereMachine("foo.com", &someProviderID, "", []string{"<nil>/32", "192.168.0.10/33"}),
98+
wantErr: true,
99+
},
100+
{
101+
name: "updating existing IP with invalid ips can not be done",
102+
oldVSphereMachine: createVSphereMachine("foo.com", nil, "", []string{"192.168.0.1/32"}),
103+
vsphereMachine: createVSphereMachine("foo.com", &someProviderID, "", []string{"<nil>/32", "192.168.0.10/33"}),
104+
wantErr: true,
105+
},
89106
{
90107
name: "updating server cannot be done",
91108
oldVSphereMachine: createVSphereMachine("foo.com", nil, "", []string{"192.168.0.1/32"}),

0 commit comments

Comments
 (0)