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

     1  package nutanix
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"strconv"
     7  	"time"
     8  
     9  	"k8s.io/apimachinery/pkg/util/validation/field"
    10  
    11  	"github.com/openshift/installer/pkg/types"
    12  	nutanixtypes "github.com/openshift/installer/pkg/types/nutanix"
    13  )
    14  
    15  // Validate executes platform-specific validation.
    16  func Validate(ic *types.InstallConfig) error {
    17  	if ic.Platform.Nutanix == nil {
    18  		return field.Required(field.NewPath("platform", "nutanix"), "nutanix validation requires a nutanix platform configuration")
    19  	}
    20  
    21  	return nil
    22  }
    23  
    24  // ValidateForProvisioning performs platform validation specifically for installer-
    25  // provisioned infrastructure. In this case, self-hosted networking is a requirement
    26  // when the installer creates infrastructure for nutanix clusters.
    27  func ValidateForProvisioning(ic *types.InstallConfig) error {
    28  	errList := field.ErrorList{}
    29  	parentPath := field.NewPath("platform", "nutanix")
    30  
    31  	if ic.Platform.Nutanix == nil {
    32  		return field.Required(parentPath, "nutanix validation requires a nutanix platform configuration")
    33  	}
    34  
    35  	ctx, cancel := context.WithTimeout(context.TODO(), 60*time.Second)
    36  	defer cancel()
    37  	p := ic.Platform.Nutanix
    38  	nc, err := nutanixtypes.CreateNutanixClient(ctx,
    39  		p.PrismCentral.Endpoint.Address,
    40  		strconv.Itoa(int(p.PrismCentral.Endpoint.Port)),
    41  		p.PrismCentral.Username,
    42  		p.PrismCentral.Password)
    43  	if err != nil {
    44  		return field.Invalid(parentPath.Child("prismCentral"), p.PrismCentral.Endpoint.Address, fmt.Sprintf("failed to connect to the prism-central with the configured credentials: %v", err))
    45  	}
    46  
    47  	// validate whether a prism element with the UUID actually exists
    48  	for _, pe := range p.PrismElements {
    49  		_, err = nc.V3.GetCluster(ctx, pe.UUID)
    50  		if err != nil {
    51  			errList = append(errList, field.Invalid(parentPath.Child("prismElements"), pe.UUID, fmt.Sprintf("the prism element %s's UUID does not correspond to a valid prism element in Prism: %v", pe.Name, err)))
    52  		}
    53  	}
    54  
    55  	// validate whether a subnet with the UUID actually exists
    56  	for _, subnetUUID := range p.SubnetUUIDs {
    57  		_, err = nc.V3.GetSubnet(ctx, subnetUUID)
    58  		if err != nil {
    59  			errList = append(errList, field.Invalid(parentPath.Child("subnetUUIDs"), subnetUUID, fmt.Sprintf("the subnet UUID does not correspond to a valid subnet in Prism: %v", err)))
    60  		}
    61  	}
    62  
    63  	// validate each FailureDomain configuration
    64  	for _, fd := range p.FailureDomains {
    65  		// validate whether the prism element with the UUID exists
    66  		_, err = nc.V3.GetCluster(ctx, fd.PrismElement.UUID)
    67  		if err != nil {
    68  			errList = append(errList, field.Invalid(parentPath.Child("failureDomains", "prismElements"), fd.PrismElement.UUID,
    69  				fmt.Sprintf("the failure domain %s configured prism element UUID does not correspond to a valid prism element in Prism: %v", fd.Name, err)))
    70  		}
    71  
    72  		// validate whether a subnet with the UUID actually exists
    73  		for _, subnetUUID := range fd.SubnetUUIDs {
    74  			_, err = nc.V3.GetSubnet(ctx, subnetUUID)
    75  			if err != nil {
    76  				errList = append(errList, field.Invalid(parentPath.Child("failureDomains", "subnetUUIDs"), subnetUUID,
    77  					fmt.Sprintf("the failure domain %s configured subnet UUID does not correspond to a valid subnet in Prism: %v", fd.Name, err)))
    78  			}
    79  		}
    80  
    81  		// validate each configured dataSource image exists
    82  		for _, dsImgRef := range fd.DataSourceImages {
    83  			switch {
    84  			case dsImgRef.UUID != "":
    85  				if _, err = nc.V3.GetImage(ctx, dsImgRef.UUID); err != nil {
    86  					errMsg := fmt.Sprintf("failureDomain %q: failed to find the dataSource image with uuid %s: %v", fd.Name, dsImgRef.UUID, err)
    87  					errList = append(errList, field.Invalid(parentPath.Child("failureDomains", "dataSourceImage", "uuid"), dsImgRef.UUID, errMsg))
    88  				}
    89  			case dsImgRef.Name != "":
    90  				if dsImgUUID, err := nutanixtypes.FindImageUUIDByName(ctx, nc, dsImgRef.Name); err != nil {
    91  					errMsg := fmt.Sprintf("failureDomain %q: failed to find the dataSource image with name %q: %v", fd.Name, dsImgRef.UUID, err)
    92  					errList = append(errList, field.Invalid(parentPath.Child("failureDomains", "dataSourceImage", "name"), dsImgRef.Name, errMsg))
    93  				} else {
    94  					dsImgRef.UUID = *dsImgUUID
    95  				}
    96  			default:
    97  				errList = append(errList, field.Required(parentPath.Child("failureDomains", "dataSourceImage"), "both the dataSourceImage's uuid and name are empty, you need to configure one."))
    98  			}
    99  		}
   100  	}
   101  
   102  	return errList.ToAggregate()
   103  }