-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
reconcile.go
477 lines (432 loc) · 16.3 KB
/
reconcile.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
// Copyright 2018 The Operator-SDK Authors
//
// 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 controller
import (
"context"
"errors"
"fmt"
"strconv"
"time"
rpb "helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/storage/driver"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/tools/record"
"k8s.io/client-go/util/retry"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"github.com/operator-framework/operator-sdk/internal/helm/internal/diff"
"github.com/operator-framework/operator-sdk/internal/helm/internal/types"
"github.com/operator-framework/operator-sdk/internal/helm/release"
)
// blank assignment to verify that HelmOperatorReconciler implements reconcile.Reconciler
var _ reconcile.Reconciler = &HelmOperatorReconciler{}
// ReleaseHookFunc defines a function signature for release hooks.
type ReleaseHookFunc func(*rpb.Release) error
// HelmOperatorReconciler reconciles custom resources as Helm releases.
type HelmOperatorReconciler struct {
Client client.Client
EventRecorder record.EventRecorder
GVK schema.GroupVersionKind
ManagerFactory release.ManagerFactory
ReconcilePeriod time.Duration
OverrideValues map[string]string
SuppressOverrideValues bool
releaseHook ReleaseHookFunc
}
const (
// uninstallFinalizer is added to CRs so they are cleaned up after uninstalling a release.
uninstallFinalizer = "helm.sdk.operatorframework.io/uninstall-release"
// Deprecated: use uninstallFinalizer. This will be removed in operator-sdk v2.0.0.
uninstallFinalizerLegacy = "uninstall-helm-release"
helmUpgradeForceAnnotation = "helm.sdk.operatorframework.io/upgrade-force"
helmUninstallWaitAnnotation = "helm.sdk.operatorframework.io/uninstall-wait"
helmReconcilePeriodAnnotation = "helm.sdk.operatorframework.io/reconcile-period"
)
// Reconcile reconciles the requested resource by installing, updating, or
// uninstalling a Helm release based on the resource's current state. If no
// release changes are necessary, Reconcile will create or patch the underlying
// resources to match the expected release manifest.
func (r HelmOperatorReconciler) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) { //nolint:gocyclo
o := &unstructured.Unstructured{}
o.SetGroupVersionKind(r.GVK)
o.SetNamespace(request.Namespace)
o.SetName(request.Name)
log := log.WithValues(
"namespace", o.GetNamespace(),
"name", o.GetName(),
"apiVersion", o.GetAPIVersion(),
"kind", o.GetKind(),
)
log.V(1).Info("Reconciling")
err := r.Client.Get(ctx, request.NamespacedName, o)
if apierrors.IsNotFound(err) {
return reconcile.Result{}, nil
}
if err != nil {
log.Error(err, "Failed to lookup resource")
return reconcile.Result{}, err
}
manager, err := r.ManagerFactory.NewManager(o, r.OverrideValues)
if err != nil {
log.Error(err, "Failed to get release manager")
return reconcile.Result{}, err
}
status := types.StatusFor(o)
log = log.WithValues("release", manager.ReleaseName())
reconcileResult := reconcile.Result{RequeueAfter: r.ReconcilePeriod}
// Determine the correct reconcile period based on the existing value in the reconciler and the
// annotations in the custom resource. If a reconcile period is specified in the custom resource
// annotations, this value will take precedence over the the existing reconcile period value
// (which came from either the command-line flag or the watches.yaml file).
finalReconcilePeriod, err := determineReconcilePeriod(r.ReconcilePeriod, o)
if err != nil {
log.Error(err, "Error: unable to parse reconcile period from the custom resource's annotations")
return reconcile.Result{}, err
}
reconcileResult.RequeueAfter = finalReconcilePeriod
if o.GetDeletionTimestamp() != nil {
if !(controllerutil.ContainsFinalizer(o, uninstallFinalizer) ||
controllerutil.ContainsFinalizer(o, uninstallFinalizerLegacy)) {
log.Info("Resource is terminated, skipping reconciliation")
return reconcile.Result{}, nil
}
uninstalledRelease, err := manager.UninstallRelease(ctx)
if err != nil && !errors.Is(err, driver.ErrReleaseNotFound) {
log.Error(err, "Failed to uninstall release")
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionReleaseFailed,
Status: types.StatusTrue,
Reason: types.ReasonUninstallError,
Message: err.Error(),
})
if err := r.updateResourceStatus(ctx, o, status); err != nil {
log.Error(err, "Failed to update status after uninstall release failure")
}
return reconcile.Result{}, err
}
status.RemoveCondition(types.ConditionReleaseFailed)
wait := hasAnnotation(helmUninstallWaitAnnotation, o)
if errors.Is(err, driver.ErrReleaseNotFound) {
log.Info("Release not found")
} else {
log.Info("Uninstalled release")
if log.V(1).Enabled() && uninstalledRelease != nil {
fmt.Println(diff.Generate(uninstalledRelease.Manifest, ""))
}
if !wait {
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionDeployed,
Status: types.StatusFalse,
Reason: types.ReasonUninstallSuccessful,
})
status.DeployedRelease = nil
}
}
if wait {
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionDeployed,
Status: types.StatusFalse,
Reason: types.ReasonUninstallSuccessful,
Message: "Waiting until all resources are deleted.",
})
}
if err := r.updateResourceStatus(ctx, o, status); err != nil {
log.Info("Failed to update CR status")
return reconcile.Result{}, err
}
if wait && status.DeployedRelease != nil && status.DeployedRelease.Manifest != "" {
log.Info("Uninstall wait")
isAllResourcesDeleted, err := manager.CleanupRelease(ctx, status.DeployedRelease.Manifest)
if err != nil {
log.Error(err, "Failed to cleanup release")
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionReleaseFailed,
Status: types.StatusTrue,
Reason: types.ReasonUninstallError,
Message: err.Error(),
})
_ = r.updateResourceStatus(ctx, o, status)
return reconcile.Result{}, err
}
if !isAllResourcesDeleted {
log.Info("Waiting until all resources are deleted")
return reconcileResult, nil
}
status.RemoveCondition(types.ConditionReleaseFailed)
}
log.Info("Removing finalizer")
controllerutil.RemoveFinalizer(o, uninstallFinalizer)
controllerutil.RemoveFinalizer(o, uninstallFinalizerLegacy)
if err := r.updateResource(ctx, o); err != nil {
log.Info("Failed to remove CR uninstall finalizer")
return reconcile.Result{}, err
}
// Since the client is hitting a cache, waiting for the
// deletion here will guarantee that the next reconciliation
// will see that the CR has been deleted and that there's
// nothing left to do.
if err := r.waitForDeletion(ctx, o); err != nil {
log.Info("Failed waiting for CR deletion")
return reconcile.Result{}, err
}
return reconcile.Result{}, nil
}
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionInitialized,
Status: types.StatusTrue,
})
if err := manager.Sync(ctx); err != nil {
log.Error(err, "Failed to sync release")
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionIrreconcilable,
Status: types.StatusTrue,
Reason: types.ReasonReconcileError,
Message: err.Error(),
})
if err := r.updateResourceStatus(ctx, o, status); err != nil {
log.Error(err, "Failed to update status after sync release failure")
}
return reconcile.Result{}, err
}
status.RemoveCondition(types.ConditionIrreconcilable)
if !manager.IsInstalled() {
for k, v := range r.OverrideValues {
if r.SuppressOverrideValues {
v = "****"
}
r.EventRecorder.Eventf(o, "Warning", "OverrideValuesInUse",
"Chart value %q overridden to %q by operator's watches.yaml", k, v)
}
installedRelease, err := manager.InstallRelease(ctx)
if err != nil {
log.Error(err, "Release failed")
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionReleaseFailed,
Status: types.StatusTrue,
Reason: types.ReasonInstallError,
Message: err.Error(),
})
if err := r.updateResourceStatus(ctx, o, status); err != nil {
log.Error(err, "Failed to update status after install release failure")
}
return reconcile.Result{}, err
}
status.RemoveCondition(types.ConditionReleaseFailed)
log.V(1).Info("Adding finalizer", "finalizer", uninstallFinalizer)
controllerutil.AddFinalizer(o, uninstallFinalizer)
if err := r.updateResource(ctx, o); err != nil {
log.Info("Failed to add CR uninstall finalizer")
return reconcile.Result{}, err
}
if r.releaseHook != nil {
if err := r.releaseHook(installedRelease); err != nil {
log.Error(err, "Failed to run release hook")
return reconcile.Result{}, err
}
}
log.Info("Installed release")
if log.V(1).Enabled() {
fmt.Println(diff.Generate("", installedRelease.Manifest))
}
log.V(1).Info("Config values", "values", installedRelease.Config)
message := ""
if installedRelease.Info != nil {
message = installedRelease.Info.Notes
}
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionDeployed,
Status: types.StatusTrue,
Reason: types.ReasonInstallSuccessful,
Message: message,
})
status.DeployedRelease = &types.HelmAppRelease{
Name: installedRelease.Name,
Manifest: installedRelease.Manifest,
}
err = r.updateResourceStatus(ctx, o, status)
return reconcileResult, err
}
if !(controllerutil.ContainsFinalizer(o, uninstallFinalizer) ||
controllerutil.ContainsFinalizer(o, uninstallFinalizerLegacy)) {
log.V(1).Info("Adding finalizer", "finalizer", uninstallFinalizer)
controllerutil.AddFinalizer(o, uninstallFinalizer)
if err := r.updateResource(ctx, o); err != nil {
log.Info("Failed to add CR uninstall finalizer")
return reconcile.Result{}, err
}
}
if manager.IsUpgradeRequired() {
for k, v := range r.OverrideValues {
if r.SuppressOverrideValues {
v = "****"
}
r.EventRecorder.Eventf(o, "Warning", "OverrideValuesInUse",
"Chart value %q overridden to %q by operator's watches.yaml", k, v)
}
force := hasAnnotation(helmUpgradeForceAnnotation, o)
previousRelease, upgradedRelease, err := manager.UpgradeRelease(ctx, release.ForceUpgrade(force))
if err != nil {
log.Error(err, "Release failed")
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionReleaseFailed,
Status: types.StatusTrue,
Reason: types.ReasonUpgradeError,
Message: err.Error(),
})
if err := r.updateResourceStatus(ctx, o, status); err != nil {
log.Error(err, "Failed to update status after sync release failure")
}
return reconcile.Result{}, err
}
status.RemoveCondition(types.ConditionReleaseFailed)
if r.releaseHook != nil {
if err := r.releaseHook(upgradedRelease); err != nil {
log.Error(err, "Failed to run release hook")
return reconcile.Result{}, err
}
}
log.Info("Upgraded release", "force", force)
if log.V(1).Enabled() {
fmt.Println(diff.Generate(previousRelease.Manifest, upgradedRelease.Manifest))
}
log.V(1).Info("Config values", "values", upgradedRelease.Config)
message := ""
if upgradedRelease.Info != nil {
message = upgradedRelease.Info.Notes
}
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionDeployed,
Status: types.StatusTrue,
Reason: types.ReasonUpgradeSuccessful,
Message: message,
})
status.DeployedRelease = &types.HelmAppRelease{
Name: upgradedRelease.Name,
Manifest: upgradedRelease.Manifest,
}
err = r.updateResourceStatus(ctx, o, status)
return reconcileResult, err
}
// If a change is made to the CR spec that causes a release failure, a
// ConditionReleaseFailed is added to the status conditions. If that change
// is then reverted to its previous state, the operator will stop
// attempting the release and will resume reconciling. In this case, we
// need to remove the ConditionReleaseFailed because the failing release is
// no longer being attempted.
status.RemoveCondition(types.ConditionReleaseFailed)
expectedRelease, err := manager.ReconcileRelease(ctx)
if err != nil {
log.Error(err, "Failed to reconcile release")
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionIrreconcilable,
Status: types.StatusTrue,
Reason: types.ReasonReconcileError,
Message: err.Error(),
})
if err := r.updateResourceStatus(ctx, o, status); err != nil {
log.Error(err, "Failed to update status after reconcile release failure")
}
return reconcile.Result{}, err
}
status.RemoveCondition(types.ConditionIrreconcilable)
if r.releaseHook != nil {
if err := r.releaseHook(expectedRelease); err != nil {
log.Error(err, "Failed to run release hook")
return reconcile.Result{}, err
}
}
log.Info("Reconciled release")
reason := types.ReasonUpgradeSuccessful
if expectedRelease.Version == 1 {
reason = types.ReasonInstallSuccessful
}
message := ""
if expectedRelease.Info != nil {
message = expectedRelease.Info.Notes
}
status.SetCondition(types.HelmAppCondition{
Type: types.ConditionDeployed,
Status: types.StatusTrue,
Reason: reason,
Message: message,
})
status.DeployedRelease = &types.HelmAppRelease{
Name: expectedRelease.Name,
Manifest: expectedRelease.Manifest,
}
err = r.updateResourceStatus(ctx, o, status)
return reconcileResult, err
}
// returns the reconcile period that will be set to the RequeueAfter field in the reconciler. If any period
// is specified in the custom resource's annotations, this will be returned. If not, the existing reconcile period
// will be returned. An error will be thrown if the custom resource time period is not in proper format.
func determineReconcilePeriod(currentPeriod time.Duration, o *unstructured.Unstructured) (time.Duration, error) {
// If custom resource annotations are present, they will take precedence over the command-line flag
if annot, exists := o.UnstructuredContent()["metadata"].(map[string]interface{})["annotations"]; exists {
if timeDuration, present := annot.(map[string]interface{})[helmReconcilePeriodAnnotation]; present {
annotationsPeriod, err := time.ParseDuration(timeDuration.(string))
if err != nil {
return currentPeriod, err // First return value does not matter, since err != nil
}
return annotationsPeriod, nil
}
}
return currentPeriod, nil
}
// returns the boolean representation of the annotation string
// will return false if annotation is not set
func hasAnnotation(anno string, o *unstructured.Unstructured) bool {
boolStr := o.GetAnnotations()[anno]
if boolStr == "" {
return false
}
value := false
if i, err := strconv.ParseBool(boolStr); err != nil {
log.Info("Could not parse annotation as a boolean",
"annotation", anno, "value informed", boolStr)
} else {
value = i
}
return value
}
func (r HelmOperatorReconciler) updateResource(ctx context.Context, o client.Object) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
return r.Client.Update(ctx, o)
})
}
func (r HelmOperatorReconciler) updateResourceStatus(ctx context.Context, o *unstructured.Unstructured, status *types.HelmAppStatus) error {
return retry.RetryOnConflict(retry.DefaultBackoff, func() error {
o.Object["status"] = status
return r.Client.Status().Update(ctx, o)
})
}
func (r HelmOperatorReconciler) waitForDeletion(ctx context.Context, o client.Object) error {
key := client.ObjectKeyFromObject(o)
tctx, cancel := context.WithTimeout(ctx, time.Second*5)
defer cancel()
return wait.PollImmediateUntil(time.Millisecond*10, func() (bool, error) {
err := r.Client.Get(tctx, key, o)
if apierrors.IsNotFound(err) {
return true, nil
}
if err != nil {
return false, err
}
return false, nil
}, tctx.Done())
}