github.com/openshift/installer@v1.4.17/pkg/destroy/openstack/glance.go (about)

     1  package openstack
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"time"
     7  
     8  	"github.com/gophercloud/gophercloud/v2/openstack/image/v2/images"
     9  	"github.com/sirupsen/logrus"
    10  	"k8s.io/apimachinery/pkg/util/wait"
    11  
    12  	openstackdefaults "github.com/openshift/installer/pkg/types/openstack/defaults"
    13  )
    14  
    15  // DeleteGlanceImage deletes the image with the specified name
    16  func DeleteGlanceImage(ctx context.Context, name string, cloud string) error {
    17  	backoffSettings := wait.Backoff{
    18  		Duration: time.Second * 20,
    19  		Steps:    30,
    20  	}
    21  
    22  	err := wait.ExponentialBackoff(backoffSettings, func() (bool, error) {
    23  		return deleteGlanceImage(ctx, name, cloud)
    24  	})
    25  	if err != nil {
    26  		return fmt.Errorf("unrecoverable error/timed out: %w", err)
    27  	}
    28  
    29  	return nil
    30  }
    31  
    32  func deleteGlanceImage(ctx context.Context, name string, cloud string) (bool, error) {
    33  	conn, err := openstackdefaults.NewServiceClient(ctx, "image", openstackdefaults.DefaultClientOpts(cloud))
    34  	if err != nil {
    35  		logrus.Warningf("There was an error during the image removal: %v", err)
    36  		return false, nil
    37  	}
    38  
    39  	listOpts := images.ListOpts{
    40  		Name: name,
    41  	}
    42  
    43  	allPages, err := images.List(conn, listOpts).AllPages(ctx)
    44  	if err != nil {
    45  		logrus.Warningf("There was an error during the image removal: %v", err)
    46  		return false, nil
    47  	}
    48  
    49  	allImages, err := images.ExtractImages(allPages)
    50  	if err != nil {
    51  		logrus.Warningf("There was an error during the image removal: %v", err)
    52  		return false, nil
    53  	}
    54  
    55  	for _, image := range allImages {
    56  		err := images.Delete(ctx, conn, image.ID).ExtractErr()
    57  		if err != nil {
    58  			logrus.Warningf("There was an error during the image removal: %v", err)
    59  			return false, nil
    60  		}
    61  	}
    62  	return true, nil
    63  }