Skip to content

Commit

Permalink
validate machine spec values (#626)
Browse files Browse the repository at this point in the history
Signed-off-by: Prajyot-Parab <[email protected]>
  • Loading branch information
Prajyot-Parab authored Apr 29, 2022
1 parent ebe18bd commit 6ffeb55
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 1 deletion.
20 changes: 19 additions & 1 deletion api/v1beta1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ limitations under the License.

package v1beta1

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

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

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

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

func validateIBMPowerVSProcessorValues(resValue string) bool {
if val, err := strconv.ParseFloat(resValue, 64); err != nil || val < 0.25 {
return false
}
return true
}

func defaultIBMVPCMachineSpec(spec *IBMVPCMachineSpec) {
if spec.Profile == "" {
spec.Profile = "bx2-2x8"
Expand Down
114 changes: 114 additions & 0 deletions api/v1beta1/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1beta1

import (
"testing"
)

func TestValidateIBMPowerVSMemoryValues(t *testing.T) {
type args struct {
n string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "N is 4",
args: args{n: "4"},
want: true,
},
{
name: "N is 10",
args: args{n: "10"},
want: true,
},
{
name: "N is 1",
args: args{n: "1"},
want: false,
},
{
name: "N is 1.25",
args: args{n: "1.25"},
want: false,
},
{
name: "N is abc",
args: args{n: "abc"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validateIBMPowerVSMemoryValues(tt.args.n); got != tt.want {
t.Errorf("validateIBMPowerVSMemoryValues() = %v, want %v", got, tt.want)
}
})
}
}

func TestValidateIBMPowerVSProcessorValues(t *testing.T) {
type args struct {
n string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "N is 0.25",
args: args{n: "0.25"},
want: true,
},
{
name: "N is 0.5",
args: args{n: "0.5"},
want: true,
},
{
name: "N is 1",
args: args{n: "1"},
want: true,
},
{
name: "N is 10",
args: args{n: "10"},
want: true,
},
{
name: "N is 0.2",
args: args{n: "0.2"},
want: false,
},
{
name: "N is abc",
args: args{n: "abc"},
want: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validateIBMPowerVSProcessorValues(tt.args.n); got != tt.want {
t.Errorf("validateIBMPowerVSProcessorValues() = %v, want %v", got, tt.want)
}
})
}
}
1 change: 1 addition & 0 deletions api/v1beta1/ibmpowervsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type IBMPowerVSMachineSpec struct {

// Processors is Number of processors allocated.
// +optional
// +kubebuilder:validation:Pattern=^\d+(\.)?(\d)?(\d)?$
Processors string `json:"processors,omitempty"`

// Memory is Amount of memory allocated (in GB)
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 a positive integer no lesser than 2")
}
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 floating-point number no lesser than 0.25")
}
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 a positive integer no lesser than 2")
}
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 floating-point number no lesser than 0.25")
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ spec:
type: string
processors:
description: Processors is Number of processors allocated.
pattern: ^\d+(\.)?(\d)?(\d)?$
type: string
providerID:
description: ProviderID is the unique identifier as specified by the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ spec:
type: string
processors:
description: Processors is Number of processors allocated.
pattern: ^\d+(\.)?(\d)?(\d)?$
type: string
providerID:
description: ProviderID is the unique identifier as specified
Expand Down

0 comments on commit 6ffeb55

Please sign in to comment.