forked from cisco-open/operator-tools
/
component.go
205 lines (183 loc) · 6.76 KB
/
component.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
// Copyright © 2020 Banzai Cloud
//
// 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 reconciler
import (
"context"
"emperror.dev/errors"
"github.com/banzaicloud/operator-tools/pkg/resources"
"github.com/banzaicloud/operator-tools/pkg/types"
"github.com/banzaicloud/operator-tools/pkg/utils"
"github.com/go-logr/logr"
"k8s.io/apimachinery/pkg/runtime"
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/reconcile"
)
type ComponentReconciler interface {
Reconcile(object runtime.Object) (*reconcile.Result, error)
RegisterWatches(*builder.Builder)
}
type Watches interface {
SetupAdditionalWatches(c controller.Controller) error
}
type ComponentWithStatus interface {
Update(object runtime.Object, status types.ReconcileStatus, msg string) error
IsSkipped(object runtime.Object) bool
IsEnabled(object runtime.Object) bool
}
type ComponentLifecycle interface {
OnFinished(object runtime.Object) error
}
// ComponentReconcilers is a list of component reconcilers that support getting components in Install and Uninstall order.
type ComponentReconcilers []ComponentReconciler
func (c ComponentReconcilers) Get(order utils.ResourceOrder) []ComponentReconciler {
if order != utils.UninstallResourceOrder {
return c
}
components := []ComponentReconciler{}
for i := len(c) - 1; i >= 0; i-- {
components = append(components, c[i])
}
return components
}
// Dispatcher orchestrates reconciliation of multiple ComponentReconciler objects
// focusing on handing off reconciled object to all of its components and calculating an aggregated result to return.
// It requires a ResourceGetter callback and optionally can leverage a ResourceFilter and a CompletionHandler
type Dispatcher struct {
client.Client
Log logr.Logger
ResourceGetter func(req ctrl.Request) (runtime.Object, error)
ResourceFilter func(runtime.Object) (bool, error)
CompletionHandler func(runtime.Object, ctrl.Result, error) (ctrl.Result, error)
ComponentReconcilers ComponentReconcilers
// ForceResourceOrder can be used to force a given resource ordering regardless of an object being deleted with
// finalizers.
ForceResourceOrder utils.ResourceOrder
}
// Reconcile implements reconcile.Reconciler in a generic way from the controller-runtime library
func (r *Dispatcher) Reconcile(_ context.Context, req ctrl.Request) (ctrl.Result, error) {
object, err := r.ResourceGetter(req)
if err != nil {
return reconcile.Result{}, errors.WithStack(err)
}
if object == nil {
return reconcile.Result{}, nil
}
if r.ResourceFilter != nil {
shouldReconcile, err := r.ResourceFilter(object)
if err != nil || !shouldReconcile {
return reconcile.Result{}, errors.WithStack(err)
}
}
result, err := r.Handle(object)
if r.CompletionHandler != nil {
return r.CompletionHandler(object, result, errors.WithStack(err))
}
if err != nil {
return result, errors.WithStack(err)
}
return result, nil
}
// Handle receives a single object and dispatches it to all the components
// Components need to understand how to interpret the object
func (r *Dispatcher) Handle(object runtime.Object) (ctrl.Result, error) {
isBeingDeleted, err := resources.IsObjectBeingDeleted(object)
if err != nil {
return ctrl.Result{}, err
}
componentExecutionOrder := utils.InstallResourceOrder
if isBeingDeleted {
componentExecutionOrder = utils.UninstallResourceOrder
}
if r.ForceResourceOrder != "" {
componentExecutionOrder = r.ForceResourceOrder
}
combinedResult := &CombinedResult{}
for _, cr := range r.ComponentReconcilers.Get(componentExecutionOrder) {
if cr, ok := cr.(ComponentWithStatus); ok {
status := types.ReconcileStatusReconciling
if cr.IsSkipped(object) {
status = types.ReconcileStatusUnmanaged
}
if uerr := cr.Update(object, status, ""); uerr != nil {
combinedResult.CombineErr(errors.WrapIf(uerr, "unable to update status for component"))
}
if cr.IsSkipped(object) {
continue
}
}
// Any patch/update command can update the object, thus sequent steps will be
// remove steps instead. To work around that let's requeue with the correct order.
currentDeletionStatus, err := resources.IsObjectBeingDeleted(object)
if err != nil {
return ctrl.Result{}, err
}
if currentDeletionStatus != isBeingDeleted {
// Requeue object so that we can start reconciling in inverse order
combinedResult.Combine(&reconcile.Result{Requeue: true, RequeueAfter: 0}, errors.New("object being deleted requeing object"))
break
}
result, err := cr.Reconcile(object)
if cr, ok := cr.(ComponentWithStatus); ok {
if err != nil {
if uerr := cr.Update(object, types.ReconcileStatusFailed, err.Error()); uerr != nil {
combinedResult.CombineErr(errors.WrapIf(uerr, "unable to update status for component"))
}
} else {
if result == nil || (!result.Requeue && result.RequeueAfter == 0) {
status := types.ReconcileStatusRemoved
if cr.IsEnabled(object) {
status = types.ReconcileStatusAvailable
}
if uerr := cr.Update(object, status, ""); uerr != nil {
combinedResult.CombineErr(errors.WrapIf(uerr, "unable to update status for component"))
}
}
}
}
if cr, ok := cr.(ComponentLifecycle); ok {
if err := cr.OnFinished(object); err != nil {
combinedResult.Combine(result, errors.WrapIf(err, "failed to notify component on finish"))
}
}
combinedResult.Combine(result, errors.WithStack(err))
if cr, ok := cr.(interface{ IsOptional() bool }); ok {
if err != nil && !cr.IsOptional() {
break
}
}
}
return combinedResult.Result, combinedResult.Err
}
// RegisterWatches dispatches the watch registration builder to all its components
func (r *Dispatcher) RegisterWatches(b *builder.Builder) *builder.Builder {
for _, cr := range r.ComponentReconcilers {
cr.RegisterWatches(b)
}
return b
}
// SetupAdditionalWatches dispatches the controller for watch registration to all its components
func (r *Dispatcher) SetupAdditionalWatches(c controller.Controller) error {
for _, cr := range r.ComponentReconcilers {
if cr, ok := cr.(Watches); ok {
err := cr.SetupAdditionalWatches(c)
if err != nil {
return errors.WithStack(err)
}
}
}
return nil
}