forked from operator-framework/operator-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
381 lines (333 loc) · 11.4 KB
/
manager.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
// 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 release
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"strings"
yaml "gopkg.in/yaml.v2"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/cli-runtime/pkg/genericclioptions/resource"
"k8s.io/client-go/rest"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/kube"
cpb "k8s.io/helm/pkg/proto/hapi/chart"
rpb "k8s.io/helm/pkg/proto/hapi/release"
"k8s.io/helm/pkg/proto/hapi/services"
"k8s.io/helm/pkg/storage"
"k8s.io/helm/pkg/tiller"
"github.com/mattbaird/jsonpatch"
"github.com/operator-framework/operator-sdk/pkg/helm/internal/types"
)
var (
// ErrNotFound indicates the release was not found.
ErrNotFound = errors.New("release not found")
)
// Manager manages a Helm release. It can install, update, reconcile,
// and uninstall a release.
type Manager interface {
ReleaseName() string
IsInstalled() bool
IsUpdateRequired() bool
Sync(context.Context) error
InstallRelease(context.Context) (*rpb.Release, error)
UpdateRelease(context.Context) (*rpb.Release, *rpb.Release, error)
ReconcileRelease(context.Context) (*rpb.Release, error)
UninstallRelease(context.Context) (*rpb.Release, error)
}
type manager struct {
storageBackend *storage.Storage
tillerKubeClient *kube.Client
chartDir string
tiller *tiller.ReleaseServer
releaseName string
namespace string
spec interface{}
status *types.HelmAppStatus
isInstalled bool
isUpdateRequired bool
deployedRelease *rpb.Release
chart *cpb.Chart
config *cpb.Config
}
// ReleaseName returns the name of the release.
func (m manager) ReleaseName() string {
return m.releaseName
}
func (m manager) IsInstalled() bool {
return m.isInstalled
}
func (m manager) IsUpdateRequired() bool {
return m.isUpdateRequired
}
// Sync ensures the Helm storage backend is in sync with the status of the
// custom resource.
func (m *manager) Sync(ctx context.Context) error {
// Get release history for this release name
releases, err := m.storageBackend.History(m.releaseName)
if err != nil && !notFoundErr(err) {
return fmt.Errorf("failed to retrieve release history: %s", err)
}
// Cleanup non-deployed release versions. If all release versions are
// non-deployed, this will ensure that failed installations are correctly
// retried.
for _, rel := range releases {
if rel.GetInfo().GetStatus().GetCode() != rpb.Status_DEPLOYED {
_, err := m.storageBackend.Delete(rel.GetName(), rel.GetVersion())
if err != nil && !notFoundErr(err) {
return fmt.Errorf("failed to delete stale release version: %s", err)
}
}
}
// Load the chart and config based on the current state of the custom resource.
chart, config, err := m.loadChartAndConfig()
if err != nil {
return fmt.Errorf("failed to load chart and config: %s", err)
}
m.chart = chart
m.config = config
// Load the most recently deployed release from the storage backend.
deployedRelease, err := m.getDeployedRelease()
if err == ErrNotFound {
return nil
}
if err != nil {
return fmt.Errorf("failed to get deployed release: %s", err)
}
m.deployedRelease = deployedRelease
m.isInstalled = true
// Get the next candidate release to determine if an update is necessary.
candidateRelease, err := m.getCandidateRelease(ctx, m.tiller, m.releaseName, chart, config)
if err != nil {
return fmt.Errorf("failed to get candidate release: %s", err)
}
if deployedRelease.GetManifest() != candidateRelease.GetManifest() {
m.isUpdateRequired = true
}
return nil
}
func notFoundErr(err error) bool {
return strings.Contains(err.Error(), "not found")
}
func (m manager) loadChartAndConfig() (*cpb.Chart, *cpb.Config, error) {
// chart is mutated by the call to processRequirements,
// so we need to reload it from disk every time.
chart, err := chartutil.LoadDir(m.chartDir)
if err != nil {
return nil, nil, fmt.Errorf("failed to load chart: %s", err)
}
cr, err := yaml.Marshal(m.spec)
if err != nil {
return nil, nil, fmt.Errorf("failed to parse values: %s", err)
}
config := &cpb.Config{Raw: string(cr)}
err = processRequirements(chart, config)
if err != nil {
return nil, nil, fmt.Errorf("failed to process chart requirements: %s", err)
}
return chart, config, nil
}
// processRequirements will process the requirements file
// It will disable/enable the charts based on condition in requirements file
// Also imports the specified chart values from child to parent.
func processRequirements(chart *cpb.Chart, values *cpb.Config) error {
err := chartutil.ProcessRequirementsEnabled(chart, values)
if err != nil {
return err
}
err = chartutil.ProcessRequirementsImportValues(chart)
if err != nil {
return err
}
return nil
}
func (m manager) getDeployedRelease() (*rpb.Release, error) {
deployedRelease, err := m.storageBackend.Deployed(m.releaseName)
if err != nil {
if strings.Contains(err.Error(), "has no deployed releases") {
return nil, ErrNotFound
}
return nil, err
}
return deployedRelease, nil
}
func (m manager) getCandidateRelease(ctx context.Context, tiller *tiller.ReleaseServer, name string, chart *cpb.Chart, config *cpb.Config) (*rpb.Release, error) {
dryRunReq := &services.UpdateReleaseRequest{
Name: name,
Chart: chart,
Values: config,
DryRun: true,
}
dryRunResponse, err := tiller.UpdateRelease(ctx, dryRunReq)
if err != nil {
return nil, err
}
return dryRunResponse.GetRelease(), nil
}
// InstallRelease performs a Helm release install.
func (m manager) InstallRelease(ctx context.Context) (*rpb.Release, error) {
return installRelease(ctx, m.tiller, m.namespace, m.releaseName, m.chart, m.config)
}
func installRelease(ctx context.Context, tiller *tiller.ReleaseServer, namespace, name string, chart *cpb.Chart, config *cpb.Config) (*rpb.Release, error) {
installReq := &services.InstallReleaseRequest{
Namespace: namespace,
Name: name,
Chart: chart,
Values: config,
}
releaseResponse, err := tiller.InstallRelease(ctx, installReq)
if err != nil {
// Workaround for helm/helm#3338
if releaseResponse.GetRelease() != nil {
uninstallReq := &services.UninstallReleaseRequest{
Name: releaseResponse.GetRelease().GetName(),
Purge: true,
}
_, uninstallErr := tiller.UninstallRelease(ctx, uninstallReq)
if uninstallErr != nil {
return nil, fmt.Errorf("failed to roll back failed installation: %s: %s", uninstallErr, err)
}
}
return nil, err
}
return releaseResponse.GetRelease(), nil
}
// UpdateRelease performs a Helm release update.
func (m manager) UpdateRelease(ctx context.Context) (*rpb.Release, *rpb.Release, error) {
updatedRelease, err := updateRelease(ctx, m.tiller, m.releaseName, m.chart, m.config)
return m.deployedRelease, updatedRelease, err
}
func updateRelease(ctx context.Context, tiller *tiller.ReleaseServer, name string, chart *cpb.Chart, config *cpb.Config) (*rpb.Release, error) {
updateReq := &services.UpdateReleaseRequest{
Name: name,
Chart: chart,
Values: config,
}
releaseResponse, err := tiller.UpdateRelease(ctx, updateReq)
if err != nil {
// Workaround for helm/helm#3338
if releaseResponse.GetRelease() != nil {
rollbackReq := &services.RollbackReleaseRequest{
Name: name,
Force: true,
}
_, rollbackErr := tiller.RollbackRelease(ctx, rollbackReq)
if rollbackErr != nil {
return nil, fmt.Errorf("failed to roll back failed update: %s: %s", rollbackErr, err)
}
}
return nil, err
}
return releaseResponse.GetRelease(), nil
}
// ReconcileRelease creates or patches resources as necessary to match the
// deployed release's manifest.
func (m manager) ReconcileRelease(ctx context.Context) (*rpb.Release, error) {
err := reconcileRelease(ctx, m.tillerKubeClient, m.namespace, m.deployedRelease.GetManifest())
return m.deployedRelease, err
}
func reconcileRelease(ctx context.Context, tillerKubeClient *kube.Client, namespace string, expectedManifest string) error {
expectedInfos, err := tillerKubeClient.BuildUnstructured(namespace, bytes.NewBufferString(expectedManifest))
if err != nil {
return err
}
return expectedInfos.Visit(func(expected *resource.Info, err error) error {
if err != nil {
return err
}
expectedClient := resource.NewClientWithOptions(expected.Client, func(r *rest.Request) {
*r = *r.Context(ctx)
})
helper := resource.NewHelper(expectedClient, expected.Mapping)
existing, err := helper.Get(expected.Namespace, expected.Name, false)
if apierrors.IsNotFound(err) {
if _, err := helper.Create(expected.Namespace, true, expected.Object, &metav1.CreateOptions{}); err != nil {
return fmt.Errorf("create error: %s", err)
}
return nil
} else if err != nil {
return err
}
patch, err := generatePatch(existing, expected.Object)
if err != nil {
return fmt.Errorf("failed to marshal JSON patch: %s", err)
}
if patch == nil {
return nil
}
_, err = helper.Patch(expected.Namespace, expected.Name, apitypes.JSONPatchType, patch, &metav1.UpdateOptions{})
if err != nil {
return fmt.Errorf("patch error: %s", err)
}
return nil
})
}
func generatePatch(existing, expected runtime.Object) ([]byte, error) {
existingJSON, err := json.Marshal(existing)
if err != nil {
return nil, err
}
expectedJSON, err := json.Marshal(expected)
if err != nil {
return nil, err
}
ops, err := jsonpatch.CreatePatch(existingJSON, expectedJSON)
if err != nil {
return nil, err
}
// We ignore the "remove" operations from the full patch because they are
// fields added by Kubernetes or by the user after the existing release
// resource has been applied. The goal for this patch is to make sure that
// the fields managed by the Helm chart are applied.
patchOps := make([]jsonpatch.JsonPatchOperation, 0)
for _, op := range ops {
if op.Operation != "remove" {
patchOps = append(patchOps, op)
}
}
// If there are no patch operations, return nil. Callers are expected
// to check for a nil response and skip the patch operation to avoid
// unnecessary chatter with the API server.
if len(patchOps) == 0 {
return nil, nil
}
return json.Marshal(patchOps)
}
// UninstallRelease performs a Helm release uninstall.
func (m manager) UninstallRelease(ctx context.Context) (*rpb.Release, error) {
return uninstallRelease(ctx, m.storageBackend, m.tiller, m.releaseName)
}
func uninstallRelease(ctx context.Context, storageBackend *storage.Storage, tiller *tiller.ReleaseServer, releaseName string) (*rpb.Release, error) {
// Get history of this release
h, err := storageBackend.History(releaseName)
if err != nil {
return nil, fmt.Errorf("failed to get release history: %s", err)
}
// If there is no history, the release has already been uninstalled,
// so return ErrNotFound.
if len(h) == 0 {
return nil, ErrNotFound
}
uninstallResponse, err := tiller.UninstallRelease(ctx, &services.UninstallReleaseRequest{
Name: releaseName,
Purge: true,
})
return uninstallResponse.GetRelease(), err
}