@@ -25,6 +25,7 @@ import (
25
25
26
26
"kmesh.net/kmesh/api/v2/workloadapi"
27
27
"kmesh.net/kmesh/daemon/options"
28
+ "kmesh.net/kmesh/pkg/bpf"
28
29
"kmesh.net/kmesh/pkg/constants"
29
30
"kmesh.net/kmesh/pkg/controller/workload/bpfcache"
30
31
"kmesh.net/kmesh/pkg/nets"
@@ -90,6 +91,7 @@ func Test_handleWorkload(t *testing.T) {
90
91
91
92
// 3.2 check service map contains service
92
93
checkServiceMap (t , p , svcID , fakeSvc , 2 )
94
+ hashNameClean (p )
93
95
}
94
96
95
97
func checkServiceMap (t * testing.T , p * Processor , svcId uint32 , fakeSvc * workloadapi.Service , endpointCount uint32 ) {
@@ -251,3 +253,52 @@ func createFakeService(name, ip, waypoint string) *workloadapi.Service {
251
253
},
252
254
}
253
255
}
256
+
257
+ func Test_deleteWorkloadWithRestart (t * testing.T ) {
258
+ workloadMap := bpfcache .NewFakeWorkloadMap (t )
259
+ defer bpfcache .CleanupFakeWorkloadMap (workloadMap )
260
+
261
+ p := newProcessor (workloadMap )
262
+
263
+ // 1. handle workload with service, but service not handled yet
264
+ // In this case, only frontend map and backend map should be updated.
265
+ wl := createTestWorkloadWithService ()
266
+ _ = p .handleDataWithService (createTestWorkloadWithService ())
267
+
268
+ workloadID := checkFrontEndMap (t , wl .Addresses [0 ], p )
269
+ checkBackendMap (t , p , workloadID , wl )
270
+
271
+ epKeys := p .bpf .EndpointIterFindKey (workloadID )
272
+ assert .Equal (t , len (epKeys ), 0 )
273
+ for svcName := range wl .Services {
274
+ endpoints := p .endpointsByService [svcName ]
275
+ assert .Len (t , endpoints , 1 )
276
+ if _ , ok := endpoints [wl .Uid ]; ok {
277
+ assert .True (t , ok )
278
+ }
279
+ }
280
+
281
+ // Set a restart label and simulate missing data in the cache
282
+ bpf .SetStartType (bpf .Restart )
283
+ for key := range wl .GetServices () {
284
+ p .ServiceCache .DeleteService (key )
285
+ }
286
+
287
+ p .compareWorkloadAndServiceWithHashName ()
288
+ hashNameClean (p )
289
+ }
290
+
291
+ // The hashname will be saved as a file by default.
292
+ // If it is not cleaned, it will affect other use cases.
293
+ func hashNameClean (p * Processor ) {
294
+ for str := range p .hashName .strToNum {
295
+ if err := p .removeWorkloadResourceByUid (str ); err != nil {
296
+ log .Errorf ("RemoveWorkloadResource failed: %v" , err )
297
+ }
298
+
299
+ if err := p .removeServiceResourceByUid (str ); err != nil {
300
+ log .Errorf ("RemoveServiceResource failed: %v" , err )
301
+ }
302
+ p .hashName .Delete (str )
303
+ }
304
+ }
0 commit comments