Skip to content

Commit 33c6fd3

Browse files
committed
WIP: Determine ports to delete based on tags
1 parent ec4bd07 commit 33c6fd3

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

upup/pkg/fi/cloudup/openstack/cloud.go

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,37 @@ func InstanceInClusterAndIG(instance servers.Server, clusterName string, instanc
617617
return true
618618
}
619619

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+
620651
func deleteGroup(c OpenstackCloud, g *cloudinstances.CloudInstanceGroup) error {
621652
cluster := g.Raw.(*kops.Cluster)
622653
allInstances, err := c.ListInstances(servers.ListOpts{
@@ -639,18 +670,10 @@ func deleteGroup(c OpenstackCloud, g *cloudinstances.CloudInstanceGroup) error {
639670
return fmt.Errorf("could not delete instance %q: %v", instance.ID, err)
640671
}
641672
}
642-
ports, err := c.ListPorts(ports.ListOpts{})
643-
if err != nil {
644-
return fmt.Errorf("could not list ports %v", err)
645-
}
646673

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
654677
}
655678

656679
sgName := g.InstanceGroup.Name

0 commit comments

Comments
 (0)