Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#632 from TaoZou1/readvpcinfo
Browse files Browse the repository at this point in the history
Get path info from store while deleting staticroute
  • Loading branch information
TaoZou1 authored Jul 15, 2024
2 parents 67a740a + 8573d72 commit 5e41468
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 20 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/staticroute/staticroute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (r *StaticRouteReconciler) Reconcile(ctx context.Context, req ctrl.Request)
if controllerutil.ContainsFinalizer(obj, commonservice.StaticRouteFinalizerName) {
metrics.CounterInc(r.Service.NSXConfig, metrics.ControllerDeleteTotal, common.MetricResTypeStaticRoute)
// TODO, update the value from 'default' to actual value, get OrgID, ProjectID, VPCID depending on obj.Namespace from vpc store
if err := r.Service.DeleteStaticRoute(req.Namespace, string(obj.UID)); err != nil {
if err := r.Service.DeleteStaticRoute(string(obj.UID)); err != nil {
log.Error(err, "delete failed, would retry exponentially", "staticroute", req.NamespacedName)
deleteFail(r, &ctx, obj, &err)
return ResultRequeue, err
Expand Down
10 changes: 5 additions & 5 deletions pkg/controllers/staticroute/staticroute_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ func TestStaticRouteReconciler_Reconcile(t *testing.T) {
return nil
})

patch := gomonkey.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, namespace string, uid string) error {
patch := gomonkey.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, uid string) error {
assert.FailNow(t, "should not be called")
return nil
})
Expand All @@ -223,7 +223,7 @@ func TestStaticRouteReconciler_Reconcile(t *testing.T) {
v1sp.Finalizers = []string{common.StaticRouteFinalizerName}
return nil
})
patch = gomonkey.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, namespace string, uid string) error {
patch = gomonkey.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, uid string) error {
return nil
})
_, ret = r.Reconcile(ctx, req)
Expand All @@ -238,7 +238,7 @@ func TestStaticRouteReconciler_Reconcile(t *testing.T) {
v1sp.Finalizers = []string{common.StaticRouteFinalizerName}
return nil
})
patch = gomonkey.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, namespace string, uid string) error {
patch = gomonkey.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, uid string) error {
return errors.New("delete failed")
})

Expand Down Expand Up @@ -333,7 +333,7 @@ func TestStaticRouteReconciler_GarbageCollector(t *testing.T) {
a = append(a, model.StaticRoutes{Id: &id, Tags: tag2})
return a
})
patch.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, namespace string, uid string) error {
patch.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, uid string) error {
assert.FailNow(t, "should not be called")
return nil
})
Expand All @@ -351,7 +351,7 @@ func TestStaticRouteReconciler_GarbageCollector(t *testing.T) {
patch.ApplyMethod(reflect.TypeOf(service), "ListStaticRoute", func(_ *staticroute.StaticRouteService) []model.StaticRoutes {
return []model.StaticRoutes{}
})
patch.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, namespace string, uid string) error {
patch.ApplyMethod(reflect.TypeOf(service), "DeleteStaticRoute", func(_ *staticroute.StaticRouteService, uid string) error {
assert.FailNow(t, "should not be called")
return nil
})
Expand Down
14 changes: 10 additions & 4 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 @@ -134,12 +135,17 @@ 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 {
func (service *StaticRouteService) DeleteStaticRoute(uid string) error {
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(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(uid)
assert.Equal(t, err, nil)
srs := returnservice.StaticRouteStore.List()
assert.Equal(t, len(srs), 0)
Expand Down

0 comments on commit 5e41468

Please sign in to comment.