Skip to content

Commit

Permalink
validate machine spec values
Browse files Browse the repository at this point in the history
Signed-off-by: Prajyot-Parab <[email protected]>
  • Loading branch information
Prajyot-Parab committed Apr 11, 2022
1 parent 7856be6 commit 17410e2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
27 changes: 26 additions & 1 deletion api/v1beta1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ limitations under the License.

package v1beta1

import "k8s.io/apimachinery/pkg/util/validation/field"
import (
"strconv"
"strings"

"k8s.io/apimachinery/pkg/util/validation/field"
)

func defaultIBMPowerVSMachineSpec(spec *IBMPowerVSMachineSpec) {
if spec.Memory == "" {
Expand Down Expand Up @@ -60,6 +65,26 @@ func validateIBMPowerVSResourceReference(res IBMPowerVSResourceReference, resTyp
return true, nil
}

func validateIBMPowerVSMemoryValues(resValue string) bool {
if val, err := strconv.ParseUint(resValue, 10, 64); err != nil || val == 0 {
return false
}
return true
}

func validateIBMPowerVSProcessorValues(resValue string) bool {
valueTypes := [...]string{"nan", "inf"}
for _, vt := range valueTypes {
if strings.Contains(strings.ToLower(resValue), vt) {
return false
}
}
if val, err := strconv.ParseFloat(resValue, 64); err != nil || val <= 0 {
return false
}
return true
}

func defaultIBMVPCMachineSpec(spec *IBMVPCMachineSpec) {
if spec.Profile == "" {
spec.Profile = "bx2-2x8"
Expand Down
20 changes: 20 additions & 0 deletions api/v1beta1/ibmpowervsmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func (r *IBMPowerVSMachine) validateIBMPowerVSMachine() error {
if err := r.validateIBMPowerVSMachineImage(); err != nil {
allErrs = append(allErrs, err)
}
if err := r.validateIBMPowerVSMachineMemory(); err != nil {
allErrs = append(allErrs, err)
}
if err := r.validateIBMPowerVSMachineProcessors(); err != nil {
allErrs = append(allErrs, err)
}
if len(allErrs) == 0 {
return nil
}
Expand Down Expand Up @@ -127,3 +133,17 @@ func (r *IBMPowerVSMachine) validateIBMPowerVSMachineImage() *field.Error {
}
return nil
}

func (r *IBMPowerVSMachine) validateIBMPowerVSMachineMemory() *field.Error {
if res := validateIBMPowerVSMemoryValues(r.Spec.Memory); !res {
return field.Invalid(field.NewPath("spec", "memory"), r.Spec.Memory, "Invalid Memory value - must be non-empty and positive numeric")
}
return nil
}

func (r *IBMPowerVSMachine) validateIBMPowerVSMachineProcessors() *field.Error {
if res := validateIBMPowerVSProcessorValues(r.Spec.Processors); !res {
return field.Invalid(field.NewPath("spec", "processors"), r.Spec.Processors, "Invalid Processors value - must be non-empty and positive numeric")
}
return nil
}
20 changes: 20 additions & 0 deletions api/v1beta1/ibmpowervsmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplate() error {
if err := r.validateIBMPowerVSMachineTemplateImage(); err != nil {
allErrs = append(allErrs, err)
}
if err := r.validateIBMPowerVSMachineTemplateMemory(); err != nil {
allErrs = append(allErrs, err)
}
if err := r.validateIBMPowerVSMachineTemplateProcessors(); err != nil {
allErrs = append(allErrs, err)
}
if len(allErrs) == 0 {
return nil
}
Expand Down Expand Up @@ -130,3 +136,17 @@ func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplateImage() *fi

return nil
}

func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplateMemory() *field.Error {
if res := validateIBMPowerVSMemoryValues(r.Spec.Template.Spec.Memory); !res {
return field.Invalid(field.NewPath("spec", "memory"), r.Spec.Template.Spec.Memory, "Invalid Memory value - must be non-empty and positive numeric")
}
return nil
}

func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplateProcessors() *field.Error {
if res := validateIBMPowerVSProcessorValues(r.Spec.Template.Spec.Processors); !res {
return field.Invalid(field.NewPath("spec", "processors"), r.Spec.Template.Spec.Processors, "Invalid Processors value - must be non-empty and positive numeric")
}
return nil
}

0 comments on commit 17410e2

Please sign in to comment.