Skip to content

Commit

Permalink
Merge pull request vmware-tanzu#570 from zhengxiexie/zhengxie/clean_a…
Browse files Browse the repository at this point in the history
…vi_subnetports

Add clean avi subnet ports before deleting vpc
  • Loading branch information
zhengxiexie authored May 11, 2024
2 parents 7291f03 + 465079a commit d0d1eff
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
58 changes: 58 additions & 0 deletions pkg/nsx/services/vpc/clean_avi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package vpc

import (
"context"
"errors"

mapset "github.com/deckarep/golang-set"

"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
nsxutil "github.com/vmware-tanzu/nsx-operator/pkg/nsx/util"
)

type (
mapInterface = map[string]interface{}
)

const (
PolicyAPI = "policy/api/v1"
AviSubnetPortsPathSuffix = "/subnets/_AVI_SUBNET--LB/ports/"
)

func httpGetAviPortsPaths(cluster *nsx.Cluster, vpcPath string) (mapset.Set, error) {
aviSubnetPortsPath := vpcPath + AviSubnetPortsPathSuffix
url := PolicyAPI + aviSubnetPortsPath

resp, err := cluster.HttpGet(url)
if err != nil {
return nil, err
}
aviPathSet := mapset.NewSet()
for _, item := range resp["results"].([]interface{}) {
aviPathSet.Add(item.(mapInterface)["path"].(string))
}
return aviPathSet, nil
}

func CleanAviSubnetPorts(ctx context.Context, cluster *nsx.Cluster, vpcPath string) error {
log.Info("Deleting Avi subnetports started")

allPaths, err := httpGetAviPortsPaths(cluster, vpcPath)
if err != nil {
return err
}

log.Info("Deleting Avi subnetport", "paths", allPaths)
for _, path := range allPaths.ToSlice() {
url := PolicyAPI + path.(string)
select {
case <-ctx.Done():
return errors.Join(nsxutil.TimeoutFailed, ctx.Err())
default:
if err := cluster.HttpDelete(url); err != nil {
return err
}
}
}
return nil
}
4 changes: 4 additions & 0 deletions pkg/nsx/services/vpc/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,10 @@ func (s *VPCService) Cleanup(ctx context.Context) error {
case <-ctx.Done():
return errors.Join(nsxutil.TimeoutFailed, ctx.Err())
default:
// first clean avi subnet ports, or else vpc delete will fail
if err := CleanAviSubnetPorts(ctx, s.NSXClient.Cluster, *vpc.Path); err != nil {
return err
}
if err := s.DeleteVPC(*vpc.Path); err != nil {
return err
}
Expand Down

0 comments on commit d0d1eff

Please sign in to comment.