Skip to content

Commit 7ade599

Browse files
committed
refactor: changes from review
Various changes as a result of review feedback. Signed-off-by: Richard Case <[email protected]>
1 parent 5bea222 commit 7ade599

File tree

8 files changed

+74
-127
lines changed

8 files changed

+74
-127
lines changed

cmd/clusterawsadm/cmd/gc/disable.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ import (
2828
)
2929

3030
func newDisableCmd() *cobra.Command {
31-
clusterName := ""
32-
namespace := ""
33-
kubeConfig := ""
34-
kubeConfigDefault := ""
31+
var (
32+
clusterName string
33+
namespace string
34+
kubeConfig string
35+
kubeConfigDefault string
36+
)
3537

3638
if home := homedir.HomeDir(); home != "" {
3739
kubeConfigDefault = filepath.Join(home, ".kube", "config")
3840
}
3941

4042
newCmd := &cobra.Command{
4143
Use: "disable",
42-
Short: "Mark a cluster as NOT requiring external resource gc",
44+
Short: "Mark a cluster as NOT requiring external resource garbage collection",
4345
Long: cmd.LongDesc(`
4446
This command will mark the given cluster as not requiring external
4547
resource garbage collection (i.e. deleting) when the cluster is
@@ -63,8 +65,7 @@ func newDisableCmd() *cobra.Command {
6365
return fmt.Errorf("creating command processor: %w", err)
6466
}
6567

66-
err = proc.Disable(cmd.Context())
67-
if err != nil {
68+
if err := proc.Disable(cmd.Context()); err != nil {
6869
return fmt.Errorf("disabling garbage collection: %w", err)
6970
}
7071
fmt.Printf("Disabled garbage collection for cluster %s/%s\n", namespace, clusterName)

cmd/clusterawsadm/cmd/gc/enable.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,20 @@ import (
2828
)
2929

3030
func newEnableCmd() *cobra.Command {
31-
clusterName := ""
32-
namespace := ""
33-
kubeConfig := ""
34-
kubeConfigDefault := ""
31+
var (
32+
clusterName string
33+
namespace string
34+
kubeConfig string
35+
kubeConfigDefault string
36+
)
3537

3638
if home := homedir.HomeDir(); home != "" {
3739
kubeConfigDefault = filepath.Join(home, ".kube", "config")
3840
}
3941

4042
newCmd := &cobra.Command{
4143
Use: "enable",
42-
Short: "Mark a cluster as requiring external resource gc",
44+
Short: "Mark a cluster as requiring external resource garbage collection",
4345
Long: cmd.LongDesc(`
4446
This command will mark the given cluster as requiring external
4547
resource garbage collection (i.e. deleting) when the cluster is
@@ -64,8 +66,7 @@ func newEnableCmd() *cobra.Command {
6466
return fmt.Errorf("creating command processor: %w", err)
6567
}
6668

67-
err = proc.Enable(cmd.Context())
68-
if err != nil {
69+
if err := proc.Enable(cmd.Context()); err != nil {
6970
return fmt.Errorf("enabling garbage collection: %w", err)
7071
}
7172
fmt.Printf("Enabled garbage collection for cluster %s/%s\n", namespace, clusterName)

cmd/clusterawsadm/gc/disable.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

cmd/clusterawsadm/gc/enable.go

Lines changed: 0 additions & 47 deletions
This file was deleted.

cmd/clusterawsadm/gc/gc.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ import (
2929

3030
infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
3131
ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/controlplane/eks/api/v1beta1"
32+
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/exp/api/v1beta1"
33+
"sigs.k8s.io/cluster-api-provider-aws/pkg/annotations"
3234
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
3335
"sigs.k8s.io/cluster-api/controllers/external"
36+
"sigs.k8s.io/cluster-api/util/patch"
3437
)
3538

3639
var (
@@ -100,6 +103,44 @@ func New(input GCInput, opts ...CmdProcessorOption) (*CmdProcessor, error) {
100103
return cmd, nil
101104
}
102105

106+
// Enable is used to enable external resource garbage collection for a cluster.
107+
func (c *CmdProcessor) Enable(ctx context.Context) error {
108+
if err := c.setAnnotationAndPatch(ctx, "true"); err != nil {
109+
return fmt.Errorf("setting gc annotation to true: %w", err)
110+
}
111+
112+
return nil
113+
}
114+
115+
// Disable is used to disable external resource garbage collection for a cluster.
116+
func (c *CmdProcessor) Disable(ctx context.Context) error {
117+
if err := c.setAnnotationAndPatch(ctx, "false"); err != nil {
118+
return fmt.Errorf("setting gc annotation to false: %w", err)
119+
}
120+
121+
return nil
122+
}
123+
124+
func (c *CmdProcessor) setAnnotationAndPatch(ctx context.Context, annotationValue string) error {
125+
infraObj, err := c.getInfraCluster(ctx)
126+
if err != nil {
127+
return err
128+
}
129+
130+
patchHelper, err := patch.NewHelper(infraObj, c.client)
131+
if err != nil {
132+
return fmt.Errorf("creating patch helper: %w", err)
133+
}
134+
135+
annotations.Set(infraObj, expinfrav1.ExternalResourceGCAnnotation, annotationValue)
136+
137+
if err := patchHelper.Patch(ctx, infraObj); err != nil {
138+
return fmt.Errorf("patching infra cluster with gc annotation: %w", err)
139+
}
140+
141+
return nil
142+
}
143+
103144
func (c *CmdProcessor) getInfraCluster(ctx context.Context) (*unstructured.Unstructured, error) {
104145
cluster := &clusterv1.Cluster{}
105146

cmd/clusterawsadm/gc/gc_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func TestDisableGC(t *testing.T) {
144144
expectError: false,
145145
},
146146
{
147-
name: "with managed control plane and with annotation",
147+
name: "with awscluster and with annotation",
148148
clusterName: testClusterName,
149149
existingObjs: newUnManagedClusterWithAnnotations(testClusterName, map[string]string{expinfrav1.ExternalResourceGCAnnotation: "true"}),
150150
expectError: false,

docs/book/src/topics/external-resource-gc.md

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,25 @@
55

66
## Overview
77

8-
Workload clusters that have been created by CAPA may have additional resources in AWS that need to be deleted when the cluster is deleted.
8+
Workload clusters that CAPA has created may have additional resources in AWS that need to be deleted when the cluster is deleted.
99

10-
For example, if the workload cluster has `Services` of type `LoadBalancer` then there will be AWS ELB/NLB provisioned. If you try to delete the workload cluster in this example it will fail as the VPC is still being used by these load balancers.
10+
For example, if the workload cluster has `Services` of type `LoadBalancer` then AWS ELB/NLB are provisioned. If you try to delete the workload cluster in this example, it will fail as these load balancers are still using the VPC.
1111

12-
This feature enables deleting these external resources as part of cluster deletion. It works by annotating the AWS infra cluster / control plane on creation. When a CAPI `Cluster` is requested to be deleted the deletion of CAPA resources is blocked depending on the status of this annotation. When the resources have been garbage collected (i.e. deleted) then the annotation is updated and normal CAPA deletion starts. This is not related to [externally managed infrastructure](https://cluster-api-aws.sigs.k8s.io/topics/bring-your-own-aws-infrastructure.html).
12+
This feature enables deletion of these external resources as part of cluster deletion. During the deletion of a workload cluster the external AWS resources that where created by the Cloud Controller Manager (CCM) in the workload cluster will be identified and deleted.
1313

14-
Currently we support cleaning up the following:
14+
> NOTE: This is not related to [externally managed infrastructure](https://cluster-api-aws.sigs.k8s.io/topics/bring-your-own-aws-infrastructure.html).
15+
16+
Currently, we support cleaning up the following:
1517

1618
- AWS ELB/NLB - by deleting `Services` of type `LoadBalancer` from the workload cluster
1719

18-
We will look to potentially supporting deleting EBS volumes in the future.
20+
We will look to support deleting EBS volumes in the future potentially.
1921

2022
> Note: this feature will likely be superseded by an upstream CAPI feature in the future when [this issue](https://github.com/kubernetes-sigs/cluster-api/issues/3075) is resolved.
2123
2224
## Enabling
2325

24-
To enable this you must set the `ExternalResourceGC` to `true` on the controller manager. The easiest way to do this is via an environment variable:
26+
To enable garbage collection, you must set the `ExternalResourceGC` feature gate to `true` on the controller manager. The easiest way to do this is via an environment variable:
2527

2628
```bash
2729
export EXP_EXTERNAL_RESOURCE_GC=true
@@ -38,7 +40,7 @@ There are 2 ways to manually disable garbage collection for an individual cluste
3840

3941
#### Using `clusterawsadm`
4042

41-
You can disable garbage collection for a cluster by running the following command:
43+
By running the following command:
4244

4345
```bash
4446
clusterawsadm gc disable --cluster-name mycluster
@@ -48,25 +50,23 @@ See the command help for more examples.
4850

4951
#### Editing `AWSCluster\AWSManagedControlPlane`
5052

51-
You can disable garbage collection for a cluster by editing your `AWSCluster` or `AWSManagedControlPlane`.
52-
53-
Either remove the `aws.cluster.x-k8s.io/external-resource-gc` or set its value to **true**.
53+
Or, by editing your `AWSCluster` or `AWSManagedControlPlane` so that the annotation `aws.cluster.x-k8s.io/external-resource-gc` is set to **false**.
5454

5555
```yaml
5656
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
5757
kind: AWSManagedControlPlane
5858
metadata:
5959
annotations:
60-
aws.cluster.x-k8s.io/external-resource-gc: "true"
60+
aws.cluster.x-k8s.io/external-resource-gc: "false"
6161
```
6262
63-
### Manually Enablind Garbage Collection for a Cluster
63+
### Manually Enabling Garbage Collection for a Cluster
6464
6565
There are 2 ways to manually enable garbage collection for an individual cluster:
6666
6767
#### Using `clusterawsadm`
6868

69-
You can enable garbage collection for a cluster by running the following command:
69+
By running the following command:
7070

7171
```bash
7272
clusterawsadm gc enable --cluster-name mycluster
@@ -76,14 +76,12 @@ See the command help for more examples.
7676

7777
#### Editing `AWSCluster\AWSManagedControlPlane`
7878

79-
You can enable garbage collection for a cluster by editing your `AWSCluster` or `AWSManagedControlPlane`.
80-
81-
Add the `aws.cluster.x-k8s.io/external-resource-gc` annotation if it doesn't exist and set its value to **false**.
79+
Or, by editing your `AWSCluster` or `AWSManagedControlPlane` o that the annotation `aws.cluster.x-k8s.io/external-resource-gc` is either removed or set to **true**.
8280

8381
```yaml
8482
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
8583
kind: AWSManagedControlPlane
8684
metadata:
8785
annotations:
88-
aws.cluster.x-k8s.io/external-resource-gc: "false"
86+
aws.cluster.x-k8s.io/external-resource-gc: "true"
8987
```

feature/feature.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const (
6868

6969
// ExternalResourceGC is used to enable the garbage collection of external resources like NLB/ALB on deletion
7070
// owner: @richardcase
71-
// alpha: v1.4
71+
// alpha: v1.5
7272
ExternalResourceGC featuregate.Feature = "ExternalResourceGC"
7373
)
7474

0 commit comments

Comments
 (0)