This repository has been archived by the owner on Oct 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
/
resource.go
345 lines (290 loc) · 11.2 KB
/
resource.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package drainerinitializer
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/ec2"
g8sv1alpha1 "github.com/giantswarm/apiextensions/v6/pkg/apis/core/v1alpha1"
infrastructurev1alpha3 "github.com/giantswarm/apiextensions/v6/pkg/apis/infrastructure/v1alpha3"
"github.com/giantswarm/microerror"
"github.com/giantswarm/micrologger"
"github.com/giantswarm/operatorkit/v7/pkg/controller/context/finalizerskeptcontext"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/giantswarm/aws-operator/v14/pkg/annotation"
"github.com/giantswarm/aws-operator/v14/service/controller/controllercontext"
"github.com/giantswarm/aws-operator/v14/service/controller/key"
"github.com/giantswarm/aws-operator/v14/service/internal/asg"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlClient "sigs.k8s.io/controller-runtime/pkg/client"
)
const (
Name = "drainerinitializer"
)
type ResourceConfig struct {
ASG asg.Interface
CtrlClient ctrlClient.Client
Logger micrologger.Logger
LabelMapFunc func(cr metav1.Object) map[string]string
LifeCycleHookName string
ToClusterFunc func(ctx context.Context, v interface{}) (infrastructurev1alpha3.AWSCluster, error)
}
type Resource struct {
asg asg.Interface
ctrlClient ctrlClient.Client
logger micrologger.Logger
labelMapFunc func(cr metav1.Object) map[string]string
lifeCycleHookName string
toClusterFunc func(ctx context.Context, v interface{}) (infrastructurev1alpha3.AWSCluster, error)
}
func NewResource(config ResourceConfig) (*Resource, error) {
if config.ASG == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.ASG must not be empty", config)
}
if config.CtrlClient == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.CtrlClient must not be empty", config)
}
if config.Logger == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.Logger must not be empty", config)
}
if config.LabelMapFunc == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.LabelMapFunc must not be empty", config)
}
if config.ToClusterFunc == nil {
return nil, microerror.Maskf(invalidConfigError, "%T.ToClusterFunc must not be empty", config)
}
if config.LifeCycleHookName == "" {
return nil, microerror.Maskf(invalidConfigError, "%T.LifeCycleHookName must not be empty", config)
}
r := &Resource{
asg: config.ASG,
ctrlClient: config.CtrlClient,
logger: config.Logger,
labelMapFunc: config.LabelMapFunc,
toClusterFunc: config.ToClusterFunc,
lifeCycleHookName: config.LifeCycleHookName,
}
return r, nil
}
func (r *Resource) Name() string {
return Name
}
func (r *Resource) createDrainerConfig(ctx context.Context, cl infrastructurev1alpha3.AWSCluster, cr metav1.Object, instanceID, privateDNS string) error {
r.logger.Debugf(ctx, "creating drainer config for ec2 instance %#q", instanceID)
dc := &g8sv1alpha1.DrainerConfig{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
annotation.InstanceID: instanceID,
},
Labels: r.labelMapFunc(cr),
Name: privateDNS,
Namespace: cr.GetNamespace(),
},
Spec: g8sv1alpha1.DrainerConfigSpec{
Guest: g8sv1alpha1.DrainerConfigSpecGuest{
Cluster: g8sv1alpha1.DrainerConfigSpecGuestCluster{
API: g8sv1alpha1.DrainerConfigSpecGuestClusterAPI{
Endpoint: key.ClusterAPIEndpoint(cl),
},
ID: key.ClusterID(cr),
},
Node: g8sv1alpha1.DrainerConfigSpecGuestNode{
Name: privateDNS,
},
},
VersionBundle: g8sv1alpha1.DrainerConfigSpecVersionBundle{
Version: "0.2.0",
},
},
}
err := r.ctrlClient.Create(ctx, dc, &ctrlClient.CreateOptions{Raw: &metav1.CreateOptions{}})
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "created drainer config for ec2 instance %#q", instanceID)
return nil
}
// ensure creates DrainerConfigs for ASG instances in terminating/wait state
// then lets node-operator to do its job.
func (r *Resource) ensure(ctx context.Context, obj interface{}) error {
cr, err := meta.Accessor(obj)
if err != nil {
return microerror.Mask(err)
}
cl, err := r.toClusterFunc(ctx, obj)
if err != nil {
return microerror.Mask(err)
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
var asgName string
{
drainable, err := r.asg.Drainable(ctx, cr)
if asg.IsNoASG(err) {
r.logger.Debugf(ctx, "did not find any auto scaling group")
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if asg.IsNoDrainable(err) {
r.logger.Debugf(ctx, "did not find any drainable auto scaling group yet")
if key.IsDeleted(cr) {
r.logger.Debugf(ctx, "keeping finalizers")
finalizerskeptcontext.SetKept(ctx)
}
r.logger.Debugf(ctx, "canceling resource")
return nil
} else if err != nil {
return microerror.Mask(err)
}
asgName = drainable
}
var instances []*autoscaling.Instance
{
r.logger.Debugf(ctx, "finding ec2 instances in %#q state", autoscaling.LifecycleStateTerminatingWait)
i := &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: []*string{
aws.String(asgName),
},
}
o, err := cc.Client.TenantCluster.AWS.AutoScaling.DescribeAutoScalingGroups(i)
if err != nil {
return microerror.Mask(err)
}
var c int
for _, g := range o.AutoScalingGroups {
for _, i := range g.Instances {
c++
r.logger.Debugf(ctx, "checking instance %#q with state %#q", *i.InstanceId, *i.LifecycleState)
if *i.LifecycleState == autoscaling.LifecycleStateTerminatingWait || *i.LifecycleState == autoscaling.LifecycleStateTerminatingProceed {
instances = append(instances, i)
}
}
}
// In case there aren't any EC2 instances in the ASG we assume all draining
// and deletion is properly done.
if c == 0 {
r.logger.Debugf(ctx, "did not find any ec2 instance")
r.logger.Debugf(ctx, "canceling resource")
return nil
}
if len(instances) == 0 {
r.logger.Debugf(ctx, "did not find ec2 instances in %#q state", autoscaling.LifecycleStateTerminatingWait)
// In case there aren't EC2 instances in Terminating:Wait state, we cancel
// and keep finalizers on delete events, so we try again on the next
// reconciliation loop.
if key.IsDeleted(cr) {
r.logger.Debugf(ctx, "keeping finalizers")
finalizerskeptcontext.SetKept(ctx)
}
r.logger.Debugf(ctx, "canceling resource")
return nil
}
r.logger.Debugf(ctx, "found %d ec2 instances in %#q state", len(instances), autoscaling.LifecycleStateTerminatingWait)
}
{
r.logger.Debugf(ctx, "ensuring drainer configs for %d ec2 instances in %#q state", len(instances), autoscaling.LifecycleStateTerminatingWait)
for _, instance := range instances {
r.logger.Debugf(ctx, "finding drainer config for ec2 instance %#q", *instance.InstanceId)
privateDNS, err := r.privateDNSForInstance(ctx, *instance.InstanceId)
if err != nil {
return microerror.Mask(err)
}
if privateDNS == "" {
// It might happen that state transitioning within EC2 happen while we
// try to gather information. An EC2 instance might be in
// Terminating:Wait state and then moves to Terminated before we get a
// chance to gather the drainer configs here. The operator then did its
// job already and we only have to deal with the edge case situation. So
// we just stop here and move on with the other instances.
r.logger.Debugf(ctx, "no private DNS for ec2 instance %#q", *instance.InstanceId)
r.logger.Debugf(ctx, "not draining ec2 instance %#q", *instance.InstanceId)
// Terminated instance that still have lifecycle action in the AWS API.
// Lets finish lifecycle hook to get rid of the instance in next loop.
err = r.completeLifeCycleHook(ctx, *instance.InstanceId, asgName)
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "completed lifecycle hook for terminated ec2 instance %#q", *instance.InstanceId)
continue
}
dc := &g8sv1alpha1.DrainerConfig{}
err = r.ctrlClient.Get(ctx, client.ObjectKey{Name: privateDNS, Namespace: cr.GetNamespace()}, dc)
if errors.IsNotFound(err) {
r.logger.Debugf(ctx, "did not find drainer config for ec2 instance %#q", *instance.InstanceId)
// create drainerConfig for the instance
err := r.createDrainerConfig(ctx, cl, cr, *instance.InstanceId, privateDNS)
if err != nil {
return microerror.Mask(err)
}
} else if err != nil {
return microerror.Mask(err)
} else {
// if the cluster id or instance id does not match, delete the bad CR and recreate it
if key.IsWrongDrainerConfig(dc, key.ClusterID(&cl), *instance.InstanceId) {
o := &metav1.DeleteOptions{}
err = r.ctrlClient.Delete(ctx, dc, &ctrlClient.DeleteOptions{Raw: o})
if err != nil {
return microerror.Mask(err)
}
r.logger.Debugf(ctx, "deleted leftover drainer config for ec2 instance %#q", *instance.InstanceId)
// cancel resource to let deletion happen the drainer config will be
// recreated with proper details next loop
r.logger.Debugf(ctx, "canceling resource")
return nil
} else {
r.logger.Debugf(ctx, "found drainer config for ec2 instance %#q", *instance.InstanceId)
}
}
}
r.logger.Debugf(ctx, "ensured drainer configs for %d ec2 instances in %#q state", len(instances), autoscaling.LifecycleStateTerminatingWait)
}
return nil
}
func (r *Resource) privateDNSForInstance(ctx context.Context, instanceID string) (string, error) {
i := &ec2.DescribeInstancesInput{
InstanceIds: []*string{
aws.String(instanceID),
},
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return "", microerror.Mask(err)
}
o, err := cc.Client.TenantCluster.AWS.EC2.DescribeInstances(i)
if err != nil {
return "", microerror.Mask(err)
}
if len(o.Reservations) != 1 {
return "", microerror.Maskf(executionFailedError, "expected 1 reservation, got %d", len(o.Reservations))
}
if len(o.Reservations[0].Instances) != 1 {
return "", microerror.Maskf(executionFailedError, "expected 1 node, got %d", len(o.Reservations[0].Instances))
}
privateDNS := *o.Reservations[0].Instances[0].PrivateDnsName
return privateDNS, nil
}
func (r *Resource) completeLifeCycleHook(ctx context.Context, instanceID, asgName string) error {
r.logger.Debugf(ctx, "completing life cycle hook action for tenant cluster node %#q", instanceID)
i := &autoscaling.CompleteLifecycleActionInput{
AutoScalingGroupName: aws.String(asgName),
InstanceId: aws.String(instanceID),
LifecycleActionResult: aws.String("CONTINUE"),
LifecycleHookName: aws.String(r.lifeCycleHookName),
}
cc, err := controllercontext.FromContext(ctx)
if err != nil {
return microerror.Mask(err)
}
_, err = cc.Client.TenantCluster.AWS.AutoScaling.CompleteLifecycleAction(i)
if IsNoActiveLifeCycleAction(err) {
r.logger.Debugf(ctx, "not found life cycle hook action for tenant cluster node %#q", instanceID)
} else if err != nil {
return microerror.Mask(err)
} else {
r.logger.Debugf(ctx, "completed life cycle hook action for tenant cluster node %#q", instanceID)
}
r.logger.Debugf(ctx, "completed life cycle hook action for tenant cluster node %#q", instanceID)
return nil
}