Skip to content

Commit d8f4b7e

Browse files
openstack: Validate controlPlanePort has subnet
Reject a controlPlanePort where the subnet filter is not set. The rest of the code (both in pre-flight validation and in machine generation) assumes that a subnet filter is set on the controlPlanePort.
1 parent 54e81ef commit d8f4b7e

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

pkg/types/openstack/validation/platform.go

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ func ValidatePlatform(p *openstack.Platform, n *types.Networking, fldPath *field
2828
}
2929
}
3030

31-
if c.OpenStack.ControlPlanePort != nil {
32-
allErrs = append(allErrs, validateControlPlanePort(c, fldPath)...)
31+
if controlPlanePort := c.OpenStack.ControlPlanePort; controlPlanePort != nil {
32+
allErrs = append(allErrs, validateControlPlanePort(controlPlanePort, fldPath.Child("controlPlanePort"))...)
3333
}
3434

3535
return allErrs
@@ -46,20 +46,31 @@ func validateLoadBalancer(lbType configv1.PlatformLoadBalancerType) bool {
4646
}
4747

4848
// validateControlPlanePort returns all the errors found when the control plane port is not valid.
49-
func validateControlPlanePort(c *types.InstallConfig, fldPath *field.Path) field.ErrorList {
50-
controlPlanePort := c.OpenStack.ControlPlanePort
49+
func validateControlPlanePort(controlPlanePort *openstack.PortTarget, fldPath *field.Path) field.ErrorList {
5150
var allErrs field.ErrorList
52-
if len(controlPlanePort.FixedIPs) <= 2 {
53-
for _, fixedIP := range controlPlanePort.FixedIPs {
51+
52+
if controlPlanePort.Network.ID != "" && !validation.ValidUUIDv4(controlPlanePort.Network.ID) {
53+
allErrs = append(allErrs, field.Invalid(fldPath.Child("network"), controlPlanePort.Network.ID, "invalid network ID: must be a UUIDv4"))
54+
}
55+
56+
fixedIPsField := fldPath.Child("fixedIPs")
57+
58+
switch l := len(controlPlanePort.FixedIPs); l {
59+
case 0:
60+
allErrs = append(allErrs, field.Required(fixedIPsField, "it is required to set a subnet filter to the controlPlanePort"))
61+
case 1, 2:
62+
for i, fixedIP := range controlPlanePort.FixedIPs {
63+
subnetField := fixedIPsField.Index(i).Child("subnet")
5464
if fixedIP.Subnet.ID != "" && !validation.ValidUUIDv4(fixedIP.Subnet.ID) {
55-
allErrs = append(allErrs, field.Invalid(fldPath.Child("controlPlanePort").Child("fixedIPs"), fixedIP.Subnet.ID, "invalid subnet ID"))
65+
allErrs = append(allErrs, field.Invalid(subnetField.Child("id"), fixedIP.Subnet.ID, "invalid subnet ID: must be a UUIDv4"))
66+
}
67+
if fixedIP.Subnet.ID == "" && fixedIP.Subnet.Name == "" {
68+
allErrs = append(allErrs, field.Required(subnetField, "either ID or Name must be set on the subnet filter"))
5669
}
5770
}
58-
if controlPlanePort.Network.ID != "" && !validation.ValidUUIDv4(controlPlanePort.Network.ID) {
59-
allErrs = append(allErrs, field.Invalid(fldPath.Child("controlPlanePort").Child("network"), controlPlanePort.Network.ID, "invalid network ID"))
60-
}
61-
} else {
62-
allErrs = append(allErrs, field.TooMany(fldPath.Child("fixedIPs"), len(controlPlanePort.FixedIPs), 2))
71+
default:
72+
allErrs = append(allErrs, field.TooMany(fixedIPsField, l, 2))
6373
}
74+
6475
return allErrs
6576
}

pkg/types/openstack/validation/platform_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func TestValidatePlatform(t *testing.T) {
141141
return p
142142
}(),
143143
networking: validNetworking(),
144-
expectedError: `^test-path\.controlPlanePort.fixedIPs: Invalid value: "fake": invalid subnet ID`,
144+
expectedError: `^test-path\.controlPlanePort.fixedIPs\[0\]\.subnet.id: Invalid value: "fake": invalid subnet ID`,
145145
},
146146
}
147147
for _, tc := range cases {

0 commit comments

Comments
 (0)