From 6ffeb55576408785d849b2a949be16a032439c6e Mon Sep 17 00:00:00 2001 From: Prajyot Parab Date: Fri, 29 Apr 2022 14:03:14 +0530 Subject: [PATCH] validate machine spec values (#626) Signed-off-by: Prajyot-Parab --- api/v1beta1/common.go | 20 ++- api/v1beta1/common_test.go | 114 ++++++++++++++++++ api/v1beta1/ibmpowervsmachine_types.go | 1 + api/v1beta1/ibmpowervsmachine_webhook.go | 20 +++ .../ibmpowervsmachinetemplate_webhook.go | 20 +++ ...e.cluster.x-k8s.io_ibmpowervsmachines.yaml | 1 + ...r.x-k8s.io_ibmpowervsmachinetemplates.yaml | 1 + 7 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 api/v1beta1/common_test.go diff --git a/api/v1beta1/common.go b/api/v1beta1/common.go index 577ddd168..487263016 100644 --- a/api/v1beta1/common.go +++ b/api/v1beta1/common.go @@ -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 == "" { @@ -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" diff --git a/api/v1beta1/common_test.go b/api/v1beta1/common_test.go new file mode 100644 index 000000000..bb1ff3ec0 --- /dev/null +++ b/api/v1beta1/common_test.go @@ -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) + } + }) + } +} diff --git a/api/v1beta1/ibmpowervsmachine_types.go b/api/v1beta1/ibmpowervsmachine_types.go index bbaa1460a..e4f4aaddf 100644 --- a/api/v1beta1/ibmpowervsmachine_types.go +++ b/api/v1beta1/ibmpowervsmachine_types.go @@ -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) diff --git a/api/v1beta1/ibmpowervsmachine_webhook.go b/api/v1beta1/ibmpowervsmachine_webhook.go index 3c5ece6b1..51b3b01ce 100644 --- a/api/v1beta1/ibmpowervsmachine_webhook.go +++ b/api/v1beta1/ibmpowervsmachine_webhook.go @@ -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 } @@ -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 +} diff --git a/api/v1beta1/ibmpowervsmachinetemplate_webhook.go b/api/v1beta1/ibmpowervsmachinetemplate_webhook.go index 0e34ae5ac..eea70818f 100644 --- a/api/v1beta1/ibmpowervsmachinetemplate_webhook.go +++ b/api/v1beta1/ibmpowervsmachinetemplate_webhook.go @@ -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 } @@ -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 +} diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachines.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachines.yaml index a317fdb27..40fb0c0b2 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachines.yaml @@ -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 diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachinetemplates.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachinetemplates.yaml index bc9313a10..ab829c5f4 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachinetemplates.yaml @@ -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