Skip to content

Commit

Permalink
Add validations for SubnetPort CRD
Browse files Browse the repository at this point in the history
Add validations for SubnetPort CR to specify one of subnet or subnetSet
under spec.
  • Loading branch information
lxiaopei committed Nov 8, 2024
1 parent bf1880a commit 36de5b0
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 24 deletions.
4 changes: 4 additions & 0 deletions build/yaml/crd/vpc/crd.nsx.vmware.com_subnetports.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ spec:
description: SubnetSet defines the parent SubnetSet name of the SubnetPort.
type: string
type: object
x-kubernetes-validations:
- message: Only one of subnet or subnetSet can be specified
rule: has(self.subnet) && !has(self.subnetSet) || !has(self.subnet)
&& has(self.subnetSet)
status:
description: SubnetPortStatus defines the observed state of SubnetPort.
properties:
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/vpc/v1alpha1/subnetport_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +kubebuilder:validation:XValidation:rule="has(self.subnet) && !has(self.subnetSet) || !has(self.subnet) && has(self.subnetSet)",message="Only one of subnet or subnetSet can be specified"
// SubnetPortSpec defines the desired state of SubnetPort.
type SubnetPortSpec struct {
// Subnet defines the parent Subnet name of the SubnetPort.
Expand Down
7 changes: 0 additions & 7 deletions pkg/controllers/subnetport/subnetport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package subnetport

import (
"context"
"errors"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -79,12 +78,6 @@ func (r *SubnetPortReconciler) Reconcile(ctx context.Context, req ctrl.Request)
log.Error(err, "unable to fetch SubnetPort CR", "SubnetPort", req.NamespacedName)
return common.ResultRequeue, err
}
if len(subnetPort.Spec.SubnetSet) > 0 && len(subnetPort.Spec.Subnet) > 0 {
err := errors.New("subnet and subnetset should not be configured at the same time")
log.Error(err, "failed to get subnet/subnetset of the subnetport", "subnetport", req.NamespacedName)
updateFail(r, ctx, subnetPort, &err)
return common.ResultNormal, err
}

if subnetPort.ObjectMeta.DeletionTimestamp.IsZero() {
metrics.CounterInc(r.SubnetPortService.NSXConfig, metrics.ControllerUpdateTotal, MetricResTypeSubnetPort)
Expand Down
17 changes: 0 additions & 17 deletions pkg/controllers/subnetport/subnetport_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,6 @@ func TestSubnetPortReconciler_Reconcile(t *testing.T) {
_, ret = r.Reconcile(ctx, req)
assert.Equal(t, err, ret)

// both subnet and subnetset are configured
sp := &v1alpha1.SubnetPort{}
k8sClient.EXPECT().Get(ctx, gomock.Any(), sp).Return(nil).Do(
func(_ context.Context, _ client.ObjectKey, obj client.Object, option ...client.GetOption) error {
v1sp := obj.(*v1alpha1.SubnetPort)
v1sp.Spec.Subnet = "subnet1"
v1sp.Spec.SubnetSet = "subnetset2"
return nil
})
patchesUpdateFail := gomonkey.ApplyFunc(updateFail,
func(r *SubnetPortReconciler, c context.Context, o *v1alpha1.SubnetPort, e *error) {
})
defer patchesUpdateFail.Reset()
err = errors.New("subnet and subnetset should not be configured at the same time")
_, ret = r.Reconcile(ctx, req)
assert.Equal(t, err, ret)

// CheckAndGetSubnetPathForSubnetPort fails
err = errors.New("CheckAndGetSubnetPathForSubnetPort failed")
patchesCheckAndGetSubnetPathForSubnetPort := gomonkey.ApplyFunc((*SubnetPortReconciler).CheckAndGetSubnetPathForSubnetPort,
Expand Down

0 comments on commit 36de5b0

Please sign in to comment.