-
Notifications
You must be signed in to change notification settings - Fork 0
/
awsvpcpeering_controller.go
552 lines (484 loc) · 17.3 KB
/
awsvpcpeering_controller.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/*
Copyright 2022.
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 clusterresources
import (
"context"
"errors"
"github.com/go-logr/logr"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/event"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"github.com/instaclustr/operator/apis/clusterresources/v1beta1"
"github.com/instaclustr/operator/pkg/instaclustr"
"github.com/instaclustr/operator/pkg/models"
"github.com/instaclustr/operator/pkg/ratelimiter"
"github.com/instaclustr/operator/pkg/scheduler"
)
// AWSVPCPeeringReconciler reconciles a AWSVPCPeering object
type AWSVPCPeeringReconciler struct {
client.Client
Scheme *runtime.Scheme
API instaclustr.API
Scheduler scheduler.Interface
EventRecorder record.EventRecorder
}
//+kubebuilder:rbac:groups=clusterresources.instaclustr.com,resources=awsvpcpeerings,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=clusterresources.instaclustr.com,resources=awsvpcpeerings/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=clusterresources.instaclustr.com,resources=awsvpcpeerings/finalizers,verbs=update
//+kubebuilder:rbac:groups="",resources=events,verbs=create;patch
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile
func (r *AWSVPCPeeringReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := log.FromContext(ctx)
aws := &v1beta1.AWSVPCPeering{}
err := r.Client.Get(ctx, req.NamespacedName, aws)
if err != nil {
if k8serrors.IsNotFound(err) {
l.Error(err, "AWS VPC Peering resource is not found", "request", req)
return ctrl.Result{}, nil
}
l.Error(err, "unable to fetch AWS VPC Peering", "request", req)
return ctrl.Result{}, err
}
switch aws.Annotations[models.ResourceStateAnnotation] {
case models.CreatingEvent:
return r.handleCreatePeering(ctx, aws, l)
case models.UpdatingEvent:
return r.handleUpdatePeering(ctx, aws, l)
case models.DeletingEvent:
return r.handleDeletePeering(ctx, aws, l)
default:
l.Info("event isn't handled",
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"Request", req,
"event", aws.Annotations[models.ResourceStateAnnotation])
return ctrl.Result{}, nil
}
}
func (r *AWSVPCPeeringReconciler) handleCreatePeering(
ctx context.Context,
aws *v1beta1.AWSVPCPeering,
l logr.Logger,
) (ctrl.Result, error) {
if aws.Status.ID == "" {
var cdcID string
var err error
if aws.Spec.ClusterRef != nil {
cdcID, err = GetDataCentreID(r.Client, ctx, aws.Spec.ClusterRef)
if err != nil {
l.Error(err, "Cannot get CDCID",
"Cluster reference", aws.Spec.ClusterRef,
)
return ctrl.Result{}, err
}
l.Info(
"Creating AWS VPC Peering resource from the cluster reference",
"cluster reference", aws.Spec.ClusterRef,
"cdcID ID", cdcID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
)
} else {
cdcID = aws.Spec.DataCentreID
l.Info(
"Creating AWS VPC Peering resource",
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
)
}
awsStatus, err := r.API.CreateAWSVPCPeering(&aws.Spec, cdcID)
if err != nil {
l.Error(
err, "cannot create AWS VPC Peering resource",
"AWS VPC Peering resource spec", aws.Spec,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.CreationFailed,
"Resource creation on the Instaclustr is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
r.EventRecorder.Eventf(
aws, models.Normal, models.Created,
"Resource creation request is sent. Resource ID: %s",
awsStatus.ID,
)
patch := aws.NewPatch()
aws.Status.PeeringStatus = *awsStatus
aws.Status.CDCID = cdcID
err = r.Status().Patch(ctx, aws, patch)
if err != nil {
l.Error(err, "cannot patch AWS VPC Peering resource status",
"AWS Peering ID", awsStatus.ID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"AWS VPC Peering metadata", aws.ObjectMeta,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.PatchFailed,
"Resource status patch is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
controllerutil.AddFinalizer(aws, models.DeletionFinalizer)
aws.Annotations[models.ResourceStateAnnotation] = models.CreatedEvent
err = r.Patch(ctx, aws, patch)
if err != nil {
l.Error(err, "cannot patch AWS VPC Peering resource metadata",
"AWS Peering ID", awsStatus.ID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"AWS VPC Peering metadata", aws.ObjectMeta,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.PatchFailed,
"Resource patch is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
l.Info(
"AWS VPC Peering resource was created",
"AWS Peering ID", awsStatus.ID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
)
}
err := r.startAWSVPCPeeringStatusJob(aws)
if err != nil {
l.Error(err, "cannot start AWS VPC Peering checker status job",
"AWS VPC Peering ID", aws.Status.ID)
r.EventRecorder.Eventf(
aws, models.Warning, models.CreationFailed,
"Resource status check job is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
r.EventRecorder.Eventf(
aws, models.Normal, models.Created,
"Resource status check job is started",
)
return ctrl.Result{}, nil
}
func (r *AWSVPCPeeringReconciler) handleUpdatePeering(
ctx context.Context,
aws *v1beta1.AWSVPCPeering,
l logr.Logger,
) (ctrl.Result, error) {
instaAWSPeering, err := r.API.GetAWSVPCPeering(aws.Status.ID)
if err != nil {
l.Error(err, "Cannot get AWS VPC Peering from Instaclutr",
"AWS VPC Peering ID", aws.Status.ID,
)
r.EventRecorder.Eventf(aws, models.Warning, models.UpdateFailed,
"Cannot get AWS VPC Peering from Instaclutr. Reason: %v",
)
return ctrl.Result{}, err
}
if aws.Annotations[models.ExternalChangesAnnotation] == models.True {
if !subnetsEqual(instaAWSPeering.PeerSubnets, aws.Spec.PeerSubnets) {
l.Info("The resource specification still differs from the Instaclustr resource specification, please reconcile it manually",
"AWS VPC ID", aws.Status.ID,
"k8s peerSubnets", aws.Spec.PeerSubnets,
"instaclustr peerSubnets", instaAWSPeering.PeerSubnets,
)
r.EventRecorder.Eventf(aws, models.Warning, models.UpdateFailed,
"The resource specification still differs from the Instaclustr resource specification, please reconcile it manually.",
)
return ctrl.Result{}, nil
}
patch := aws.NewPatch()
delete(aws.Annotations, models.ExternalChangesAnnotation)
err = r.Patch(ctx, aws, patch)
if err != nil {
l.Error(err, "Cannot delete external changes annotation from the resource",
"AWS VPC Peering ID", aws.Status.ID,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.PatchFailed,
"Deleting external changes annotation is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
l.Info("External changes of the k8s resource specification was fixed",
"AWS VPC Peering ID", aws.Status.ID,
)
r.EventRecorder.Eventf(aws, models.Normal, models.ExternalChanges,
"External changes of the k8s resource specification was fixed",
)
return ctrl.Result{}, nil
}
err = r.API.UpdatePeering(aws.Status.ID, instaclustr.AWSPeeringEndpoint, &aws.Spec)
if err != nil {
l.Error(err, "cannot update AWS VPC Peering",
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"Subnets", aws.Spec.PeerSubnets,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.UpdateFailed,
"Resource update on the Instaclustr API is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
patch := aws.NewPatch()
aws.Annotations[models.ResourceStateAnnotation] = models.UpdatedEvent
err = r.Patch(ctx, aws, patch)
if err != nil {
l.Error(err, "cannot patch AWS VPC Peering resource metadata",
"AWS Peering ID", aws.Status.ID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"AWS VPC Peering metadata", aws.ObjectMeta,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.PatchFailed,
"Resource patch is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
l.Info("AWS VPC Peering resource has been updated",
"AWS VPC Peering ID", aws.Status.ID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"AWS VPC Peering Data Centre ID", aws.Status.CDCID,
"AWS VPC Peering Status", aws.Status.PeeringStatus,
)
return ctrl.Result{}, nil
}
func (r *AWSVPCPeeringReconciler) handleDeletePeering(
ctx context.Context,
aws *v1beta1.AWSVPCPeering,
l logr.Logger,
) (ctrl.Result, error) {
status, err := r.API.GetPeeringStatus(aws.Status.ID, instaclustr.AWSPeeringEndpoint)
if err != nil && !errors.Is(err, instaclustr.NotFound) {
l.Error(
err, "cannot get AWS VPC Peering status from the Instaclustr API",
"AWS Peering ID", status.ID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.FetchFailed,
"Resource fetch from the Instaclustr API is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
if status != nil {
err = r.API.DeletePeering(aws.Status.ID, instaclustr.AWSPeeringEndpoint)
if err != nil {
l.Error(err, "cannot update AWS VPC Peering resource status",
"AWS Peering ID", aws.Status.ID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"AWS VPC Peering metadata", aws.ObjectMeta,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.DeletionFailed,
"Resource deletion on the Instaclustr API is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
r.EventRecorder.Eventf(
aws, models.Normal, models.DeletionStarted,
"Resource deletion request is sent",
)
return ctrl.Result{}, err
}
r.Scheduler.RemoveJob(aws.GetJobID(scheduler.StatusChecker))
patch := aws.NewPatch()
controllerutil.RemoveFinalizer(aws, models.DeletionFinalizer)
aws.Annotations[models.ResourceStateAnnotation] = models.DeletedEvent
err = r.Patch(ctx, aws, patch)
if err != nil {
l.Error(err, "cannot patch AWS VPC Peering resource metadata",
"AWS Peering ID", aws.Status.ID,
"AWS Account ID", aws.Spec.PeerAWSAccountID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"AWS VPC Peering metadata", aws.ObjectMeta,
)
r.EventRecorder.Eventf(
aws, models.Warning, models.PatchFailed,
"Resource patch is failed. Reason: %v",
err,
)
return ctrl.Result{}, err
}
l.Info("AWS VPC Peering has been deleted",
"AWS VPC Peering ID", aws.Status.ID,
"VPC ID", aws.Spec.PeerVPCID,
"Region", aws.Spec.PeerRegion,
"AWS VPC Peering Data Centre ID", aws.Status.CDCID,
"AWS VPC Peering Status", aws.Status.PeeringStatus,
)
return ctrl.Result{}, nil
}
func (r *AWSVPCPeeringReconciler) startAWSVPCPeeringStatusJob(awsPeering *v1beta1.AWSVPCPeering) error {
job := r.newWatchStatusJob(awsPeering)
err := r.Scheduler.ScheduleJob(awsPeering.GetJobID(scheduler.StatusChecker), scheduler.ClusterStatusInterval, job)
if err != nil {
return err
}
return nil
}
func (r *AWSVPCPeeringReconciler) newWatchStatusJob(awsPeering *v1beta1.AWSVPCPeering) scheduler.Job {
l := log.Log.WithValues("component", "AWSVPCPeeringStatusJob")
return func() error {
ctx := context.Background()
namespacedName := client.ObjectKeyFromObject(awsPeering)
err := r.Get(ctx, namespacedName, awsPeering)
if k8serrors.IsNotFound(err) {
l.Info("Resource is not found in the k8s cluster. Closing Instaclustr status sync.",
"namespaced name", namespacedName,
)
r.Scheduler.RemoveJob(awsPeering.GetJobID(scheduler.StatusChecker))
return nil
}
instaAWSPeering, err := r.API.GetAWSVPCPeering(awsPeering.Status.ID)
if err != nil {
if errors.Is(err, instaclustr.NotFound) {
return r.handleExternalDelete(ctx, awsPeering)
}
l.Error(err, "cannot get AWS VPC Peering Status from Inst API",
"AWS VPC Peering ID", awsPeering.Status.ID,
)
return err
}
instaPeeringStatus := v1beta1.PeeringStatus{
ID: instaAWSPeering.ID,
StatusCode: instaAWSPeering.StatusCode,
}
if !arePeeringStatusesEqual(&instaPeeringStatus, &awsPeering.Status.PeeringStatus) {
l.Info("AWS VPC Peering status of k8s is different from Instaclustr. Reconcile statuses..",
"AWS VPC Peering Status from Inst API", instaPeeringStatus,
"AWS VPC Peering Status", awsPeering.Status)
patch := awsPeering.NewPatch()
awsPeering.Status.PeeringStatus = instaPeeringStatus
err := r.Status().Patch(ctx, awsPeering, patch)
if err != nil {
return err
}
}
if awsPeering.Status.StatusCode == models.AWSVPCPeeringStatusCodeDeleted {
l.Info("The AWSPeering was deleted on AWS, stopping job...")
r.EventRecorder.Event(awsPeering, models.Warning, models.DeletedEvent,
"The AWSPeering was deleted on AWS, stopping job...",
)
r.Scheduler.RemoveJob(awsPeering.GetJobID(scheduler.StatusChecker))
return nil
}
if awsPeering.Annotations[models.ResourceStateAnnotation] != models.UpdatingEvent &&
awsPeering.Annotations[models.ExternalChangesAnnotation] != models.True &&
!subnetsEqual(instaAWSPeering.PeerSubnets, awsPeering.Spec.PeerSubnets) {
l.Info("The k8s resource specification doesn't match the specification of Instaclustr, please change it manually",
"k8s peerSubnets", instaAWSPeering.PeerSubnets,
"instaclutr peerSubnets", awsPeering.Spec.PeerSubnets,
)
patch := awsPeering.NewPatch()
awsPeering.Annotations[models.ExternalChangesAnnotation] = models.True
err = r.Patch(ctx, awsPeering, patch)
if err != nil {
l.Error(err, "Cannot patch the resource with external changes annotation")
return err
}
r.EventRecorder.Event(awsPeering, models.Warning, models.ExternalChanges,
"k8s spec doesn't match spec of Instaclutr, please change it manually",
)
}
return nil
}
}
// SetupWithManager sets up the controller with the Manager.
func (r *AWSVPCPeeringReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
WithOptions(controller.Options{
RateLimiter: ratelimiter.NewItemExponentialFailureRateLimiterWithMaxTries(ratelimiter.DefaultBaseDelay, ratelimiter.DefaultMaxDelay)}).
For(&v1beta1.AWSVPCPeering{}, builder.WithPredicates(predicate.Funcs{
CreateFunc: func(event event.CreateEvent) bool {
event.Object.SetAnnotations(map[string]string{models.ResourceStateAnnotation: models.CreatingEvent})
if event.Object.GetDeletionTimestamp() != nil {
event.Object.SetAnnotations(map[string]string{models.ResourceStateAnnotation: models.DeletingEvent})
}
return true
},
UpdateFunc: func(event event.UpdateEvent) bool {
newObj := event.ObjectNew.(*v1beta1.AWSVPCPeering)
if newObj.DeletionTimestamp != nil {
newObj.Annotations[models.ResourceStateAnnotation] = models.DeletingEvent
return true
}
if newObj.Status.ID == "" {
newObj.Annotations[models.ResourceStateAnnotation] = models.CreatingEvent
return true
}
if newObj.Generation == event.ObjectOld.GetGeneration() {
return false
}
newObj.Annotations[models.ResourceStateAnnotation] = models.UpdatingEvent
return true
},
DeleteFunc: func(event event.DeleteEvent) bool {
return false
},
GenericFunc: func(event event.GenericEvent) bool {
event.Object.SetAnnotations(map[string]string{models.ResourceStateAnnotation: models.GenericEvent})
return true
},
})).Complete(r)
}
func (r *AWSVPCPeeringReconciler) handleExternalDelete(ctx context.Context, key *v1beta1.AWSVPCPeering) error {
l := log.FromContext(ctx)
patch := key.NewPatch()
key.Status.StatusCode = models.DeletedStatus
err := r.Status().Patch(ctx, key, patch)
if err != nil {
return err
}
l.Info(instaclustr.MsgInstaclustrResourceNotFound)
r.EventRecorder.Eventf(key, models.Warning, models.ExternalDeleted, instaclustr.MsgInstaclustrResourceNotFound)
r.Scheduler.RemoveJob(key.GetJobID(scheduler.StatusChecker))
return nil
}