@@ -20,6 +20,7 @@ import (
2020 kubeinformers "k8s.io/client-go/informers"
2121 k8sfake "k8s.io/client-go/kubernetes/fake"
2222 core "k8s.io/client-go/testing"
23+ "k8s.io/client-go/tools/cache"
2324)
2425
2526type netPolFixture struct {
@@ -165,13 +166,22 @@ func addNetPol(t *testing.T, f *netPolFixture, netPolObj *networkingv1.NetworkPo
165166 f .netPolController .processNextWorkItem ()
166167}
167168
168- func deleteNetPol (t * testing.T , f * netPolFixture , netPolObj * networkingv1.NetworkPolicy ) {
169+ func deleteNetPol (t * testing.T , f * netPolFixture , netPolObj * networkingv1.NetworkPolicy , isDeletedFinalStateUnknownObject IsDeletedFinalStateUnknownObject ) {
169170 addNetPol (t , f , netPolObj )
170171 t .Logf ("Complete adding network policy event" )
171172
172173 // simulate network policy deletion event and delete network policy object from sharedInformer cache
173174 f .kubeInformer .Networking ().V1 ().NetworkPolicies ().Informer ().GetIndexer ().Delete (netPolObj )
174- f .netPolController .deleteNetworkPolicy (netPolObj )
175+ if isDeletedFinalStateUnknownObject {
176+ netPolKey := getKey (netPolObj , t )
177+ tombstone := cache.DeletedFinalStateUnknown {
178+ Key : netPolKey ,
179+ Obj : netPolObj ,
180+ }
181+ f .netPolController .deleteNetworkPolicy (tombstone )
182+ } else {
183+ f .netPolController .deleteNetworkPolicy (netPolObj )
184+ }
175185
176186 if f .netPolController .workqueue .Len () == 0 {
177187 f .isEnqueueEventIntoWorkQueue = false
@@ -305,13 +315,54 @@ func TestDeleteNetworkPolicy(t *testing.T) {
305315 defer close (stopCh )
306316 f .newNetPolController (stopCh )
307317
308- deleteNetPol (t , f , netPolObj )
318+ deleteNetPol (t , f , netPolObj , DeletedFinalStateknownObject )
309319 testCases := []expectedNetPolValues {
310320 {1 , 0 , 0 , false , true , 0 , nil , 1 , nil },
311321 }
312322 checkNetPolTestResult ("TestDelNetPol" , f , testCases )
313323}
314324
325+ func TestDeleteNetworkPolicyWithTombstone (t * testing.T ) {
326+ netPolObj := createNetPol ()
327+
328+ f := newNetPolFixture (t )
329+ f .isEnqueueEventIntoWorkQueue = false
330+ f .netPolLister = append (f .netPolLister , netPolObj )
331+ f .kubeobjects = append (f .kubeobjects , netPolObj )
332+ stopCh := make (chan struct {})
333+ defer close (stopCh )
334+ f .newNetPolController (stopCh )
335+
336+ netPolKey := getKey (netPolObj , t )
337+ tombstone := cache.DeletedFinalStateUnknown {
338+ Key : netPolKey ,
339+ Obj : netPolObj ,
340+ }
341+
342+ f .netPolController .deleteNetworkPolicy (tombstone )
343+ testCases := []expectedNetPolValues {
344+ {1 , 0 , 0 , false , false , 0 , nil , 0 , nil },
345+ }
346+ checkNetPolTestResult ("TestDeleteNetworkPolicyWithTombstone" , f , testCases )
347+ }
348+
349+ func TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy (t * testing.T ) {
350+ netPolObj := createNetPol ()
351+
352+ f := newNetPolFixture (t )
353+ f .netPolLister = append (f .netPolLister , netPolObj )
354+ f .kubeobjects = append (f .kubeobjects , netPolObj )
355+ stopCh := make (chan struct {})
356+ defer close (stopCh )
357+ f .newNetPolController (stopCh )
358+
359+ deleteNetPol (t , f , netPolObj , DeletedFinalStateUnknownObject )
360+ testCases := []expectedNetPolValues {
361+ {1 , 0 , 0 , false , true , 0 , nil , 1 , nil },
362+ }
363+ checkNetPolTestResult ("TestDeleteNetworkPolicyWithTombstoneAfterAddingNetworkPolicy" , f , testCases )
364+ }
365+
315366// this unit test is for the case where states of network policy are changed, but network policy controller does not need to reconcile.
316367// Check it with expectedEnqueueEventIntoWorkQueue variable.
317368func TestUpdateNetworkPolicy (t * testing.T ) {
0 commit comments