forked from mittwald/go-helm-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
965 lines (791 loc) · 29.7 KB
/
client.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
package helmclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"strings"
"golang.org/x/exp/slices"
"k8s.io/apimachinery/pkg/api/errors"
"github.com/spf13/pflag"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/downloader"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/registry"
"helm.sh/helm/v3/pkg/release"
"helm.sh/helm/v3/pkg/repo"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
var storage = repo.File{}
const (
defaultCachePath = "/tmp/.helmcache"
defaultRepositoryConfigPath = "/tmp/.helmrepo"
)
// New returns a new Helm client with the provided options
func New(options *Options) (Client, error) {
settings := cli.New()
err := setEnvSettings(&options, settings)
if err != nil {
return nil, err
}
return newClient(options, settings.RESTClientGetter(), settings)
}
// NewClientFromKubeConf returns a new Helm client constructed with the provided kubeconfig & RESTClient (optional) options.
func NewClientFromKubeConf(options *KubeConfClientOptions, restClientOpts ...RESTClientOption) (Client, error) {
settings := cli.New()
if options.KubeConfig == nil {
return nil, fmt.Errorf("kubeconfig missing")
}
clientGetter := NewRESTClientGetter(options.Namespace, options.KubeConfig, nil, restClientOpts...)
if options.KubeContext != "" {
settings.KubeContext = options.KubeContext
}
return newClient(options.Options, clientGetter, settings)
}
// NewClientFromRestConf returns a new Helm client constructed with the provided REST config options.
func NewClientFromRestConf(options *RestConfClientOptions) (Client, error) {
settings := cli.New()
clientGetter := NewRESTClientGetter(options.Namespace, nil, options.RestConfig)
return newClient(options.Options, clientGetter, settings)
}
// newClient is used by both NewClientFromKubeConf and NewClientFromRestConf
// and returns a new Helm client via the provided options and REST config.
func newClient(options *Options, clientGetter genericclioptions.RESTClientGetter, settings *cli.EnvSettings) (Client, error) {
err := setEnvSettings(&options, settings)
if err != nil {
return nil, err
}
debugLog := options.DebugLog
if debugLog == nil {
debugLog = func(format string, v ...interface{}) {
log.Printf(format, v...)
}
}
if options.Output == nil {
options.Output = os.Stdout
}
actionConfig := new(action.Configuration)
err = actionConfig.Init(
clientGetter,
settings.Namespace(),
os.Getenv("HELM_DRIVER"),
debugLog,
)
if err != nil {
return nil, err
}
registryClient, err := registry.NewClient(
registry.ClientOptDebug(settings.Debug),
registry.ClientOptCredentialsFile(settings.RegistryConfig),
)
if err != nil {
return nil, err
}
actionConfig.RegistryClient = registryClient
return &HelmClient{
Settings: settings,
Providers: getter.All(settings),
storage: &storage,
ActionConfig: actionConfig,
linting: options.Linting,
DebugLog: debugLog,
output: options.Output,
}, nil
}
// setEnvSettings sets the client's environment settings based on the provided client configuration.
func setEnvSettings(ppOptions **Options, settings *cli.EnvSettings) error {
if *ppOptions == nil {
*ppOptions = &Options{
RepositoryConfig: defaultRepositoryConfigPath,
RepositoryCache: defaultCachePath,
Linting: true,
}
}
options := *ppOptions
// set the namespace with this ugly workaround because cli.EnvSettings.namespace is private
// thank you helm!
if options.Namespace != "" {
pflags := pflag.NewFlagSet("", pflag.ContinueOnError)
settings.AddFlags(pflags)
err := pflags.Parse([]string{"-n", options.Namespace})
if err != nil {
return err
}
}
if options.RepositoryConfig == "" {
options.RepositoryConfig = defaultRepositoryConfigPath
}
if options.RepositoryCache == "" {
options.RepositoryCache = defaultCachePath
}
settings.RepositoryCache = options.RepositoryCache
settings.RepositoryConfig = options.RepositoryConfig
settings.Debug = options.Debug
if options.RegistryConfig != "" {
settings.RegistryConfig = options.RegistryConfig
}
return nil
}
// AddOrUpdateChartRepo adds or updates the provided helm chart repository.
func (c *HelmClient) AddOrUpdateChartRepo(entry repo.Entry) error {
chartRepo, err := repo.NewChartRepository(&entry, c.Providers)
if err != nil {
return err
}
chartRepo.CachePath = c.Settings.RepositoryCache
if c.storage.Has(entry.Name) {
c.DebugLog("WARNING: repository name %q already exists", entry.Name)
return nil
}
if !registry.IsOCI(entry.URL) {
_, err = chartRepo.DownloadIndexFile()
if err != nil {
return err
}
}
c.storage.Update(&entry)
err = c.storage.WriteFile(c.Settings.RepositoryConfig, 0o644)
if err != nil {
return err
}
return nil
}
// UpdateChartRepos updates the list of chart repositories stored in the client's cache.
func (c *HelmClient) UpdateChartRepos() error {
for _, entry := range c.storage.Repositories {
chartRepo, err := repo.NewChartRepository(entry, c.Providers)
if err != nil {
return err
}
chartRepo.CachePath = c.Settings.RepositoryCache
_, err = chartRepo.DownloadIndexFile()
if err != nil {
return err
}
c.storage.Update(entry)
}
return c.storage.WriteFile(c.Settings.RepositoryConfig, 0o644)
}
// InstallOrUpgradeChart installs or upgrades the provided chart and returns the corresponding release.
// Namespace and other context is provided via the helmclient.Options struct when instantiating a client.
func (c *HelmClient) InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) {
exists, err := c.chartExists(spec)
if err != nil {
return nil, err
}
if exists {
return c.upgrade(ctx, spec, opts)
}
return c.install(ctx, spec, opts)
}
// InstallChart installs the provided chart and returns the corresponding release.
// Namespace and other context is provided via the helmclient.Options struct when instantiating a client.
func (c *HelmClient) InstallChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) {
return c.install(ctx, spec, opts)
}
// UpgradeChart upgrades the provided chart and returns the corresponding release.
// Namespace and other context is provided via the helmclient.Options struct when instantiating a client.
func (c *HelmClient) UpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) {
return c.upgrade(ctx, spec, opts)
}
// ListDeployedReleases lists all deployed releases.
// Namespace and other context is provided via the helmclient.Options struct when instantiating a client.
func (c *HelmClient) ListDeployedReleases() ([]*release.Release, error) {
return c.listReleases(action.ListDeployed)
}
// ListReleasesByStateMask lists all releases filtered by stateMask.
// Namespace and other context is provided via the helmclient.Options struct when instantiating a client.
func (c *HelmClient) ListReleasesByStateMask(states action.ListStates) ([]*release.Release, error) {
return c.listReleases(states)
}
// GetReleaseValues returns the (optionally, all computed) values for the specified release.
func (c *HelmClient) GetReleaseValues(name string, allValues bool) (map[string]interface{}, error) {
return c.getReleaseValues(name, allValues)
}
// GetRelease returns a release specified by name.
func (c *HelmClient) GetRelease(name string) (*release.Release, error) {
return c.getRelease(name)
}
// RollbackRelease implicitly rolls back a release to the last revision.
func (c *HelmClient) RollbackRelease(spec *ChartSpec) error {
return c.rollbackRelease(spec)
}
// UninstallRelease uninstalls the provided release
func (c *HelmClient) UninstallRelease(spec *ChartSpec) error {
return c.uninstallRelease(spec)
}
// UninstallReleaseByName uninstalls a release identified by the provided 'name'.
func (c *HelmClient) UninstallReleaseByName(name string) error {
return c.uninstallReleaseByName(name)
}
// install installs the provided chart.
// Optionally lints the chart if the linting flag is set.
func (c *HelmClient) install(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) {
client := action.NewInstall(c.ActionConfig)
mergeInstallOptions(spec, client)
// NameAndChart returns either the TemplateName if set,
// the ReleaseName if set or the generatedName as the first return value.
releaseName, _, err := client.NameAndChart([]string{spec.ChartName})
if err != nil {
return nil, err
}
client.ReleaseName = releaseName
if client.Version == "" {
client.Version = ">0.0.0-0"
}
if opts != nil {
if opts.PostRenderer != nil {
client.PostRenderer = opts.PostRenderer
}
}
helmChart, chartPath, err := c.GetChart(spec.ChartName, &client.ChartPathOptions)
if err != nil {
return nil, err
}
if helmChart.Metadata.Type != "" && helmChart.Metadata.Type != "application" {
return nil, fmt.Errorf(
"chart %q has an unsupported type and is not installable: %q",
helmChart.Metadata.Name,
helmChart.Metadata.Type,
)
}
helmChart, err = updateDependencies(helmChart, &client.ChartPathOptions, chartPath, c, client.DependencyUpdate, spec)
if err != nil {
return nil, err
}
p := getter.All(c.Settings)
values, err := spec.GetValuesMap(p)
if err != nil {
return nil, err
}
if c.linting {
err = c.lint(chartPath, values)
if err != nil {
return nil, err
}
}
rel, err := client.RunWithContext(ctx, helmChart, values)
if err != nil {
return rel, err
}
c.DebugLog("release installed successfully: %s/%s-%s", rel.Name, rel.Chart.Metadata.Name, rel.Chart.Metadata.Version)
return rel, nil
}
// upgrade upgrades a chart and CRDs.
// Optionally lints the chart if the linting flag is set.
func (c *HelmClient) upgrade(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error) {
client := action.NewUpgrade(c.ActionConfig)
mergeUpgradeOptions(spec, client)
client.Install = true
if client.Version == "" {
client.Version = ">0.0.0-0"
}
if opts != nil {
if opts.PostRenderer != nil {
client.PostRenderer = opts.PostRenderer
}
}
helmChart, chartPath, err := c.GetChart(spec.ChartName, &client.ChartPathOptions)
if err != nil {
return nil, err
}
helmChart, err = updateDependencies(helmChart, &client.ChartPathOptions, chartPath, c, client.DependencyUpdate, spec)
if err != nil {
return nil, err
}
p := getter.All(c.Settings)
values, err := spec.GetValuesMap(p)
if err != nil {
return nil, err
}
if c.linting {
err = c.lint(chartPath, values)
if err != nil {
return nil, err
}
}
if !spec.SkipCRDs && spec.UpgradeCRDs {
c.DebugLog("upgrading crds")
err = c.upgradeCRDs(ctx, helmChart)
if err != nil {
return nil, err
}
}
upgradedRelease, upgradeErr := client.RunWithContext(ctx, spec.ReleaseName, helmChart, values)
if upgradeErr != nil {
resultErr := upgradeErr
if upgradedRelease == nil && opts != nil && opts.RollBack != nil {
rollbackErr := opts.RollBack.RollbackRelease(spec)
if rollbackErr != nil {
resultErr = fmt.Errorf("release failed, rollback failed: release error: %w, rollback error: %v", upgradeErr, rollbackErr)
} else {
resultErr = fmt.Errorf("release failed, rollback succeeded: release error: %w", upgradeErr)
}
}
c.DebugLog("release upgrade failed: %s", resultErr)
return nil, resultErr
}
c.DebugLog("release upgraded successfully: %s/%s-%s", upgradedRelease.Name, upgradedRelease.Chart.Metadata.Name, upgradedRelease.Chart.Metadata.Version)
return upgradedRelease, nil
}
// uninstallRelease uninstalls the provided release.
func (c *HelmClient) uninstallRelease(spec *ChartSpec) error {
client := action.NewUninstall(c.ActionConfig)
mergeUninstallReleaseOptions(spec, client)
resp, err := client.Run(spec.ReleaseName)
if err != nil {
return err
}
c.DebugLog("release uninstalled, response: %v", resp)
return nil
}
// uninstallReleaseByName uninstalls a release identified by the provided 'name'.
func (c *HelmClient) uninstallReleaseByName(name string) error {
client := action.NewUninstall(c.ActionConfig)
resp, err := client.Run(name)
if err != nil {
return err
}
c.DebugLog("release uninstalled, response: %v", resp)
return nil
}
// lint lints a chart's values.
func (c *HelmClient) lint(chartPath string, values map[string]interface{}) error {
client := action.NewLint()
result := client.Run([]string{chartPath}, values)
for _, err := range result.Errors {
c.DebugLog("Error %s", err)
}
if len(result.Errors) > 0 {
return fmt.Errorf("linting for chartpath %q failed", chartPath)
}
return nil
}
// TemplateChart returns a rendered version of the provided ChartSpec 'spec' by performing a "dry-run" install.
func (c *HelmClient) TemplateChart(spec *ChartSpec, options *HelmTemplateOptions) ([]byte, error) {
client := action.NewInstall(c.ActionConfig)
mergeInstallOptions(spec, client)
client.DryRun = true
client.ReleaseName = spec.ReleaseName
client.Replace = true // Skip the name check
client.ClientOnly = true
client.IncludeCRDs = true
if options != nil {
client.KubeVersion = options.KubeVersion
client.APIVersions = options.APIVersions
}
// NameAndChart returns either the TemplateName if set,
// the ReleaseName if set or the generatedName as the first return value.
releaseName, _, err := client.NameAndChart([]string{spec.ChartName})
if err != nil {
return nil, err
}
client.ReleaseName = releaseName
if client.Version == "" {
client.Version = ">0.0.0-0"
}
helmChart, chartPath, err := c.GetChart(spec.ChartName, &client.ChartPathOptions)
if err != nil {
return nil, err
}
if helmChart.Metadata.Type != "" && helmChart.Metadata.Type != "application" {
return nil, fmt.Errorf(
"chart %q has an unsupported type and is not installable: %q",
helmChart.Metadata.Name,
helmChart.Metadata.Type,
)
}
helmChart, err = updateDependencies(helmChart, &client.ChartPathOptions, chartPath, c, client.DependencyUpdate, spec)
if err != nil {
return nil, err
}
p := getter.All(c.Settings)
values, err := spec.GetValuesMap(p)
if err != nil {
return nil, err
}
out := new(bytes.Buffer)
rel, err := client.Run(helmChart, values)
// We ignore a potential error here because, when the --debug flag was specified,
// we always want to print the YAML, even if it is not valid. The error is still returned afterwards.
if rel != nil {
var manifests bytes.Buffer
fmt.Fprintln(&manifests, strings.TrimSpace(rel.Manifest))
if !client.DisableHooks {
for _, m := range rel.Hooks {
fmt.Fprintf(&manifests, "---\n# Source: %s\n%s\n", m.Path, m.Manifest)
}
}
// if we have a list of files to render, then check that each of the
// provided files exists in the chart.
fmt.Fprintf(out, "%s", manifests.String())
}
return out.Bytes(), err
}
// LintChart fetches a chart using the provided ChartSpec 'spec' and lints it's values.
func (c *HelmClient) LintChart(spec *ChartSpec) error {
_, chartPath, err := c.GetChart(spec.ChartName, &action.ChartPathOptions{
Version: spec.Version,
})
if err != nil {
return err
}
p := getter.All(c.Settings)
values, err := spec.GetValuesMap(p)
if err != nil {
return err
}
return c.lint(chartPath, values)
}
// SetDebugLog set's a Helm client's DebugLog to the desired 'debugLog'.
func (c *HelmClient) SetDebugLog(debugLog action.DebugLog) {
c.DebugLog = debugLog
}
// ListReleaseHistory lists the last 'max' number of entries
// in the history of the release identified by 'name'.
func (c *HelmClient) ListReleaseHistory(name string, max int) ([]*release.Release, error) {
client := action.NewHistory(c.ActionConfig)
client.Max = max
return client.Run(name)
}
// upgradeCRDs upgrades the CRDs of the provided chart.
func (c *HelmClient) upgradeCRDs(ctx context.Context, chartInstance *chart.Chart) error {
cfg, err := c.ActionConfig.RESTClientGetter.ToRESTConfig()
if err != nil {
return err
}
k8sClient, err := clientset.NewForConfig(cfg)
if err != nil {
return err
}
for _, crd := range chartInstance.CRDObjects() {
if err := c.upgradeCRD(ctx, k8sClient, crd); err != nil {
return err
}
c.DebugLog("CRD %s upgraded successfully for chart: %s", crd.Name, chartInstance.Metadata.Name)
}
return nil
}
// upgradeCRD upgrades the CRD 'crd' using the provided k8s client.
func (c *HelmClient) upgradeCRD(ctx context.Context, k8sClient *clientset.Clientset, crd chart.CRD) error {
// use this ugly detour to parse the crdYaml to a CustomResourceDefinitions-Object because direct
// yaml-unmarshalling does not find the correct keys
jsonCRD, err := yaml.ToJSON(crd.File.Data)
if err != nil {
return err
}
var typeMeta metav1.TypeMeta
err = json.Unmarshal(jsonCRD, &typeMeta)
if err != nil {
return err
}
switch typeMeta.APIVersion {
default:
return fmt.Errorf("WARNING: failed to upgrade CRD %q: unsupported api-version %q", crd.Name, typeMeta.APIVersion)
case "apiextensions.k8s.io/v1beta1":
return c.upgradeCRDV1Beta1(ctx, k8sClient, jsonCRD)
case "apiextensions.k8s.io/v1":
return c.upgradeCRDV1(ctx, k8sClient, jsonCRD)
}
}
func (c *HelmClient) createCRDV1(ctx context.Context, cl *clientset.Clientset, crd *v1.CustomResourceDefinition) error {
if _, err := cl.ApiextensionsV1().CustomResourceDefinitions().Create(ctx, crd, metav1.CreateOptions{}); err != nil {
return err
}
c.DebugLog("create ran successful for CRD: %s", crd.Name)
return nil
}
func (c *HelmClient) createCRDV1Beta1(ctx context.Context, cl *clientset.Clientset, crd *v1beta1.CustomResourceDefinition) error {
if _, err := cl.ApiextensionsV1beta1().CustomResourceDefinitions().Create(ctx, crd, metav1.CreateOptions{}); err != nil {
return err
}
c.DebugLog("create ran successful for CRD: %s", crd.Name)
return nil
}
// upgradeCRDV1Beta1 upgrades a CRD of the v1beta1 API version using the provided k8s client and CRD yaml.
func (c *HelmClient) upgradeCRDV1Beta1(ctx context.Context, cl *clientset.Clientset, rawCRD []byte) error {
var crdObj v1beta1.CustomResourceDefinition
if err := json.Unmarshal(rawCRD, &crdObj); err != nil {
return err
}
existingCRDObj, err := cl.ApiextensionsV1beta1().CustomResourceDefinitions().Get(ctx, crdObj.Name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return c.createCRDV1Beta1(ctx, cl, &crdObj)
}
return err
}
// Check that the storage version does not change through the update.
oldStorageVersion := v1beta1.CustomResourceDefinitionVersion{}
for _, oldVersion := range existingCRDObj.Spec.Versions {
if oldVersion.Storage {
oldStorageVersion = oldVersion
}
}
i := 0
for _, newVersion := range crdObj.Spec.Versions {
if newVersion.Storage {
i++
if newVersion.Name != oldStorageVersion.Name {
return fmt.Errorf("ERROR: storage version of CRD %q changed, aborting upgrade", crdObj.Name)
}
}
if i > 1 {
return fmt.Errorf("ERROR: more than one storage version set on CRD %q, aborting upgrade", crdObj.Name)
}
}
if reflect.DeepEqual(existingCRDObj.Spec.Versions, crdObj.Spec.Versions) {
c.DebugLog("INFO: new version of CRD %q contains no changes, skipping upgrade", crdObj.Name)
return nil
}
crdObj.ResourceVersion = existingCRDObj.ResourceVersion
if _, err := cl.ApiextensionsV1beta1().CustomResourceDefinitions().Update(ctx, &crdObj, metav1.UpdateOptions{DryRun: []string{"All"}}); err != nil {
return err
}
c.DebugLog("upgrade ran successful for CRD (dry run): %s", crdObj.Name)
if _, err = cl.ApiextensionsV1beta1().CustomResourceDefinitions().Update(ctx, &crdObj, metav1.UpdateOptions{}); err != nil {
return err
}
c.DebugLog("upgrade ran successful for CRD: %s", crdObj.Name)
return nil
}
// upgradeCRDV1Beta1 upgrades a CRD of the v1 API version using the provided k8s client and CRD yaml.
func (c *HelmClient) upgradeCRDV1(ctx context.Context, cl *clientset.Clientset, rawCRD []byte) error {
var crdObj v1.CustomResourceDefinition
if err := json.Unmarshal(rawCRD, &crdObj); err != nil {
return err
}
existingCRDObj, err := cl.ApiextensionsV1().CustomResourceDefinitions().Get(ctx, crdObj.Name, metav1.GetOptions{})
if err != nil {
if errors.IsNotFound(err) {
return c.createCRDV1(ctx, cl, &crdObj)
}
return err
}
// Check to ensure that no previously existing API version is deleted through the upgrade.
if len(existingCRDObj.Spec.Versions) > len(crdObj.Spec.Versions) {
c.DebugLog("WARNING: new version of CRD %q would remove an existing API version, skipping upgrade", crdObj.Name)
return nil
}
// Check that the storage version does not change through the update.
oldStorageVersion := v1.CustomResourceDefinitionVersion{}
for _, oldVersion := range existingCRDObj.Spec.Versions {
if oldVersion.Storage {
oldStorageVersion = oldVersion
}
}
i := 0
for _, newVersion := range crdObj.Spec.Versions {
if newVersion.Storage {
i++
if newVersion.Name != oldStorageVersion.Name {
return fmt.Errorf("ERROR: storage version of CRD %q changed, aborting upgrade", crdObj.Name)
}
}
if i > 1 {
return fmt.Errorf("ERROR: more than one storage version set on CRD %q, aborting upgrade", crdObj.Name)
}
}
if reflect.DeepEqual(existingCRDObj.Spec.Versions, crdObj.Spec.Versions) {
c.DebugLog("INFO: new version of CRD %q contains no changes, skipping upgrade", crdObj.Name)
return nil
}
crdObj.ResourceVersion = existingCRDObj.ResourceVersion
if _, err := cl.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, &crdObj, metav1.UpdateOptions{DryRun: []string{"All"}}); err != nil {
return err
}
c.DebugLog("upgrade ran successful for CRD (dry run): %s", crdObj.Name)
if _, err := cl.ApiextensionsV1().CustomResourceDefinitions().Update(ctx, &crdObj, metav1.UpdateOptions{}); err != nil {
return err
}
c.DebugLog("upgrade ran successful for CRD: %s", crdObj.Name)
return nil
}
// GetChart returns a chart matching the provided chart name and options.
func (c *HelmClient) GetChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error) {
chartPath, err := chartPathOptions.LocateChart(chartName, c.Settings)
if err != nil {
return nil, "", err
}
helmChart, err := loader.Load(chartPath)
if err != nil {
return nil, "", err
}
if helmChart.Metadata.Deprecated {
c.DebugLog("WARNING: This chart (%q) is deprecated", helmChart.Metadata.Name)
}
return helmChart, chartPath, err
}
// RunTests runs the tests that were deployed with the release provided. It returns true
// if all the tests ran successfully and false in all other cases.
// NOTE: error = nil implies that all tests ran to either success or failure.
func (c *HelmClient) RunChartTests(releaseName string) (bool, error) {
client := action.NewReleaseTesting(c.ActionConfig)
if c.Settings.Namespace() == "" {
return false, fmt.Errorf("namespace not set")
}
client.Namespace = c.Settings.Namespace()
rel, err := client.Run(releaseName)
if err != nil && rel == nil {
return false, fmt.Errorf("unable to find release '%s': %v", releaseName, err)
}
// Check that there are no test failures
return checkReleaseForTestFailure(rel) == false, nil
}
// chartExists checks whether a chart is already installed
// in a namespace or not based on the provided chart spec.
// Note that this function only considers the contained chart name and namespace.
func (c *HelmClient) chartExists(spec *ChartSpec) (bool, error) {
releases, err := c.listReleases(action.ListAll)
if err != nil {
return false, err
}
for _, r := range releases {
if r.Name == spec.ReleaseName && r.Namespace == spec.Namespace {
return true, nil
}
}
return false, nil
}
// listReleases lists all releases that match the given state.
func (c *HelmClient) listReleases(state action.ListStates) ([]*release.Release, error) {
listClient := action.NewList(c.ActionConfig)
listClient.StateMask = state
return listClient.Run()
}
// getReleaseValues returns the values for the provided release 'name'.
// If allValues = true is specified, all computed values are returned.
func (c *HelmClient) getReleaseValues(name string, allValues bool) (map[string]interface{}, error) {
getReleaseValuesClient := action.NewGetValues(c.ActionConfig)
getReleaseValuesClient.AllValues = allValues
return getReleaseValuesClient.Run(name)
}
// getRelease returns a release matching the provided 'name'.
func (c *HelmClient) getRelease(name string) (*release.Release, error) {
getReleaseClient := action.NewGet(c.ActionConfig)
return getReleaseClient.Run(name)
}
// rollbackRelease implicitly rolls back a release to the last revision.
func (c *HelmClient) rollbackRelease(spec *ChartSpec) error {
client := action.NewRollback(c.ActionConfig)
mergeRollbackOptions(spec, client)
return client.Run(spec.ReleaseName)
}
// updateDependencies checks dependencies for given helmChart and updates dependencies with metadata if dependencyUpdate is true. returns updated HelmChart
func updateDependencies(helmChart *chart.Chart, chartPathOptions *action.ChartPathOptions, chartPath string, c *HelmClient, dependencyUpdate bool, spec *ChartSpec) (*chart.Chart, error) {
if req := helmChart.Metadata.Dependencies; req != nil {
if err := action.CheckDependencies(helmChart, req); err != nil {
if dependencyUpdate {
man := &downloader.Manager{
ChartPath: chartPath,
Keyring: chartPathOptions.Keyring,
SkipUpdate: false,
Getters: c.Providers,
RepositoryConfig: c.Settings.RepositoryConfig,
RepositoryCache: c.Settings.RepositoryCache,
Out: c.output,
}
if err := man.Update(); err != nil {
return nil, err
}
helmChart, _, err = c.GetChart(spec.ChartName, chartPathOptions)
if err != nil {
return nil, err
}
} else {
return nil, err
}
}
}
return helmChart, nil
}
// checkReleaseForTestFailure parses the list of hooks in the release
// and checks the status of the test hooks, returning true if any test has Phase != Succeeded
// Returns false if all tests have passed (including if there are no tests)
func checkReleaseForTestFailure(rel *release.Release) bool {
// Check if any test failed
hooksToCheck := []*release.Hook{}
for _, hook := range rel.Hooks {
// Only check the Phase for events which are supposed to get triggered for "test" hook
if slices.Contains(hook.Events, release.HookTest) {
hooksToCheck = append(hooksToCheck, hook)
}
}
return slices.ContainsFunc(hooksToCheck, func(h *release.Hook) bool {
return h.LastRun.Phase != release.HookPhaseSucceeded
})
}
// mergeRollbackOptions merges values of the provided chart to helm rollback options used by the client.
func mergeRollbackOptions(chartSpec *ChartSpec, rollbackOptions *action.Rollback) {
rollbackOptions.DisableHooks = chartSpec.DisableHooks
rollbackOptions.DryRun = chartSpec.DryRun
rollbackOptions.Timeout = chartSpec.Timeout
rollbackOptions.CleanupOnFail = chartSpec.CleanupOnFail
rollbackOptions.Force = chartSpec.Force
rollbackOptions.MaxHistory = chartSpec.MaxHistory
rollbackOptions.Recreate = chartSpec.Recreate
rollbackOptions.Wait = chartSpec.Wait
rollbackOptions.WaitForJobs = chartSpec.WaitForJobs
}
// mergeInstallOptions merges values of the provided chart to helm install options used by the client.
func mergeInstallOptions(chartSpec *ChartSpec, installOptions *action.Install) {
installOptions.CreateNamespace = chartSpec.CreateNamespace
installOptions.DisableHooks = chartSpec.DisableHooks
installOptions.Replace = chartSpec.Replace
installOptions.Wait = chartSpec.Wait
installOptions.DependencyUpdate = chartSpec.DependencyUpdate
installOptions.Timeout = chartSpec.Timeout
installOptions.Namespace = chartSpec.Namespace
installOptions.ReleaseName = chartSpec.ReleaseName
installOptions.Version = chartSpec.Version
installOptions.GenerateName = chartSpec.GenerateName
installOptions.NameTemplate = chartSpec.NameTemplate
installOptions.Atomic = chartSpec.Atomic
installOptions.SkipCRDs = chartSpec.SkipCRDs
installOptions.DryRun = chartSpec.DryRun
installOptions.DryRunOption = chartSpec.DryRunOption
installOptions.SubNotes = chartSpec.SubNotes
installOptions.WaitForJobs = chartSpec.WaitForJobs
installOptions.Labels = chartSpec.Labels
}
// mergeUpgradeOptions merges values of the provided chart to helm upgrade options used by the client.
func mergeUpgradeOptions(chartSpec *ChartSpec, upgradeOptions *action.Upgrade) {
upgradeOptions.Version = chartSpec.Version
upgradeOptions.Namespace = chartSpec.Namespace
upgradeOptions.Timeout = chartSpec.Timeout
upgradeOptions.Wait = chartSpec.Wait
upgradeOptions.DependencyUpdate = chartSpec.DependencyUpdate
upgradeOptions.DisableHooks = chartSpec.DisableHooks
upgradeOptions.Force = chartSpec.Force
upgradeOptions.ResetValues = chartSpec.ResetValues
upgradeOptions.ReuseValues = chartSpec.ReuseValues
upgradeOptions.Recreate = chartSpec.Recreate
upgradeOptions.MaxHistory = chartSpec.MaxHistory
upgradeOptions.Atomic = chartSpec.Atomic
upgradeOptions.CleanupOnFail = chartSpec.CleanupOnFail
upgradeOptions.DryRun = chartSpec.DryRun
upgradeOptions.DryRunOption = chartSpec.DryRunOption
upgradeOptions.SubNotes = chartSpec.SubNotes
upgradeOptions.WaitForJobs = chartSpec.WaitForJobs
upgradeOptions.Labels = chartSpec.Labels
}
// mergeUninstallReleaseOptions merges values of the provided chart to helm uninstall options used by the client.
func mergeUninstallReleaseOptions(chartSpec *ChartSpec, uninstallReleaseOptions *action.Uninstall) {
uninstallReleaseOptions.DisableHooks = chartSpec.DisableHooks
uninstallReleaseOptions.Timeout = chartSpec.Timeout
uninstallReleaseOptions.DryRun = chartSpec.DryRun
uninstallReleaseOptions.Description = chartSpec.Description
uninstallReleaseOptions.KeepHistory = chartSpec.KeepHistory
uninstallReleaseOptions.Wait = chartSpec.Wait
}