Skip to content

Commit 5def5da

Browse files
authored
Merge pull request kubernetes-sigs#1233 from jwmay2012/skip-custom-subnet-delete
Skip deletion of Subnet when created ouside CAPG.
2 parents 5f96c1a + b57f2d8 commit 5def5da

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

cloud/services/compute/subnets/reconcile.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,16 @@ package subnets
1919
import (
2020
"context"
2121

22+
infrav1 "sigs.k8s.io/cluster-api-provider-gcp/api/v1beta1"
23+
2224
"github.com/GoogleCloudPlatform/k8s-cloud-provider/pkg/cloud/meta"
2325
"google.golang.org/api/compute/v1"
2426

2527
"sigs.k8s.io/cluster-api-provider-gcp/cloud/gcperrors"
2628
"sigs.k8s.io/controller-runtime/pkg/log"
2729
)
2830

29-
// Reconcile reconcile cluster network components.
31+
// Reconcile reconciles cluster network components.
3032
func (s *Service) Reconcile(ctx context.Context) error {
3133
logger := log.FromContext(ctx)
3234
logger.Info("Reconciling subnetwork resources")
@@ -43,13 +45,31 @@ func (s *Service) Reconcile(ctx context.Context) error {
4345
func (s *Service) Delete(ctx context.Context) error {
4446
logger := log.FromContext(ctx)
4547
for _, subnetSpec := range s.scope.SubnetSpecs() {
46-
logger.V(2).Info("Deleting a subnet", "name", subnetSpec.Name)
4748
subnetKey := meta.RegionalKey(subnetSpec.Name, s.getSubnetRegion(subnetSpec))
48-
err := s.subnets.Delete(ctx, subnetKey)
49-
if err != nil && !gcperrors.IsNotFound(err) {
50-
logger.Error(err, "Error deleting subnet", "name", subnetSpec.Name)
49+
logger.V(2).Info("Looking for subnet before deleting it", "name", subnetSpec.Name)
50+
subnet, err := s.subnets.Get(ctx, subnetKey)
51+
if err != nil {
52+
if gcperrors.IsNotFound(err) {
53+
continue
54+
}
55+
logger.Error(err, "Error getting subnet", "name", subnetSpec.Name)
5156
return err
5257
}
58+
59+
// Skip delete if subnet was not created by CAPG.
60+
// If subnet description is not set by the Spec, or by our default value, then assume it was created externally.
61+
if subnet.Description != infrav1.ClusterTagKey(s.scope.Name()) && (subnetSpec.Description == "" || subnet.Description != subnetSpec.Description) {
62+
logger.V(2).Info("Skipping subnet deletion as it was created outside of Cluster API", "name", subnetSpec.Name)
63+
return nil
64+
}
65+
66+
logger.V(2).Info("Deleting a subnet", "name", subnetSpec.Name)
67+
if err := s.subnets.Delete(ctx, subnetKey); err != nil {
68+
if !gcperrors.IsNotFound(err) {
69+
logger.Error(err, "Error deleting subnet", "name", subnetSpec.Name)
70+
return err
71+
}
72+
}
5373
}
5474

5575
return nil

cloud/services/compute/subnets/reconcile_test.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ var fakeGCPCluster = &infrav1.GCPCluster{
6161
Network: infrav1.NetworkSpec{
6262
Subnets: infrav1.Subnets{
6363
infrav1.SubnetSpec{
64-
Name: "workers",
65-
CidrBlock: "10.0.0.1/28",
66-
Region: "us-central1",
67-
Purpose: ptr.To[string]("INTERNAL_HTTPS_LOAD_BALANCER"),
64+
Name: "workers",
65+
CidrBlock: "10.0.0.1/28",
66+
Region: "us-central1",
67+
Purpose: ptr.To[string]("INTERNAL_HTTPS_LOAD_BALANCER"),
68+
Description: ptr.To[string](infrav1.ClusterTagKey(fakeCluster.Name)),
6869
},
6970
},
7071
},
@@ -212,9 +213,26 @@ func TestService_Delete(t *testing.T) {
212213
DeleteError: map[meta.Key]error{
213214
*meta.RegionalKey(fakeGCPCluster.Spec.Network.Subnets[0].Name, fakeGCPCluster.Spec.Region): &googleapi.Error{Code: http.StatusBadRequest},
214215
},
216+
Objects: map[meta.Key]*cloud.MockSubnetworksObj{
217+
*meta.RegionalKey(fakeGCPCluster.Spec.Network.Subnets[0].Name, fakeGCPCluster.Spec.Region): {Obj: compute.Subnetwork{Description: infrav1.ClusterTagKey(fakeCluster.Name)}},
218+
},
215219
},
216220
wantErr: true,
217221
},
222+
{
223+
name: "subnet not created by CAPI, should not try to delete it",
224+
scope: func() Scope { return clusterScope },
225+
mockSubnetworks: &cloud.MockSubnetworks{
226+
ProjectRouter: &cloud.SingleProjectRouter{ID: "my-proj"},
227+
DeleteError: map[meta.Key]error{
228+
*meta.RegionalKey(fakeGCPCluster.Spec.Network.Subnets[0].Name, fakeGCPCluster.Spec.Region): &googleapi.Error{Code: http.StatusBadRequest},
229+
},
230+
Objects: map[meta.Key]*cloud.MockSubnetworksObj{
231+
*meta.RegionalKey(fakeGCPCluster.Spec.Network.Subnets[0].Name, fakeGCPCluster.Spec.Region): {Obj: compute.Subnetwork{Description: "my-custom-subnet-description"}},
232+
},
233+
},
234+
wantErr: false,
235+
},
218236
}
219237
for _, tt := range tests {
220238
t.Run(tt.name, func(t *testing.T) {

test/e2e/data/cni/calico/calico.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ spec:
468468
numAllowedLocalASNumbers:
469469
description: Maximum number of local AS numbers that are allowed in
470470
the AS path for received routes. This removes BGP loop prevention
471-
and should only be used if absolutely necesssary.
471+
and should only be used if absolutely necessary.
472472
format: int32
473473
type: integer
474474
password:

0 commit comments

Comments
 (0)