Skip to content

Commit 69c64a6

Browse files
authored
Merge pull request #5291 from adriananeci/fix_byo_infra_deletion
🐛 Fix cluster deletion when using BYO AWS infra mode and Secondary CIDR
2 parents 0c446b1 + 6a57ec7 commit 69c64a6

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

pkg/cloud/services/network/secondarycidr.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ func (s *Service) associateSecondaryCidrs() error {
7979
}
8080

8181
func (s *Service) disassociateSecondaryCidrs() error {
82+
// If the VPC is unmanaged or not yet populated, return early.
83+
if s.scope.VPC().IsUnmanaged(s.scope.Name()) || s.scope.VPC().ID == "" {
84+
return nil
85+
}
86+
8287
secondaryCidrBlocks := s.scope.AllSecondaryCidrBlocks()
8388
if len(secondaryCidrBlocks) == 0 {
8489
return nil

pkg/cloud/services/network/secondarycidr_test.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ func TestServiceDiassociateSecondaryCidr(t *testing.T) {
243243
tests := []struct {
244244
name string
245245
fillAWSManagedControlPlaneSecondaryCIDR bool
246+
unmanagedVPC bool
246247
networkSecondaryCIDRBlocks []infrav1.VpcCidrBlock
247248
expect func(m *mocks.MockEC2APIMockRecorder)
248249
wantErr bool
@@ -289,7 +290,7 @@ func TestServiceDiassociateSecondaryCidr(t *testing.T) {
289290
},
290291
},
291292
{
292-
name: "Should return error if failed to diassociate secondary cidr block",
293+
name: "Should return error if failed to disassociate secondary cidr block",
293294
fillAWSManagedControlPlaneSecondaryCIDR: true,
294295
expect: func(m *mocks.MockEC2APIMockRecorder) {
295296
m.DescribeVpcsWithContext(context.TODO(), gomock.AssignableToTypeOf(&ec2.DescribeVpcsInput{})).Return(&ec2.DescribeVpcsOutput{
@@ -320,6 +321,17 @@ func TestServiceDiassociateSecondaryCidr(t *testing.T) {
320321
},
321322
wantErr: false,
322323
},
324+
{
325+
name: "Should successfully return from disassociating secondary CIDR blocks if VPC is in unmanaged mode",
326+
fillAWSManagedControlPlaneSecondaryCIDR: true,
327+
unmanagedVPC: true,
328+
expect: func(m *mocks.MockEC2APIMockRecorder) {
329+
// No calls expected
330+
m.DescribeVpcsWithContext(context.TODO(), gomock.Any()).Times(0)
331+
m.DisassociateVpcCidrBlockWithContext(context.TODO(), gomock.Any()).Times(0)
332+
},
333+
wantErr: false,
334+
},
323335
{
324336
name: "Should successfully disassociate existing secondary CIDR blocks",
325337
fillAWSManagedControlPlaneSecondaryCIDR: false,
@@ -381,6 +393,18 @@ func TestServiceDiassociateSecondaryCidr(t *testing.T) {
381393
if !tt.fillAWSManagedControlPlaneSecondaryCIDR {
382394
mcpScope.ControlPlane.Spec.SecondaryCidrBlock = nil
383395
}
396+
397+
if !tt.unmanagedVPC {
398+
mcpScope.ControlPlane.Spec.NetworkSpec.VPC = infrav1.VPCSpec{
399+
ID: subnetsVPCID,
400+
Tags: infrav1.Tags{
401+
infrav1.ClusterTagKey("test-cluster"): "owned",
402+
},
403+
}
404+
405+
mcpScope.Cluster.Name = "test-cluster"
406+
}
407+
384408
mcpScope.ControlPlane.Spec.NetworkSpec.VPC.SecondaryCidrBlocks = tt.networkSecondaryCIDRBlocks
385409

386410
s := NewService(mcpScope)

0 commit comments

Comments
 (0)