Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: fix(openstack): determine ports to delete based on tags #16961

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions upup/pkg/fi/cloudup/openstack/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,37 @@ func InstanceInClusterAndIG(instance servers.Server, clusterName string, instanc
return true
}

func deletePorts(c OpenstackCloud, instanceGroupName string, clusterName string) error {
tags := []string{
fmt.Sprintf("%s=%s", TagClusterName, clusterName),
fmt.Sprintf("%s=%s", TagKopsInstanceGroup, instanceGroupName),
}

ports, err := c.ListPorts(ports.ListOpts{Tags: strings.Join(tags, ",")})
if err != nil {
return fmt.Errorf("could not list ports %v", err)
}

for _, port := range ports {
// previous approach was problematic:
// for example in case there is a group called "worker" and "worker-2", it will delete ports of "worker" as well,
// because there might be port names like:
// * "port-worker-2-<clusterName>"
// * "port-worker-20-<clusterName>"
klog.V(2).Infof("Delete port '%s' (%s)", port.Name, port.ID)
err := c.DeletePort(port.ID)

// TODO:
// really give up after trying to delete one port? other ports will be orphaned
// better to try all ports and collect errors?
if err != nil {
return fmt.Errorf("could not delete port %q: %v", port.ID, err)
}
}

return nil
}

func deleteGroup(c OpenstackCloud, g *cloudinstances.CloudInstanceGroup) error {
cluster := g.Raw.(*kops.Cluster)
allInstances, err := c.ListInstances(servers.ListOpts{
Expand All @@ -639,18 +670,10 @@ func deleteGroup(c OpenstackCloud, g *cloudinstances.CloudInstanceGroup) error {
return fmt.Errorf("could not delete instance %q: %v", instance.ID, err)
}
}
ports, err := c.ListPorts(ports.ListOpts{})
if err != nil {
return fmt.Errorf("could not list ports %v", err)
}

for _, port := range ports {
if strings.HasPrefix(port.Name, fmt.Sprintf("port-%s", g.InstanceGroup.Name)) && fi.ArrayContains(port.Tags, fmt.Sprintf("%s=%s", TagClusterName, cluster.Name)) {
err := c.DeletePort(port.ID)
if err != nil {
return fmt.Errorf("could not delete port %q: %v", port.ID, err)
}
}
err = deletePorts(c, g.InstanceGroup.Name, cluster.Name)
if err != nil {
return err
}

sgName := g.InstanceGroup.Name
Expand Down
Loading