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

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"k8s.io/apimachinery/pkg/util/validation/field"
     7  	"k8s.io/utils/strings/slices"
     8  
     9  	"github.com/openshift/installer/pkg/types"
    10  	"github.com/openshift/installer/pkg/types/vsphere"
    11  	"github.com/openshift/installer/pkg/validate"
    12  )
    13  
    14  const (
    15  	defaultCoresPerSocket = int32(4)
    16  	defaultNumCPUs        = int32(4)
    17  )
    18  
    19  // ValidateMachinePool checks that the specified machine pool is valid.
    20  func ValidateMachinePool(platform *vsphere.Platform, machinePool *types.MachinePool, fldPath *field.Path) field.ErrorList {
    21  	vspherePool := machinePool.Platform.VSphere
    22  	allErrs := field.ErrorList{}
    23  	if vspherePool.DiskSizeGB < 0 {
    24  		allErrs = append(allErrs, field.Invalid(fldPath.Child("diskSizeGB"), vspherePool.DiskSizeGB, "storage disk size must be positive"))
    25  	}
    26  	if vspherePool.MemoryMiB < 0 {
    27  		allErrs = append(allErrs, field.Invalid(fldPath.Child("memoryMB"), vspherePool.MemoryMiB, "memory size must be positive"))
    28  	}
    29  	numCPUs := vspherePool.NumCPUs
    30  	if numCPUs < 0 {
    31  		allErrs = append(allErrs, field.Invalid(fldPath.Child("cpus"), numCPUs, "number of CPUs must be positive"))
    32  	}
    33  	numCoresPerSocket := vspherePool.NumCoresPerSocket
    34  	if numCoresPerSocket < 0 {
    35  		allErrs = append(allErrs, field.Invalid(fldPath.Child("coresPerSocket"), numCoresPerSocket, "cores per socket must be positive"))
    36  	}
    37  
    38  	// Either the number set by the user or a default value
    39  	if numCPUs <= 0 {
    40  		numCPUs = defaultNumCPUs
    41  	}
    42  	if numCoresPerSocket <= 0 {
    43  		numCoresPerSocket = defaultCoresPerSocket
    44  	}
    45  
    46  	if numCoresPerSocket > numCPUs {
    47  		errorMsg := fmt.Sprintf("cores per socket must be less than the number of CPUs (which is by default %d)", defaultNumCPUs)
    48  		allErrs = append(allErrs, field.Invalid(fldPath.Child("coresPerSocket"), numCoresPerSocket, errorMsg))
    49  	} else if numCPUs%numCoresPerSocket != 0 {
    50  		errMsg := fmt.Sprintf("numCPUs specified should be a multiple of cores per socket (which is by default %d)", defaultCoresPerSocket)
    51  		allErrs = append(allErrs, field.Invalid(fldPath.Child("cpus"), numCPUs, errMsg))
    52  	}
    53  
    54  	if len(vspherePool.Zones) > 0 {
    55  		var zoneRefs []string
    56  		if len(platform.FailureDomains) == 0 {
    57  			return append(allErrs, field.Required(fldPath.Child("zones"), "failureDomains must be defined if zones are defined"))
    58  		}
    59  		for index, zone := range vspherePool.Zones {
    60  			err := validate.ClusterName1035(zone)
    61  			if err != nil {
    62  				allErrs = append(allErrs, field.Invalid(fldPath.Child("zones"), vspherePool.Zones, err.Error()))
    63  			}
    64  
    65  			// This checks to make sure ref is not duplicated
    66  			if slices.Contains(zoneRefs, zone) {
    67  				allErrs = append(allErrs, field.Duplicate(fldPath.Child("zones").Index(index), zone))
    68  			} else {
    69  				zoneRefs = append(zoneRefs, zone)
    70  			}
    71  
    72  			// Verify zone references a valid failure domain
    73  			zoneDefined := false
    74  			for _, failureDomain := range platform.FailureDomains {
    75  				if failureDomain.Name == zone {
    76  					zoneDefined = true
    77  				}
    78  			}
    79  			if !zoneDefined {
    80  				allErrs = append(allErrs, field.Invalid(fldPath.Child("zones"), zone, "zone not defined in failureDomains"))
    81  			}
    82  		}
    83  	} else if len(platform.FailureDomains) > 0 {
    84  		for _, failureDomain := range platform.FailureDomains {
    85  			vspherePool.Zones = append(vspherePool.Zones, failureDomain.Name)
    86  		}
    87  	}
    88  	return allErrs
    89  }