Skip to content

Commit

Permalink
Fix network/image webhook validations
Browse files Browse the repository at this point in the history
Signed-off-by: Prajyot-Parab <[email protected]>
  • Loading branch information
Prajyot-Parab committed Jan 25, 2022
1 parent d42e191 commit 09cbd00
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 10 deletions.
10 changes: 6 additions & 4 deletions api/v1beta1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions api/v1beta1/ibmpowervscluster_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions api/v1beta1/ibmpowervsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down
14 changes: 12 additions & 2 deletions api/v1beta1/ibmpowervsmachine_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
14 changes: 12 additions & 2 deletions api/v1beta1/ibmpowervsmachinetemplate_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 09cbd00

Please sign in to comment.