github.com/openshift/installer@v1.4.17/pkg/types/ibmcloud/validation/machinepool.go (about)

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/IBM-Cloud/bluemix-go/crn"
     8  	"k8s.io/apimachinery/pkg/util/validation/field"
     9  
    10  	"github.com/openshift/installer/pkg/types/ibmcloud"
    11  )
    12  
    13  // ValidateMachinePool validates the MachinePool.
    14  func ValidateMachinePool(platform *ibmcloud.Platform, mp *ibmcloud.MachinePool, path *field.Path) field.ErrorList {
    15  	allErrs := field.ErrorList{}
    16  	for i, zone := range mp.Zones {
    17  		if !strings.HasPrefix(zone, platform.Region) {
    18  			allErrs = append(allErrs, field.Invalid(path.Child("zones").Index(i), zone, fmt.Sprintf("zone not in configured region (%s)", platform.Region)))
    19  		}
    20  	}
    21  
    22  	if mp.DedicatedHosts != nil {
    23  		allErrs = append(allErrs, validateDedicatedHosts(mp.DedicatedHosts, mp.InstanceType, mp.Zones, path.Child("dedicatedHosts"))...)
    24  
    25  		if mp.InstanceType == "" {
    26  			allErrs = append(allErrs, field.Invalid(path.Child("type"), mp.InstanceType, "type is required, default type not supported for dedicated hosts"))
    27  		}
    28  	}
    29  
    30  	if mp.BootVolume != nil {
    31  		allErrs = append(allErrs, validateBootVolume(mp.BootVolume, path.Child("bootVolume"))...)
    32  	}
    33  	return allErrs
    34  }
    35  
    36  func validateBootVolume(bv *ibmcloud.BootVolume, path *field.Path) field.ErrorList {
    37  	allErrs := field.ErrorList{}
    38  	if bv.EncryptionKey != "" {
    39  		_, parseErr := crn.Parse(bv.EncryptionKey)
    40  		if parseErr != nil {
    41  			allErrs = append(allErrs, field.Invalid(path.Child("encryptionKey"), bv.EncryptionKey, "encryptionKey is not a valid IBM CRN"))
    42  		}
    43  	}
    44  	return allErrs
    45  }
    46  
    47  func validateDedicatedHosts(dhosts []ibmcloud.DedicatedHost, itype string, zones []string, path *field.Path) field.ErrorList {
    48  	allErrs := field.ErrorList{}
    49  
    50  	// Length of dedicated hosts must match platform zones
    51  	if len(dhosts) != len(zones) {
    52  		allErrs = append(allErrs, field.Invalid(path, dhosts, fmt.Sprintf("number of dedicated hosts does not match list of zones (%s)", zones)))
    53  	}
    54  
    55  	for i, dhost := range dhosts {
    56  		// Dedicated host name or profile is required
    57  		if dhost.Name == "" && dhost.Profile == "" {
    58  			allErrs = append(allErrs, field.Invalid(path.Index(i), dhost.Profile, "name or profile must be set"))
    59  		}
    60  
    61  		// Instance type must be in the same profile family as dedicated host
    62  		if dhost.Profile != "" && itype != "" {
    63  			if strings.Split(dhost.Profile, "-")[0] != strings.Split(itype, "-")[0] {
    64  				allErrs = append(allErrs, field.Invalid(path.Index(i).Child("profile"), dhost.Profile, fmt.Sprintf("profile does not support expected instance type (%s)", itype)))
    65  			}
    66  		}
    67  	}
    68  
    69  	return allErrs
    70  }