-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreconciler.go
101 lines (88 loc) · 2.89 KB
/
reconciler.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
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// 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 simplecontrollerruntime
import (
"context"
"errors"
"fmt"
"time"
)
type RequeueAfterError struct {
d time.Duration
msg string
}
func (rae RequeueAfterError) Error() string {
if rae.msg != "" {
return fmt.Sprintf("%v so requeuing after %v", rae.msg, rae.d)
}
return fmt.Sprintf("requeuing after %v", rae.d)
}
func NewRequeueAfterError(d time.Duration, msg string) error {
return RequeueAfterError{d, msg}
}
type Reconciler[T ReconcileEvent] struct {
// Reconcile function which will be called whenever there is an event on EventChan
ReconcileFunction func(context.Context, T) error
// Maximum amount of time to spend trying to reconcile one item before
// moving onto the next item.
ReconcileTimeout time.Duration
// The queue which the reconciler will receive events to reconcile on.
Queue Dequeuer[T]
}
func (r *Reconciler[T]) Start(ctx context.Context) {
go func() {
logger := GetLoggerFromContextOrDefault(ctx)
for {
// queue.Get will block until an item is available to
// return.
item, err := r.Queue.Dequeue(ctx)
if err != nil {
logger.ErrorContext(ctx, "Failed to get item from queue", "error", err)
} else {
// NOTE: shadowing logger variable is intentional
logger := logger.With("item", item)
logger.InfoContext(ctx, "Reconciling item")
timeoutCtx, cancel := context.WithTimeout(ctx, r.ReconcileTimeout)
err := r.ReconcileFunction(timeoutCtx, item)
// Make sure timeout context is canceled to
// prevent orphaned resources
cancel()
// If reconcile has requested that we requeue the item
// by returning a RequeueAfterError then requeue the
// item with the duration specified, otherwise mark the
// item as Done.
var requeueAfterError RequeueAfterError
switch {
case errors.As(err, &requeueAfterError):
logger.InfoContext(ctx, "Requeue item", "reason", err)
r.Queue.RequeueAfter(item, requeueAfterError.d)
case err != nil:
logger.ErrorContext(ctx, "Failed to reconcile item", "error", err)
fallthrough
default:
r.Queue.Done(item)
}
}
// Check if the parent context done if so we also need
// to exit.
select {
case <-ctx.Done():
logger.InfoContext(ctx, "Shutting down", "error", ctx.Err())
return
default:
}
}
}()
}