Skip to content

Commit

Permalink
Fix cluster deletion when using BYO infra
Browse files Browse the repository at this point in the history
  • Loading branch information
adriananeci committed Jan 20, 2025
1 parent 07a93a5 commit 6a57ec7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions pkg/cloud/services/network/secondarycidr.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (s *Service) associateSecondaryCidrs() error {
}

func (s *Service) disassociateSecondaryCidrs() error {
// If the VPC is unmanaged or not yet populated, return early.
if s.scope.VPC().IsUnmanaged(s.scope.Name()) || s.scope.VPC().ID == "" {
return nil
}

secondaryCidrBlocks := s.scope.AllSecondaryCidrBlocks()
if len(secondaryCidrBlocks) == 0 {
return nil
Expand Down
26 changes: 25 additions & 1 deletion pkg/cloud/services/network/secondarycidr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ func TestServiceDiassociateSecondaryCidr(t *testing.T) {
tests := []struct {
name string
fillAWSManagedControlPlaneSecondaryCIDR bool
unmanagedVPC bool
networkSecondaryCIDRBlocks []infrav1.VpcCidrBlock
expect func(m *mocks.MockEC2APIMockRecorder)
wantErr bool
Expand Down Expand Up @@ -289,7 +290,7 @@ func TestServiceDiassociateSecondaryCidr(t *testing.T) {
},
},
{
name: "Should return error if failed to diassociate secondary cidr block",
name: "Should return error if failed to disassociate secondary cidr block",
fillAWSManagedControlPlaneSecondaryCIDR: true,
expect: func(m *mocks.MockEC2APIMockRecorder) {
m.DescribeVpcsWithContext(context.TODO(), gomock.AssignableToTypeOf(&ec2.DescribeVpcsInput{})).Return(&ec2.DescribeVpcsOutput{
Expand Down Expand Up @@ -320,6 +321,17 @@ func TestServiceDiassociateSecondaryCidr(t *testing.T) {
},
wantErr: false,
},
{
name: "Should successfully return from disassociating secondary CIDR blocks if VPC is in unmanaged mode",
fillAWSManagedControlPlaneSecondaryCIDR: true,
unmanagedVPC: true,
expect: func(m *mocks.MockEC2APIMockRecorder) {
// No calls expected
m.DescribeVpcsWithContext(context.TODO(), gomock.Any()).Times(0)
m.DisassociateVpcCidrBlockWithContext(context.TODO(), gomock.Any()).Times(0)
},
wantErr: false,
},
{
name: "Should successfully disassociate existing secondary CIDR blocks",
fillAWSManagedControlPlaneSecondaryCIDR: false,
Expand Down Expand Up @@ -381,6 +393,18 @@ func TestServiceDiassociateSecondaryCidr(t *testing.T) {
if !tt.fillAWSManagedControlPlaneSecondaryCIDR {
mcpScope.ControlPlane.Spec.SecondaryCidrBlock = nil
}

if !tt.unmanagedVPC {
mcpScope.ControlPlane.Spec.NetworkSpec.VPC = infrav1.VPCSpec{
ID: subnetsVPCID,
Tags: infrav1.Tags{
infrav1.ClusterTagKey("test-cluster"): "owned",
},
}

mcpScope.Cluster.Name = "test-cluster"
}

mcpScope.ControlPlane.Spec.NetworkSpec.VPC.SecondaryCidrBlocks = tt.networkSecondaryCIDRBlocks

s := NewService(mcpScope)
Expand Down

0 comments on commit 6a57ec7

Please sign in to comment.