@@ -617,6 +617,37 @@ func InstanceInClusterAndIG(instance servers.Server, clusterName string, instanc
617
617
return true
618
618
}
619
619
620
+ func deletePorts (c OpenstackCloud , instanceGroupName string , clusterName string ) error {
621
+ tags := []string {
622
+ fmt .Sprintf ("%s=%s" , TagClusterName , clusterName ),
623
+ fmt .Sprintf ("%s=%s" , TagKopsInstanceGroup , instanceGroupName ),
624
+ }
625
+
626
+ ports , err := c .ListPorts (ports.ListOpts {Tags : strings .Join (tags , "," )})
627
+ if err != nil {
628
+ return fmt .Errorf ("could not list ports %v" , err )
629
+ }
630
+
631
+ for _ , port := range ports {
632
+ // previous approach was problematic:
633
+ // for example in case there is a group called "worker" and "worker-2", it will delete ports of "worker" as well,
634
+ // because there might be port names like:
635
+ // * "port-worker-2-<clusterName>"
636
+ // * "port-worker-20-<clusterName>"
637
+ klog .V (2 ).Infof ("Delete port '%s' (%s)" , port .Name , port .ID )
638
+ err := c .DeletePort (port .ID )
639
+
640
+ // TODO:
641
+ // really give up after trying to delete one port? other ports will be orphaned
642
+ // better to try all ports and collect errors?
643
+ if err != nil {
644
+ return fmt .Errorf ("could not delete port %q: %v" , port .ID , err )
645
+ }
646
+ }
647
+
648
+ return nil
649
+ }
650
+
620
651
func deleteGroup (c OpenstackCloud , g * cloudinstances.CloudInstanceGroup ) error {
621
652
cluster := g .Raw .(* kops.Cluster )
622
653
allInstances , err := c .ListInstances (servers.ListOpts {
@@ -639,18 +670,10 @@ func deleteGroup(c OpenstackCloud, g *cloudinstances.CloudInstanceGroup) error {
639
670
return fmt .Errorf ("could not delete instance %q: %v" , instance .ID , err )
640
671
}
641
672
}
642
- ports , err := c .ListPorts (ports.ListOpts {})
643
- if err != nil {
644
- return fmt .Errorf ("could not list ports %v" , err )
645
- }
646
673
647
- for _ , port := range ports {
648
- if strings .HasPrefix (port .Name , fmt .Sprintf ("port-%s" , g .InstanceGroup .Name )) && fi .ArrayContains (port .Tags , fmt .Sprintf ("%s=%s" , TagClusterName , cluster .Name )) {
649
- err := c .DeletePort (port .ID )
650
- if err != nil {
651
- return fmt .Errorf ("could not delete port %q: %v" , port .ID , err )
652
- }
653
- }
674
+ err = deletePorts (c , g .InstanceGroup .Name , cluster .Name )
675
+ if err != nil {
676
+ return err
654
677
}
655
678
656
679
sgName := g .InstanceGroup .Name
0 commit comments