-
Notifications
You must be signed in to change notification settings - Fork 166
/
Copy pathrelease.go
242 lines (208 loc) · 7.54 KB
/
release.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
/*
Copyright 2022 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 (
"errors"
"sort"
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/apis/meta"
"github.com/fluxcd/pkg/runtime/conditions"
helmrelease "helm.sh/helm/v3/pkg/release"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v2 "github.com/fluxcd/helm-controller/api/v2beta2"
"github.com/fluxcd/helm-controller/internal/action"
"github.com/fluxcd/helm-controller/internal/release"
"github.com/fluxcd/helm-controller/internal/storage"
)
var (
// ErrNoLatest is returned when the HelmRelease has no latest release
// but this is required by the ActionReconciler.
ErrNoLatest = errors.New("no latest release")
// ErrReleaseMismatch is returned when the resulting release after running
// an action does not match the expected latest and/or previous release.
// This can happen for actions where targeting a release by version is not
// possible, for example while running tests.
ErrReleaseMismatch = errors.New("release mismatch")
)
// mutateObservedRelease is a function that mutates the Observation with the
// given HelmRelease object.
type mutateObservedRelease func(*v2.HelmRelease, release.Observation) release.Observation
// observedReleases is a map of Helm releases as observed to be written to the
// Helm storage. The key is the version of the release.
type observedReleases map[int]release.Observation
// sortedVersions returns the versions of the observed releases in descending
// order.
func (r observedReleases) sortedVersions() (versions []int) {
for ver := range r {
versions = append(versions, ver)
}
sort.Sort(sort.Reverse(sort.IntSlice(versions)))
return
}
// recordOnObject records the observed releases on the HelmRelease object.
func (r observedReleases) recordOnObject(obj *v2.HelmRelease, mutators ...mutateObservedRelease) {
switch len(r) {
case 0:
return
case 1:
var obs release.Observation
for _, o := range r {
obs = o
}
for _, mut := range mutators {
obs = mut(obj, obs)
}
obj.Status.History = append(v2.Snapshots{release.ObservedToSnapshot(obs)}, obj.Status.History...)
default:
versions := r.sortedVersions()
obs := r[versions[0]]
for _, mut := range mutators {
obs = mut(obj, obs)
}
obj.Status.History = append(v2.Snapshots{release.ObservedToSnapshot(obs)}, obj.Status.History...)
for _, ver := range versions[1:] {
for i := range obj.Status.History {
snap := obj.Status.History[i]
if snap.Targets(r[ver].Name, r[ver].Namespace, r[ver].Version) {
obs := r[ver]
obs.OCIDigest = snap.OCIDigest
newSnap := release.ObservedToSnapshot(obs)
newSnap.SetTestHooks(snap.GetTestHooks())
obj.Status.History[i] = newSnap
return
}
}
}
}
}
func mutateOCIDigest(obj *v2.HelmRelease, obs release.Observation) release.Observation {
obs.OCIDigest = obj.Status.LastAttemptedRevisionDigest
return obs
}
func releaseToObservation(rls *helmrelease.Release, snapshot *v2.Snapshot) release.Observation {
obs := release.ObserveRelease(rls)
obs.OCIDigest = snapshot.OCIDigest
return obs
}
// observeRelease returns a storage.ObserveFunc that stores the observed
// releases in the given observedReleases map.
// It can be used for Helm actions that modify multiple releases in the
// Helm storage, such as install and upgrade.
func observeRelease(observed observedReleases) storage.ObserveFunc {
return func(rls *helmrelease.Release) {
obs := release.ObserveRelease(rls)
observed[obs.Version] = obs
}
}
// summarize composes a Ready condition out of the Remediated, TestSuccess and
// Released conditions of the given Request.Object, and sets it on the object.
//
// The composition is made by sorting them by highest generation and priority
// of the summary conditions, taking the first result.
//
// Not taking the generation of the object itself into account ensures that if
// the change in generation of the resource does not result in a release, the
// Ready condition is still reflected for the current generation based on a
// release made for the previous generation.
//
// It takes the current specification of the object into account, and deals
// with the conditional handling of TestSuccess. Deleting the condition when
// tests are not enabled, and excluding it when failures must be ignored.
//
// If Ready=True, any Stalled condition is removed.
func summarize(req *Request) {
var sumConds = []string{v2.RemediatedCondition, v2.ReleasedCondition}
if req.Object.GetTest().Enable && !req.Object.GetTest().IgnoreFailures {
sumConds = []string{v2.RemediatedCondition, v2.TestSuccessCondition, v2.ReleasedCondition}
}
// Remove any stale TestSuccess condition as soon as tests are disabled.
if !req.Object.GetTest().Enable {
conditions.Delete(req.Object, v2.TestSuccessCondition)
}
conds := req.Object.Status.Conditions
if len(conds) == 0 {
// Nothing to summarize if there are no conditions.
return
}
sort.SliceStable(conds, func(i, j int) bool {
iPos, ok := inStringSlice(sumConds, conds[i].Type)
if !ok {
return false
}
jPos, ok := inStringSlice(sumConds, conds[j].Type)
if !ok {
return true
}
return (conds[i].ObservedGeneration >= conds[j].ObservedGeneration) && (iPos < jPos)
})
status := conds[0].Status
// Any remediated state is considered an error.
if conds[0].Type == v2.RemediatedCondition {
status = metav1.ConditionFalse
}
if status == metav1.ConditionTrue {
conditions.Delete(req.Object, meta.StalledCondition)
}
conditions.Set(req.Object, &metav1.Condition{
Type: meta.ReadyCondition,
Status: status,
Reason: conds[0].Reason,
Message: conds[0].Message,
ObservedGeneration: req.Object.Generation,
})
}
// eventMessageWithLog returns an event message composed out of the given
// message and any log messages by appending them to the message.
func eventMessageWithLog(msg string, log *action.LogBuffer) string {
if log != nil && log.Len() > 0 {
msg = msg + "\n\nLast Helm logs:\n\n" + log.String()
}
return msg
}
// addMeta is a function that adds metadata to an event map.
type addMeta func(map[string]string)
// metaOCIDigestKey is the key for the OCI digest metadata.
const metaOCIDigestKey = "oci-digest"
// eventMeta returns the event (annotation) metadata based on the given
// parameters.
func eventMeta(revision, token string, metas ...addMeta) map[string]string {
var metadata map[string]string
if revision != "" || token != "" {
metadata = make(map[string]string)
if revision != "" {
metadata[eventMetaGroupKey(eventv1.MetaRevisionKey)] = revision
}
if token != "" {
metadata[eventMetaGroupKey(eventv1.MetaTokenKey)] = token
}
}
for _, add := range metas {
add(metadata)
}
return metadata
}
func addOCIDigest(digest string) addMeta {
return func(m map[string]string) {
if digest != "" {
if m == nil {
m = make(map[string]string)
}
m[eventMetaGroupKey(metaOCIDigestKey)] = digest
}
}
}
// eventMetaGroupKey returns the event (annotation) metadata key prefixed with
// the group.
func eventMetaGroupKey(key string) string {
return v2.GroupVersion.Group + "/" + key
}