From 09cbd00e37230ff6fab6d754f77cfe81d58d3ae5 Mon Sep 17 00:00:00 2001 From: Prajyot-Parab Date: Tue, 25 Jan 2022 08:28:15 +0000 Subject: [PATCH] Fix network/image webhook validations Signed-off-by: Prajyot-Parab --- api/v1beta1/common.go | 10 ++++++---- api/v1beta1/ibmpowervscluster_webhook.go | 4 ++-- api/v1beta1/ibmpowervsmachine_types.go | 2 ++ api/v1beta1/ibmpowervsmachine_webhook.go | 14 ++++++++++++-- api/v1beta1/ibmpowervsmachinetemplate_webhook.go | 14 ++++++++++++-- ...ucture.cluster.x-k8s.io_ibmpowervsclusters.yaml | 2 ++ ...ucture.cluster.x-k8s.io_ibmpowervsmachines.yaml | 4 ++++ ...luster.x-k8s.io_ibmpowervsmachinetemplates.yaml | 4 ++++ 8 files changed, 44 insertions(+), 10 deletions(-) diff --git a/api/v1beta1/common.go b/api/v1beta1/common.go index 0ef792d8bb..4e43bb50f5 100644 --- a/api/v1beta1/common.go +++ b/api/v1beta1/common.go @@ -16,6 +16,8 @@ limitations under the License. package v1beta1 +import "k8s.io/apimachinery/pkg/util/validation/field" + func defaultIBMPowerVSMachineSpec(spec *IBMPowerVSMachineSpec) { if spec.Memory == "" { @@ -52,11 +54,11 @@ func validateIBMPowerVSProcType(spec IBMPowerVSMachineSpec) (bool, IBMPowerVSMac return false, spec } -func validateIBMPowerVSNetwork(network IBMPowerVSResourceReference) (bool, IBMPowerVSResourceReference) { - if network.ID != nil && network.Name != nil { - return false, network +func validateIBMPowerVSResourceReference(res IBMPowerVSResourceReference, resType string) (bool, *field.Error) { + if res.ID != nil && res.Name != nil { + return false, field.Invalid(field.NewPath("spec", resType), res, "Only one of "+resType+" - ID or Name may be specified") } - return true, IBMPowerVSResourceReference{} + return true, nil } func defaultIBMVPCMachineSpec(spec *IBMVPCMachineSpec) { diff --git a/api/v1beta1/ibmpowervscluster_webhook.go b/api/v1beta1/ibmpowervscluster_webhook.go index b46033646c..0f1b7f7f32 100644 --- a/api/v1beta1/ibmpowervscluster_webhook.go +++ b/api/v1beta1/ibmpowervscluster_webhook.go @@ -82,8 +82,8 @@ func (r *IBMPowerVSCluster) validateIBMPowerVSCluster() error { } func (r *IBMPowerVSCluster) validateIBMPowerVSClusterNetwork() *field.Error { - if res, net := validateIBMPowerVSNetwork(r.Spec.Network); !res { - return field.Invalid(field.NewPath("spec", "network"), net, "Only one of Network - ID or Name may be specified") + if res, err := validateIBMPowerVSResourceReference(r.Spec.Network, "Network"); !res { + return err } return nil } diff --git a/api/v1beta1/ibmpowervsmachine_types.go b/api/v1beta1/ibmpowervsmachine_types.go index 7314acb277..1313be19a4 100644 --- a/api/v1beta1/ibmpowervsmachine_types.go +++ b/api/v1beta1/ibmpowervsmachine_types.go @@ -74,10 +74,12 @@ type IBMPowerVSMachineSpec struct { // a validation error. type IBMPowerVSResourceReference struct { // ID of resource + // +kubebuilder:validation:MinLength=1 // +optional ID *string `json:"id,omitempty"` // Name of resource + // +kubebuilder:validation:MinLength=1 // +optional Name *string `json:"name,omitempty"` } diff --git a/api/v1beta1/ibmpowervsmachine_webhook.go b/api/v1beta1/ibmpowervsmachine_webhook.go index 364169d59a..f3f042c960 100644 --- a/api/v1beta1/ibmpowervsmachine_webhook.go +++ b/api/v1beta1/ibmpowervsmachine_webhook.go @@ -78,6 +78,9 @@ func (r *IBMPowerVSMachine) validateIBMPowerVSMachine() error { if err := r.validateIBMPowerVSMachineNetwork(); err != nil { allErrs = append(allErrs, err) } + if err := r.validateIBMPowerVSMachineImage(); err != nil { + allErrs = append(allErrs, err) + } if len(allErrs) == 0 { return nil } @@ -102,8 +105,15 @@ func (r *IBMPowerVSMachine) validateIBMPowerVSMachineProcType() *field.Error { } func (r *IBMPowerVSMachine) validateIBMPowerVSMachineNetwork() *field.Error { - if res, net := validateIBMPowerVSNetwork(r.Spec.Network); !res { - return field.Invalid(field.NewPath("spec", "network"), net, "Only one of Network - ID or Name may be specified") + if res, err := validateIBMPowerVSResourceReference(r.Spec.Network, "Network"); !res { + return err + } + return nil +} + +func (r *IBMPowerVSMachine) validateIBMPowerVSMachineImage() *field.Error { + if res, err := validateIBMPowerVSResourceReference(r.Spec.Image, "Image"); !res { + return err } return nil } diff --git a/api/v1beta1/ibmpowervsmachinetemplate_webhook.go b/api/v1beta1/ibmpowervsmachinetemplate_webhook.go index 97f719e732..75a8b7d75b 100644 --- a/api/v1beta1/ibmpowervsmachinetemplate_webhook.go +++ b/api/v1beta1/ibmpowervsmachinetemplate_webhook.go @@ -78,6 +78,9 @@ func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplate() error { if err := r.validateIBMPowerVSMachineTemplateNetwork(); err != nil { allErrs = append(allErrs, err) } + if err := r.validateIBMPowerVSMachineTemplateImage(); err != nil { + allErrs = append(allErrs, err) + } if len(allErrs) == 0 { return nil } @@ -102,8 +105,15 @@ func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplateProcType() } func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplateNetwork() *field.Error { - if res, net := validateIBMPowerVSNetwork(r.Spec.Template.Spec.Network); !res { - return field.Invalid(field.NewPath("spec", "template", "spec", "network"), net, "Only one of Network - ID or Name may be specified") + if res, err := validateIBMPowerVSResourceReference(r.Spec.Template.Spec.Network, "Network"); !res { + return err + } + return nil +} + +func (r *IBMPowerVSMachineTemplate) validateIBMPowerVSMachineTemplateImage() *field.Error { + if res, err := validateIBMPowerVSResourceReference(r.Spec.Template.Spec.Image, "Image"); !res { + return err } return nil } diff --git a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsclusters.yaml b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsclusters.yaml index 8299157f06..38eb6351b8 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsclusters.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsclusters.yaml @@ -126,9 +126,11 @@ spec: properties: id: description: ID of resource + minLength: 1 type: string name: description: Name of resource + minLength: 1 type: string type: object serviceInstanceID: 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 5a7201193d..afc80a6de4 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachines.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachines.yaml @@ -193,9 +193,11 @@ spec: properties: id: description: ID of resource + minLength: 1 type: string name: description: Name of resource + minLength: 1 type: string type: object memory: @@ -207,9 +209,11 @@ spec: properties: id: description: ID of resource + minLength: 1 type: string name: description: Name of resource + minLength: 1 type: string type: object procType: 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 e92154c288..aaec69948c 100644 --- a/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachinetemplates.yaml +++ b/config/crd/bases/infrastructure.cluster.x-k8s.io_ibmpowervsmachinetemplates.yaml @@ -150,9 +150,11 @@ spec: properties: id: description: ID of resource + minLength: 1 type: string name: description: Name of resource + minLength: 1 type: string type: object memory: @@ -164,9 +166,11 @@ spec: properties: id: description: ID of resource + minLength: 1 type: string name: description: Name of resource + minLength: 1 type: string type: object procType: