From 95f012867c78a5339b63d400e46a50be3714240c Mon Sep 17 00:00:00 2001 From: Sean Pang Date: Mon, 14 Aug 2023 13:49:20 +0800 Subject: [PATCH] Fix errors when deleting ip block from ipblock store --- pkg/nsx/services/vpc/store.go | 16 ++++++++++++++++ pkg/nsx/services/vpc/vpc.go | 15 +++++++-------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/pkg/nsx/services/vpc/store.go b/pkg/nsx/services/vpc/store.go index 552b15d60..317d1da9c 100644 --- a/pkg/nsx/services/vpc/store.go +++ b/pkg/nsx/services/vpc/store.go @@ -124,3 +124,19 @@ func (vs *VPCStore) GetByKey(key string) *model.Vpc { } return nil } + +func (is *IPBlockStore) GetByIndexAndPath(index string, value string, path string) *model.IpAddressBlock { + indexResults, err := is.ResourceStore.Indexer.ByIndex(index, value) + if err != nil { + log.Error(err, "failed to get obj by index", "index", value) + return nil + } + + for _, item := range indexResults { + block := item.(model.IpAddressBlock) + if *block.Path == path { + return &block + } + } + return nil +} diff --git a/pkg/nsx/services/vpc/vpc.go b/pkg/nsx/services/vpc/vpc.go index 2c60d4777..d666b2150 100644 --- a/pkg/nsx/services/vpc/vpc.go +++ b/pkg/nsx/services/vpc/vpc.go @@ -188,14 +188,13 @@ func (service *VPCService) DeleteIPBlock(vpc model.Vpc) error { vpcCRUid = *tag.Tag } } - log.Info("search ip block from store using index", "index", common.TagScopeVPCCRUID, "value", vpcCRUid) - // TODO: bugfix, seems using vpc cr uid can not get the ip blocks from cache, need further checking - ipblocks := service.IpblockStore.GetByIndex(common.TagScopeVPCCRUID, vpcCRUid) - if ipblocks != nil && len(ipblocks) != 0 { - log.Info("deleting ip blocks", "IPBlock", ipblocks[0]) - b := ipblocks[0].(model.IpAddressBlock) - b.MarkedForDelete = &MarkedForDelete - service.IpblockStore.Operate(&ipblocks[0]) + log.Info("search ip block from store using index and path", "index", common.TagScopeVPCCRUID, "value", vpcCRUid, "path", block) + // using index vpc cr id may get multiple ipblocks, add path to filter the correct one + ipblock := service.IpblockStore.GetByIndexAndPath(common.TagScopeVPCCRUID, vpcCRUid, block) + if ipblock != nil { + log.Info("deleting ip blocks", "IPBlock", ipblock) + ipblock.MarkedForDelete = &MarkedForDelete + service.IpblockStore.Operate(ipblock) } } log.Info("successfully deleted all ip blocks")