@@ -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