Skip to content

Commit

Permalink
Merge pull request #869 from lxiaopei/topic/lxiaopei/subnetport_spec
Browse files Browse the repository at this point in the history
Add validations for SubnetPort CRD
  • Loading branch information
lxiaopei authored Nov 22, 2024
2 parents cc38dd5 + fb2defe commit 1cf8cb7
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 23 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 or both set
to empty in which case default SubnetSet for VM will be used
rule: '!has(self.subnetSet) || !has(self.subnet)'
status:
description: SubnetPortStatus defines the observed state of SubnetPort.
properties:
Expand Down
43 changes: 43 additions & 0 deletions build/yaml/samples/nsx_v1alpha1_subnetport.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
apiVersion: crd.nsx.vmware.com/v1alpha1
kind: SubnetPort
metadata:
name: subnetport-sample-a
spec:
subnetSet: vm-subnetset
status:
attachment:
id: 35323036-6439-4932-ad36-3930372d3438
conditions:
- lastTransitionTime: "2024-11-20T22:23:10Z"
message: NSX subnet port has been successfully created/updated
reason: SubnetPortReady
status: "True"
type: Ready
networkInterfaceConfig:
ipAddresses:
- gateway: 172.26.0.1
ipAddress: 172.26.0.3/28
logicalSwitchUUID: 49fa0a2d-8fd2-4c85-87ca-2495e8a86d06
macAddress: 04:50:56:00:94:00
---
# SubnetPort CR sample without specifying subnet or subnetSet
apiVersion: crd.nsx.vmware.com/v1alpha1
kind: SubnetPort
metadata:
name: subnetport-sample-b
spec:
status:
attachment:
id: 35323036-6439-4932-ad36-3930372d3438
conditions:
- lastTransitionTime: "2024-11-20T22:23:10Z"
message: NSX subnet port has been successfully created/updated
reason: SubnetPortReady
status: "True"
type: Ready
networkInterfaceConfig:
ipAddresses:
- gateway: 172.26.0.1
ipAddress: 172.26.0.3/28
logicalSwitchUUID: 49fa0a2d-8fd2-4c85-87ca-2495e8a86d06
macAddress: 04:50:56:00:94:00
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.subnetSet) || !has(self.subnet)",message="Only one of subnet or subnetSet can be specified or both set to empty in which case default SubnetSet for VM will be used"
// SubnetPortSpec defines the desired state of SubnetPort.
type SubnetPortSpec struct {
// Subnet defines the parent Subnet name of the SubnetPort.
Expand Down
5 changes: 0 additions & 5 deletions pkg/controllers/subnetport/subnetport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,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")
r.StatusUpdater.UpdateFail(ctx, subnetPort, err, "Failed to get Subnet/SubnetSet of the SubnetPort", setSubnetPortReadyStatusFalse, r.SubnetPortService)
return common.ResultNormal, err
}

if subnetPort.ObjectMeta.DeletionTimestamp.IsZero() {
r.StatusUpdater.IncreaseUpdateTotal()
Expand Down
25 changes: 7 additions & 18 deletions pkg/controllers/subnetport/subnetport_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func TestSubnetPortReconciler_Reconcile(t *testing.T) {
},
},
},
SubnetPortStore: &subnetport.SubnetPortStore{},
}
subnetService := &subnet.SubnetService{
Service: servicecommon.Service{
Expand Down Expand Up @@ -133,31 +134,19 @@ func TestSubnetPortReconciler_Reconcile(t *testing.T) {
_, ret = r.Reconcile(ctx, req)
assert.Equal(t, err, ret)

patches := gomonkey.ApplyFunc(setAddressBindingStatusBySubnetPort, func(client client.Client, ctx context.Context, subnetPort *v1alpha1.SubnetPort, subnetPortService *subnetport.SubnetPortService, transitionTime metav1.Time, e error) {
})
defer patches.Reset()

// 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
})
err = errors.New("subnet and subnetset should not be configured at the same time")
k8sClient.EXPECT().Status().Return(fakewriter)
_, ret = r.Reconcile(ctx, req)
assert.Equal(t, err, ret)

// CheckAndGetSubnetPathForSubnetPort fails
sp := &v1alpha1.SubnetPort{}
err = errors.New("CheckAndGetSubnetPathForSubnetPort failed")
patchesCheckAndGetSubnetPathForSubnetPort := gomonkey.ApplyFunc((*SubnetPortReconciler).CheckAndGetSubnetPathForSubnetPort,
func(r *SubnetPortReconciler, ctx context.Context, obj *v1alpha1.SubnetPort) (bool, string, error) {
return false, "", err
})
defer patchesCheckAndGetSubnetPathForSubnetPort.Reset()
patchesGetByKey := gomonkey.ApplyFunc((*subnetport.SubnetPortStore).GetByKey,
func(s *subnetport.SubnetPortStore, key string) *model.VpcSubnetPort {
return nil
})
defer patchesGetByKey.Reset()
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)
Expand Down

0 comments on commit 1cf8cb7

Please sign in to comment.