Skip to content

Commit 2489b05

Browse files
committed
Improve IBMPowerVSCluster deletion
1 parent 839de51 commit 2489b05

File tree

3 files changed

+16
-44
lines changed

3 files changed

+16
-44
lines changed

cloud/scope/powervs_cluster.go

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,12 +2285,12 @@ func (s *PowerVSClusterScope) DeleteLoadBalancer() (bool, error) {
22852285
continue
22862286
}
22872287

2288-
lb, _, err := s.IBMVPCClient.GetLoadBalancer(&vpcv1.GetLoadBalancerOptions{
2288+
lb, resp, err := s.IBMVPCClient.GetLoadBalancer(&vpcv1.GetLoadBalancerOptions{
22892289
ID: lb.ID,
22902290
})
22912291

22922292
if err != nil {
2293-
if strings.Contains(err.Error(), string(VPCLoadBalancerNotFound)) {
2293+
if resp.StatusCode == ResourceNotFoundCode {
22942294
s.Info("VPC load balancer successfully deleted")
22952295
continue
22962296
}
@@ -2324,10 +2324,10 @@ func (s *PowerVSClusterScope) DeleteVPCSecurityGroups() error {
23242324
s.Info("Skipping VPC security group deletion as resource is not created by controller", "ID", *securityGroup.ID)
23252325
continue
23262326
}
2327-
if _, _, err := s.IBMVPCClient.GetSecurityGroup(&vpcv1.GetSecurityGroupOptions{
2327+
if _, resp, err := s.IBMVPCClient.GetSecurityGroup(&vpcv1.GetSecurityGroupOptions{
23282328
ID: securityGroup.ID,
23292329
}); err != nil {
2330-
if strings.Contains(err.Error(), string(VPCSecurityGroupNotFound)) {
2330+
if resp.StatusCode == ResourceNotFoundCode {
23312331
s.Info("VPC security group has been already deleted", "ID", *securityGroup.ID)
23322332
continue
23332333
}
@@ -2356,12 +2356,12 @@ func (s *PowerVSClusterScope) DeleteVPCSubnet() (bool, error) {
23562356
continue
23572357
}
23582358

2359-
net, _, err := s.IBMVPCClient.GetSubnet(&vpcv1.GetSubnetOptions{
2359+
net, resp, err := s.IBMVPCClient.GetSubnet(&vpcv1.GetSubnetOptions{
23602360
ID: subnet.ID,
23612361
})
23622362

23632363
if err != nil {
2364-
if strings.Contains(err.Error(), string(VPCSubnetNotFound)) {
2364+
if resp.StatusCode == ResourceNotFoundCode {
23652365
s.Info("VPC subnet successfully deleted")
23662366
continue
23672367
}
@@ -2398,12 +2398,12 @@ func (s *PowerVSClusterScope) DeleteVPC() (bool, error) {
23982398
return false, nil
23992399
}
24002400

2401-
vpc, _, err := s.IBMVPCClient.GetVPC(&vpcv1.GetVPCOptions{
2401+
vpc, resp, err := s.IBMVPCClient.GetVPC(&vpcv1.GetVPCOptions{
24022402
ID: s.IBMPowerVSCluster.Status.VPC.ID,
24032403
})
24042404

24052405
if err != nil {
2406-
if strings.Contains(err.Error(), string(VPCNotFound)) {
2406+
if resp.StatusCode == ResourceNotFoundCode {
24072407
s.Info("VPC successfully deleted")
24082408
return false, nil
24092409
}
@@ -2433,12 +2433,12 @@ func (s *PowerVSClusterScope) DeleteTransitGateway() (bool, error) {
24332433
return false, nil
24342434
}
24352435

2436-
tg, _, err := s.TransitGatewayClient.GetTransitGateway(&tgapiv1.GetTransitGatewayOptions{
2436+
tg, resp, err := s.TransitGatewayClient.GetTransitGateway(&tgapiv1.GetTransitGatewayOptions{
24372437
ID: s.IBMPowerVSCluster.Status.TransitGateway.ID,
24382438
})
24392439

24402440
if err != nil {
2441-
if strings.Contains(err.Error(), string(TransitGatewayNotFound)) {
2441+
if resp.StatusCode == ResourceNotFoundCode {
24422442
s.Info("Transit gateway successfully deleted")
24432443
return false, nil
24442444
}
@@ -2541,26 +2541,13 @@ func (s *PowerVSClusterScope) DeleteServiceInstance() (bool, error) {
25412541
return false, nil
25422542
}
25432543

2544-
// If PowerVS service instance is in failed state, proceed with deletion instead of checking for existing network resources.
2545-
if serviceInstance != nil && *serviceInstance.State != string(infrav1beta2.ServiceInstanceStateFailed) {
2546-
servers, err := s.IBMPowerVSClient.GetAllDHCPServers()
2547-
if err != nil {
2548-
return false, fmt.Errorf("error fetching networks in the PowerVS service instance: %w", err)
2549-
}
2550-
2551-
if len(servers) > 0 {
2552-
s.Info("Wait for DHCP server to be deleted before deleting PowerVS service instance")
2553-
return true, nil
2554-
}
2555-
}
2556-
25572544
if _, err = s.ResourceClient.DeleteResourceInstance(&resourcecontrollerv2.DeleteResourceInstanceOptions{
25582545
ID: serviceInstance.ID,
25592546
}); err != nil {
25602547
s.Error(err, "failed to delete Power VS service instance")
25612548
return false, err
25622549
}
2563-
s.Info("PowerVS service instance successfully deleted")
2550+
25642551
return true, nil
25652552
}
25662553

@@ -2575,11 +2562,11 @@ func (s *PowerVSClusterScope) DeleteCOSInstance() error {
25752562
return nil
25762563
}
25772564

2578-
cosInstance, _, err := s.ResourceClient.GetResourceInstance(&resourcecontrollerv2.GetResourceInstanceOptions{
2565+
cosInstance, resp, err := s.ResourceClient.GetResourceInstance(&resourcecontrollerv2.GetResourceInstanceOptions{
25792566
ID: s.IBMPowerVSCluster.Status.COSInstance.ID,
25802567
})
25812568
if err != nil {
2582-
if strings.Contains(err.Error(), string(COSInstanceNotFound)) {
2569+
if resp.StatusCode == ResourceNotFoundCode {
25832570
return nil
25842571
}
25852572
return fmt.Errorf("failed to fetch COS service instance: %w", err)

cloud/scope/types.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,9 @@ package scope
2020
type ResourceNotFound string
2121

2222
var (
23-
// VPCLoadBalancerNotFound is the error returned when a VPC load balancer is not found.
24-
VPCLoadBalancerNotFound = ResourceNotFound("cannot be found")
25-
26-
// VPCSubnetNotFound is the error returned when a VPC subnet is not found.
27-
VPCSubnetNotFound = ResourceNotFound("Subnet not found")
28-
29-
// VPCNotFound is the error returned when a VPC is not found.
30-
VPCNotFound = ResourceNotFound("VPC not found")
31-
32-
// TransitGatewayNotFound is the error returned when a transit gateway is not found.
33-
TransitGatewayNotFound = ResourceNotFound("gateway was not found")
23+
// ResourceNotFoundCode indicates the http status code used to denote that the resource not exists.
24+
ResourceNotFoundCode = 404
3425

3526
// DHCPServerNotFound is the error returned when a DHCP server is not found.
3627
DHCPServerNotFound = ResourceNotFound("dhcp server does not exist")
37-
38-
// COSInstanceNotFound is the error returned when a COS service instance is not found.
39-
COSInstanceNotFound = ResourceNotFound("COS instance unavailable")
40-
41-
// VPCSecurityGroupNotFound is the error returned when a VPC security group is not found.
42-
VPCSecurityGroupNotFound = ResourceNotFound("Security group not found")
4328
)

controllers/ibmpowervscluster_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ func (r *IBMPowerVSClusterReconciler) reconcileDelete(ctx context.Context, clust
310310

311311
clusterScope.Info("Deleting DHCP server")
312312
if err := clusterScope.DeleteDHCPServer(); err != nil {
313-
allErrs = append(allErrs, errors.Wrapf(err, "failed to delete DHCP server"))
313+
clusterScope.Error(err, "failed to delete DHCP server, attempting to delete Power VS service instance!")
314314
}
315315

316316
clusterScope.Info("Deleting Power VS service instance")

0 commit comments

Comments
 (0)