Skip to content

Commit 2b12a1d

Browse files
committed
Refine the clean up logic
1 parent 0c6e006 commit 2b12a1d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+3842
-1860
lines changed

pkg/clean/clean.go

+31-52
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ package clean
66
import (
77
"context"
88
"errors"
9-
"fmt"
109
"time"
1110

1211
"github.com/vmware-tanzu/nsx-operator/pkg/config"
@@ -24,7 +23,6 @@ import (
2423

2524
"github.com/go-logr/logr"
2625
"k8s.io/apimachinery/pkg/util/wait"
27-
"k8s.io/client-go/util/retry"
2826
)
2927

3028
var Backoff = wait.Backoff{
@@ -66,18 +64,10 @@ func Clean(ctx context.Context, cf *config.NSXOperatorConfig, log *logr.Logger,
6664
var cleanupService *CleanupService
6765
var err error
6866
go func() {
69-
cleanupService, err = InitializeCleanupService(cf, nsxClient)
67+
cleanupService, err = InitializeCleanupService(cf, nsxClient, log)
7068
errChan <- err
7169
}()
7270

73-
retriable := func(err error) bool {
74-
if err != nil && !errors.As(err, &nsxutil.TimeoutFailed) {
75-
log.Info("Retrying to clean up NSX resources", "error", err)
76-
return true
77-
}
78-
return false
79-
}
80-
8171
select {
8272
case err := <-errChan:
8373
if err != nil {
@@ -86,46 +76,27 @@ func Clean(ctx context.Context, cf *config.NSXOperatorConfig, log *logr.Logger,
8676
case <-ctx.Done():
8777
return errors.Join(nsxutil.TimeoutFailed, ctx.Err())
8878
}
89-
if cleanupService.err != nil {
90-
return errors.Join(nsxutil.InitCleanupServiceFailed, cleanupService.err)
91-
} else {
92-
for _, clean := range cleanupService.cleans {
93-
if err := retry.OnError(Backoff, retriable, wrapCleanFunc(ctx, clean)); err != nil {
94-
return errors.Join(nsxutil.CleanupResourceFailed, err)
95-
}
96-
}
79+
80+
if cleanupService.svcErr != nil {
81+
return errors.Join(nsxutil.InitCleanupServiceFailed, cleanupService.svcErr)
9782
}
98-
// delete DLB group -> delete virtual servers -> DLB services -> DLB pools -> persistent profiles for DLB
99-
if err := retry.OnError(retry.DefaultRetry, func(err error) bool {
100-
if err != nil {
101-
log.Info("Retrying to clean up DLB resources", "error", err)
102-
return true
103-
}
104-
return false
105-
}, func() error {
106-
if err := CleanDLB(ctx, nsxClient.Cluster, cf, log); err != nil {
107-
return fmt.Errorf("Failed to clean up specific resource: %w", err)
108-
}
109-
return nil
110-
}); err != nil {
111-
return err
83+
84+
cleanupService.log = log
85+
86+
if err := cleanupService.cleanupVPCResources(ctx); err != nil {
87+
return errors.Join(nsxutil.CleanupResourceFailed, err)
88+
}
89+
90+
if err := cleanupService.cleanupInfraResources(ctx); err != nil {
91+
return errors.Join(nsxutil.CleanupResourceFailed, err)
11292
}
11393

11494
log.Info("Cleanup NSX resources successfully")
11595
return nil
11696
}
11797

118-
func wrapCleanFunc(ctx context.Context, clean cleanup) func() error {
119-
return func() error {
120-
if err := clean.Cleanup(ctx); err != nil {
121-
return err
122-
}
123-
return nil
124-
}
125-
}
126-
12798
// InitializeCleanupService initializes all the CR services
128-
func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Client) (*CleanupService, error) {
99+
func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Client, log *logr.Logger) (*CleanupService, error) {
129100
cleanupService := NewCleanupService()
130101

131102
commonService := common.Service{
@@ -138,42 +109,49 @@ func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Clien
138109
// Use Fluent Interface to escape error check hell
139110

140111
wrapInitializeSubnetService := func(service common.Service) cleanupFunc {
141-
return func() (cleanup, error) {
112+
return func() (interface{}, error) {
142113
return subnet.InitializeSubnetService(service)
143114
}
144115
}
145116
wrapInitializeSecurityPolicy := func(service common.Service) cleanupFunc {
146-
return func() (cleanup, error) {
147-
return securitypolicy.InitializeSecurityPolicy(service, vpcService)
117+
return func() (interface{}, error) {
118+
return securitypolicy.InitializeSecurityPolicy(service, vpcService, true)
148119
}
149120
}
150121
wrapInitializeVPC := func(service common.Service) cleanupFunc {
151-
return func() (cleanup, error) {
122+
return func() (interface{}, error) {
152123
return vpcService, vpcErr
153124
}
154125
}
155126

156127
wrapInitializeStaticRoute := func(service common.Service) cleanupFunc {
157-
return func() (cleanup, error) {
128+
return func() (interface{}, error) {
158129
return sr.InitializeStaticRoute(service, vpcService)
159130
}
160131
}
161132

162133
wrapInitializeSubnetPort := func(service common.Service) cleanupFunc {
163-
return func() (cleanup, error) {
134+
return func() (interface{}, error) {
164135
return subnetport.InitializeSubnetPort(service)
165136
}
166137
}
167138
wrapInitializeIPAddressAllocation := func(service common.Service) cleanupFunc {
168-
return func() (cleanup, error) {
139+
return func() (interface{}, error) {
169140
return ipaddressallocation.InitializeIPAddressAllocation(service, vpcService, true)
170141
}
171142
}
172143
wrapInitializeSubnetBinding := func(service common.Service) cleanupFunc {
173-
return func() (cleanup, error) {
144+
return func() (interface{}, error) {
174145
return subnetbinding.InitializeService(service)
175146
}
176147
}
148+
wrapInitializeLBInfraCleaner := func(service common.Service) cleanupFunc {
149+
return func() (interface{}, error) {
150+
return &LBInfraCleaner{Service: service, log: log}, nil
151+
}
152+
}
153+
154+
cleanupService.vpcService = vpcService
177155
// TODO: initialize other CR services
178156
cleanupService = cleanupService.
179157
AddCleanupService(wrapInitializeSubnetPort(commonService)).
@@ -182,7 +160,8 @@ func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Clien
182160
AddCleanupService(wrapInitializeSecurityPolicy(commonService)).
183161
AddCleanupService(wrapInitializeStaticRoute(commonService)).
184162
AddCleanupService(wrapInitializeVPC(commonService)).
185-
AddCleanupService(wrapInitializeIPAddressAllocation(commonService))
163+
AddCleanupService(wrapInitializeIPAddressAllocation(commonService)).
164+
AddCleanupService(wrapInitializeLBInfraCleaner(commonService))
186165

187166
return cleanupService, nil
188167
}

pkg/clean/clean_dlb.go

-90
This file was deleted.

0 commit comments

Comments
 (0)