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

Check cluster information when filtering instances #16112

Merged
merged 2 commits into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
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
28 changes: 24 additions & 4 deletions upup/pkg/fi/cloudup/openstack/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,27 +603,48 @@ func (c *openstackCloud) DeleteGroup(g *cloudinstances.CloudInstanceGroup) error
return deleteGroup(c, g)
}

// InstanceInClusterAndIG checks if instance is in current cluster and instancegroup
func InstanceInClusterAndIG(instance servers.Server, clusterName string, instanceGroupName string) bool {
value, ok := instance.Metadata[TagKopsInstanceGroup]
if !ok || value != instanceGroupName {
return false
}
cName, clusterok := instance.Metadata["k8s"]
if !clusterok || cName != clusterName {
return false
}
return true
}

func deleteGroup(c OpenstackCloud, g *cloudinstances.CloudInstanceGroup) error {
instances, err := c.ListInstances(servers.ListOpts{
cluster := g.Raw.(*kops.Cluster)
allInstances, err := c.ListInstances(servers.ListOpts{
Name: fmt.Sprintf("^%s", g.InstanceGroup.Name),
})
if err != nil {
return err
}

instances := []servers.Server{}
for _, instance := range allInstances {
if !InstanceInClusterAndIG(instance, cluster.Name, g.InstanceGroup.Name) {
continue
}
instances = append(instances, instance)
}
for _, instance := range instances {
err := c.DeleteInstanceWithID(instance.ID)
if err != nil {
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)) {
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)
Expand All @@ -640,7 +661,6 @@ func deleteGroup(c OpenstackCloud, g *cloudinstances.CloudInstanceGroup) error {
return fmt.Errorf("could not list server groups %v", err)
}

cluster := g.Raw.(*kops.Cluster)
for _, sg := range sgs {
if fmt.Sprintf("%s-%s", cluster.Name, sgName) == sg.Name {
if len(sg.Members) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions upup/pkg/fi/cloudup/openstack/server_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ func osBuildCloudInstanceGroup(c OpenstackCloud, cluster *kops.Cluster, ig *kops
return nil, err
}
for _, instance := range instances {
value, ok := instance.Metadata[TagKopsInstanceGroup]
if !ok || value != ig.Name {
if !InstanceInClusterAndIG(instance, cluster.Name, ig.Name) {
continue
}

igObservedGeneration := instance.Metadata[INSTANCE_GROUP_GENERATION]
clusterObservedGeneration := instance.Metadata[CLUSTER_GENERATION]
observedName := fmt.Sprintf("%s-%s", clusterObservedGeneration, igObservedGeneration)
Expand Down