-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathclean.go
More file actions
228 lines (201 loc) · 8.34 KB
/
clean.go
File metadata and controls
228 lines (201 loc) · 8.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0 */
package clean
import (
"context"
"errors"
"time"
"github.com/vmware-tanzu/nsx-operator/pkg/config"
"github.com/vmware-tanzu/nsx-operator/pkg/logger"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/inventory"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/ipaddressallocation"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/nsxserviceaccount"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/securitypolicy"
sr "github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/staticroute"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnet"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnetbinding"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnetipreservation"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/subnetport"
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/vpc"
nsxutil "github.com/vmware-tanzu/nsx-operator/pkg/nsx/util"
"github.com/vmware-tanzu/nsx-operator/pkg/util"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/util/wait"
)
var Backoff = wait.Backoff{
Steps: 12,
Duration: 10 * time.Second,
Factor: 2.0,
Jitter: 0.1,
}
// Clean cleans up NSX resources,
// including security policy, static route, subnet, subnet port, subnet set, vpc, ip pool, nsx service account
// besides, it also cleans up DLB resources, which was previously implemented in nsx-ncp,
// it is usually used when nsx-operator is uninstalled and remove all the resources created by nsx-operator
// return error if any, return nil if no error
// the error type include followings:
// ValidationFailed indicate that the config is incorrect and failed to pass validation
// GetNSXClientFailed indicate that could not retrieve nsx client to perform cleanup operation
// InitCleanupServiceFailed indicate that error happened when trying to initialize cleanup service
// CleanupResourceFailed indicate that the cleanup operation failed at some services, the detailed will in the service logs
func Clean(ctx context.Context, cf *config.NSXOperatorConfig, log *logr.Logger, debug bool, logLevel int, logColor bool) error {
// Clean needs to support many instances which each have its own logger
if log == nil {
logg := logger.ZapCustomLogger(debug, logLevel, logColor).Logger
log = &logg
}
log.Info("Starting NSX cleanup")
if err := cf.ValidateConfigFromCmd(); err != nil {
return errors.Join(nsxutil.ValidationFailed, err)
}
cf.LibMode = true
nsxClient := nsx.GetClient(cf)
if nsxClient == nil {
return nsxutil.GetNSXClientFailed
}
// add timeout for initialization
errChan := make(chan error)
var cleanupService *CleanupService
var err error
go func() {
cleanupService, err = InitializeCleanupService(cf, nsxClient, log)
errChan <- err
}()
select {
case err := <-errChan:
if err != nil {
return errors.Join(nsxutil.InitCleanupServiceFailed, err)
}
case <-ctx.Done():
return errors.Join(nsxutil.TimeoutFailed, ctx.Err())
}
if cleanupService.svcErr != nil {
return errors.Join(nsxutil.InitCleanupServiceFailed, cleanupService.svcErr)
}
cleanupService.log = log
if err := cleanupService.cleanupVPCResources(ctx); err != nil {
return errors.Join(nsxutil.CleanupResourceFailed, err)
}
if err := cleanupService.cleanupInfraResources(ctx); err != nil {
return errors.Join(nsxutil.CleanupResourceFailed, err)
}
if err := cleanupService.cleanupHealthResources(ctx); err != nil {
return errors.Join(nsxutil.CleanupResourceFailed, err)
}
log.Info("Cleanup NSX resources successfully")
return nil
}
// InitializeCleanupService initializes all the CR services
func InitializeCleanupService(cf *config.NSXOperatorConfig, nsxClient *nsx.Client, log *logr.Logger) (*CleanupService, error) {
cleanupService := NewCleanupService()
commonService := common.Service{
NSXClient: nsxClient,
NSXConfig: cf,
}
vpcService, vpcErr := vpc.InitializeVPC(commonService)
ipAddressAllocationService, err := ipaddressallocation.InitializeIPAddressAllocation(commonService, vpcService, true)
if err != nil {
return nil, err
}
// initialize all the CR services
// Use Fluent Interface to escape error check hell
wrapInitializeSubnetService := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return subnet.InitializeSubnetService(service)
}
}
wrapInitializeSecurityPolicy := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return securitypolicy.InitializeSecurityPolicy(service, vpcService, true)
}
}
wrapInitializeVPC := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return vpcService, vpcErr
}
}
wrapInitializeStaticRoute := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return sr.InitializeStaticRoute(service, vpcService)
}
}
wrapInitializeSubnetPort := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return subnetport.InitializeSubnetPort(service, vpcService, ipAddressAllocationService)
}
}
wrapInitializeIPAddressAllocation := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return ipAddressAllocationService, nil
}
}
wrapInitializeSubnetBinding := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return subnetbinding.InitializeService(service)
}
}
wrapInitializeSubnetIPReservation := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return subnetipreservation.InitializeService(service)
}
}
wrapInitializeInventory := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return inventory.InitializeService(service, true)
}
}
wrapInitializeLBInfraCleaner := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return &LBInfraCleaner{Service: service, log: log}, nil
}
}
wrapInitializeHealthCleaner := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
clusterUUID := util.GetClusterUUID(cf.Cluster).String()
return NewHealthCleaner(service, log, nsxClient, clusterUUID), nil
}
}
wrapInitializeNSXServiceAccount := func(service common.Service) cleanupFunc {
return func() (interface{}, error) {
return nsxserviceaccount.InitializeNSXServiceAccount(service)
}
}
cleanupService.vpcService = vpcService
// TODO: initialize other CR services
loggedAdd := func(name string, f cleanupFunc) {
if cleanupService.svcErr != nil {
log.Info("Skipping service initialization due to previous error", "service", name, "error", cleanupService.svcErr)
return
}
log.Info("Initializing cleanup service", "service", name)
cleanupService = cleanupService.AddCleanupService(f)
if cleanupService.svcErr != nil {
log.Error(cleanupService.svcErr, "Service initialization FAILED", "service", name)
} else {
log.Info("Service initialization succeeded", "service", name)
}
}
loggedAdd("SubnetPort", wrapInitializeSubnetPort(commonService))
loggedAdd("SubnetBinding", wrapInitializeSubnetBinding(commonService))
loggedAdd("SubnetIPReservation", wrapInitializeSubnetIPReservation(commonService))
loggedAdd("SubnetService", wrapInitializeSubnetService(commonService))
loggedAdd("SecurityPolicy", wrapInitializeSecurityPolicy(commonService))
loggedAdd("StaticRoute", wrapInitializeStaticRoute(commonService))
loggedAdd("VPC", wrapInitializeVPC(commonService))
loggedAdd("IPAddressAllocation", wrapInitializeIPAddressAllocation(commonService))
loggedAdd("Inventory", wrapInitializeInventory(commonService))
loggedAdd("LBInfraCleaner", wrapInitializeLBInfraCleaner(commonService))
loggedAdd("HealthCleaner", wrapInitializeHealthCleaner(commonService))
loggedAdd("NSXServiceAccount", wrapInitializeNSXServiceAccount(commonService))
log.Info("Cleanup service initialization summary",
"vpcPreCleaners", len(cleanupService.vpcPreCleaners),
"vpcChildrenCleaners", len(cleanupService.vpcChildrenCleaners),
"infraCleaners", len(cleanupService.infraCleaners),
"healthCleaners", len(cleanupService.healthCleaners))
if cleanupService.svcErr != nil {
log.Error(cleanupService.svcErr, "Service initialization error detected - some cleaners may not be registered")
}
return cleanupService, nil
}