github.com/openshift/installer@v1.4.17/pkg/destroy/ibmcloud/loadbalancer.go (about)

     1  package ibmcloud
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  
     7  	"github.com/IBM/vpc-go-sdk/vpcv1"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  const loadBalancerTypeName = "load balancer"
    12  
    13  // listLoadBalancers lists subnets in the vpc
    14  func (o *ClusterUninstaller) listLoadBalancers() (cloudResources, error) {
    15  	o.Logger.Debugf("Listing load balancers")
    16  	ctx, cancel := o.contextWithTimeout()
    17  	defer cancel()
    18  
    19  	options := o.vpcSvc.NewListLoadBalancersOptions()
    20  	resources, _, err := o.vpcSvc.ListLoadBalancersWithContext(ctx, options)
    21  	if err != nil {
    22  		return nil, errors.Wrapf(err, "failed to list load balancers")
    23  	}
    24  
    25  	result := []cloudResource{}
    26  	for _, loadbalancer := range resources.LoadBalancers {
    27  		if strings.Contains(*loadbalancer.Name, o.InfraID) {
    28  			result = append(result, cloudResource{
    29  				key:      *loadbalancer.ID,
    30  				name:     *loadbalancer.Name,
    31  				status:   *loadbalancer.ProvisioningStatus,
    32  				typeName: loadBalancerTypeName,
    33  				id:       *loadbalancer.ID,
    34  			})
    35  		}
    36  	}
    37  
    38  	return cloudResources{}.insert(result...), nil
    39  }
    40  
    41  func (o *ClusterUninstaller) deleteLoadBalancer(item cloudResource) error {
    42  	if item.status == vpcv1.LoadBalancerProvisioningStatusDeletePendingConst {
    43  		o.Logger.Debugf("Waiting for load balancer %q to delete", item.name)
    44  		return nil
    45  	}
    46  
    47  	o.Logger.Debugf("Deleting load balancer %q", item.name)
    48  	ctx, cancel := o.contextWithTimeout()
    49  	defer cancel()
    50  
    51  	options := o.vpcSvc.NewDeleteLoadBalancerOptions(item.id)
    52  	details, err := o.vpcSvc.DeleteLoadBalancerWithContext(ctx, options)
    53  
    54  	if err != nil && details != nil && details.StatusCode == http.StatusNotFound {
    55  		// The resource is gone.
    56  		o.deletePendingItems(item.typeName, []cloudResource{item})
    57  		o.Logger.Infof("Deleted load balancer %q", item.name)
    58  		return nil
    59  	}
    60  
    61  	if err != nil && details != nil && details.StatusCode != http.StatusNotFound {
    62  		return errors.Wrapf(err, "Failed to delete load balancer %s", item.name)
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  // destroyLoadBalancers removes all load balancer resources that have a name prefixed
    69  // with the cluster's infra ID.
    70  func (o *ClusterUninstaller) destroyLoadBalancers() error {
    71  	found, err := o.listLoadBalancers()
    72  	if err != nil {
    73  		return err
    74  	}
    75  
    76  	items := o.insertPendingItems(loadBalancerTypeName, found.list())
    77  	for _, item := range items {
    78  		if _, ok := found[item.key]; !ok {
    79  			// This item has finished deletion.
    80  			o.deletePendingItems(item.typeName, []cloudResource{item})
    81  			o.Logger.Infof("Deleted load balancer %q", item.name)
    82  			continue
    83  		}
    84  		err := o.deleteLoadBalancer(item)
    85  		if err != nil {
    86  			o.errorTracker.suppressWarning(item.key, err, o.Logger)
    87  		}
    88  	}
    89  
    90  	if items = o.getPendingItems(loadBalancerTypeName); len(items) > 0 {
    91  		return errors.Errorf("%d items pending", len(items))
    92  	}
    93  	return nil
    94  }