Skip to content

Commit

Permalink
Delete the glance image with RHCOS when the cluster is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedosin committed Oct 14, 2019
1 parent 8c78182 commit ff961c1
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions pkg/destroy/openstack/openstack.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumes"
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
"github.com/gophercloud/gophercloud/openstack/imageservice/v2/images"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/apiversions"
"github.com/gophercloud/gophercloud/openstack/loadbalancer/v2/loadbalancers"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/layer3/floatingips"
Expand Down Expand Up @@ -133,6 +134,7 @@ func populateDeleteFuncs(funcs map[string]deleteFunc) {
funcs["deleteContainers"] = deleteContainers
funcs["deleteVolumes"] = deleteVolumes
funcs["deleteFloatingIPs"] = deleteFloatingIPs
funcs["deleteImages"] = deleteImages
}

// filterObjects will do client-side filtering given an appropriately filled out
Expand Down Expand Up @@ -876,3 +878,41 @@ func deleteFloatingIPs(opts *clientconfig.ClientOpts, filter Filter, logger logr
}
return len(allFloatingIPs) == 0, nil
}

func deleteImages(opts *clientconfig.ClientOpts, filter Filter, logger logrus.FieldLogger) (bool, error) {
logger.Debug("Deleting openstack base image")
defer logger.Debugf("Exiting deleting openstack base image")

conn, err := clientconfig.NewServiceClient("image", opts)
if err != nil {
logger.Fatalf("%v", err)
os.Exit(1)
}

listOpts := images.ListOpts{
Tags: filterTags(filter),
}

allPages, err := images.List(conn, listOpts).AllPages()
if err != nil {
logger.Fatalf("%v", err)
os.Exit(1)
}

allImages, err := images.ExtractImages(allPages)
if err != nil {
logger.Fatalf("%v", err)
os.Exit(1)
}

for _, image := range allImages {
logger.Debugf("Deleting image: %+v", image.ID)
err := images.Delete(conn, image.ID).ExtractErr()
if err != nil {
// This can fail if the image is still in use by other VMs
logger.Debugf("Deleting Image failed: %v", err)
return false, nil
}
}
return true, nil
}

0 comments on commit ff961c1

Please sign in to comment.