Skip to content

Commit 7a37b8c

Browse files
committed
ut for config update with kmesh restart
Signed-off-by: let-bit <[email protected]>
1 parent 40e4b20 commit 7a37b8c

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

pkg/controller/workload/workload_processor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -603,15 +603,15 @@ func (p *Processor) handleAddressTypeResponse(rsp *service_discovery_v3.DeltaDis
603603
}
604604

605605
_ = p.handleRemovedAddresses(rsp.RemovedResources)
606-
p.compareWorkloadAndService()
606+
p.compareWorkloadAndServiceWithHashName()
607607

608608
return err
609609
}
610610

611611
// When processing the workload's response for the first time,
612612
// fetch the data from the /mnt/workload_hash_name.yaml file
613613
// and compare it with the data in the cache.
614-
func (p *Processor) compareWorkloadAndService() {
614+
func (p *Processor) compareWorkloadAndServiceWithHashName() {
615615
var (
616616
bk = bpf.BackendKey{}
617617
bv = bpf.BackendValue{}

pkg/controller/workload/workload_processor_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"kmesh.net/kmesh/api/v2/workloadapi"
2727
"kmesh.net/kmesh/daemon/options"
28+
"kmesh.net/kmesh/pkg/bpf"
2829
"kmesh.net/kmesh/pkg/constants"
2930
"kmesh.net/kmesh/pkg/controller/workload/bpfcache"
3031
"kmesh.net/kmesh/pkg/nets"
@@ -90,6 +91,7 @@ func Test_handleWorkload(t *testing.T) {
9091

9192
// 3.2 check service map contains service
9293
checkServiceMap(t, p, svcID, fakeSvc, 2)
94+
hashNameClean(p)
9395
}
9496

9597
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 {
251253
},
252254
}
253255
}
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

Comments
 (0)