Skip to content

Commit 7b5086d

Browse files
mdbooths3rj1k
authored andcommitted
Simplify webhook test
1 parent f983e5f commit 7b5086d

File tree

1 file changed

+10
-33
lines changed

1 file changed

+10
-33
lines changed

pkg/webhooks/openstackcluster_webhook.go

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"fmt"
2222
"reflect"
2323

24-
"k8s.io/apimachinery/pkg/api/equality"
2524
"k8s.io/apimachinery/pkg/runtime"
2625
"k8s.io/apimachinery/pkg/util/validation/field"
2726
"k8s.io/utils/ptr"
@@ -201,15 +200,17 @@ func (*openStackClusterWebhook) ValidateUpdate(_ context.Context, oldObjRaw, new
201200
allErrs = append(allErrs, field.Forbidden(field.NewPath("spec", "managedSubnets"), "cannot add or remove subnets"))
202201
} else {
203202
// Build maps of subnets by CIDR
204-
oldSubnetMap := make(map[string]infrav1.SubnetSpec)
205-
newSubnetMap := make(map[string]infrav1.SubnetSpec)
203+
oldSubnetMap := make(map[string]*infrav1.SubnetSpec)
206204

207-
for _, subnet := range oldObj.Spec.ManagedSubnets {
208-
oldSubnetMap[subnet.CIDR] = subnet
205+
for i := range oldObj.Spec.ManagedSubnets {
206+
oldSubnet := &oldObj.Spec.ManagedSubnets[i]
207+
oldSubnetMap[oldSubnet.CIDR] = oldSubnet
209208
}
210209

211210
// Check if all new subnets have matching old subnets with the same CIDR
212-
for _, newSubnet := range newObj.Spec.ManagedSubnets {
211+
for i := range newObj.Spec.ManagedSubnets {
212+
newSubnet := &newObj.Spec.ManagedSubnets[i]
213+
213214
oldSubnet, exists := oldSubnetMap[newSubnet.CIDR]
214215
if !exists {
215216
allErrs = append(allErrs, field.Forbidden(
@@ -219,34 +220,10 @@ func (*openStackClusterWebhook) ValidateUpdate(_ context.Context, oldObjRaw, new
219220
continue
220221
}
221222

222-
// Check if AllocationPools have changed
223-
if !equality.Semantic.DeepEqual(oldSubnet.AllocationPools, newSubnet.AllocationPools) {
224-
allErrs = append(allErrs, field.Forbidden(
225-
field.NewPath("spec", "managedSubnets").Child("allocationPools"),
226-
"cannot modify allocation pools in existing subnet",
227-
))
228-
}
229-
230-
newSubnetMap[newSubnet.CIDR] = newSubnet
223+
// DNSNameservers is mutable
224+
oldSubnet.DNSNameservers = nil
225+
newSubnet.DNSNameservers = nil
231226
}
232-
233-
// Create modified copies of the subnets with DNSNameservers cleared
234-
oldSubnets := make([]infrav1.SubnetSpec, 0, len(oldObj.Spec.ManagedSubnets))
235-
newSubnets := make([]infrav1.SubnetSpec, 0, len(newObj.Spec.ManagedSubnets))
236-
for _, subnet := range oldObj.Spec.ManagedSubnets {
237-
subnetCopy := subnet
238-
subnetCopy.DNSNameservers = nil
239-
oldSubnets = append(oldSubnets, subnetCopy)
240-
241-
if newSubnet, exists := newSubnetMap[subnet.CIDR]; exists {
242-
newSubnetCopy := newSubnet
243-
newSubnetCopy.DNSNameservers = nil
244-
newSubnets = append(newSubnets, newSubnetCopy)
245-
}
246-
}
247-
248-
oldObj.Spec.ManagedSubnets = oldSubnets
249-
newObj.Spec.ManagedSubnets = newSubnets
250227
}
251228
}
252229

0 commit comments

Comments
 (0)