-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathcorrect_cluster_drift.go
124 lines (104 loc) · 4.05 KB
/
correct_cluster_drift.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
/*
Copyright 2023 The Flux 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 reconcile
import (
"context"
"strings"
corev1 "k8s.io/api/core/v1"
apierrutil "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"
"github.com/fluxcd/pkg/ssa"
"github.com/fluxcd/pkg/ssa/jsondiff"
v2 "github.com/fluxcd/helm-controller/api/v2"
"github.com/fluxcd/helm-controller/internal/action"
)
// CorrectClusterDrift is a reconciler that attempts to correct the cluster state
// of a Helm release. It does so by applying the Helm release's desired state
// to the cluster based on a jsondiff.DiffSet.
//
// The reconciler will only attempt to correct the cluster state if the Helm
// release has drift detection enabled and the jsondiff.DiffSet is not empty.
//
// The reconciler will emit a Kubernetes event upon completion indicating
// whether the cluster state was successfully corrected or not.
type CorrectClusterDrift struct {
configFactory *action.ConfigFactory
eventRecorder record.EventRecorder
diff jsondiff.DiffSet
fieldManager string
}
func NewCorrectClusterDrift(configFactory *action.ConfigFactory, recorder record.EventRecorder, diff jsondiff.DiffSet, fieldManager string) *CorrectClusterDrift {
return &CorrectClusterDrift{
configFactory: configFactory,
eventRecorder: recorder,
diff: diff,
fieldManager: fieldManager,
}
}
func (r *CorrectClusterDrift) Reconcile(ctx context.Context, req *Request) error {
if req.Object.GetDriftDetection().GetMode() != v2.DriftDetectionEnabled || len(r.diff) == 0 {
return nil
}
ctx, cancel := context.WithTimeout(ctx, req.Object.GetTimeout().Duration)
defer cancel()
// Update condition to reflect the current status.
conditions.MarkUnknown(req.Object, meta.ReadyCondition, meta.ProgressingReason, "correcting cluster drift")
changeSet, err := action.ApplyDiff(ctx, r.configFactory.Build(nil), r.diff, r.fieldManager)
r.report(req.Object, changeSet, err)
return nil
}
func (r *CorrectClusterDrift) report(obj *v2.HelmRelease, changeSet *ssa.ChangeSet, err error) {
cur := obj.Status.History.Latest()
switch {
case err != nil:
var sb strings.Builder
sb.WriteString("Failed to ")
if changeSet != nil && len(changeSet.Entries) > 0 {
sb.WriteString("partially ")
}
sb.WriteString("correct cluster state of release ")
sb.WriteString(cur.FullReleaseName())
sb.WriteString(":\n")
if agErr, ok := err.(apierrutil.Aggregate); ok {
for i := range agErr.Errors() {
if i > 0 {
sb.WriteString("\n")
}
sb.WriteString(agErr.Errors()[i].Error())
}
} else {
sb.WriteString(err.Error())
}
if changeSet != nil && len(changeSet.Entries) > 0 {
sb.WriteString("\n\n")
sb.WriteString("Successful corrections:\n")
sb.WriteString(changeSet.String())
}
r.eventRecorder.AnnotatedEventf(obj, eventMeta(cur.ChartVersion, cur.ConfigDigest,
addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)), corev1.EventTypeWarning,
"DriftCorrectionFailed", sb.String())
case changeSet != nil && len(changeSet.Entries) > 0:
r.eventRecorder.AnnotatedEventf(obj, eventMeta(cur.ChartVersion, cur.ConfigDigest,
addAppVersion(cur.AppVersion), addOCIDigest(cur.OCIDigest)), corev1.EventTypeNormal,
"DriftCorrected", "Cluster state of release %s has been corrected:\n%s",
obj.Status.History.Latest().FullReleaseName(), changeSet.String())
}
}
func (r *CorrectClusterDrift) Name() string {
return "correct cluster drift"
}
func (r *CorrectClusterDrift) Type() ReconcilerType {
return ReconcilerTypeDriftCorrection
}