-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-implemented DLB cleanup in nsx-operator
Delete DLB resources. The size of load balancer service can be, SMALL, MEDIUM, LARGE, XLARGE, or DLB. The first four sizes are realized on Edge node as a centralized load balancer. DLB is realized on each ESXi hypervisor as a distributed load balancer. Previously, this cleanup function was implemented in NCP nsx_policy_cleanup.py. Now, it is re-implemented in nsx-operator pkg/clean/. Signed-off-by: Xie Zheng <[email protected]>
- Loading branch information
1 parent
6ba6790
commit 283c1b3
Showing
11 changed files
with
122 additions
and
281 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
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
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,94 @@ | ||
package clean | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
neturl "net/url" | ||
"strings" | ||
|
||
"github.com/vmware-tanzu/nsx-operator/pkg/config" | ||
"github.com/vmware-tanzu/nsx-operator/pkg/nsx" | ||
nsxutil "github.com/vmware-tanzu/nsx-operator/pkg/nsx/util" | ||
) | ||
|
||
type ( | ||
mapInterface = map[string]interface{} | ||
) | ||
|
||
const TagDLB = "DLB" | ||
|
||
func appendIfNotExist(slice []string, s string) []string { | ||
for _, item := range slice { | ||
if item == s { | ||
return slice | ||
} | ||
} | ||
return append(slice, s) | ||
} | ||
|
||
func httpQueryDLBResources(cluster *nsx.Cluster, cf *config.NSXOperatorConfig, resource string) ([]string, error) { | ||
queryParam := "resource_type:" + resource + | ||
"&tags.scope:ncp\\/cluster" + | ||
"&tags.tag:" + cf.Cluster + | ||
"&tags.scope:ncp\\/created_for" + | ||
"&tags.tag:" + TagDLB | ||
|
||
pairs := strings.Split(queryParam, "&") | ||
params := make(map[string]string) | ||
for _, pair := range pairs { | ||
kv := strings.Split(pair, ":") | ||
if len(kv) == 2 { | ||
params[kv[0]] = kv[1] | ||
} | ||
} | ||
var encodedPairs []string | ||
for key, value := range params { | ||
encodedKey := neturl.QueryEscape(key) | ||
encodedValue := neturl.QueryEscape(value) | ||
encodedPairs = append(encodedPairs, fmt.Sprintf("%s:%s", encodedKey, encodedValue)) | ||
} | ||
|
||
encodedQuery := strings.Join(encodedPairs, "%20AND%20") | ||
url := "api/v1/search/query?query=" + encodedQuery | ||
|
||
resp, err := cluster.HttpGet(url) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var resourcePath []string | ||
for _, item := range resp["results"].([]interface{}) { | ||
resourcePath = appendIfNotExist(resourcePath, item.(mapInterface)["path"].(string)) | ||
} | ||
return resourcePath, nil | ||
} | ||
|
||
func CleanDLB(ctx context.Context, cluster *nsx.Cluster, cf *config.NSXOperatorConfig) error { | ||
log.Info("Deleting DLB resources started") | ||
|
||
resources := []string{"Group", "LBVirtualServer", "LBService", "LBPool", "LBCookiePersistenceProfile"} | ||
var allPaths []string | ||
|
||
for _, resource := range resources { | ||
paths, err := httpQueryDLBResources(cluster, cf, resource) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info(resource, "count", len(paths)) | ||
allPaths = append(allPaths, paths...) | ||
} | ||
|
||
log.Info("Deleting DLB resources", "paths", allPaths) | ||
for _, path := range allPaths { | ||
url := "api/v1" + path | ||
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
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.