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

     1  package validation
     2  
     3  import (
     4  	"fmt"
     5  	"regexp"
     6  	"strings"
     7  	"unicode"
     8  
     9  	"k8s.io/apimachinery/pkg/util/sets"
    10  	"k8s.io/apimachinery/pkg/util/validation/field"
    11  
    12  	"github.com/openshift/installer/pkg/types"
    13  	"github.com/openshift/installer/pkg/types/gcp"
    14  )
    15  
    16  // ValidateMachinePool checks that the specified machine pool is valid.
    17  func ValidateMachinePool(platform *gcp.Platform, p *gcp.MachinePool, fldPath *field.Path) field.ErrorList {
    18  	allErrs := field.ErrorList{}
    19  	for i, zone := range p.Zones {
    20  		if !strings.HasPrefix(zone, platform.Region) {
    21  			allErrs = append(allErrs, field.Invalid(fldPath.Child("zones").Index(i), zone, fmt.Sprintf("Zone not in configured region (%s)", platform.Region)))
    22  		}
    23  	}
    24  	if p.OSDisk.DiskSizeGB != 0 {
    25  		if p.OSDisk.DiskSizeGB < 16 {
    26  			allErrs = append(allErrs, field.Invalid(fldPath.Child("diskSizeGB"), p.OSDisk.DiskSizeGB, "must be at least 16GB in size"))
    27  		} else if p.OSDisk.DiskSizeGB > 65536 {
    28  			allErrs = append(allErrs, field.Invalid(fldPath.Child("diskSizeGB"), p.OSDisk.DiskSizeGB, "exceeding maximum GCP disk size limit, must be below 65536"))
    29  		}
    30  	}
    31  
    32  	if p.OSDisk.DiskType != "" {
    33  		diskTypes := sets.NewString("pd-balanced", "pd-ssd", "pd-standard", "hyperdisk-balanced")
    34  		if !diskTypes.Has(p.OSDisk.DiskType) {
    35  			allErrs = append(allErrs, field.NotSupported(fldPath.Child("diskType"), p.OSDisk.DiskType, diskTypes.List()))
    36  		}
    37  	}
    38  
    39  	if p.ConfidentialCompute == string(gcp.EnabledFeature) && p.OnHostMaintenance != string(gcp.OnHostMaintenanceTerminate) {
    40  		allErrs = append(allErrs, field.Invalid(fldPath.Child("OnHostMaintenance"), p.OnHostMaintenance, "OnHostMaintenace must be set to Terminate when ConfidentialCompute is Enabled"))
    41  	}
    42  
    43  	for i, tag := range p.Tags {
    44  		if tag == "" {
    45  			allErrs = append(allErrs, field.Invalid(fldPath.Child("tags").Index(i), tag, fmt.Sprintf("tag can not be empty")))
    46  		} else if !unicode.IsLetter(rune(tag[0])) || (!unicode.IsLetter(rune(tag[len(tag)-1])) && !unicode.IsNumber(rune(tag[len(tag)-1]))) {
    47  			allErrs = append(allErrs, field.Invalid(fldPath.Child("tags").Index(i), tag, fmt.Sprintf("tag can only start with a letter and must end with a letter or a number")))
    48  		} else if !regexp.MustCompile(`^[a-z0-9-]*$`).MatchString(tag) {
    49  			allErrs = append(allErrs, field.Invalid(fldPath.Child("tags").Index(i), tag, fmt.Sprintf("tag can only contain lowercase letters, numbers, and dashes")))
    50  		} else if len(tag) > 63 {
    51  			allErrs = append(allErrs, field.Invalid(fldPath.Child("tags").Index(i), tag, fmt.Sprintf("maximum number of characters is 63")))
    52  		}
    53  	}
    54  	return allErrs
    55  }
    56  
    57  // ValidateServiceAccount does not do any checks on the service account since it can be set for all nodes and
    58  // in non-shared vpn installations.
    59  func ValidateServiceAccount(platform *gcp.Platform, p *types.MachinePool, fldPath *field.Path) field.ErrorList {
    60  	return field.ErrorList{}
    61  }
    62  
    63  // ValidateMasterDiskType checks that the specified disk type is valid for control plane.
    64  func ValidateMasterDiskType(p *types.MachinePool, fldPath *field.Path) field.ErrorList {
    65  	allErrs := field.ErrorList{}
    66  	if p.Name == "master" && p.Platform.GCP.OSDisk.DiskType != "" {
    67  		diskTypes := sets.NewString("pd-ssd")
    68  		if !diskTypes.Has(p.Platform.GCP.OSDisk.DiskType) {
    69  			allErrs = append(allErrs, field.NotSupported(fldPath.Child("diskType"), p.Platform.GCP.OSDisk.DiskType, diskTypes.List()))
    70  		}
    71  	}
    72  	return allErrs
    73  }
    74  
    75  // ValidateDefaultDiskType checks that the specified disk type is valid for default GCP Machine Platform.
    76  func ValidateDefaultDiskType(p *gcp.MachinePool, fldPath *field.Path) field.ErrorList {
    77  	allErrs := field.ErrorList{}
    78  
    79  	if p != nil && p.OSDisk.DiskType != "" {
    80  		diskTypes := sets.NewString("pd-ssd")
    81  
    82  		if !diskTypes.Has(p.OSDisk.DiskType) {
    83  			allErrs = append(allErrs, field.NotSupported(fldPath.Child("diskType"), p.OSDisk.DiskType, diskTypes.List()))
    84  		}
    85  	}
    86  
    87  	return allErrs
    88  }