github.com/openshift/installer@v1.4.17/pkg/asset/manifests/nutanix/infrastructure.go (about)

     1  package nutanix
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  	"time"
     8  
     9  	"k8s.io/utils/ptr"
    10  
    11  	configv1 "github.com/openshift/api/config/v1"
    12  	"github.com/openshift/installer/pkg/asset/installconfig"
    13  	"github.com/openshift/installer/pkg/types/nutanix"
    14  )
    15  
    16  // GetInfrastructureNutanixPlatformSpec constructs NutanixPlatformSpec for the infrastructure spec.
    17  func GetInfrastructureNutanixPlatformSpec(ic *installconfig.InstallConfig) (*configv1.NutanixPlatformSpec, error) {
    18  	nutanixPlatform := ic.Config.Nutanix
    19  
    20  	// Retrieve the prism element name
    21  	ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
    22  	defer cancel()
    23  
    24  	var peName string
    25  	if len(nutanixPlatform.PrismElements[0].Name) == 0 {
    26  		nc, err := nutanix.CreateNutanixClient(ctx,
    27  			nutanixPlatform.PrismCentral.Endpoint.Address,
    28  			strconv.Itoa(int(nutanixPlatform.PrismCentral.Endpoint.Port)),
    29  			nutanixPlatform.PrismCentral.Username,
    30  			nutanixPlatform.PrismCentral.Password)
    31  		if err != nil {
    32  			return nil, fmt.Errorf("unable to connect to Prism Central %s: %w", nutanixPlatform.PrismCentral.Endpoint.Address, err)
    33  		}
    34  		pe, err := nc.V3.GetCluster(ctx, nutanixPlatform.PrismElements[0].UUID)
    35  		if err != nil {
    36  			return nil, fmt.Errorf("fail to find the Prism Element (cluster) with uuid %s: %w", nutanixPlatform.PrismElements[0].UUID, err)
    37  		}
    38  		peName = *pe.Spec.Name
    39  	} else {
    40  		peName = nutanixPlatform.PrismElements[0].Name
    41  	}
    42  
    43  	platformSpec := &configv1.NutanixPlatformSpec{
    44  		PrismCentral: configv1.NutanixPrismEndpoint{
    45  			Address: nutanixPlatform.PrismCentral.Endpoint.Address,
    46  			Port:    nutanixPlatform.PrismCentral.Endpoint.Port,
    47  		},
    48  		PrismElements: []configv1.NutanixPrismElementEndpoint{{
    49  			Name: peName,
    50  			Endpoint: configv1.NutanixPrismEndpoint{
    51  				Address: nutanixPlatform.PrismElements[0].Endpoint.Address,
    52  				Port:    nutanixPlatform.PrismElements[0].Endpoint.Port,
    53  			},
    54  		}},
    55  	}
    56  
    57  	// failure domains configuration
    58  	failureDomains := make([]configv1.NutanixFailureDomain, 0, len(nutanixPlatform.FailureDomains))
    59  	for _, fd := range nutanixPlatform.FailureDomains {
    60  		subnets := make([]configv1.NutanixResourceIdentifier, 0, len(fd.SubnetUUIDs))
    61  		for _, subnetUUID := range fd.SubnetUUIDs {
    62  			subnets = append(subnets, configv1.NutanixResourceIdentifier{
    63  				Type: configv1.NutanixIdentifierUUID,
    64  				UUID: ptr.To[string](subnetUUID),
    65  			})
    66  		}
    67  
    68  		failureDomain := configv1.NutanixFailureDomain{
    69  			Name: fd.Name,
    70  			Cluster: configv1.NutanixResourceIdentifier{
    71  				Type: configv1.NutanixIdentifierUUID,
    72  				UUID: ptr.To[string](fd.PrismElement.UUID),
    73  			},
    74  			Subnets: subnets,
    75  		}
    76  		failureDomains = append(failureDomains, failureDomain)
    77  	}
    78  	platformSpec.FailureDomains = failureDomains
    79  
    80  	return platformSpec, nil
    81  }