Skip to content

[release-1.5] feat: external load balancer garbage collection (part 3) - add gc to reconciliation #3646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (t Template) ControllersPolicy() *iamv1.PolicyDocument {
"elasticloadbalancing:CreateLoadBalancer",
"elasticloadbalancing:ConfigureHealthCheck",
"elasticloadbalancing:DeleteLoadBalancer",
"elasticloadbalancing:DeleteTargetGroup",
"elasticloadbalancing:DescribeLoadBalancers",
"elasticloadbalancing:DescribeLoadBalancerAttributes",
"elasticloadbalancing:ApplySecurityGroupsToLoadBalancer",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Resources:
- elasticloadbalancing:CreateLoadBalancer
- elasticloadbalancing:ConfigureHealthCheck
- elasticloadbalancing:DeleteLoadBalancer
- elasticloadbalancing:DeleteTargetGroup
- elasticloadbalancing:DescribeLoadBalancers
- elasticloadbalancing:DescribeLoadBalancerAttributes
- elasticloadbalancing:ApplySecurityGroupsToLoadBalancer
Expand Down
84 changes: 84 additions & 0 deletions cmd/clusterawsadm/cmd/gc/disable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gc

import (
"fmt"
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/client-go/util/homedir"

gcproc "sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/gc"
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
)

func newDisableCmd() *cobra.Command {
var (
clusterName string
namespace string
kubeConfig string
kubeConfigDefault string
)

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

newCmd := &cobra.Command{
Use: "disable",
Short: "Mark a cluster as NOT requiring external resource garbage collection",
Long: cmd.LongDesc(`
This command will mark the given cluster as not requiring external
resource garbage collection (i.e. deleting) when the cluster is
requested to be deleted.
`),
Example: cmd.Examples(`
# Disable GC for a cluster using existing k8s context
clusterawsadm gc disable --cluster-name=test-cluster

# Disable GC for a cluster using kubeconfig
clusterawsadm gc disable --cluster-name=test-cluster --kubeconfig=test.kubeconfig
`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
proc, err := gcproc.New(gcproc.GCInput{
ClusterName: clusterName,
Namespace: namespace,
KubeconfigPath: kubeConfig,
})
if err != nil {
return fmt.Errorf("creating command processor: %w", err)
}

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

return nil
},
}

newCmd.Flags().StringVar(&clusterName, "cluster-name", "", "The name of the CAPA cluster")
newCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "The namespace for the cluster definition")
newCmd.Flags().StringVar(&kubeConfig, "kubeconfig", kubeConfigDefault, "Path to the kubeconfig file to use")

newCmd.MarkFlagRequired("cluster-name") //nolint: errcheck

return newCmd
}
85 changes: 85 additions & 0 deletions cmd/clusterawsadm/cmd/gc/enable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gc

import (
"fmt"
"path/filepath"

"github.com/spf13/cobra"
"k8s.io/client-go/util/homedir"

gcproc "sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/gc"
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
)

func newEnableCmd() *cobra.Command {
var (
clusterName string
namespace string
kubeConfig string
kubeConfigDefault string
)

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

newCmd := &cobra.Command{
Use: "enable",
Short: "Mark a cluster as requiring external resource garbage collection",
Long: cmd.LongDesc(`
This command will mark the given cluster as requiring external
resource garbage collection (i.e. deleting) when the cluster is
requested to be deleted. This works by adding an annotation to the
infra cluster.
`),
Example: cmd.Examples(`
# Enable GC for a cluster using existing k8s context
clusterawsadm gc enable --cluster-name=test-cluster

# Enable GC for a cluster using kubeconfig
clusterawsadm gc enable --cluster-name=test-cluster --kubeconfig=test.kubeconfig
`),
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
proc, err := gcproc.New(gcproc.GCInput{
ClusterName: clusterName,
Namespace: namespace,
KubeconfigPath: kubeConfig,
})
if err != nil {
return fmt.Errorf("creating command processor: %w", err)
}

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

return nil
},
}

newCmd.Flags().StringVar(&clusterName, "cluster-name", "", "The name of the CAPA cluster")
newCmd.Flags().StringVarP(&namespace, "namespace", "n", "default", "The namespace for the cluster definition")
newCmd.Flags().StringVar(&kubeConfig, "kubeconfig", kubeConfigDefault, "Path to the kubeconfig file to use")

newCmd.MarkFlagRequired("cluster-name") //nolint: errcheck

return newCmd
}
41 changes: 41 additions & 0 deletions cmd/clusterawsadm/cmd/gc/gc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2022 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package gc

import (
"github.com/spf13/cobra"
)

// RootCmd is the root of the `gc command`.
func RootCmd() *cobra.Command {
newCmd := &cobra.Command{
Use: "gc [command]",
Short: "Commands related to garbage collecting external resources of clusters",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
if err := cmd.Help(); err != nil {
return err
}
return nil
},
}

newCmd.AddCommand(newEnableCmd())
newCmd.AddCommand(newDisableCmd())

return newCmd
}
2 changes: 2 additions & 0 deletions cmd/clusterawsadm/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/bootstrap"
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/controller"
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/eks"
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/gc"
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/resource"
"sigs.k8s.io/cluster-api-provider-aws/cmd/clusterawsadm/cmd/version"
"sigs.k8s.io/cluster-api/cmd/clusterctl/cmd"
Expand Down Expand Up @@ -71,6 +72,7 @@ func RootCmd() *cobra.Command {
newCmd.AddCommand(eks.RootCmd())
newCmd.AddCommand(controller.RootCmd())
newCmd.AddCommand(resource.RootCmd())
newCmd.AddCommand(gc.RootCmd())

return newCmd
}
Expand Down
Loading