github.com/openshift/installer@v1.4.17/pkg/types/validation/machinepools.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" 9 "github.com/openshift/installer/pkg/types/aws" 10 awsvalidation "github.com/openshift/installer/pkg/types/aws/validation" 11 "github.com/openshift/installer/pkg/types/azure" 12 azurevalidation "github.com/openshift/installer/pkg/types/azure/validation" 13 "github.com/openshift/installer/pkg/types/baremetal" 14 baremetalvalidation "github.com/openshift/installer/pkg/types/baremetal/validation" 15 "github.com/openshift/installer/pkg/types/gcp" 16 gcpvalidation "github.com/openshift/installer/pkg/types/gcp/validation" 17 "github.com/openshift/installer/pkg/types/ibmcloud" 18 ibmcloudvalidation "github.com/openshift/installer/pkg/types/ibmcloud/validation" 19 "github.com/openshift/installer/pkg/types/openstack" 20 openstackvalidation "github.com/openshift/installer/pkg/types/openstack/validation" 21 "github.com/openshift/installer/pkg/types/ovirt" 22 ovirtvalidation "github.com/openshift/installer/pkg/types/ovirt/validation" 23 "github.com/openshift/installer/pkg/types/powervs" 24 powervsvalidation "github.com/openshift/installer/pkg/types/powervs/validation" 25 "github.com/openshift/installer/pkg/types/vsphere" 26 vspherevalidation "github.com/openshift/installer/pkg/types/vsphere/validation" 27 ) 28 29 var ( 30 validHyperthreadingModes = map[types.HyperthreadingMode]bool{ 31 types.HyperthreadingDisabled: true, 32 types.HyperthreadingEnabled: true, 33 } 34 35 validHyperthreadingModeValues = func() []string { 36 v := make([]string, 0, len(validHyperthreadingModes)) 37 for m := range validHyperthreadingModes { 38 v = append(v, string(m)) 39 } 40 return v 41 }() 42 43 validArchitectures = map[types.Architecture]bool{ 44 types.ArchitectureAMD64: true, 45 types.ArchitectureS390X: true, 46 types.ArchitecturePPC64LE: true, 47 types.ArchitectureARM64: true, 48 } 49 50 validArchitectureValues = func() []string { 51 v := make([]string, 0, len(validArchitectures)) 52 for m := range validArchitectures { 53 v = append(v, string(m)) 54 } 55 return v 56 }() 57 ) 58 59 // ValidateMachinePool checks that the specified machine pool is valid. 60 func ValidateMachinePool(platform *types.Platform, p *types.MachinePool, fldPath *field.Path) field.ErrorList { 61 allErrs := field.ErrorList{} 62 if p.Replicas != nil { 63 if *p.Replicas < 0 { 64 allErrs = append(allErrs, field.Invalid(fldPath.Child("replicas"), p.Replicas, "number of replicas must not be negative")) 65 } 66 } else { 67 allErrs = append(allErrs, field.Required(fldPath.Child("replicas"), "replicas is required")) 68 } 69 if !validHyperthreadingModes[p.Hyperthreading] { 70 allErrs = append(allErrs, field.NotSupported(fldPath.Child("hyperthreading"), p.Hyperthreading, validHyperthreadingModeValues)) 71 } 72 if !validArchitectures[p.Architecture] { 73 allErrs = append(allErrs, field.NotSupported(fldPath.Child("architecture"), p.Architecture, validArchitectureValues)) 74 } 75 if platform.AWS != nil { 76 allErrs = append(allErrs, awsvalidation.ValidateMachinePoolArchitecture(p, fldPath.Child("architecture"))...) 77 } 78 allErrs = append(allErrs, validateMachinePoolPlatform(platform, &p.Platform, p, fldPath.Child("platform"))...) 79 return allErrs 80 } 81 82 func validateMachinePoolPlatform(platform *types.Platform, p *types.MachinePoolPlatform, pool *types.MachinePool, fldPath *field.Path) field.ErrorList { 83 allErrs := field.ErrorList{} 84 platformName := platform.Name() 85 validate := func(n string, value interface{}, validation func(*field.Path) field.ErrorList) { 86 f := fldPath.Child(n) 87 if platformName == n { 88 allErrs = append(allErrs, validation(f)...) 89 } else { 90 allErrs = append(allErrs, field.Invalid(f, value, fmt.Sprintf("cannot specify %q for machine pool when cluster is using %q", n, platformName))) 91 } 92 } 93 if platform.AWS != nil { 94 allErrs = append(allErrs, awsvalidation.ValidateAMIID(platform.AWS, p.AWS, fldPath.Child("aws"))...) 95 } 96 if p.AWS != nil { 97 validate(aws.Name, p.AWS, func(f *field.Path) field.ErrorList { return awsvalidation.ValidateMachinePool(platform.AWS, p.AWS, f) }) 98 } 99 if p.Azure != nil { 100 validate(azure.Name, p.Azure, func(f *field.Path) field.ErrorList { 101 return azurevalidation.ValidateMachinePool(p.Azure, pool.Name, platform.Azure, f) 102 }) 103 } 104 if p.GCP != nil { 105 validate(gcp.Name, p.GCP, func(f *field.Path) field.ErrorList { return validateGCPMachinePool(platform, p, pool, f) }) 106 } 107 if p.IBMCloud != nil { 108 validate(ibmcloud.Name, p.IBMCloud, func(f *field.Path) field.ErrorList { 109 return ibmcloudvalidation.ValidateMachinePool(platform.IBMCloud, p.IBMCloud, f) 110 }) 111 } 112 if p.BareMetal != nil { 113 validate(baremetal.Name, p.BareMetal, func(f *field.Path) field.ErrorList { return baremetalvalidation.ValidateMachinePool(p.BareMetal, f) }) 114 } 115 if p.VSphere != nil { 116 validate(vsphere.Name, p.VSphere, func(f *field.Path) field.ErrorList { 117 return vspherevalidation.ValidateMachinePool(platform.VSphere, pool, f) 118 }) 119 } 120 if p.Ovirt != nil { 121 validate(ovirt.Name, p.Ovirt, func(f *field.Path) field.ErrorList { return ovirtvalidation.ValidateMachinePool(p.Ovirt, f) }) 122 } 123 if p.PowerVS != nil { 124 validate(powervs.Name, p.PowerVS, func(f *field.Path) field.ErrorList { return powervsvalidation.ValidateMachinePool(p.PowerVS, f) }) 125 } 126 if p.OpenStack != nil { 127 validate(openstack.Name, p.OpenStack, func(f *field.Path) field.ErrorList { 128 return openstackvalidation.ValidateMachinePool(platform.OpenStack, p.OpenStack, pool.Name, f) 129 }) 130 } 131 return allErrs 132 } 133 134 func validateGCPMachinePool(platform *types.Platform, p *types.MachinePoolPlatform, pool *types.MachinePool, f *field.Path) field.ErrorList { 135 allErrs := field.ErrorList{} 136 137 allErrs = append(allErrs, gcpvalidation.ValidateMachinePool(platform.GCP, p.GCP, f)...) 138 allErrs = append(allErrs, gcpvalidation.ValidateMasterDiskType(pool, f)...) 139 allErrs = append(allErrs, gcpvalidation.ValidateServiceAccount(platform.GCP, pool, f)...) 140 141 return allErrs 142 }