Skip to content

Commit b990849

Browse files
authored
Fix bug which would create duplicated subnets when realization fails (vmware-tanzu#781)
Subnets are allocated from subnet sets, it's id is suffixed with a random string, if the subnet failed to realize, and not being deleted, then a new subnet with different suffix will be created, thus leading a mount of duplicated subnets being left in the system. Signed-off-by: Xie Zheng <[email protected]>
1 parent fa4dbe6 commit b990849

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

pkg/nsx/services/subnet/subnet.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ func (service *SubnetService) createOrUpdateSubnet(obj client.Object, nsxSubnet
139139
// For SubnetSets, since the ID includes a random value, the created NSX Subnet needs to be deleted and recreated.
140140
if err = realizeService.CheckRealizeState(backoff, *nsxSubnet.Path, "RealizedLogicalSwitch"); err != nil {
141141
log.Error(err, "failed to check subnet realization state", "ID", *nsxSubnet.Id)
142+
// Delete the subnet if realization check fails, avoiding creating duplicate subnets continuously.
143+
deleteErr := service.DeleteSubnet(*nsxSubnet)
144+
if deleteErr != nil {
145+
log.Error(deleteErr, "failed to delete subnet after realization check failure", "ID", *nsxSubnet.Id)
146+
return "", fmt.Errorf("realization check failed: %v; deletion failed: %v", err, deleteErr)
147+
}
142148
return "", err
143149
}
144150
if err = service.SubnetStore.Apply(nsxSubnet); err != nil {

0 commit comments

Comments
 (0)