Skip to content

Commit 299da1a

Browse files
EmilienMopenshift-cherrypick-robot
authored andcommitted
Bug 2073398: Fix InstanceCreate port & trunk cleanup
With this change, the port and trunk cleanups are triggered by any early failure of InstanceCreate. When InstanceCreate creates ports and if it creates trunks, these resources will be deleted if any of the next steps fail and cause the server never to be created. Note: the trunk will be deleted first, then the ports.
1 parent 12df76b commit 299da1a

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

pkg/cloud/openstack/clients/machineservice.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,18 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
663663
if err != nil {
664664
return nil, fmt.Errorf("Failed to create port err: %v", err)
665665
}
666+
defer func() {
667+
// If the server is created in Nova, the lifetime of the associated ports will be
668+
// bound to it. This deferred cleanup function tackles the case where a port was created
669+
// but never attached to a server. In that case, the port must be removed manually.
670+
if server == nil {
671+
if err := ports.Delete(is.networkClient, port.ID).ExtractErr(); err != nil {
672+
klog.Infof("Failed to delete stale port %q", port.ID)
673+
} else {
674+
klog.Infof("Deleted stale port %q", port.ID)
675+
}
676+
}
677+
}()
666678

667679
portTags := deduplicateList(append(machineTags, portOpt.Tags...))
668680
_, err = attributestags.ReplaceAll(is.networkClient, "ports", port.ID, attributestags.ReplaceAllOpts{
@@ -675,10 +687,20 @@ func (is *InstanceService) InstanceCreate(clusterName string, name string, clust
675687
})
676688

677689
if config.Trunk == true {
678-
_, err := getOrCreateTrunk(is, port, machineTags)
690+
trunk, err := getOrCreateTrunk(is, port, machineTags)
679691
if err != nil {
680692
return nil, err
681693
}
694+
defer func() {
695+
// We need to cleanup the trunks before we remove the staled ports.
696+
if server == nil {
697+
if err := trunks.Delete(is.networkClient, trunk.ID).ExtractErr(); err != nil {
698+
klog.Infof("Failed to delete stale trunk %q", trunk.ID)
699+
} else {
700+
klog.Infof("Deleted stale trunk %q", trunk.ID)
701+
}
702+
}
703+
}()
682704
}
683705
}
684706

0 commit comments

Comments
 (0)