Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix network/image webhook validations #523

Merged
merged 1 commit into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions api/v1beta1/ibmpowervscluster_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type IBMPowerVSClusterSpec struct {
// Important: Run "make" to regenerate code after modifying this file

// ServiceInstanceID is the id of the power cloud instance where the vsi instance will get deployed
// +kubebuilder:validation:MinLength=1
ServiceInstanceID string `json:"serviceInstanceID"`

// Network is the reference to the Network to use for this cluster.
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
}
3 changes: 3 additions & 0 deletions api/v1beta1/ibmpowervsmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type IBMPowerVSMachineSpec struct {
// Important: Run "make" to regenerate code after modifying this file

// ServiceInstanceID is the id of the power cloud instance where the vsi instance will get deployed
// +kubebuilder:validation:MinLength=1
ServiceInstanceID string `json:"serviceInstanceID"`

// SSHKey is the name of the SSH key pair provided to the vsi for authenticating users
Expand Down Expand Up @@ -74,10 +75,12 @@ type IBMPowerVSMachineSpec struct {
// a validation error.
type IBMPowerVSResourceReference struct {
// ID of resource
// +kubebuilder:validation:MinLength=1
Prajyot-Parab marked this conversation as resolved.
Show resolved Hide resolved
// +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,14 +126,17 @@ spec:
properties:
id:
description: ID of resource
minLength: 1
type: string
name:
description: Name of resource
minLength: 1
type: string
type: object
serviceInstanceID:
description: ServiceInstanceID is the id of the power cloud instance
where the vsi instance will get deployed
minLength: 1
type: string
required:
- network
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 All @@ -226,6 +230,7 @@ spec:
serviceInstanceID:
description: ServiceInstanceID is the id of the power cloud instance
where the vsi instance will get deployed
minLength: 1
type: string
sshKey:
description: SSHKey is the name of the SSH key pair provided to the
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 All @@ -183,6 +187,7 @@ spec:
serviceInstanceID:
description: ServiceInstanceID is the id of the power cloud
instance where the vsi instance will get deployed
minLength: 1
type: string
sshKey:
description: SSHKey is the name of the SSH key pair provided
Expand Down
2 changes: 1 addition & 1 deletion controllers/ibmpowervscluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var _ = Describe("IBMPowerVSClusterReconciler", func() {
Log: klogr.New(),
}

instance := &v1beta1.IBMPowerVSCluster{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}}
instance := &v1beta1.IBMPowerVSCluster{ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}, Spec: v1beta1.IBMPowerVSClusterSpec{ServiceInstanceID: "foo"}}

// Create the IBMPowerVSCluster object and expect the Reconcile to be created
Expect(k8sClient.Create(ctx, instance)).To(Succeed())
Expand Down