Skip to content

Commit 5bea222

Browse files
committed
feat: garbage collection feature enable
This change uses the new garbage collection service enables this during the reconciliation of `AWSCluster` and `AWSManagedControlPlane`. Its enabled via a new feature flag `ExternalResourceGC` which is disabled by default. If the feature flag is enabled then the the gc service is called in `reconcileDelete` for the infra clusters. The actual gc service does the work of cleanup. New commands have been added to `clusterawsadm` to allow users to opt-in/out an already existing cluster from garbage collection. Additionally, with the new mocks folder introduced with the gc service the existing mocks have been deleted and tests/controllers updated. Signed-off-by: Richard Case <[email protected]>
1 parent daab273 commit 5bea222

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1213
-33767
lines changed

cmd/clusterawsadm/cloudformation/bootstrap/cluster_api_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func (t Template) ControllersPolicy() *iamv1.PolicyDocument {
132132
"elasticloadbalancing:CreateLoadBalancer",
133133
"elasticloadbalancing:ConfigureHealthCheck",
134134
"elasticloadbalancing:DeleteLoadBalancer",
135+
"elasticloadbalancing:DeleteTargetGroup",
135136
"elasticloadbalancing:DescribeLoadBalancers",
136137
"elasticloadbalancing:DescribeLoadBalancerAttributes",
137138
"elasticloadbalancing:ApplySecurityGroupsToLoadBalancer",

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/customsuffix.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/default.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_all_secret_backends.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ Resources:
190190
- elasticloadbalancing:CreateLoadBalancer
191191
- elasticloadbalancing:ConfigureHealthCheck
192192
- elasticloadbalancing:DeleteLoadBalancer
193+
- elasticloadbalancing:DeleteTargetGroup
193194
- elasticloadbalancing:DescribeLoadBalancers
194195
- elasticloadbalancing:DescribeLoadBalancerAttributes
195196
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_bootstrap_user.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Resources:
189189
- elasticloadbalancing:CreateLoadBalancer
190190
- elasticloadbalancing:ConfigureHealthCheck
191191
- elasticloadbalancing:DeleteLoadBalancer
192+
- elasticloadbalancing:DeleteTargetGroup
192193
- elasticloadbalancing:DescribeLoadBalancers
193194
- elasticloadbalancing:DescribeLoadBalancerAttributes
194195
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_custom_bootstrap_user.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Resources:
189189
- elasticloadbalancing:CreateLoadBalancer
190190
- elasticloadbalancing:ConfigureHealthCheck
191191
- elasticloadbalancing:DeleteLoadBalancer
192+
- elasticloadbalancing:DeleteTargetGroup
192193
- elasticloadbalancing:DescribeLoadBalancers
193194
- elasticloadbalancing:DescribeLoadBalancerAttributes
194195
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_different_instance_profiles.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_eks_console.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_eks_default_roles.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_eks_disable.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_eks_kms_prefix.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_extra_statements.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ Resources:
189189
- elasticloadbalancing:CreateLoadBalancer
190190
- elasticloadbalancing:ConfigureHealthCheck
191191
- elasticloadbalancing:DeleteLoadBalancer
192+
- elasticloadbalancing:DeleteTargetGroup
192193
- elasticloadbalancing:DescribeLoadBalancers
193194
- elasticloadbalancing:DescribeLoadBalancerAttributes
194195
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_s3_bucket.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cloudformation/bootstrap/fixtures/with_ssm_secret_backend.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ Resources:
184184
- elasticloadbalancing:CreateLoadBalancer
185185
- elasticloadbalancing:ConfigureHealthCheck
186186
- elasticloadbalancing:DeleteLoadBalancer
187+
- elasticloadbalancing:DeleteTargetGroup
187188
- elasticloadbalancing:DescribeLoadBalancers
188189
- elasticloadbalancing:DescribeLoadBalancerAttributes
189190
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer

cmd/clusterawsadm/cmd/gc/disable.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gc
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
23+
"github.com/spf13/cobra"
24+
"k8s.io/client-go/util/homedir"
25+
26+
gcproc "sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/gc"
27+
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
28+
)
29+
30+
func newDisableCmd() *cobra.Command {
31+
clusterName := ""
32+
namespace := ""
33+
kubeConfig := ""
34+
kubeConfigDefault := ""
35+
36+
if home := homedir.HomeDir(); home != "" {
37+
kubeConfigDefault = filepath.Join(home, ".kube", "config")
38+
}
39+
40+
newCmd := &cobra.Command{
41+
Use: "disable",
42+
Short: "Mark a cluster as NOT requiring external resource gc",
43+
Long: cmd.LongDesc(`
44+
This command will mark the given cluster as not requiring external
45+
resource garbage collection (i.e. deleting) when the cluster is
46+
requested to be deleted.
47+
`),
48+
Example: cmd.Examples(`
49+
# Disable GC for a cluster using existing k8s context
50+
clusterawsadm gc disable --cluster-name=test-cluster
51+
52+
# Disable GC for a cluster using kubeconfig
53+
clusterawsadm gc disable --cluster-name=test-cluster --kubeconfig=test.kubeconfig
54+
`),
55+
Args: cobra.NoArgs,
56+
RunE: func(cmd *cobra.Command, args []string) error {
57+
proc, err := gcproc.New(gcproc.GCInput{
58+
ClusterName: clusterName,
59+
Namespace: namespace,
60+
KubeconfigPath: kubeConfig,
61+
})
62+
if err != nil {
63+
return fmt.Errorf("creating command processor: %w", err)
64+
}
65+
66+
err = proc.Disable(cmd.Context())
67+
if err != nil {
68+
return fmt.Errorf("disabling garbage collection: %w", err)
69+
}
70+
fmt.Printf("Disabled garbage collection for cluster %s/%s\n", namespace, clusterName)
71+
72+
return nil
73+
},
74+
}
75+
76+
newCmd.Flags().StringVar(&clusterName, "cluster-name", "", "The name of the CAPA cluster")
77+
newCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "The namespace for the cluster definition")
78+
newCmd.Flags().StringVar(&kubeConfig, "kubeconfig", kubeConfigDefault, "Path to the kubeconfig file to use")
79+
80+
newCmd.MarkFlagRequired("cluster-name") //nolint: errcheck
81+
82+
return newCmd
83+
}

cmd/clusterawsadm/cmd/gc/enable.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gc
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
23+
"github.com/spf13/cobra"
24+
"k8s.io/client-go/util/homedir"
25+
26+
gcproc "sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/gc"
27+
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
28+
)
29+
30+
func newEnableCmd() *cobra.Command {
31+
clusterName := ""
32+
namespace := ""
33+
kubeConfig := ""
34+
kubeConfigDefault := ""
35+
36+
if home := homedir.HomeDir(); home != "" {
37+
kubeConfigDefault = filepath.Join(home, ".kube", "config")
38+
}
39+
40+
newCmd := &cobra.Command{
41+
Use: "enable",
42+
Short: "Mark a cluster as requiring external resource gc",
43+
Long: cmd.LongDesc(`
44+
This command will mark the given cluster as requiring external
45+
resource garbage collection (i.e. deleting) when the cluster is
46+
requested to be deleted. This works by adding an annotation to the
47+
infra cluster.
48+
`),
49+
Example: cmd.Examples(`
50+
# Enable GC for a cluster using existing k8s context
51+
clusterawsadm gc enable --cluster-name=test-cluster
52+
53+
# Enable GC for a cluster using kubeconfig
54+
clusterawsadm gc enable --cluster-name=test-cluster --kubeconfig=test.kubeconfig
55+
`),
56+
Args: cobra.NoArgs,
57+
RunE: func(cmd *cobra.Command, args []string) error {
58+
proc, err := gcproc.New(gcproc.GCInput{
59+
ClusterName: clusterName,
60+
Namespace: namespace,
61+
KubeconfigPath: kubeConfig,
62+
})
63+
if err != nil {
64+
return fmt.Errorf("creating command processor: %w", err)
65+
}
66+
67+
err = proc.Enable(cmd.Context())
68+
if err != nil {
69+
return fmt.Errorf("enabling garbage collection: %w", err)
70+
}
71+
fmt.Printf("Enabled garbage collection for cluster %s/%s\n", namespace, clusterName)
72+
73+
return nil
74+
},
75+
}
76+
77+
newCmd.Flags().StringVar(&clusterName, "cluster-name", "", "The name of the CAPA cluster")
78+
newCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "The namespace for the cluster definition")
79+
newCmd.Flags().StringVar(&kubeConfig, "kubeconfig", kubeConfigDefault, "Path to the kubeconfig file to use")
80+
81+
newCmd.MarkFlagRequired("cluster-name") //nolint: errcheck
82+
83+
return newCmd
84+
}

cmd/clusterawsadm/cmd/gc/gc.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gc
18+
19+
import (
20+
"github.com/spf13/cobra"
21+
)
22+
23+
// RootCmd is the root of the `gc command`.
24+
func RootCmd() *cobra.Command {
25+
newCmd := &cobra.Command{
26+
Use: "gc [command]",
27+
Short: "Commands related to garbage collecting external resources of clusters",
28+
Args: cobra.NoArgs,
29+
RunE: func(cmd *cobra.Command, args []string) error {
30+
if err := cmd.Help(); err != nil {
31+
return err
32+
}
33+
return nil
34+
},
35+
}
36+
37+
newCmd.AddCommand(newEnableCmd())
38+
newCmd.AddCommand(newDisableCmd())
39+
40+
return newCmd
41+
}

cmd/clusterawsadm/cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/bootstrap"
3030
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/controller"
3131
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/eks"
32+
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/gc"
3233
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/resource"
3334
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/version"
3435
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
@@ -71,6 +72,7 @@ func RootCmd() *cobra.Command {
7172
newCmd.AddCommand(eks.RootCmd())
7273
newCmd.AddCommand(controller.RootCmd())
7374
newCmd.AddCommand(resource.RootCmd())
75+
newCmd.AddCommand(gc.RootCmd())
7476

7577
return newCmd
7678
}

cmd/clusterawsadm/gc/disable.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package gc
18+
19+
import (
20+
"context"
21+
"fmt"
22+
23+
expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/exp/api/v1beta1"
24+
"sigs.k8s.io/cluster-api-provider-aws/pkg/annotations"
25+
"sigs.k8s.io/cluster-api/util/patch"
26+
)
27+
28+
// Disable is used to disable external resource garbage collection for a cluster.
29+
func (c *CmdProcessor) Disable(ctx context.Context) error {
30+
infraObj, err := c.getInfraCluster(ctx)
31+
if err != nil {
32+
return err
33+
}
34+
35+
patchHelper, err := patch.NewHelper(infraObj, c.client)
36+
if err != nil {
37+
return fmt.Errorf("creating patch helper: %w", err)
38+
}
39+
40+
annotations.Set(infraObj, expinfrav1.ExternalResourceGCAnnotation, "false")
41+
42+
if err := patchHelper.Patch(ctx, infraObj); err != nil {
43+
return fmt.Errorf("patching infra cluster with gc annotation: %w", err)
44+
}
45+
46+
return nil
47+
}

0 commit comments

Comments
 (0)