github.com/openshift/installer@v1.4.17/pkg/infrastructure/vsphere/clusterapi/clusterapi.go (about)

     1  package clusterapi
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path"
     7  	"strings"
     8  
     9  	"github.com/vmware/govmomi/object"
    10  	"sigs.k8s.io/cluster-api-provider-vsphere/apis/v1beta1"
    11  	"sigs.k8s.io/cluster-api-provider-vsphere/pkg/session"
    12  
    13  	"github.com/openshift/installer/pkg/asset/installconfig"
    14  	"github.com/openshift/installer/pkg/infrastructure/clusterapi"
    15  	"github.com/openshift/installer/pkg/rhcos/cache"
    16  	"github.com/openshift/installer/pkg/types/vsphere"
    17  )
    18  
    19  // Provider is the vSphere implementation of the clusterapi InfraProvider.
    20  type Provider struct {
    21  	clusterapi.InfraProvider
    22  }
    23  
    24  var _ clusterapi.PreProvider = Provider{}
    25  
    26  // Name returns the vsphere provider name.
    27  func (p Provider) Name() string {
    28  	return vsphere.Name
    29  }
    30  
    31  // PublicGatherEndpoint indicates that machine ready checks should NOT wait for an ExternalIP
    32  // in the status when declaring machines ready.
    33  func (Provider) PublicGatherEndpoint() clusterapi.GatherEndpoint { return clusterapi.InternalIP }
    34  
    35  func initializeFoldersAndTemplates(ctx context.Context, cachedImage string, failureDomain vsphere.FailureDomain, session *session.Session, diskType vsphere.DiskType, clusterID, tagID string) error {
    36  	finder := session.Finder
    37  
    38  	dc, err := finder.Datacenter(ctx, failureDomain.Topology.Datacenter)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	dcFolders, err := dc.Folders(ctx)
    43  	if err != nil {
    44  		return fmt.Errorf("unable to get datacenter folder: %w", err)
    45  	}
    46  
    47  	folderPath := path.Join(dcFolders.VmFolder.InventoryPath, clusterID)
    48  
    49  	// we must set the Folder to the infraId somewhere, we will need to remove that.
    50  	// if we are overwriting folderPath it needs to have a slash (path)
    51  	folder := failureDomain.Topology.Folder
    52  	if strings.Contains(folder, "/") {
    53  		folderPath = folder
    54  	}
    55  
    56  	var folderObj *object.Folder
    57  
    58  	// Only createFolder() and attach the tag if the folder does not exist prior to installing
    59  	if folderObj, err = folderExists(ctx, folderPath, session); folderObj == nil && err == nil {
    60  		folderObj, err = createFolder(ctx, folderPath, session)
    61  		if err != nil {
    62  			return fmt.Errorf("unable to create folder: %w", err)
    63  		}
    64  		// attach tag to folder
    65  		err = session.TagManager.AttachTag(ctx, tagID, folderObj.Reference())
    66  		if err != nil {
    67  			return fmt.Errorf("unable to attach tag to folder: %w", err)
    68  		}
    69  	} else if err != nil {
    70  		return fmt.Errorf("unable to get folder: %w", err)
    71  	}
    72  
    73  	// if the template is empty, the ova must be imported
    74  	if len(failureDomain.Topology.Template) == 0 {
    75  		if err = importRhcosOva(ctx, session, folderObj,
    76  			cachedImage, clusterID, tagID, string(diskType), failureDomain); err != nil {
    77  			return fmt.Errorf("failed to import ova: %w", err)
    78  		}
    79  	}
    80  	return nil
    81  }
    82  
    83  // PreProvision creates the vCenter objects required prior to running capv.
    84  func (p Provider) PreProvision(ctx context.Context, in clusterapi.PreProvisionInput) error {
    85  	/*
    86  	 * one locally cached image
    87  	 * one tag and tag category per vcenter
    88  	 * one folder per datacenter
    89  	 * one template per region/zone aka failuredomain
    90  	 */
    91  	installConfig := in.InstallConfig
    92  	clusterID := &installconfig.ClusterID{InfraID: in.InfraID}
    93  	var tagID string
    94  
    95  	cachedImage, err := cache.DownloadImageFile(in.RhcosImage.ControlPlane, cache.InstallerApplicationName)
    96  	if err != nil {
    97  		return fmt.Errorf("failed to use cached vsphere image: %w", err)
    98  	}
    99  
   100  	for _, vcenter := range installConfig.Config.VSphere.VCenters {
   101  		server := vcenter.Server
   102  		vctrSession, err := installConfig.VSphere.Session(context.TODO(), server)
   103  
   104  		if err != nil {
   105  			return err
   106  		}
   107  
   108  		tagID, err = createClusterTagID(ctx, vctrSession, clusterID.InfraID)
   109  		if err != nil {
   110  			return err
   111  		}
   112  
   113  		for i := range in.MachineManifests {
   114  			if vm, ok := in.MachineManifests[i].(*v1beta1.VSphereMachine); ok {
   115  				if vm.Spec.Server == server {
   116  					vm.Spec.TagIDs = append(vm.Spec.TagIDs, tagID)
   117  				}
   118  			}
   119  		}
   120  
   121  		for _, failureDomain := range installConfig.Config.VSphere.FailureDomains {
   122  			if failureDomain.Server != server {
   123  				continue
   124  			}
   125  
   126  			if err = initializeFoldersAndTemplates(ctx, cachedImage, failureDomain, vctrSession, installConfig.Config.VSphere.DiskType, clusterID.InfraID, tagID); err != nil {
   127  				return fmt.Errorf("unable to initialize folders and templates: %w", err)
   128  			}
   129  		}
   130  	}
   131  
   132  	return nil
   133  }