Skip to content

Commit 1cf8cb7

Browse files
authored
Merge pull request vmware-tanzu#869 from lxiaopei/topic/lxiaopei/subnetport_spec
Add validations for SubnetPort CRD
2 parents cc38dd5 + fb2defe commit 1cf8cb7

File tree

5 files changed

+55
-23
lines changed

5 files changed

+55
-23
lines changed

build/yaml/crd/vpc/crd.nsx.vmware.com_subnetports.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ spec:
5959
description: SubnetSet defines the parent SubnetSet name of the SubnetPort.
6060
type: string
6161
type: object
62+
x-kubernetes-validations:
63+
- message: Only one of subnet or subnetSet can be specified or both set
64+
to empty in which case default SubnetSet for VM will be used
65+
rule: '!has(self.subnetSet) || !has(self.subnet)'
6266
status:
6367
description: SubnetPortStatus defines the observed state of SubnetPort.
6468
properties:
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
apiVersion: crd.nsx.vmware.com/v1alpha1
2+
kind: SubnetPort
3+
metadata:
4+
name: subnetport-sample-a
5+
spec:
6+
subnetSet: vm-subnetset
7+
status:
8+
attachment:
9+
id: 35323036-6439-4932-ad36-3930372d3438
10+
conditions:
11+
- lastTransitionTime: "2024-11-20T22:23:10Z"
12+
message: NSX subnet port has been successfully created/updated
13+
reason: SubnetPortReady
14+
status: "True"
15+
type: Ready
16+
networkInterfaceConfig:
17+
ipAddresses:
18+
- gateway: 172.26.0.1
19+
ipAddress: 172.26.0.3/28
20+
logicalSwitchUUID: 49fa0a2d-8fd2-4c85-87ca-2495e8a86d06
21+
macAddress: 04:50:56:00:94:00
22+
---
23+
# SubnetPort CR sample without specifying subnet or subnetSet
24+
apiVersion: crd.nsx.vmware.com/v1alpha1
25+
kind: SubnetPort
26+
metadata:
27+
name: subnetport-sample-b
28+
spec:
29+
status:
30+
attachment:
31+
id: 35323036-6439-4932-ad36-3930372d3438
32+
conditions:
33+
- lastTransitionTime: "2024-11-20T22:23:10Z"
34+
message: NSX subnet port has been successfully created/updated
35+
reason: SubnetPortReady
36+
status: "True"
37+
type: Ready
38+
networkInterfaceConfig:
39+
ipAddresses:
40+
- gateway: 172.26.0.1
41+
ipAddress: 172.26.0.3/28
42+
logicalSwitchUUID: 49fa0a2d-8fd2-4c85-87ca-2495e8a86d06
43+
macAddress: 04:50:56:00:94:00

pkg/apis/vpc/v1alpha1/subnetport_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
88
)
99

10+
// +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"
1011
// SubnetPortSpec defines the desired state of SubnetPort.
1112
type SubnetPortSpec struct {
1213
// Subnet defines the parent Subnet name of the SubnetPort.

pkg/controllers/subnetport/subnetport_controller.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ func (r *SubnetPortReconciler) Reconcile(ctx context.Context, req ctrl.Request)
8989
log.Error(err, "unable to fetch SubnetPort CR", "SubnetPort", req.NamespacedName)
9090
return common.ResultRequeue, err
9191
}
92-
if len(subnetPort.Spec.SubnetSet) > 0 && len(subnetPort.Spec.Subnet) > 0 {
93-
err := errors.New("subnet and subnetset should not be configured at the same time")
94-
r.StatusUpdater.UpdateFail(ctx, subnetPort, err, "Failed to get Subnet/SubnetSet of the SubnetPort", setSubnetPortReadyStatusFalse, r.SubnetPortService)
95-
return common.ResultNormal, err
96-
}
9792

9893
if subnetPort.ObjectMeta.DeletionTimestamp.IsZero() {
9994
r.StatusUpdater.IncreaseUpdateTotal()

pkg/controllers/subnetport/subnetport_controller_test.go

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ func TestSubnetPortReconciler_Reconcile(t *testing.T) {
7676
},
7777
},
7878
},
79+
SubnetPortStore: &subnetport.SubnetPortStore{},
7980
}
8081
subnetService := &subnet.SubnetService{
8182
Service: servicecommon.Service{
@@ -133,31 +134,19 @@ func TestSubnetPortReconciler_Reconcile(t *testing.T) {
133134
_, ret = r.Reconcile(ctx, req)
134135
assert.Equal(t, err, ret)
135136

136-
patches := gomonkey.ApplyFunc(setAddressBindingStatusBySubnetPort, func(client client.Client, ctx context.Context, subnetPort *v1alpha1.SubnetPort, subnetPortService *subnetport.SubnetPortService, transitionTime metav1.Time, e error) {
137-
})
138-
defer patches.Reset()
139-
140-
// both subnet and subnetset are configured
141-
sp := &v1alpha1.SubnetPort{}
142-
k8sClient.EXPECT().Get(ctx, gomock.Any(), sp).Return(nil).Do(
143-
func(_ context.Context, _ client.ObjectKey, obj client.Object, option ...client.GetOption) error {
144-
v1sp := obj.(*v1alpha1.SubnetPort)
145-
v1sp.Spec.Subnet = "subnet1"
146-
v1sp.Spec.SubnetSet = "subnetset2"
147-
return nil
148-
})
149-
err = errors.New("subnet and subnetset should not be configured at the same time")
150-
k8sClient.EXPECT().Status().Return(fakewriter)
151-
_, ret = r.Reconcile(ctx, req)
152-
assert.Equal(t, err, ret)
153-
154137
// CheckAndGetSubnetPathForSubnetPort fails
138+
sp := &v1alpha1.SubnetPort{}
155139
err = errors.New("CheckAndGetSubnetPathForSubnetPort failed")
156140
patchesCheckAndGetSubnetPathForSubnetPort := gomonkey.ApplyFunc((*SubnetPortReconciler).CheckAndGetSubnetPathForSubnetPort,
157141
func(r *SubnetPortReconciler, ctx context.Context, obj *v1alpha1.SubnetPort) (bool, string, error) {
158142
return false, "", err
159143
})
160144
defer patchesCheckAndGetSubnetPathForSubnetPort.Reset()
145+
patchesGetByKey := gomonkey.ApplyFunc((*subnetport.SubnetPortStore).GetByKey,
146+
func(s *subnetport.SubnetPortStore, key string) *model.VpcSubnetPort {
147+
return nil
148+
})
149+
defer patchesGetByKey.Reset()
161150
k8sClient.EXPECT().Get(ctx, gomock.Any(), sp).Return(nil).Do(
162151
func(_ context.Context, _ client.ObjectKey, obj client.Object, option ...client.GetOption) error {
163152
v1sp := obj.(*v1alpha1.SubnetPort)

0 commit comments

Comments
 (0)