Skip to content

Commit 3886c9f

Browse files
committed
Fix: Move share cleanup to CleanupBeforeVPCDeletion phase to unblock VPC deletion
- Add CleanupBeforeVPCDeletion to SecurityPolicyService and LBInfraCleaner - Remove share cleanup from CleanupInfraResources phase Change-Id: Ib6814933c24252eba19483ec3d2abd27fbafa7de
1 parent 372acca commit 3886c9f

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed

pkg/clean/clean_lb_infra.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,28 @@ func keyFunc(obj interface{}) (string, error) {
5353
}
5454
}
5555

56+
// CleanupBeforeVPCDeletion cleans up LB-related shares before VPC deletion
57+
func (s *LBInfraCleaner) CleanupBeforeVPCDeletion(ctx context.Context) error {
58+
s.log.Info("Cleaning up LB infra shares before VPC deletion")
59+
60+
// Clean up shares
61+
if err := s.cleanupInfraShares(ctx); err != nil {
62+
s.log.Error(err, "Failed to clean up infra shares")
63+
return err
64+
}
65+
66+
s.log.Info("Successfully cleaned up LB infra shares")
67+
return nil
68+
}
69+
5670
// CleanupInfraResources is to clean up the LB related resources created under path /infra, including,
57-
// group/share/cert/LBAppProfile/LBPersistentProfile/dlb virtual servers/dlb services/dlb groups/dlb pools
71+
// cert/LBAppProfile/LBPersistentProfile/dlb virtual servers/dlb services/dlb groups/dlb pools
5872
func (s *LBInfraCleaner) CleanupInfraResources(ctx context.Context) error {
5973
// LB virtual server has dependencies on LB pool, so we can't delete vs and pool in parallel.
6074
if err := s.cleanupInfraDLBVirtualServers(ctx); err != nil {
6175
s.log.Error(err, "Failed to clean up DLB virtual servers")
6276
return err
6377
}
64-
if err := s.cleanupInfraShares(ctx); err != nil {
65-
s.log.Error(err, "Failed to clean up infra Shareds")
66-
return err
67-
}
6878

6979
parallelCleaners := []func(ctx context.Context) error{
7080
s.cleanupInfraDLBPools,

pkg/clean/clean_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ func TestInitializeCleanupService_Success(t *testing.T) {
213213
cleanupService, err := InitializeCleanupService(cf, nsxClient, &log)
214214
assert.NoError(t, err)
215215
assert.NotNil(t, cleanupService)
216-
assert.Len(t, cleanupService.vpcPreCleaners, 6)
216+
// vpcPreCleaners: SubnetPort, SubnetBinding, SubnetIPReservation, Inventory, SecurityPolicy, LBInfraCleaner, NSXServiceAccount, HealthCleaner = 8
217+
assert.Len(t, cleanupService.vpcPreCleaners, 8)
217218
assert.Len(t, cleanupService.vpcChildrenCleaners, 5)
218219
assert.Len(t, cleanupService.infraCleaners, 2)
219220
}
@@ -256,7 +257,8 @@ func TestInitializeCleanupService_VPCError(t *testing.T) {
256257
assert.NotNil(t, cleanupService)
257258
// Note, the services added after VPCService should fail because of the error returned in `InitializeVPC`.
258259
assert.Len(t, cleanupService.vpcChildrenCleaners, 3)
259-
assert.Len(t, cleanupService.vpcPreCleaners, 3)
260+
// vpcPreCleaners: SubnetPort, SubnetBinding, SubnetIPReservation, SecurityPolicy = 4 (services initialized before VPC error)
261+
assert.Len(t, cleanupService.vpcPreCleaners, 4)
260262
assert.Len(t, cleanupService.infraCleaners, 1)
261263
assert.Equal(t, expectedError, cleanupService.svcErr)
262264
}

pkg/nsx/services/securitypolicy/cleanup.go

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,49 @@ import (
99
"github.com/vmware-tanzu/nsx-operator/pkg/nsx/services/common"
1010
)
1111

12+
// CleanupBeforeVPCDeletion cleans up SecurityPolicy, Rules, and Shares before VPC deletion to avoid dependency issues.
13+
// SecurityPolicy and Rules must be deleted first because Shares cannot be deleted while they are still being consumed.
14+
func (service *SecurityPolicyService) CleanupBeforeVPCDeletion(ctx context.Context) error {
15+
log.Info("Cleaning up security policies, rules, and shares before VPC deletion")
16+
17+
if err := service.cleanupRulesByVPC(ctx, ""); err != nil {
18+
log.Error(err, "Failed to clean up rules")
19+
return err
20+
}
21+
log.Info("Successfully cleaned all rules")
22+
23+
if err := service.cleanupSecurityPoliciesByVPC(ctx, ""); err != nil {
24+
log.Error(err, "Failed to clean up security policies")
25+
return err
26+
}
27+
log.Info("Successfully cleaned all security policies")
28+
29+
// Step 3: Clean both project and infra shares
30+
for _, config := range []struct {
31+
store *ShareStore
32+
builder *common.PolicyTreeBuilder[*model.Share]
33+
name string
34+
}{
35+
{
36+
store: service.projectShareStore,
37+
builder: service.projectShareBuilder,
38+
name: "project",
39+
}, {
40+
store: service.infraShareStore,
41+
builder: service.infraShareBuilder,
42+
name: "infra",
43+
},
44+
} {
45+
if err := cleanShares(ctx, config.store, config.builder, service.NSXClient); err != nil {
46+
log.Error(err, "Failed to clean shares", "type", config.name)
47+
return err
48+
}
49+
log.Info("Successfully cleaned shares", "type", config.name)
50+
}
51+
52+
return nil
53+
}
54+
1255
// CleanupVPCChildResources is called when cleaning up the VPC related resources. For the resources in an auto-created
1356
// VPC, this function is called after the VPC is deleted on NSX, so the provider only needs to clean up with the local
1457
// cache. For the resources in a pre-created VPC, this function is called to delete resources on NSX and in the local cache.
@@ -105,22 +148,6 @@ func (service *SecurityPolicyService) cleanupGroupsByVPC(ctx context.Context, vp
105148

106149
// CleanupInfraResources is to clean up the resources created by SecurityPolicyService under path /infra.
107150
func (service *SecurityPolicyService) CleanupInfraResources(ctx context.Context) error {
108-
for _, config := range []struct {
109-
store *ShareStore
110-
builder *common.PolicyTreeBuilder[*model.Share]
111-
}{
112-
{
113-
store: service.projectShareStore,
114-
builder: service.projectShareBuilder,
115-
}, {
116-
store: service.infraShareStore,
117-
builder: service.infraShareBuilder,
118-
},
119-
} {
120-
if err := cleanShares(ctx, config.store, config.builder, service.NSXClient); err != nil {
121-
return err
122-
}
123-
}
124151
for _, config := range []struct {
125152
store *GroupStore
126153
builder *common.PolicyTreeBuilder[*model.Group]

pkg/nsx/services/securitypolicy/cleanup_test.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ import (
1919

2020
var (
2121
projectPath = "/orgs/default/projects/project-1"
22-
infraShareId = "infra-share"
23-
projectShareId = "proj-share"
2422
infraGroupId = "infra-group"
2523
projectGroupId = "proj-group"
2624
vpcPath = fmt.Sprintf("%s/vpcs/vpc-1", projectPath)
@@ -46,22 +44,6 @@ func TestCleanupInfraResources(t *testing.T) {
4644
orgRootClient := mock_org_root.NewMockOrgRootClient(ctrl)
4745
svc := prepareServiceForCleanup(orgRootClient)
4846

49-
infraShare := &model.Share{
50-
Id: String(infraShareId),
51-
Path: String(fmt.Sprintf("/infra/shares/%s", infraShareId)),
52-
ParentPath: String("/infra"),
53-
Tags: infraResourceTags,
54-
}
55-
svc.infraShareStore.Add(infraShare)
56-
57-
projectShare := &model.Share{
58-
Id: String(projectShareId),
59-
Path: String(fmt.Sprintf("%s/shares/%s", projectPath, infraShareId)),
60-
ParentPath: String(projectPath),
61-
Tags: infraResourceTags,
62-
}
63-
svc.projectShareStore.Add(projectShare)
64-
6547
infraGroup := &model.Group{
6648
Id: String(infraGroupId),
6749
Path: String(fmt.Sprintf("/infra/domains/default/groups/%s", infraGroupId)),
@@ -78,25 +60,23 @@ func TestCleanupInfraResources(t *testing.T) {
7860
}
7961
svc.projectGroupStore.Add(projectGroup)
8062

81-
assert.Equal(t, 1, len(svc.infraShareStore.List()))
8263
assert.Equal(t, 1, len(svc.infraGroupStore.List()))
8364
assert.Equal(t, 1, len(svc.projectGroupStore.List()))
84-
assert.Equal(t, 1, len(svc.projectShareStore.List()))
8565

66+
// infraGroupBuilder uses InfraClient.Patch
8667
patches := gomonkey.ApplyMethodSeq(svc.NSXClient.InfraClient, "Patch", []gomonkey.OutputCell{{
8768
Values: gomonkey.Params{nil},
88-
Times: 2,
69+
Times: 1,
8970
}})
9071
defer patches.Reset()
91-
orgRootClient.EXPECT().Patch(gomock.Any(), gomock.Any()).Return(nil).Times(2)
72+
// projectGroupBuilder uses OrgRootClient.Patch
73+
orgRootClient.EXPECT().Patch(gomock.Any(), gomock.Any()).Return(nil).Times(1)
9274

9375
ctx := context.Background()
9476
err := svc.CleanupInfraResources(ctx)
9577
require.NoError(t, err)
96-
assert.Equal(t, 0, len(svc.infraShareStore.List()))
9778
assert.Equal(t, 0, len(svc.infraGroupStore.List()))
9879
assert.Equal(t, 0, len(svc.projectGroupStore.List()))
99-
assert.Equal(t, 0, len(svc.projectShareStore.List()))
10080
}
10181

10282
func TestCleanupVPCChildResources(t *testing.T) {

0 commit comments

Comments
 (0)