-
Notifications
You must be signed in to change notification settings - Fork 552
/
gc.go
196 lines (154 loc) · 5.48 KB
/
gc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
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 provides a way to handle AWS garbage collection on deletion.
package gc
import (
"context"
"fmt"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
_ "k8s.io/client-go/plugin/pkg/client/auth/exec" // import all auth plugins
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // import all oidc plugins
"k8s.io/client-go/tools/clientcmd"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
ekscontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/eks/api/v1beta2"
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/annotations"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/controllers/external"
"sigs.k8s.io/cluster-api/util/patch"
)
var (
scheme = runtime.NewScheme()
)
func init() {
_ = clusterv1.AddToScheme(scheme)
_ = infrav1.AddToScheme(scheme)
_ = ekscontrolplanev1.AddToScheme(scheme)
}
// CmdProcessor handles the garbage collection commands.
type CmdProcessor struct {
client client.Client
clusterName string
namespace string
}
// GCInput holds the configuration for the command processor.
type GCInput struct {
ClusterName string
Namespace string
KubeconfigPath string
}
// CmdProcessorOption is a function type to supply options when creating the command processor.
type CmdProcessorOption func(proc *CmdProcessor) error
// WithClient is an option that enable you to explicitly supply a client.
func WithClient(client client.Client) CmdProcessorOption {
return func(proc *CmdProcessor) error {
proc.client = client
return nil
}
}
// New creates a new instance of the command processor.
func New(input GCInput, opts ...CmdProcessorOption) (*CmdProcessor, error) {
cmd := &CmdProcessor{
clusterName: input.ClusterName,
namespace: input.Namespace,
}
for _, opt := range opts {
if err := opt(cmd); err != nil {
return nil, fmt.Errorf("applying option: %w", err)
}
}
if cmd.client == nil {
config, err := clientcmd.BuildConfigFromFlags("", input.KubeconfigPath)
if err != nil {
return nil, fmt.Errorf("building client config: %w", err)
}
cl, err := client.New(config, client.Options{Scheme: scheme})
if err != nil {
return nil, fmt.Errorf("creating new client: %w", err)
}
cmd.client = cl
}
return cmd, nil
}
// Enable is used to enable external resource garbage collection for a cluster.
func (c *CmdProcessor) Enable(ctx context.Context) error {
if err := c.setAnnotationAndPatch(ctx, infrav1.ExternalResourceGCAnnotation, "true"); err != nil {
return fmt.Errorf("setting gc annotation to true: %w", err)
}
return nil
}
// Disable is used to disable external resource garbage collection for a cluster.
func (c *CmdProcessor) Disable(ctx context.Context) error {
if err := c.setAnnotationAndPatch(ctx, infrav1.ExternalResourceGCAnnotation, "false"); err != nil {
return fmt.Errorf("setting gc annotation to false: %w", err)
}
return nil
}
// Configure is used to configure external resource garbage collection for a cluster.
func (c *CmdProcessor) Configure(ctx context.Context, gcTasks []string) error {
supportedGCTasks := []infrav1.GCTask{infrav1.GCTaskLoadBalancer, infrav1.GCTaskTargetGroup, infrav1.GCTaskSecurityGroup}
for _, gcTask := range gcTasks {
found := false
for _, supportedGCTask := range supportedGCTasks {
if gcTask == string(supportedGCTask) {
found = true
break
}
}
if !found {
return fmt.Errorf("unsupported gc task: %s", gcTask)
}
}
annotationValue := strings.Join(gcTasks, ",")
if err := c.setAnnotationAndPatch(ctx, infrav1.ExternalResourceGCTasksAnnotation, annotationValue); err != nil {
return fmt.Errorf("setting gc tasks annotation to %s: %w", annotationValue, err)
}
return nil
}
func (c *CmdProcessor) setAnnotationAndPatch(ctx context.Context, annotationName, annotationValue string) error {
infraObj, err := c.getInfraCluster(ctx)
if err != nil {
return err
}
patchHelper, err := patch.NewHelper(infraObj, c.client)
if err != nil {
return fmt.Errorf("creating patch helper: %w", err)
}
if annotationValue != "" {
annotations.Set(infraObj, annotationName, annotationValue)
} else {
annotations.Delete(infraObj, annotationName)
}
if err := patchHelper.Patch(ctx, infraObj); err != nil {
return fmt.Errorf("patching infra cluster with gc annotation: %w", err)
}
return nil
}
func (c *CmdProcessor) getInfraCluster(ctx context.Context) (*unstructured.Unstructured, error) {
cluster := &clusterv1.Cluster{}
key := client.ObjectKey{
Name: c.clusterName,
Namespace: c.namespace,
}
if err := c.client.Get(ctx, key, cluster); err != nil {
return nil, fmt.Errorf("getting capi cluster %s/%s: %w", c.namespace, c.clusterName, err)
}
ref := cluster.Spec.InfrastructureRef
obj, err := external.Get(ctx, c.client, ref, cluster.Namespace)
if err != nil {
return nil, fmt.Errorf("getting infra cluster %s/%s: %w", ref.Namespace, ref.Name, err)
}
return obj, nil
}