@@ -6,7 +6,6 @@ package clean
6
6
import (
7
7
"context"
8
8
"errors"
9
- "fmt"
10
9
"time"
11
10
12
11
"github.com/vmware-tanzu/nsx-operator/pkg/config"
@@ -24,7 +23,6 @@ import (
24
23
25
24
"github.com/go-logr/logr"
26
25
"k8s.io/apimachinery/pkg/util/wait"
27
- "k8s.io/client-go/util/retry"
28
26
)
29
27
30
28
var Backoff = wait.Backoff {
@@ -66,18 +64,10 @@ func Clean(ctx context.Context, cf *config.NSXOperatorConfig, log *logr.Logger,
66
64
var cleanupService * CleanupService
67
65
var err error
68
66
go func () {
69
- cleanupService , err = InitializeCleanupService (cf , nsxClient )
67
+ cleanupService , err = InitializeCleanupService (cf , nsxClient , log )
70
68
errChan <- err
71
69
}()
72
70
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
-
81
71
select {
82
72
case err := <- errChan :
83
73
if err != nil {
@@ -86,46 +76,27 @@ func Clean(ctx context.Context, cf *config.NSXOperatorConfig, log *logr.Logger,
86
76
case <- ctx .Done ():
87
77
return errors .Join (nsxutil .TimeoutFailed , ctx .Err ())
88
78
}
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 )
97
82
}
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 )
112
92
}
113
93
114
94
log .Info ("Cleanup NSX resources successfully" )
115
95
return nil
116
96
}
117
97
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
-
127
98
// 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 ) {
129
100
cleanupService := NewCleanupService ()
130
101
131
102
commonService := common.Service {
@@ -138,42 +109,49 @@ func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Clien
138
109
// Use Fluent Interface to escape error check hell
139
110
140
111
wrapInitializeSubnetService := func (service common.Service ) cleanupFunc {
141
- return func () (cleanup , error ) {
112
+ return func () (interface {} , error ) {
142
113
return subnet .InitializeSubnetService (service )
143
114
}
144
115
}
145
116
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 )
148
119
}
149
120
}
150
121
wrapInitializeVPC := func (service common.Service ) cleanupFunc {
151
- return func () (cleanup , error ) {
122
+ return func () (interface {} , error ) {
152
123
return vpcService , vpcErr
153
124
}
154
125
}
155
126
156
127
wrapInitializeStaticRoute := func (service common.Service ) cleanupFunc {
157
- return func () (cleanup , error ) {
128
+ return func () (interface {} , error ) {
158
129
return sr .InitializeStaticRoute (service , vpcService )
159
130
}
160
131
}
161
132
162
133
wrapInitializeSubnetPort := func (service common.Service ) cleanupFunc {
163
- return func () (cleanup , error ) {
134
+ return func () (interface {} , error ) {
164
135
return subnetport .InitializeSubnetPort (service )
165
136
}
166
137
}
167
138
wrapInitializeIPAddressAllocation := func (service common.Service ) cleanupFunc {
168
- return func () (cleanup , error ) {
139
+ return func () (interface {} , error ) {
169
140
return ipaddressallocation .InitializeIPAddressAllocation (service , vpcService , true )
170
141
}
171
142
}
172
143
wrapInitializeSubnetBinding := func (service common.Service ) cleanupFunc {
173
- return func () (cleanup , error ) {
144
+ return func () (interface {} , error ) {
174
145
return subnetbinding .InitializeService (service )
175
146
}
176
147
}
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
177
155
// TODO: initialize other CR services
178
156
cleanupService = cleanupService .
179
157
AddCleanupService (wrapInitializeSubnetPort (commonService )).
@@ -182,7 +160,8 @@ func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Clien
182
160
AddCleanupService (wrapInitializeSecurityPolicy (commonService )).
183
161
AddCleanupService (wrapInitializeStaticRoute (commonService )).
184
162
AddCleanupService (wrapInitializeVPC (commonService )).
185
- AddCleanupService (wrapInitializeIPAddressAllocation (commonService ))
163
+ AddCleanupService (wrapInitializeIPAddressAllocation (commonService )).
164
+ AddCleanupService (wrapInitializeLBInfraCleaner (commonService ))
186
165
187
166
return cleanupService , nil
188
167
}
0 commit comments