github.com/openshift/installer@v1.4.17/pkg/types/ovirt/validation/machinepool.go (about) 1 package validation 2 3 import ( 4 "fmt" 5 6 "k8s.io/apimachinery/pkg/util/validation/field" 7 8 "github.com/openshift/installer/pkg/types/ovirt" 9 "github.com/openshift/installer/pkg/validate" 10 ) 11 12 // ValidateMachinePool checks that the specified machine pool is valid. 13 func ValidateMachinePool(p *ovirt.MachinePool, fldPath *field.Path) field.ErrorList { 14 allErrs := field.ErrorList{} 15 16 if p.CPU != nil { 17 if p.CPU.Cores <= 0 { 18 allErrs = append(allErrs, field.Invalid(fldPath.Child("cores"), p.CPU.Cores, "CPU cores must be positive")) 19 } 20 if p.CPU.Sockets <= 0 { 21 allErrs = append(allErrs, field.Invalid(fldPath.Child("sockets"), p.CPU.Sockets, "CPU sockets must be positive")) 22 } 23 if p.CPU.Threads <= 0 { 24 allErrs = append(allErrs, field.Invalid(fldPath.Child("threads"), p.CPU.Threads, "CPU threads must be positive")) 25 } 26 } 27 28 if p.MemoryMB < 0 { 29 allErrs = append(allErrs, field.Invalid(fldPath.Child("memoryMB"), p.MemoryMB, "Memory value must be nonnegative")) 30 } 31 32 if p.VMType != "" && !ValidVMType(p.VMType) { 33 allErrs = append(allErrs, field.Invalid(fldPath.Child("vmType"), p.VMType, fmt.Sprintf("VM type must be one of %s", supportedVMTypes()))) 34 } 35 36 if p.InstanceTypeID != "" { 37 if p.CPU != nil { 38 allErrs = append(allErrs, field.Invalid(fldPath.Child("instanceTypeID"), p.InstanceTypeID, "mixing instanceTypeID and CPU is not supported")) 39 } 40 if p.MemoryMB > 0 { 41 allErrs = append(allErrs, field.Invalid(fldPath.Child("instanceTypeID"), p.InstanceTypeID, "mixing instanceTypeID and Memory is not supported")) 42 } 43 if err := validate.UUID(p.InstanceTypeID); err != nil { 44 allErrs = append(allErrs, field.Invalid(fldPath.Child("instanceTypeID"), p.InstanceTypeID, err.Error())) 45 } 46 } 47 48 if p.OSDisk != nil { 49 if p.OSDisk.SizeGB <= 0 { 50 allErrs = append(allErrs, field.Invalid(fldPath.Child("sizeGB"), p.OSDisk.SizeGB, "disk size must be positive")) 51 } 52 } 53 54 if p.AutoPinningPolicy != "" && !ValidAutoPinningPolicy(p.AutoPinningPolicy) { 55 allErrs = append(allErrs, field.NotSupported(fldPath.Child("autoPinningPolicy"), p.AutoPinningPolicy, 56 []string{string(ovirt.AutoPinningNone), string(ovirt.AutoPinningResizeAndPin)})) 57 } 58 59 if p.Hugepages > 0 { 60 if p.Hugepages != 2048 && p.Hugepages != 1048576 { 61 allErrs = append(allErrs, field.NotSupported(fldPath.Child("hugepages"), p.Hugepages, 62 []string{ovirt.Hugepages2MB.String(), ovirt.Hugepages1GB.String()})) 63 } 64 } 65 66 switch p.Format { 67 case "": 68 case "raw": 69 case "cow": 70 default: 71 allErrs = append(allErrs, field.NotSupported( 72 fldPath.Child("format"), 73 p.Format, 74 []string{"", "raw", "cow"}, 75 )) 76 } 77 78 return allErrs 79 } 80 81 // ValidVMType returns true if the vmType is supported. 82 func ValidVMType(vmType ovirt.VMType) bool { 83 for _, v := range supportedVMTypes() { 84 if vmType == v { 85 return true 86 } 87 } 88 return false 89 } 90 91 // supportedVMTypes returns a slice of all supported VMTypes. 92 func supportedVMTypes() []ovirt.VMType { 93 return []ovirt.VMType{ 94 ovirt.VMTypeDesktop, 95 ovirt.VMTypeServer, 96 ovirt.VMTypeHighPerformance, 97 } 98 } 99 100 // ValidAutoPinningPolicy returns true if the AutoPinningPolicy is supported. 101 func ValidAutoPinningPolicy(autoPinningPolicy ovirt.AutoPinningPolicy) bool { 102 for _, v := range supportedAutoPinningPolicies() { 103 if autoPinningPolicy == v { 104 return true 105 } 106 } 107 return false 108 } 109 110 // supportedAutoPinningPolicies returns a slice of all supported AutoPinningPolicy. 111 func supportedAutoPinningPolicies() []ovirt.AutoPinningPolicy { 112 return []ovirt.AutoPinningPolicy{ 113 ovirt.AutoPinningNone, 114 ovirt.AutoPinningResizeAndPin, 115 } 116 }