-
Notifications
You must be signed in to change notification settings - Fork 9
/
cpl_helm.go
518 lines (486 loc) · 18.6 KB
/
cpl_helm.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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
Copyright 2023 The Kubebb 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 helm
import (
"context"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/go-logr/logr"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli/values"
"helm.sh/helm/v3/pkg/helmpath"
"helm.sh/helm/v3/pkg/release"
hrepo "helm.sh/helm/v3/pkg/repo"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
corev1alpha1 "github.com/kubebb/core/api/v1alpha1"
"github.com/kubebb/core/pkg/utils"
)
type CoreHelm interface {
InstallOrUpgrade(ctx context.Context, chartName string) (rel *release.Release, err error)
Uninstall(ctx context.Context) error
GetLastRelease() (rel *release.Release, err error)
GetManifestsByDryRun(ctx context.Context, chartName string) (data string, err error)
Rollback(ctx context.Context) error
GetOCIRepoCharts(ctx context.Context, pullURL string, skipTags map[string]bool) (latest *chart.Metadata, all []*hrepo.ChartVersion, err error)
PullAndParse(ctx context.Context, pullURL, version string) (out string, chartRequested *chart.Chart, err error)
Pull(ctx context.Context, pullURL, version string) (out, dir, entryName string, err error)
Template(ctx context.Context, version string, localPath string) (rel *release.Release, err error)
}
var _ CoreHelm = &CoreHelmWrapper{}
// CoreHelmWrapper is a wrapper for helm command
type CoreHelmWrapper struct {
*HelmWrapper
cpl *corev1alpha1.ComponentPlan
repo *corev1alpha1.Repository
component *corev1alpha1.Component
cli client.Client
logger logr.Logger
}
// NewCoreHelmWrapper returns a new helmWrapper instance
func NewCoreHelmWrapper(getter genericclioptions.RESTClientGetter, namespace string, logger logr.Logger, cli client.Client, cpl *corev1alpha1.ComponentPlan, repo *corev1alpha1.Repository, component *corev1alpha1.Component) (*CoreHelmWrapper, error) {
h, err := NewHelmWrapper(getter, namespace, logger)
if err != nil {
return nil, err
}
return &CoreHelmWrapper{
HelmWrapper: h,
cpl: cpl,
repo: repo,
component: component,
cli: cli,
logger: logger,
}, nil
}
// InstallOrUpgrade installs / ungrade a helm chart to the cluster
func (c *CoreHelmWrapper) InstallOrUpgrade(ctx context.Context, chartName string) (rel *release.Release, err error) {
return c.installOrUpgrade(ctx, chartName, false)
}
// Uninstall installs a helm chart to the cluster
func (c *CoreHelmWrapper) Uninstall(ctx context.Context) (err error) {
defer func() {
if err != nil {
if strings.HasPrefix(err.Error(), "uninstallation completed with 1 error(s): uninstall: Failed to purge the release: release: not found") { //nolint:dupword
err = nil
}
}
}()
rel, err := c.GetLastRelease()
if err != nil {
return err
}
if rel == nil {
return nil
}
var parseDescription bool
var relNs, relName, relUID string
if rel.Info != nil && rel.Info.Status == release.StatusDeployed && !strings.HasPrefix(rel.Info.Description, "Rollback ") {
// descrpiton match, the chart is exactly installed by this componentplan
relNs, relName, relUID, _, _ = ParseDescription(rel.Info.Description)
parseDescription = true
}
if parseDescription {
if c.cpl.Namespace == relNs || c.cpl.Name == relName || string(c.cpl.GetUID()) == relUID {
return c.uninstall(ctx)
}
} else {
// version match, the chart is exactly installed by this componentplan
if rel.Version == c.cpl.Status.InstalledRevision {
return c.uninstall(ctx)
}
// when installing/upgrading/rollingback, description will be setted by helm, so we compare helm release version and component status
if c.cpl.Status.InstalledRevision+1 == rel.Version {
if c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonInstalling) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonUpgrading) || c.cpl.IsActionedReason(corev1alpha1.ComponentPlanReasonRollingBack) {
return c.uninstall(ctx)
}
}
}
return nil
}
// GetLastRelease get last release revision
func (c *CoreHelmWrapper) GetLastRelease() (rel *release.Release, err error) {
return c.HelmWrapper.GetLastRelease(c.cpl.GetReleaseName())
}
// GetManifestsByDryRun get helm templates by dryRun
func (c *CoreHelmWrapper) GetManifestsByDryRun(ctx context.Context, chartName string) (data string, err error) {
rel, err := c.installOrUpgrade(ctx, chartName, true)
if err != nil {
return "", err
}
return rel.Manifest, nil
}
// installOrUpgrade installs / ungrade a helm chart to the cluster
func (c *CoreHelmWrapper) installOrUpgrade(ctx context.Context, chartName string, dryRun bool) (rel *release.Release, err error) {
rel, err = c.HelmWrapper.GetLastRelease(c.cpl.GetReleaseName())
if err != nil {
return nil, err
}
if rel == nil {
rel, err = c.install(ctx, dryRun, chartName)
if err != nil {
return nil, err
}
c.logger.Info(fmt.Sprintf("helm install completed dryRun:%t", dryRun), ReleaseLog(rel)...)
} else {
c.logger.Info("helm find last release", ReleaseLog(rel)...)
rel, err = c.upgrade(ctx, dryRun, chartName)
if err != nil {
return nil, err
}
c.logger.Info(fmt.Sprintf("upgrade completed dryRun:%t", dryRun), ReleaseLog(rel)...)
}
return rel, nil
}
// ReleaseLog generates a log slice for a Helm release object.
func ReleaseLog(rel *release.Release) []interface{} {
l := []interface{}{"release.Version", rel.Version}
if rel.Info != nil {
// do not show release.Info.Note which is long and not useful for controllers
l = append(l, "release.Info.status", rel.Info.Status, "release.Info.firstDeployed", rel.Info.FirstDeployed, "release.Info.lastDeployed", rel.Info.LastDeployed, "release.Info.deleted", rel.Info.Deleted, "release.Info.description", rel.Info.Description)
}
return l
}
func (c *CoreHelmWrapper) Rollback(ctx context.Context) (err error) {
return c.rollback(ctx)
}
// GetOCIRepoCharts retrieves the latest chart metadata and all component versions for a given OCI repository.
func (c *CoreHelmWrapper) GetOCIRepoCharts(ctx context.Context, pullURL string, skipTags map[string]bool) (latest *chart.Metadata, all []*hrepo.ChartVersion, err error) {
tags, err := c.config.RegistryClient.Tags(strings.TrimPrefix(pullURL, "oci://"))
if err != nil {
return nil, nil, err
}
if len(tags) == 0 {
return nil, nil, nil
}
latestOne := tags[0]
if skipTags[latestOne] {
return nil, nil, nil
}
for i, tag := range tags {
if skipTags[tag] {
continue // It is ok for deprecated chart. Because once a chart is deprecated the expectation is the chart will see no further development. The Version will increase. All charts are immutable.
}
out, c, err := c.PullAndParse(ctx, pullURL, tag)
if err != nil {
return nil, nil, err
}
if i == 0 {
latest = c.Metadata
}
all = append(all, &hrepo.ChartVersion{
Metadata: c.Metadata,
URLs: nil,
Created: time.Now(),
Removed: false,
Digest: ParseDigestFromPullOut(out),
})
}
return latest, all, nil
}
func (c *CoreHelmWrapper) PullAndParse(ctx context.Context, pullURL, version string) (out string, chartRequested *chart.Chart, err error) {
out, dir, entryName, err := c.Pull(ctx, pullURL, version)
if err != nil {
c.logger.Error(err, "cannot download chart")
return out, nil, err
}
defer os.RemoveAll(dir)
chartRequested, err = loader.Load(dir + "/" + entryName)
if err != nil {
c.logger.Error(err, "Cannot load chart")
return out, nil, err
}
return out, chartRequested, nil
}
func (c *CoreHelmWrapper) Pull(ctx context.Context, pullURL, version string) (out, dir, entryName string, err error) {
entryName = utils.GetOCIEntryName(pullURL)
if !c.repo.IsOCI() {
entryName = utils.GetHTTPEntryName(pullURL)
}
i := c.HelmWrapper.GetDefaultPullCfg()
if c.repo.Spec.AuthSecret != "" {
i.Username, i.Password, i.CaFile, i.CertFile, i.KeyFile, err = corev1alpha1.ParseRepoSecret(c.cli, c.repo)
if err != nil {
return "", "", "", err
}
}
i.Version = version
i.Settings = settings
i.Devel = false
i.VerifyLater = false
i.Untar = true
i.UntarDir, err = os.MkdirTemp("", "kubebb-*")
if err != nil {
c.logger.Error(err, "Failed to create a new dir")
_ = os.RemoveAll(i.UntarDir)
return "", "", "", err
}
out, err = c.HelmWrapper.Pull(ctx, c.logger, i, pullURL)
if err != nil {
c.logger.Error(err, "cannot download chart")
_ = os.RemoveAll(i.UntarDir)
return "", "", "", err
}
c.logger.Info(out)
return out, i.UntarDir, entryName, nil
}
func (c *CoreHelmWrapper) Template(ctx context.Context, version, localPath string) (rel *release.Release, err error) {
i, _, _, _, _, _, _ := c.GetDefaultTemplateCfg()
i.DryRun = true
i.ReleaseName = "release-name"
i.Replace = true // skip the name check
i.ClientOnly = true // do not validate
i.IncludeCRDs = false // we just want images info, no crd
i.CreateNamespace = false // installed in the same namespace with ComponentPlan
i.DisableHooks = true // we just want images info, no crd
i.Replace = false // reuse the given name is not safe in production
i.Devel = false // just set `>0.0.0-0` to ComponentPlan.Spec.Version is enough
i.DependencyUpdate = true
i.DisableOpenAPIValidation = true // we just want images info, no crd
i.SkipCRDs = true // we just want images info, no crd
i.SubNotes = false // we cant see notes or subnotes
i.Version = version
i.Verify = false // TODO enable these args after we can config keyring
i.RepoURL = "" // We only support adding a repo and then installing it, not installing it directly via an url.
i.Keyring = "" // TODO enable these args after we can config keyring
if c.repo.Spec.AuthSecret != "" {
i.Username, i.Password, i.CaFile, i.CertFile, i.KeyFile, err = corev1alpha1.ParseRepoSecret(c.cli, c.repo)
if err != nil {
return nil, err
}
}
i.InsecureSkipTLSverify = c.repo.Spec.Insecure
i.PassCredentialsAll = false // TODO do we need add this args to override?
validate := false
skipTests := false
includeCrds := false
kubeVersion := ""
extraAPIs := []string{}
showFiles := []string{}
ValueOpts := c.SetValuesOptions(nil, nil, nil, nil)
rel, _, err = c.HelmWrapper.Template(ctx, c.logger, i, ValueOpts, i.ReleaseName, localPath, kubeVersion, validate, includeCrds, skipTests, extraAPIs, showFiles)
if err != nil {
return nil, err
}
return rel, err
}
func (c *CoreHelmWrapper) install(ctx context.Context, dryRun bool, chartName string) (rel *release.Release, err error) {
log := c.logger.WithValues("ComponentPlan", klog.KObj(c.cpl))
if c.repo.IsOCI() {
log.Info("Installing OCI Component")
} else {
log.Info("Installing non-OCI Component")
}
if dryRun {
log.WithValues("dryRun", true)
}
i := c.HelmWrapper.GetDefaultInstallCfg()
i.CreateNamespace = false // installed in the same namespace with ComponentPlan
i.DryRun = dryRun
i.DisableHooks = c.cpl.Spec.DisableHooks
i.Replace = false // reuse the given name is not safe in production
i.Timeout = c.cpl.Spec.Timeout()
i.Wait = c.cpl.Spec.Wait
i.WaitForJobs = c.cpl.Spec.WaitForJobs
i.GenerateName = false // we use ComponentPlan.Spec.Name
i.NameTemplate = "" // we use ComponentPlan.Spec.Name
i.Description = generateDescription(c.cpl)
i.Devel = false // just set `>0.0.0-0` to ComponentPlan.Spec.Version is enough
i.DependencyUpdate = true
i.DisableOpenAPIValidation = c.cpl.Spec.DisableOpenAPIValidation
i.Atomic = c.cpl.Spec.Atomic
i.SkipCRDs = c.cpl.Spec.SkipCRDs
i.SubNotes = false // we cant see notes or subnotes
i.Version = c.cpl.Spec.InstallVersion
i.Verify = false // TODO enable these args after we can config keyring
i.RepoURL = "" // We only support adding a repo and then installing it, not installing it directly via an url.
i.Keyring = "" // TODO enable these args after we can config keyring
if c.repo.Spec.AuthSecret != "" {
i.Username, i.Password, i.CaFile, i.CertFile, i.KeyFile, err = corev1alpha1.ParseRepoSecret(c.cli, c.repo)
if err != nil {
return nil, err
}
}
i.InsecureSkipTLSverify = c.repo.Spec.Insecure
i.PassCredentialsAll = false // TODO do we need add this args to override?
i.PostRenderer = newPostRenderer(c.repo.Spec.ImageOverride, c.cpl.Spec.Override.Images)
log.V(1).Info(fmt.Sprintf("Original chart version: %q", i.Version))
i.ReleaseName = c.cpl.GetReleaseName()
valueOpts, err := c.setVals(ctx)
if err != nil {
return nil, err
}
i.Namespace = c.cpl.Namespace
if dryRun {
i.ClientOnly = true
}
var out string
rel, out, err = c.HelmWrapper.Install(ctx, c.logger, i, valueOpts, c.cpl.GetReleaseName(), chartName)
c.logger.V(0).Info(out)
return
}
func (c *CoreHelmWrapper) upgrade(ctx context.Context, dryRun bool, chartName string) (rel *release.Release, err error) {
log := c.logger.WithValues("ComponentPlan", klog.KObj(c.cpl))
if c.repo.IsOCI() {
log.Info("Upgrading OCI Component")
} else {
log.Info("Upgrading non-OCI Component")
}
if dryRun {
log.WithValues("dryRun", true)
}
i, _ := c.GetDefaultUpgradeCfg()
i.Install = false // we just want to upgrade, for install, we use Install function
i.Devel = false // just set `>0.0.0-0` to ComponentPlan.Spec.Version is enough
i.DryRun = dryRun
i.Force = c.cpl.Spec.Force
i.DisableHooks = c.cpl.Spec.DisableHooks
i.DisableOpenAPIValidation = c.cpl.Spec.DisableOpenAPIValidation
i.SkipCRDs = c.cpl.Spec.SkipCRDs
i.Timeout = c.cpl.Spec.Timeout()
i.ResetValues = false // dont reset value to default
i.ReuseValues = false // dont reuse value
i.Wait = c.cpl.Spec.Wait
i.WaitForJobs = c.cpl.Spec.WaitForJobs
i.Atomic = c.cpl.Spec.Atomic
i.MaxHistory = c.cpl.Spec.GetMaxHistory()
i.CleanupOnFail = c.cpl.Spec.CleanupOnFail
i.SubNotes = false // we cant see notes or subnotes
i.Description = generateDescription(c.cpl)
i.DependencyUpdate = true
i.Version = c.cpl.Spec.InstallVersion
i.Verify = false // TODO enable these args after we can config keyring
i.RepoURL = "" // We only support adding a repo and then installing it, not installing it directly via a url.
i.Keyring = "" // TODO enable these args after we can config keyring
if c.repo.Spec.AuthSecret != "" {
i.Username, i.Password, i.CaFile, i.CertFile, i.KeyFile, err = corev1alpha1.ParseRepoSecret(c.cli, c.repo)
if err != nil {
return nil, err
}
}
i.InsecureSkipTLSverify = c.repo.Spec.Insecure
i.PassCredentialsAll = false // TODO do we need add this args to override?
i.PostRenderer = newPostRenderer(c.repo.Spec.ImageOverride, c.cpl.Spec.Override.Images)
valueOpts, err := c.setVals(ctx)
if err != nil {
return nil, err
}
i.Namespace = c.cpl.Namespace
var out string
rel, out, err = c.Upgrade(ctx, c.logger, i, valueOpts, c.cpl.GetReleaseName(), chartName, false)
c.logger.V(0).Info(out)
return rel, err
}
func (c *CoreHelmWrapper) uninstall(ctx context.Context) (err error) {
log := c.logger.WithValues("ComponentPlan", klog.KObj(c.cpl))
i := c.GetDefaultUninstallCfg()
i.DryRun = false // do not need to simulate the installation
i.DisableHooks = c.cpl.Spec.DisableHooks
i.KeepHistory = c.cpl.Spec.KeepHistory
i.Timeout = c.cpl.Spec.Timeout()
i.Wait = c.cpl.Spec.Wait
i.Description = generateDescription(c.cpl)
out, err := c.HelmWrapper.Uninstall(ctx, c.logger, i, c.cpl.GetReleaseName())
if err != nil {
return err
}
log.Info(out)
return nil
}
func (c *CoreHelmWrapper) rollback(ctx context.Context) (err error) {
log := c.logger.WithValues("ComponentPlan", klog.KObj(c.cpl))
i := c.GetDefaultRollbackCfg()
i.DryRun = false // do not need to simulate the rollback
i.DisableHooks = c.cpl.Spec.DisableHooks
i.Timeout = c.cpl.Spec.Timeout()
i.Wait = c.cpl.Spec.Wait
i.WaitForJobs = c.cpl.Spec.WaitForJobs
i.CleanupOnFail = c.cpl.Spec.CleanupOnFail
i.MaxHistory = c.cpl.Spec.GetMaxHistory()
i.Recreate = c.cpl.Spec.RecreatePods
i.Force = c.cpl.Spec.Force
i.Version = c.cpl.Status.InstalledRevision
out, err := c.HelmWrapper.Rollback(ctx, c.logger, i, c.cpl.GetReleaseName(), c.cpl.Status.InstalledRevision)
if err != nil {
return err
}
log.Info(fmt.Sprintf("release \"%s\" rollback to revision:%d", c.cpl.GetReleaseName(), c.cpl.Status.InstalledRevision))
log.Info(out)
return nil
}
func (c *CoreHelmWrapper) setVals(ctx context.Context) (valueOpts *values.Options, err error) {
valueOpts = &values.Options{}
for _, valuesFrom := range c.cpl.Spec.Override.ValuesFrom {
fileName, err := valuesFrom.Parse(ctx, c.cli, c.cpl.Namespace, valuesFrom.GetValuesFileDir(helmpath.CachePath(""), c.cpl.Namespace))
if err != nil {
return nil, err
}
valueOpts.ValueFiles = append(valueOpts.ValueFiles, fileName)
c.logger.V(1).Info(fmt.Sprintf("Add Override.ValuesFrom From: %s", fileName))
}
if o := c.cpl.Spec.Override; o.Values != nil {
fileName, err := utils.ParseValues(o.GetValueFileDir(helmpath.CachePath(""), c.cpl.Namespace, c.cpl.Name), o.Values)
if err != nil {
return nil, err
}
valueOpts.ValueFiles = append(valueOpts.ValueFiles, fileName)
c.logger.V(1).Info(fmt.Sprintf("Add Override.Values From: %s", fileName))
}
if set := c.cpl.Spec.Override.Set; len(set) != 0 {
valueOpts.Values = c.cpl.Spec.Override.Set
}
if set := c.cpl.Spec.Override.SetString; len(set) != 0 {
valueOpts.StringValues = c.cpl.Spec.Override.SetString
}
return valueOpts, nil
}
func generateDescription(plan *corev1alpha1.ComponentPlan) string {
return fmt.Sprintf("core:%s/%s/%s/%d %s", plan.GetNamespace(), plan.GetName(), plan.GetUID(), plan.GetGeneration(), plan.Spec.Config.Description)
}
func ParseDescription(desc string) (ns, name, uid string, generation int64, raw string) {
raw = desc
if !strings.HasPrefix(desc, "core:") {
return
}
other := strings.TrimPrefix(desc, "core:")
s := strings.Split(other, " ")
if len(s) < 2 {
return
}
s0 := s[0]
t := strings.Split(s0, "/")
if len(t) != 4 {
return
}
ns = t[0]
name = t[1]
uid = t[2]
generation, _ = strconv.ParseInt(t[3], 10, 64)
return ns, name, uid, generation, strings.Join(s[1:], " ")
}
// ParseDigestFromPullOut extracts the sha256 digest from the `helm pull` output string.
// It takes a string as input parameter which represents the output string.
// It returns a string which is the extracted sha256 digest.
func ParseDigestFromPullOut(out string) string {
for _, l := range strings.Split(out, "\n") {
l = strings.TrimSpace(l)
if strings.HasPrefix(l, "Digest: sha256:") {
return strings.TrimPrefix(l, "Digest: sha256:")
}
}
return ""
}