Skip to content

Commit

Permalink
Get path info from store while deleting staticroute
Browse files Browse the repository at this point in the history
In previous version, controller tried to get org/project/vpc info from
vpc service. But vpc service may delete vpc before staticroute.

Read org/project/vpc from staticroute path
  • Loading branch information
TaoZou1 committed Jul 10, 2024
1 parent 4bc8285 commit 2073e82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
12 changes: 9 additions & 3 deletions pkg/nsx/services/staticroute/staticroute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/vmware-tanzu/nsx-operator/pkg/logger"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
nsxutil "github.com/vmware-tanzu/nsx-operator/pkg/nsx/util"
"github.com/vmware-tanzu/nsx-operator/pkg/util"
)

type StaticRouteService struct {
Expand Down Expand Up @@ -135,11 +136,16 @@ func (service *StaticRouteService) GetUID(staticroute *model.StaticRoutes) *stri
}

func (service *StaticRouteService) DeleteStaticRoute(namespace string, uid string) error {
vpc := service.VPCService.ListVPCInfo(namespace)
if len(vpc) == 0 {
id := String(util.GenerateID(uid, "sr", "", ""))
staticroute := service.StaticRouteStore.GetByKey(*id)
if staticroute == nil {
return nil
}
return service.DeleteStaticRouteByPath(vpc[0].OrgID, vpc[0].ProjectID, vpc[0].ID, uid)
vpcResourceInfo, err := common.ParseVPCResourcePath(*staticroute.Path)
if err != nil {
return err
}
return service.DeleteStaticRouteByPath(vpcResourceInfo.OrgID, vpcResourceInfo.ProjectID, vpcResourceInfo.ID, *id)
}

func (service *StaticRouteService) ListStaticRoute() []*model.StaticRoutes {
Expand Down
16 changes: 6 additions & 10 deletions pkg/nsx/services/staticroute/staticroute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,21 @@ func TestStaticRouteService_DeleteStaticRoute(t *testing.T) {
t.Error(err)
}

id := "vpc-1"
sr1 := &model.StaticRoutes{Id: &id}
uid := "uid-123"
id := "sr_uid-123"
path := "/orgs/default/projects/project-1/vpcs/vpc-1"
sr1 := &model.StaticRoutes{Id: &id, Path: &path}

returnservice.StaticRouteStore.Add(sr1)

patches := gomonkey.ApplyMethod(reflect.TypeOf(returnservice.VPCService), "ListVPCInfo", func(_ common.VPCServiceProvider, ns string) []common.VPCResourceInfo {
id := "vpc-1"
return []common.VPCResourceInfo{{OrgID: "default", ProjectID: "project-1", VPCID: "vpc-1", ID: id}}
})

// no record found
mockStaticRouteclient.EXPECT().Delete(mock.Anything, mock.Anything, mock.Anything, mock.Anything).Times(0)
err = returnservice.DeleteStaticRoute("123", "vpc1")
err = returnservice.DeleteStaticRoute("123", id)
assert.Equal(t, err, nil)
defer patches.Reset()

// delete record
mockStaticRouteclient.EXPECT().Delete("default", "project-1", "vpc-1", id).Return(nil).Times(1)
err = returnservice.DeleteStaticRoute("123", id)
err = returnservice.DeleteStaticRoute("123", uid)
assert.Equal(t, err, nil)
srs := returnservice.StaticRouteStore.List()
assert.Equal(t, len(srs), 0)
Expand Down

0 comments on commit 2073e82

Please sign in to comment.