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

     1  // Package bootstrap uses Terraform to remove bootstrap resources.
     2  package bootstrap
     3  
     4  import (
     5  	"context"
     6  	"fmt"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"github.com/pkg/errors"
    11  	"github.com/sirupsen/logrus"
    12  
    13  	"github.com/openshift/api/features"
    14  	"github.com/openshift/installer/pkg/asset/cluster/metadata"
    15  	osp "github.com/openshift/installer/pkg/destroy/openstack"
    16  	"github.com/openshift/installer/pkg/infrastructure/openstack/preprovision"
    17  	infra "github.com/openshift/installer/pkg/infrastructure/platform"
    18  	ibmcloudtfvars "github.com/openshift/installer/pkg/tfvars/ibmcloud"
    19  	"github.com/openshift/installer/pkg/types"
    20  	typesazure "github.com/openshift/installer/pkg/types/azure"
    21  	"github.com/openshift/installer/pkg/types/featuregates"
    22  	ibmcloudtypes "github.com/openshift/installer/pkg/types/ibmcloud"
    23  	"github.com/openshift/installer/pkg/types/openstack"
    24  )
    25  
    26  // Destroy uses Terraform to remove bootstrap resources.
    27  func Destroy(ctx context.Context, dir string) (err error) {
    28  	metadata, err := metadata.Load(dir)
    29  	if err != nil {
    30  		return err
    31  	}
    32  
    33  	platform := metadata.Platform()
    34  	if platform == "" {
    35  		return errors.New("no platform configured in metadata")
    36  	}
    37  
    38  	if platform == openstack.Name {
    39  		if err := preprovision.SetTerraformEnvironment(); err != nil {
    40  			return errors.Wrapf(err, "Failed to  initialize infrastructure")
    41  		}
    42  
    43  		imageName := metadata.InfraID + "-ignition"
    44  		if err := osp.DeleteGlanceImage(ctx, imageName, metadata.OpenStack.Cloud); err != nil {
    45  			return errors.Wrapf(err, "Failed to delete glance image %s", imageName)
    46  		}
    47  	}
    48  
    49  	// Azure Stack uses the Azure platform but has its own Terraform configuration.
    50  	if platform == typesazure.Name && metadata.Azure.CloudName == typesazure.StackCloud {
    51  		platform = typesazure.StackTerraformName
    52  	}
    53  
    54  	// IBM Cloud allows override of service endpoints, which would be required during bootstrap destroy.
    55  	// Create a JSON file with overrides, if these endpoints are present
    56  	if platform == ibmcloudtypes.Name && metadata.IBMCloud != nil && len(metadata.IBMCloud.ServiceEndpoints) > 0 {
    57  		// Build the JSON containing the endpoint overrides for IBM Cloud Services.
    58  		jsonData, err := ibmcloudtfvars.CreateEndpointJSON(metadata.IBMCloud.ServiceEndpoints, metadata.IBMCloud.Region)
    59  		if err != nil {
    60  			return fmt.Errorf("failed generating endpoint override JSON data for bootstrap destroy: %w", err)
    61  		}
    62  
    63  		// If JSON data was generated, create the JSON file for IBM Cloud Terraform provider to use during destroy.
    64  		if jsonData != nil {
    65  			endpointsFilePath := filepath.Join(dir, ibmcloudtfvars.IBMCloudEndpointJSONFileName)
    66  			if err := os.WriteFile(endpointsFilePath, jsonData, 0o600); err != nil {
    67  				return fmt.Errorf("failed to write IBM Cloud service endpoint override JSON file for bootstrap destroy: %w", err)
    68  			}
    69  			logrus.Debugf("generated ibm endpoint overrides file: %s", endpointsFilePath)
    70  		}
    71  	}
    72  
    73  	// Get cluster profile for new FeatureGate access.  Blank is no longer an option, so default to
    74  	// SelfManaged.
    75  	clusterProfile := types.GetClusterProfileName()
    76  	featureSets, ok := features.AllFeatureSets()[clusterProfile]
    77  	if !ok {
    78  		return fmt.Errorf("no feature sets for cluster profile %q", clusterProfile)
    79  	}
    80  	fg := featuregates.FeatureGateFromFeatureSets(featureSets, metadata.FeatureSet, metadata.CustomFeatureSet)
    81  
    82  	provider, err := infra.ProviderForPlatform(platform, fg)
    83  	if err != nil {
    84  		return fmt.Errorf("error getting infrastructure provider: %w", err)
    85  	}
    86  
    87  	if err := provider.DestroyBootstrap(ctx, dir); err != nil {
    88  		return fmt.Errorf("error destroying bootstrap resources %w", err)
    89  	}
    90  
    91  	return nil
    92  }