forked from vmware-tanzu/nsx-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request vmware-tanzu#570 from zhengxiexie/zhengxie/clean_a…
…vi_subnetports Add clean avi subnet ports before deleting vpc
- Loading branch information
Showing
2 changed files
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters