Skip to content

Commit

Permalink
installconfig/gcp/validation: handle custom machine types
Browse files Browse the repository at this point in the history
Prior to this change, the validation on gcp instance types would return
an error that the type did not exist. This change adds the logic to
properly handle custom machine types while validating their CPU and
Memeory values.
  • Loading branch information
jstuever committed Nov 16, 2020
1 parent 8d9d7cb commit d51f38d
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/asset/installconfig/gcp/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net"
"net/http"
"strconv"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -66,6 +67,34 @@ func ValidateInstanceType(client API, ic *types.InstallConfig, fieldPath *field.
errMsg := fmt.Sprintf("instance type does not meet minimum resource requirements of %d MB Memory", req.minimumMemory)
allErrs = append(allErrs, field.Invalid(fieldPath.Child("type"), instanceType, errMsg))
}
} else if index := strings.Index(instanceType, "custom-"); index != -1 {

// Ensure the CPU and MEMORY are defined in custom instance type names
if customConfig := strings.Split(instanceType[index+len("custom-"):], "-"); len(customConfig) == 2 {

// Ensure the custom CPU is a valid integer
if customCpus, err := strconv.ParseInt(customConfig[0], 10, 0); err != nil {
errMsg := fmt.Sprintf("instance type %s is not a valid custom type", instanceType)
allErrs = append(allErrs, field.Invalid(fieldPath.Child("type"), instanceType, errMsg))
} else if customCpus < req.minimumVCpus {
errMsg := fmt.Sprintf("instance type does not meet minimum resource requirements of %d vCPUs", req.minimumVCpus)
allErrs = append(allErrs, field.Invalid(fieldPath.Child("type"), instanceType, errMsg))
}

// Ensure the custom Memory is a valid integer
if customMemory, err := strconv.ParseInt(customConfig[1], 10, 0); err != nil {
errMsg := fmt.Sprintf("instance type %s is not a valid custom type", instanceType)
allErrs = append(allErrs, field.Invalid(fieldPath.Child("type"), instanceType, errMsg))
} else if customMemory < req.minimumMemory {
errMsg := fmt.Sprintf("instance type does not meet minimum resource requirements of %d MB Memory", req.minimumMemory)
allErrs = append(allErrs, field.Invalid(fieldPath.Child("type"), instanceType, errMsg))
}

} else {
errMsg := fmt.Sprintf("instance type %s is not a valid custom type", instanceType)
allErrs = append(allErrs, field.Invalid(fieldPath.Child("type"), instanceType, errMsg))
}

} else {
errMsg := fmt.Sprintf("instance type %s not found", instanceType)
allErrs = append(allErrs, field.Invalid(fieldPath.Child("type"), instanceType, errMsg))
Expand Down

0 comments on commit d51f38d

Please sign in to comment.