Skip to content

Commit 762863a

Browse files
authored
Remove Subnet Finalizer (vmware-tanzu#769)
Remove SubnetSet Finalizer Add unit-test for Subnet controller Signed-off-by: Wenqi Qiu <[email protected]>
1 parent edb6207 commit 762863a

File tree

15 files changed

+974
-360
lines changed

15 files changed

+974
-360
lines changed

pkg/controllers/common/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func AllocateSubnetFromSubnetSet(subnetSet *v1alpha1.SubnetSet, vpcService servi
4242
return *nsxSubnet.Path, nil
4343
}
4444
}
45-
tags := subnetService.GenerateSubnetNSTags(subnetSet, subnetSet.Namespace)
45+
tags := subnetService.GenerateSubnetNSTags(subnetSet)
4646
if tags == nil {
4747
return "", errors.New("failed to generate subnet tags")
4848
}

pkg/controllers/subnet/namespace_handler.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,30 @@ import (
1818
"github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1"
1919
)
2020

21-
// Subnet controller should watch event of namespace, when there are some updates of namespace labels,
21+
// Subnet controller should watch event of Namespace, when there are some updates of Namespace labels,
2222
// controller should build tags and update VpcSubnet according to new labels.
2323

2424
type EnqueueRequestForNamespace struct {
2525
Client client.Client
2626
}
2727

2828
func (e *EnqueueRequestForNamespace) Create(_ context.Context, _ event.CreateEvent, _ workqueue.RateLimitingInterface) {
29-
log.V(1).Info("namespace create event, do nothing")
29+
log.V(1).Info("Namespace create event, do nothing")
3030
}
3131

3232
func (e *EnqueueRequestForNamespace) Delete(_ context.Context, _ event.DeleteEvent, _ workqueue.RateLimitingInterface) {
33-
log.V(1).Info("namespace delete event, do nothing")
33+
log.V(1).Info("Namespace delete event, do nothing")
3434
}
3535

3636
func (e *EnqueueRequestForNamespace) Generic(_ context.Context, _ event.GenericEvent, _ workqueue.RateLimitingInterface) {
37-
log.V(1).Info("namespace generic event, do nothing")
37+
log.V(1).Info("Namespace generic event, do nothing")
3838
}
3939

4040
func (e *EnqueueRequestForNamespace) Update(_ context.Context, updateEvent event.UpdateEvent, l workqueue.RateLimitingInterface) {
4141
obj := updateEvent.ObjectNew.(*v1.Namespace)
42-
err := reconcileSubnet(e.Client, obj.Name, l)
42+
err := requeueSubnet(e.Client, obj.Name, l)
4343
if err != nil {
44-
log.Error(err, "failed to reconcile subnet")
44+
log.Error(err, "Failed to requeue Subnet")
4545
}
4646
}
4747

@@ -52,9 +52,9 @@ var PredicateFuncsNs = predicate.Funcs{
5252
UpdateFunc: func(e event.UpdateEvent) bool {
5353
oldObj := e.ObjectOld.(*v1.Namespace)
5454
newObj := e.ObjectNew.(*v1.Namespace)
55-
log.V(1).Info("receive namespace update event", "name", oldObj.Name)
55+
log.V(1).Info("Receive Namespace update event", "Name", oldObj.Name)
5656
if reflect.DeepEqual(oldObj.ObjectMeta.Labels, newObj.ObjectMeta.Labels) {
57-
log.Info("labels of namespace are not changed", "name", oldObj.Name)
57+
log.Info("Labels of Namespace are not changed", "Name", oldObj.Name)
5858
return false
5959
}
6060
return true
@@ -64,17 +64,17 @@ var PredicateFuncsNs = predicate.Funcs{
6464
},
6565
}
6666

67-
func reconcileSubnet(c client.Client, namespace string, q workqueue.RateLimitingInterface) error {
67+
func requeueSubnet(c client.Client, ns string, q workqueue.RateLimitingInterface) error {
6868
subnetList := &v1alpha1.SubnetList{}
69-
err := c.List(context.Background(), subnetList, client.InNamespace(namespace))
69+
err := c.List(context.Background(), subnetList, client.InNamespace(ns))
7070
if err != nil {
71-
log.Error(err, "failed to list all the subnets")
71+
log.Error(err, "Failed to list all the Subnets")
7272
return err
7373
}
7474

7575
for _, subnet_item := range subnetList.Items {
76-
log.Info("reconcile subnet because namespace update",
77-
"namespace", subnet_item.Namespace, "name", subnet_item.Name)
76+
log.Info("Requeue Subnet because Namespace update",
77+
"Namespace", subnet_item.Namespace, "Name", subnet_item.Name)
7878
q.Add(reconcile.Request{
7979
NamespacedName: types.NamespacedName{
8080
Name: subnet_item.Name,
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package subnet
2+
3+
import (
4+
"context"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/mock"
10+
v1 "k8s.io/api/core/v1"
11+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12+
"sigs.k8s.io/controller-runtime/pkg/client/fake"
13+
"sigs.k8s.io/controller-runtime/pkg/event"
14+
15+
"github.com/vmware-tanzu/nsx-operator/pkg/apis/vpc/v1alpha1"
16+
)
17+
18+
type MockRateLimitingInterface struct {
19+
mock.Mock
20+
}
21+
22+
func (m *MockRateLimitingInterface) Add(item interface{}) {
23+
}
24+
25+
func (m *MockRateLimitingInterface) Len() int {
26+
return 0
27+
}
28+
29+
func (m *MockRateLimitingInterface) Get() (item interface{}, shutdown bool) {
30+
return
31+
}
32+
33+
func (m *MockRateLimitingInterface) Done(item interface{}) {
34+
return
35+
}
36+
37+
func (m *MockRateLimitingInterface) ShutDown() {
38+
}
39+
40+
func (m *MockRateLimitingInterface) ShutDownWithDrain() {
41+
}
42+
43+
func (m *MockRateLimitingInterface) ShuttingDown() bool {
44+
return true
45+
}
46+
47+
func (m *MockRateLimitingInterface) AddAfter(item interface{}, duration time.Duration) {
48+
return
49+
}
50+
51+
func (m *MockRateLimitingInterface) AddRateLimited(item interface{}) {
52+
m.Called(item)
53+
}
54+
55+
func (m *MockRateLimitingInterface) Forget(item interface{}) {
56+
m.Called(item)
57+
}
58+
59+
func (m *MockRateLimitingInterface) NumRequeues(item interface{}) int {
60+
args := m.Called(item)
61+
return args.Int(0)
62+
}
63+
64+
func TestEnqueueRequestForNamespace(t *testing.T) {
65+
fakeClient := fake.NewClientBuilder().Build()
66+
queue := new(MockRateLimitingInterface)
67+
handler := &EnqueueRequestForNamespace{Client: fakeClient}
68+
69+
t.Run("Update event with changed labels", func(t *testing.T) {
70+
oldNamespace := &v1.Namespace{
71+
ObjectMeta: metav1.ObjectMeta{
72+
Name: "test-ns",
73+
Labels: map[string]string{"key": "old"},
74+
},
75+
}
76+
newNamespace := &v1.Namespace{
77+
ObjectMeta: metav1.ObjectMeta{
78+
Name: "test-ns",
79+
Labels: map[string]string{"key": "new"},
80+
},
81+
}
82+
updateEvent := event.UpdateEvent{
83+
ObjectOld: oldNamespace,
84+
ObjectNew: newNamespace,
85+
}
86+
queue.On("Add", mock.Anything).Return()
87+
88+
handler.Update(context.Background(), updateEvent, queue)
89+
})
90+
91+
t.Run("Update event with unchanged labels", func(t *testing.T) {
92+
oldNamespace := &v1.Namespace{
93+
ObjectMeta: metav1.ObjectMeta{
94+
Name: "test-ns",
95+
Labels: map[string]string{"key": "same"},
96+
},
97+
}
98+
newNamespace := &v1.Namespace{
99+
ObjectMeta: metav1.ObjectMeta{
100+
Name: "test-ns",
101+
Labels: map[string]string{"key": "same"},
102+
},
103+
}
104+
updateEvent := event.UpdateEvent{
105+
ObjectOld: oldNamespace,
106+
ObjectNew: newNamespace,
107+
}
108+
109+
queue.On("Add", mock.Anything).Return()
110+
111+
handler.Update(context.Background(), updateEvent, queue)
112+
113+
queue.AssertNotCalled(t, "Add", mock.Anything)
114+
})
115+
116+
t.Run("Requeue subnet function", func(t *testing.T) {
117+
ns := "test-ns"
118+
119+
schem := fake.NewClientBuilder().Build().Scheme()
120+
v1alpha1.AddToScheme(schem)
121+
queue.On("Add", mock.Anything).Return()
122+
123+
err := requeueSubnet(fakeClient, ns, queue)
124+
125+
assert.NoError(t, err)
126+
queue.AssertNumberOfCalls(t, "Add", 0)
127+
})
128+
}

0 commit comments

Comments
 (0)