This repository has been archived by the owner on Jun 13, 2023. It is now read-only.
/
options.go
353 lines (276 loc) · 9.24 KB
/
options.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
package v2
import (
"context"
"errors"
"os"
"time"
"github.com/kyma-project/module-manager/internal"
"github.com/kyma-project/module-manager/pkg/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/manager"
)
const (
FinalizerDefault = "declarative.kyma-project.io/finalizer"
FieldOwnerDefault = "declarative.kyma-project.io/applier"
EventRecorderDefault = "declarative.kyma-project.io/events"
DefaultSkipReconcileLabel = "declarative.kyma-project.io/skip-reconciliation"
DefaultCacheKey = "declarative.kyma-project.io/cache-key"
DefaultInMemoryParseTTL = 24 * time.Hour
)
func DefaultOptions() *Options {
return (&Options{}).Apply(
WithDeleteCRDs(false),
WithNamespace(metav1.NamespaceDefault, false),
WithFinalizer(FinalizerDefault),
WithFieldOwner(FieldOwnerDefault),
WithPostRenderTransform(
managedByDeclarativeV2,
watchedByOwnedBy,
kymaComponentTransform,
disclaimerTransform,
),
WithPermanentConsistencyCheck(false),
WithSingletonClientCache(NewMemorySingletonClientCache()),
WithClientCacheKeyFromLabelOrResource(DefaultCacheKey),
WithManifestCache(os.TempDir()),
WithSkipReconcileOn(SkipReconcileOnDefaultLabelPresentAndTrue),
WithManifestParser(NewInMemoryCachedManifestParser(DefaultInMemoryParseTTL)),
)
}
type Options struct {
record.EventRecorder
Config *rest.Config
client.Client
TargetCluster ClusterFn
SpecResolver
ClientCache
ClientCacheKeyFn
ManifestParser
ManifestCache
CustomReadyCheck ReadyCheck
Namespace string
CreateNamespace bool
Finalizer string
ServerSideApply bool
FieldOwner client.FieldOwner
PostRenderTransforms []ObjectTransform
PostRuns []PostRun
PreDeletes []PreDelete
DeletePrerequisites bool
ShouldSkip SkipReconcile
CtrlOnSuccess ctrl.Result
}
type Option interface {
Apply(options *Options)
}
func (o *Options) Apply(options ...Option) *Options {
for i := range options {
options[i].Apply(o)
}
return o
}
type WithNamespaceOption struct {
name string
createIfMissing bool
}
func WithNamespace(name string, createIfMissing bool) WithNamespaceOption {
return WithNamespaceOption{
name: name,
createIfMissing: createIfMissing,
}
}
func (o WithNamespaceOption) Apply(options *Options) {
options.Namespace = o.name
options.CreateNamespace = o.createIfMissing
}
type WithFieldOwner client.FieldOwner
func (o WithFieldOwner) Apply(options *Options) {
options.FieldOwner = client.FieldOwner(o)
}
type WithFinalizer string
func (o WithFinalizer) Apply(options *Options) {
options.Finalizer = string(o)
}
type WithManagerOption struct {
manager.Manager
}
func WithManager(mgr manager.Manager) WithManagerOption {
return WithManagerOption{Manager: mgr}
}
func (o WithManagerOption) Apply(options *Options) {
options.EventRecorder = o.GetEventRecorderFor(EventRecorderDefault)
options.Config = o.GetConfig()
options.Client = o.GetClient()
}
type WithCustomResourceLabels labels.Set
func (o WithCustomResourceLabels) Apply(options *Options) {
labelTransform := func(ctx context.Context, object Object, resources []*unstructured.Unstructured) error {
for _, targetResource := range resources {
lbls := targetResource.GetLabels()
if lbls == nil {
lbls = labels.Set{}
}
for s := range o {
lbls[s] = o[s]
}
targetResource.SetLabels(lbls)
}
return nil
}
options.PostRenderTransforms = append(options.PostRenderTransforms, labelTransform)
}
func WithSpecResolver(resolver SpecResolver) SpecResolverOption {
return SpecResolverOption{resolver}
}
type SpecResolverOption struct {
SpecResolver
}
func (o SpecResolverOption) Apply(options *Options) {
options.SpecResolver = o
}
type ObjectTransform = func(context.Context, Object, []*unstructured.Unstructured) error
func WithPostRenderTransform(transforms ...ObjectTransform) PostRenderTransformOption {
return PostRenderTransformOption{transforms}
}
type PostRenderTransformOption struct {
ObjectTransforms []ObjectTransform
}
func (o PostRenderTransformOption) Apply(options *Options) {
options.PostRenderTransforms = append(options.PostRenderTransforms, o.ObjectTransforms...)
}
// Hook defines a Hook into the declarative reconciliation
// skr is the runtime cluster
// kcp is the control-plane cluster
// obj is guaranteed to be the reconciled object and also to always preside in kcp.
type Hook func(ctx context.Context, skr Client, kcp client.Client, obj Object) error
// WARNING: DO NOT USE THESE HOOKS IF YOU DO NOT KNOW THE RECONCILIATION LIFECYCLE OF THE DECLARATIVE API.
// IT CAN BREAK YOUR RECONCILIATION AND IF YOU ADJUST THE OBJECT, ALSO LEAD TO
// INVALID STATES.
type (
// PostRun is executed after every successful render+reconciliation of the manifest.
PostRun Hook
// PreDelete is executed before any deletion of resources calculated from the status.
PreDelete Hook
)
// WithPostRun applies PostRun.
type WithPostRun []PostRun
func (o WithPostRun) Apply(options *Options) {
options.PostRuns = append(options.PostRuns, o...)
}
// WithPreDelete applies PreDelete.
type WithPreDelete []PreDelete
func (o WithPreDelete) Apply(options *Options) {
options.PreDeletes = append(options.PreDeletes, o...)
}
type WithPeriodicConsistencyCheck time.Duration
func (o WithPeriodicConsistencyCheck) Apply(options *Options) {
options.CtrlOnSuccess.RequeueAfter = time.Duration(o)
}
type WithPermanentConsistencyCheck bool
func (o WithPermanentConsistencyCheck) Apply(options *Options) {
options.CtrlOnSuccess = ctrl.Result{Requeue: bool(o)}
}
type WithSingletonClientCacheOption struct {
ClientCache
}
func WithSingletonClientCache(cache ClientCache) WithSingletonClientCacheOption {
return WithSingletonClientCacheOption{ClientCache: cache}
}
func (o WithSingletonClientCacheOption) Apply(options *Options) {
options.ClientCache = o
}
type WithDeleteCRDs bool
func (o WithDeleteCRDs) Apply(options *Options) {
options.DeletePrerequisites = bool(o)
}
type ManifestCache string
const NoManifestCache ManifestCache = "no-cache"
type WithManifestCache ManifestCache
func (o WithManifestCache) Apply(options *Options) {
options.ManifestCache = ManifestCache(o)
}
func WithManifestParser(parser ManifestParser) WithManifestParserOption {
return WithManifestParserOption{ManifestParser: parser}
}
type WithManifestParserOption struct {
ManifestParser
}
func (o WithManifestParserOption) Apply(options *Options) {
options.ManifestParser = o.ManifestParser
}
type WithCustomReadyCheckOption struct {
ReadyCheck
}
func WithCustomReadyCheck(check ReadyCheck) WithCustomReadyCheckOption {
return WithCustomReadyCheckOption{ReadyCheck: check}
}
func (o WithCustomReadyCheckOption) Apply(options *Options) {
options.CustomReadyCheck = o
}
type ClusterFn func(context.Context, Object) (*types.ClusterInfo, error)
func WithRemoteTargetCluster(configFn ClusterFn) WithRemoteTargetClusterOption {
return WithRemoteTargetClusterOption{ClusterFn: configFn}
}
type WithRemoteTargetClusterOption struct {
ClusterFn
}
func (o WithRemoteTargetClusterOption) Apply(options *Options) {
options.TargetCluster = o.ClusterFn
}
func WithSkipReconcileOn(skipReconcile SkipReconcile) WithSkipReconcileOnOption {
return WithSkipReconcileOnOption{skipReconcile: skipReconcile}
}
type SkipReconcile func(context.Context, Object) (skip bool)
// SkipReconcileOnDefaultLabelPresentAndTrue determines SkipReconcile by checking if DefaultSkipReconcileLabel is true.
func SkipReconcileOnDefaultLabelPresentAndTrue(ctx context.Context, object Object) bool {
log.FromContext(ctx, "skip-label", DefaultSkipReconcileLabel).
V(internal.DebugLogLevel).Info("resource gets skipped because of label")
return object.GetLabels() != nil && object.GetLabels()[DefaultSkipReconcileLabel] == "true"
}
type WithSkipReconcileOnOption struct {
skipReconcile SkipReconcile
}
func (o WithSkipReconcileOnOption) Apply(options *Options) {
options.ShouldSkip = o.skipReconcile
}
type ClientCacheKeyFn func(ctx context.Context, obj Object) any
func WithClientCacheKeyFromLabelOrResource(label string) WithClientCacheKeyOption {
cacheKey := func(ctx context.Context, resource Object) any {
logger := log.FromContext(ctx)
if resource == nil {
return client.ObjectKey{}
}
label, err := internal.GetResourceLabel(resource, label)
objectKey := client.ObjectKeyFromObject(resource)
var labelErr *types.LabelNotFoundError
if errors.As(err, &labelErr) {
logger.V(internal.DebugLogLevel).Info(
label+" missing on resource, it will be cached "+
"based on resource name and namespace.",
"resource", objectKey,
)
return objectKey
}
logger.V(internal.DebugLogLevel).Info(
"resource will be cached based on "+label,
"resource", objectKey,
"label", label,
"labelValue", label,
)
return client.ObjectKey{Name: label, Namespace: resource.GetNamespace()}
}
return WithClientCacheKeyOption{ClientCacheKeyFn: cacheKey}
}
type WithClientCacheKeyOption struct {
ClientCacheKeyFn
}
func (o WithClientCacheKeyOption) Apply(options *Options) {
options.ClientCacheKeyFn = o.ClientCacheKeyFn
}