From 52573c87a4679544aedeb17feaf0a8d08584edc9 Mon Sep 17 00:00:00 2001 From: Xie Zheng Date: Thu, 22 Aug 2024 16:03:03 +0800 Subject: [PATCH] Fix delete ipaddressallocation bug Fix this issue that if failed to create ipaddressallocation, to delete the stale also failed, for the reason that can't find the resource. Besides, also update patching error including getting error info to status. Signed-off-by: Xie Zheng --- .../services/ipaddressallocation/compare.go | 11 +++++---- .../ipaddressallocation.go | 24 ++++++++++--------- pkg/nsx/services/ipaddressallocation/store.go | 2 +- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/pkg/nsx/services/ipaddressallocation/compare.go b/pkg/nsx/services/ipaddressallocation/compare.go index d490986d8..acec5441c 100644 --- a/pkg/nsx/services/ipaddressallocation/compare.go +++ b/pkg/nsx/services/ipaddressallocation/compare.go @@ -13,12 +13,15 @@ type ( type Comparable = common.Comparable -func (iap *IpAddressAllocation) Key() string { - return *iap.Id +func (ipa *IpAddressAllocation) Key() string { + return *ipa.Id } -func (iap *IpAddressAllocation) Value() data.DataValue { - s := &IpAddressAllocation{Id: iap.Id, DisplayName: iap.DisplayName, Tags: iap.Tags, AllocationSize: iap.AllocationSize, IpAddressBlockVisibility: iap.IpAddressBlockVisibility} +func (ipa *IpAddressAllocation) Value() data.DataValue { + if ipa == nil { + return nil + } + s := &IpAddressAllocation{Id: ipa.Id, DisplayName: ipa.DisplayName, Tags: ipa.Tags, AllocationSize: ipa.AllocationSize, IpAddressBlockVisibility: ipa.IpAddressBlockVisibility} dataValue, _ := ComparableToIpAddressAllocation(s).GetDataValue__() return dataValue } diff --git a/pkg/nsx/services/ipaddressallocation/ipaddressallocation.go b/pkg/nsx/services/ipaddressallocation/ipaddressallocation.go index 376d2793c..a5cf749f4 100644 --- a/pkg/nsx/services/ipaddressallocation/ipaddressallocation.go +++ b/pkg/nsx/services/ipaddressallocation/ipaddressallocation.go @@ -98,30 +98,32 @@ func (service *IPAddressAllocationService) CreateOrUpdateIPAddressAllocation(obj func (service *IPAddressAllocationService) Apply(nsxIPAddressAllocation *model.VpcIpAddressAllocation) error { ns := service.GetIPAddressAllocationNamespace(nsxIPAddressAllocation) - var err error VPCInfo := service.VPCService.ListVPCInfo(ns) if len(VPCInfo) == 0 { - err = util.NoEffectiveOption{Desc: "no valid org and project for ipaddressallocation"} + err := util.NoEffectiveOption{Desc: "no valid org and project for ipaddressallocation"} return err } - err = service.NSXClient.IPAddressAllocationClient.Patch(VPCInfo[0].OrgID, VPCInfo[0].ProjectID, VPCInfo[0].ID, *nsxIPAddressAllocation.Id, *nsxIPAddressAllocation) - err = util.NSXApiError(err) - if err != nil { + errPatch := service.NSXClient.IPAddressAllocationClient.Patch(VPCInfo[0].OrgID, VPCInfo[0].ProjectID, VPCInfo[0].ID, *nsxIPAddressAllocation.Id, *nsxIPAddressAllocation) + errPatch = util.NSXApiError(errPatch) + if errPatch != nil { // not return err, try to get it from nsx, in case if cidr not realized at the first time // so it can be patched in the next time and reacquire cidr - log.Error(err, "patch failed, try to get it from nsx", "nsxIPAddressAllocation", nsxIPAddressAllocation) + log.Error(errPatch, "patch failed, try to get it from nsx", "nsxIPAddressAllocation", nsxIPAddressAllocation) } // get back from nsx, it contains path which is used to parse vpc info when deleting - nsxIPAddressAllocationNew, err := service.NSXClient.IPAddressAllocationClient.Get(VPCInfo[0].OrgID, VPCInfo[0].ProjectID, VPCInfo[0].ID, *nsxIPAddressAllocation.Id) - err = util.NSXApiError(err) - if err != nil { - return err + nsxIPAddressAllocationNew, errGet := service.NSXClient.IPAddressAllocationClient.Get(VPCInfo[0].OrgID, VPCInfo[0].ProjectID, VPCInfo[0].ID, *nsxIPAddressAllocation.Id) + errGet = util.NSXApiError(errGet) + if errGet != nil { + if errPatch != nil { + return fmt.Errorf("error get %s, error patch %s", errGet.Error(), errPatch.Error()) + } + return errGet } if nsxIPAddressAllocationNew.AllocationIps == nil { err := fmt.Errorf("cidr not realized yet") return err } - err = service.ipAddressAllocationStore.Apply(&nsxIPAddressAllocationNew) + err := service.ipAddressAllocationStore.Apply(&nsxIPAddressAllocationNew) if err != nil { return err } diff --git a/pkg/nsx/services/ipaddressallocation/store.go b/pkg/nsx/services/ipaddressallocation/store.go index 5b0047374..e0efdcdac 100644 --- a/pkg/nsx/services/ipaddressallocation/store.go +++ b/pkg/nsx/services/ipaddressallocation/store.go @@ -84,7 +84,7 @@ func (ipAddressAllocationStore *IPAddressAllocationStore) GetByIndex(uid types.U nsxIPAddressAllocation = t } else { log.Info("did not get ipaddressallocation with index", "UID", string(uid)) - return nsxIPAddressAllocation, nil + return nil, nil } return nsxIPAddressAllocation, nil }