forked from jenkins-x/jx
/
helm_template.go
1155 lines (1030 loc) · 36.3 KB
/
helm_template.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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package helm
import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/proto/hapi/chart"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/olli-ai/jx/v2/pkg/errorutil"
"github.com/olli-ai/jx/v2/pkg/kube"
"github.com/olli-ai/jx/v2/pkg/util"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
// AnnotationChartName stores the chart name
AnnotationChartName = "jenkins.io/chart"
// AnnotationAppVersion stores the chart's app version
AnnotationAppVersion = "jenkins.io/chart-app-version"
// AnnotationAppDescription stores the chart's app version
AnnotationAppDescription = "jenkins.io/chart-description"
// AnnotationAppRepository stores the chart's app repository
AnnotationAppRepository = "jenkins.io/chart-repository"
// LabelReleaseName stores the chart release name
LabelReleaseName = "jenkins.io/chart-release"
// LabelNamespace stores the chart namespace for cluster wide resources
LabelNamespace = "jenkins.io/namespace"
// LabelReleaseChartVersion stores the version of a chart installation in a label
LabelReleaseChartVersion = "jenkins.io/version"
// LabelAppName stores the chart's app name
LabelAppName = "jenkins.io/app-name"
// LabelAppVersion stores the chart's app version
LabelAppVersion = "jenkins.io/app-version"
// LabelReleaseHookChartVersion stores the version for a hook
LabelReleaseHookChartVersion = "jenkins.io/hook-version"
hookFailed = "hook-failed"
hookSucceeded = "hook-succeeded"
beforeHookCreation = "before-hook-creation"
// resourcesSeparator is used to separate multiple objects stored in the same YAML file
resourcesSeparator = "---"
)
// HelmTemplate implements common helm actions but purely as client side operations
// delegating a separate Helmer such as HelmCLI for the client side operations
type HelmTemplate struct {
Client *HelmCLI
WorkDir string
CWD string
Binary string
Runner util.Commander
KubectlValidate bool
KubeClient kubernetes.Interface
Namespace string
}
// NewHelmTemplate creates a new HelmTemplate instance configured to the given client side Helmer
func NewHelmTemplate(client *HelmCLI, workDir string, kubeClient kubernetes.Interface, ns string) *HelmTemplate {
cli := &HelmTemplate{
Client: client,
WorkDir: workDir,
Runner: client.Runner,
Binary: "kubectl",
CWD: client.CWD,
KubectlValidate: false,
KubeClient: kubeClient,
Namespace: ns,
}
return cli
}
type HelmHook struct {
Kind string
Name string
File string
Hooks []string
HookDeletePolicies []string
}
// SetHost is used to point at a locally running tiller
func (h *HelmTemplate) SetHost(tillerAddress string) {
// NOOP
}
// SetCWD configures the common working directory of helm CLI
func (h *HelmTemplate) SetCWD(dir string) {
h.Client.SetCWD(dir)
h.CWD = dir
}
// HelmBinary return the configured helm CLI
func (h *HelmTemplate) HelmBinary() string {
return h.Client.HelmBinary()
}
// SetHelmBinary configure a new helm CLI
func (h *HelmTemplate) SetHelmBinary(binary string) {
h.Client.SetHelmBinary(binary)
}
// Init executes the helm init command according with the given flags
func (h *HelmTemplate) Init(clientOnly bool, serviceAccount string, tillerNamespace string, upgrade bool) error {
return h.Client.Init(true, serviceAccount, tillerNamespace, upgrade)
}
// AddRepo adds a new helm repo with the given name and URL
func (h *HelmTemplate) AddRepo(repo, URL, username, password string) error {
return h.Client.AddRepo(repo, URL, username, password)
}
// RemoveRepo removes the given repo from helm
func (h *HelmTemplate) RemoveRepo(repo string) error {
return h.Client.RemoveRepo(repo)
}
// ListRepos list the installed helm repos together with their URL
func (h *HelmTemplate) ListRepos() (map[string]string, error) {
return h.Client.ListRepos()
}
// SearchCharts searches for all the charts matching the given filter
func (h *HelmTemplate) SearchCharts(filter string, allVersions bool) ([]ChartSummary, error) {
return h.Client.SearchCharts(filter, false)
}
// IsRepoMissing checks if the repository with the given URL is missing from helm
func (h *HelmTemplate) IsRepoMissing(URL string) (bool, string, error) {
return h.Client.IsRepoMissing(URL)
}
// UpdateRepo updates the helm repositories
func (h *HelmTemplate) UpdateRepo() error {
return h.Client.UpdateRepo()
}
// RemoveRequirementsLock removes the requirements.lock file from the current working directory
func (h *HelmTemplate) RemoveRequirementsLock() error {
return h.Client.RemoveRequirementsLock()
}
// BuildDependency builds the helm dependencies of the helm chart from the current working directory
func (h *HelmTemplate) BuildDependency() error {
return h.Client.BuildDependency()
}
// ListReleases lists the releases in ns
func (h *HelmTemplate) ListReleases(ns string) (map[string]ReleaseSummary, []string, error) {
list, err := h.KubeClient.AppsV1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return nil, nil, errors.WithStack(err)
}
charts := make(map[string]ReleaseSummary)
keys := make([]string, 0)
if list != nil {
for _, deploy := range list.Items {
labels := deploy.Labels
ann := deploy.Annotations
if labels != nil && ann != nil {
status := "ERROR"
if deploy.Status.Replicas > 0 {
if deploy.Status.UnavailableReplicas > 0 {
status = "PENDING"
} else {
status = "DEPLOYED"
}
}
updated := deploy.CreationTimestamp.Format("Mon Jan 2 15:04:05 2006")
chartName := ann[AnnotationChartName]
chartVersion := labels[LabelReleaseChartVersion]
releaseName := labels[LabelReleaseName]
keys = append(keys, releaseName)
charts[releaseName] = ReleaseSummary{
Chart: chartName,
ChartFullName: chartName + "-" + chartVersion,
Revision: strconv.FormatInt(deploy.Generation, 10),
Updated: updated,
Status: status,
ChartVersion: chartVersion,
ReleaseName: releaseName,
AppVersion: ann[AnnotationAppVersion],
Namespace: ns,
}
}
}
}
return charts, keys, nil
}
// FindChart find a chart in the current working directory, if no chart file is found an error is returned
func (h *HelmTemplate) FindChart() (string, error) {
return h.Client.FindChart()
}
// Lint lints the helm chart from the current working directory and returns the warnings in the output
func (h *HelmTemplate) Lint(valuesFiles []string) (string, error) {
return h.Client.Lint(valuesFiles)
}
// Env returns the environment variables for the helmer
func (h *HelmTemplate) Env() map[string]string {
return h.Client.Env()
}
// PackageChart packages the chart from the current working directory
func (h *HelmTemplate) PackageChart() error {
return h.Client.PackageChart()
}
// Version executes the helm version command and returns its output
func (h *HelmTemplate) Version(tls bool) (string, error) {
return h.Client.Version(tls)
}
// Template generates the YAML from the chart template to the given directory
func (h *HelmTemplate) Template(chart string, releaseName string, ns string, outDir string, upgrade bool, values []string, valueStrings []string,
valueFiles []string) error {
return h.Client.Template(chart, releaseName, ns, outDir, upgrade, values, valueStrings, valueFiles)
}
// MultiTemplate generates the YAML from the chart template to the given directory
func (h *HelmTemplate) MultiTemplate(chart string, releaseName string, ns string, outDir string, upgrade bool, values []string, valueStrings []string,
valueFiles []string) error {
return h.Client.MultiTemplate(chart, releaseName, ns, outDir, upgrade, values, valueStrings, valueFiles)
}
// Mutation API
func (h *HelmTemplate) auxInstallChart(multi, create, force, wait bool,
chart string, releaseName string, ns string, version string, timeout int,
values []string, valueStrings []string, valueFiles []string,
repo string, username string, password string) error {
err := h.clearOutputDir(releaseName)
if err != nil {
return err
}
outputDir, _, chartsDir, err := h.getDirectories(releaseName)
if err != nil {
return err
}
// check if we are installing a chart from the filesystem
chartDir := filepath.Join(h.CWD, chart)
exists, err := util.DirExists(chartDir)
if err != nil {
return err
}
if !exists {
log.Logger().Debugf("Fetching chart: %s", chart)
chartDir, err = h.fetchChart(chart, version, chartsDir, repo, username, password)
if err != nil {
return err
}
}
if multi {
err = h.Client.MultiTemplate(chartDir, releaseName, ns, outputDir, false, values, valueStrings, valueFiles)
} else {
err = h.Client.Template(chartDir, releaseName, ns, outputDir, false, values, valueStrings, valueFiles)
}
if err != nil {
return err
}
// Skip the chart when no resources are generated by the template
if empty, err := util.IsEmpty(outputDir); empty || err != nil {
return nil
}
metadata, versionText, err := h.getChart(chartDir, version)
if err != nil {
return err
}
helmHooks, err := h.addLabelsToFiles(chart, releaseName, versionText, metadata, ns)
if err != nil {
return err
}
helmCrdPhase := "crd-install"
helmPrePhase := "pre-install"
helmPostPhase := "post-install"
if !create {
helmPrePhase = "pre-upgrade"
helmPostPhase = "post-upgrade"
// Hack helm hooks
if len(MatchingHooks(helmHooks, "post-install", "")) > 0 &&
len(MatchingHooks(helmHooks, helmPostPhase, "")) == 0 {
helmPostPhase = "post-install"
if len(MatchingHooks(helmHooks, "pre-install", "")) > 0 &&
len(MatchingHooks(helmHooks, helmPrePhase, "")) == 0 {
helmPrePhase = "pre-install"
}
}
}
err = h.runHooks(helmHooks, helmCrdPhase, ns, chart, releaseName, wait, create, force)
if err != nil {
return err
}
err = h.runHooks(helmHooks, helmPrePhase, ns, chart, releaseName, wait, create, force)
if err != nil {
return err
}
err = h.kubectlApply(ns, releaseName, wait, create, force, outputDir)
if err != nil {
err2 := h.deleteHooks(helmHooks, helmPrePhase, hookFailed, ns)
return errorutil.CombineErrors(err, err2)
}
err = h.deleteHooks(helmHooks, helmPrePhase, hookSucceeded, ns)
if err != nil {
log.Logger().Warnf("Failed to delete the %s hook, due to: %s", helmPrePhase, err)
}
err = h.runHooks(helmHooks, helmPostPhase, ns, chart, releaseName, wait, create, force)
if err != nil {
err2 := h.deleteHooks(helmHooks, helmPostPhase, hookFailed, ns)
return errorutil.CombineErrors(err, err2)
}
err = h.deleteHooks(helmHooks, helmPostPhase, hookSucceeded, ns)
err2 := h.deleteOldResources(ns, releaseName, versionText, wait)
return errorutil.CombineErrors(err, err2)
}
// InstallChart installs a helm chart according with the given flags
func (h *HelmTemplate) InstallChart(chart string, releaseName string, ns string, version string, timeout int,
values []string, valueStrings []string, valueFiles []string, repo string, username string, password string) error {
return h.auxInstallChart(false, true, true, true, chart, releaseName, ns,
version, timeout, values, valueStrings, valueFiles, repo, username, password)
}
// UpgradeChart upgrades a helm chart according with given helm flags
func (h *HelmTemplate) UpgradeChart(chart string, releaseName string, ns string, version string, install bool, timeout int, force bool, wait bool, values []string, valueStrings []string, valueFiles []string, repo string, username string, password string) error {
return h.auxInstallChart(false, false, force, wait, chart, releaseName, ns,
version, timeout, values, valueStrings, valueFiles, repo, username, password)
}
// MultiInstallChart installs a helm chart according with the given flags
func (h *HelmTemplate) InstallMultiChart(chart string, releaseName string, ns string, version string, timeout int,
values []string, valueStrings []string, valueFiles []string, repo string, username string, password string) error {
return h.auxInstallChart(true, true, true, true, chart, releaseName, ns,
version, timeout, values, valueStrings, valueFiles, repo, username, password)
}
// MultiUpgradeChart upgrades a helm chart according with given helm flags
func (h *HelmTemplate) UpgradeMultiChart(chart string, releaseName string, ns string, version string, install bool, timeout int, force bool, wait bool, values []string, valueStrings []string, valueFiles []string, repo string, username string, password string) error {
return h.auxInstallChart(true, false, force, wait, chart, releaseName, ns,
version, timeout, values, valueStrings, valueFiles, repo, username, password)
}
// FetchChart fetches a Helm Chart
func (h *HelmTemplate) FetchChart(chart string, version string, untar bool, untardir string, repo string,
username string, password string) error {
_, err := h.fetchChart(chart, version, untardir, repo, username, password)
return err
}
func (h *HelmTemplate) DecryptSecrets(location string) error {
return h.Client.DecryptSecrets(location)
}
func (h *HelmTemplate) kubectlApply(ns string, releaseName string, wait bool, create bool, force bool, dir string) error {
namespacesDir := filepath.Join(dir, "namespaces")
if _, err := os.Stat(namespacesDir); !os.IsNotExist(err) {
fileInfo, err := ioutil.ReadDir(namespacesDir)
if err != nil {
return errors.Wrapf(err, "unable to locate subdirs in %s", namespacesDir)
}
for _, path := range fileInfo {
namespace := filepath.Base(path.Name())
fullPath := filepath.Join(namespacesDir, path.Name())
log.Logger().Debugf("Applying generated chart %q YAML via kubectl in dir: %s to namespace %s", releaseName, fullPath, namespace)
command := "apply"
if create {
command = "create"
}
args := []string{command, "--recursive", "-f", fullPath, "-l", LabelReleaseName + "=" + releaseName}
applyNs := namespace
if applyNs == "" {
applyNs = ns
}
if applyNs != "" {
args = append(args, "--namespace", applyNs)
}
if wait && !create {
args = append(args, "--wait")
}
if !h.KubectlValidate {
args = append(args, "--validate=false")
}
err = h.runKubectl(args...)
if err != nil {
return err
}
log.Logger().Info("")
}
return err
}
log.Logger().Debugf("Applying generated chart %q YAML via kubectl in dir: %s to namespace %s", releaseName, dir, ns)
command := "apply"
if create {
command = "create"
}
args := []string{command, "--recursive", "-f", dir, "-l", LabelReleaseName + "=" + releaseName}
if ns != "" {
args = append(args, "--namespace", ns)
}
if wait && !create {
args = append(args, "--wait")
}
if force {
args = append(args, "--force")
}
if !h.KubectlValidate {
args = append(args, "--validate=false")
}
err := h.runKubectl(args...)
if err != nil {
return err
}
return nil
}
func (h *HelmTemplate) kubectlApplyFile(ns string, helmHook string, wait bool, create bool, force bool, file string) error {
log.Logger().Debugf("Applying Helm hook %s YAML via kubectl in file: %s", helmHook, file)
command := "apply"
if create {
command = "create"
}
args := []string{command, "-f", file}
if ns != "" {
args = append(args, "--namespace", ns)
}
if wait && !create {
args = append(args, "--wait")
}
if force && !create {
args = append(args, "--force")
}
if !h.KubectlValidate {
args = append(args, "--validate=false")
}
err := h.runKubectl(args...)
return err
}
func (h *HelmTemplate) kubectlDeleteFile(ns string, file string) error {
log.Logger().Debugf("Deleting helm hook sources from file: %s", file)
return h.runKubectl("delete", "-f", file, "--namespace", ns, "--wait")
}
func (h *HelmTemplate) deleteOldResources(ns string, releaseName string, versionText string, wait bool) error {
selector := LabelReleaseName + "=" + releaseName + "," +
LabelReleaseChartVersion + "," +
LabelReleaseChartVersion + "!=" + versionText
err := h.deleteNamespacedResourcesBySelector(ns, selector, wait, "older releases")
if err != nil {
return err
}
return h.deleteClusterResourcesBySelector(ns, selector, wait, "older releases")
}
func (h *HelmTemplate) deleteNamespacedResourcesBySelector(ns string, selector string, wait bool, message string) error {
kinds := []string{"job", "deployment", "pvc", "configmap", "release", "sa", "role", "rolebinding", "secret"}
errList := []error{}
log.Logger().Debugf("Removing Kubernetes resources from %s using selector: %s from %s", message, util.ColorInfo(selector), strings.Join(kinds, " "))
errs := h.deleteResourcesBySelector(ns, kinds, selector, wait)
errList = append(errList, errs...)
return errorutil.CombineErrors(errList...)
}
func (h *HelmTemplate) deleteClusterResourcesBySelector(ns string, selector string, wait bool, message string) error {
clusterKinds := []string{"all", "clusterrole", "clusterrolebinding"}
errList := []error{}
hasPermissions, errs := kube.CanI(h.KubeClient, kube.Delete, kube.All)
errList = append(errList, errs...)
if hasPermissions {
selector += "," + LabelNamespace + "=" + ns
log.Logger().Debugf("Removing Kubernetes resources from %s using selector: %s from %s", message, util.ColorInfo(selector), strings.Join(clusterKinds, " "))
errs = h.deleteResourcesBySelector("", clusterKinds, selector, wait)
errList = append(errList, errs...)
}
return errorutil.CombineErrors(errList...)
}
func (h *HelmTemplate) deleteResourcesBySelector(ns string, kinds []string, selector string, wait bool) []error {
errList := []error{}
for _, kind := range kinds {
args := []string{"delete", kind, "--ignore-not-found", "-l", selector}
if ns != "" {
args = append(args, "--namespace", ns)
}
if wait {
args = append(args, "--wait")
}
output, err := h.runKubectlWithOutput(args...)
if err != nil {
errList = append(errList, err)
} else {
output = strings.TrimSpace(output)
if output != "No resources found" {
log.Logger().Info(output)
}
}
}
return errList
}
// isClusterKind returns true if the kind or resource name is a cluster wide resource
func isClusterKind(kind string) bool {
lower := strings.ToLower(kind)
return strings.HasPrefix(lower, "cluster") || strings.HasPrefix(lower, "namespace")
}
// DeleteRelease removes the given release
func (h *HelmTemplate) DeleteRelease(ns string, releaseName string, purge bool) error {
if ns == "" {
ns = h.Namespace
}
selector := LabelReleaseName + "=" + releaseName
err := h.deleteNamespacedResourcesBySelector(ns, selector, true, fmt.Sprintf("release %s", releaseName))
if err != nil {
return err
}
return h.deleteClusterResourcesBySelector(ns, selector, true, fmt.Sprintf("release %s", releaseName))
}
// StatusRelease returns the output of the helm status command for a given release
func (h *HelmTemplate) StatusRelease(ns string, releaseName string) error {
releases, _, err := h.ListReleases(ns)
if err != nil {
return errors.Wrap(err, "listing current chart releases")
}
if _, ok := releases[releaseName]; ok {
return nil
}
return fmt.Errorf("chart release %q not found", releaseName)
}
// StatusReleaseWithOutput returns the output of the helm status command for a given release
func (h *HelmTemplate) StatusReleaseWithOutput(ns string, releaseName string, outputFormat string) (string, error) {
return h.Client.StatusReleaseWithOutput(ns, releaseName, outputFormat)
}
func (h *HelmTemplate) getDirectories(releaseName string) (string, string, string, error) {
if releaseName == "" {
return "", "", "", fmt.Errorf("No release name specified!")
}
if h.WorkDir == "" {
var err error
h.WorkDir, err = ioutil.TempDir("", "helm-template-workdir-")
if err != nil {
return "", "", "", errors.Wrap(err, "Failed to create temporary directory for helm template workdir")
}
}
workDir := h.WorkDir
outDir := filepath.Join(workDir, releaseName, "output")
helmHookDir := filepath.Join(workDir, releaseName, "helmHooks")
chartsDir := filepath.Join(workDir, releaseName, "chartFiles")
dirs := []string{outDir, helmHookDir, chartsDir}
for _, d := range dirs {
err := os.MkdirAll(d, util.DefaultWritePermissions)
if err != nil {
return "", "", "", err
}
}
return outDir, helmHookDir, chartsDir, nil
}
// clearOutputDir removes all files in the helm output dir
func (h *HelmTemplate) clearOutputDir(releaseName string) error {
dir, helmDir, chartsDir, err := h.getDirectories(releaseName)
if err != nil {
return err
}
return util.RecreateDirs(dir, helmDir, chartsDir)
}
func (h *HelmTemplate) fetchChart(chart string, version string, dir string, repo string, username string,
password string) (string, error) {
exists, err := util.FileExists(chart)
if err != nil {
return "", err
}
if exists {
log.Logger().Infof("Chart dir already exists: %s", dir)
return chart, nil
}
if dir == "" {
return "", fmt.Errorf("must specify dir for chart %s", chart)
}
args := []string{
"fetch", "-d", dir, "--untar", chart,
}
if repo != "" {
args = append(args, "--repo", repo)
}
if version != "" {
args = append(args, "--version", version)
}
if username != "" {
args = append(args, "--username", username)
}
if password != "" {
args = append(args, "--password", password)
}
err = h.Client.runHelm(args...)
if err != nil {
return "", err
}
answer := dir
files, err := ioutil.ReadDir(dir)
if err != nil {
return "", err
}
for _, f := range files {
if f.IsDir() {
answer = filepath.Join(dir, f.Name())
break
}
}
log.Logger().Debugf("Fetched chart %s to dir %s", chart, answer)
return answer, nil
}
func (h *HelmTemplate) addLabelsToFiles(chart string, releaseName string, version string, metadata *chart.Metadata, ns string) ([]*HelmHook, error) {
dir, helmHookDir, _, err := h.getDirectories(releaseName)
if err != nil {
return nil, err
}
return addLabelsToChartYaml(dir, helmHookDir, chart, releaseName, version, metadata, ns)
}
func splitObjectsInFiles(inputFile string, baseDir string, relativePath, defaultNamespace string) ([]string, error) {
result := make([]string, 0)
f, err := os.Open(inputFile)
if err != nil {
return result, errors.Wrapf(err, "opening inputFile %q", inputFile)
}
defer f.Close()
scanner := bufio.NewScanner(f)
var buf bytes.Buffer
fileName := filepath.Base(inputFile)
count := 0
for scanner.Scan() {
line := scanner.Text()
if line == resourcesSeparator {
// ensure that we actually have YAML in the buffer
data := buf.Bytes()
if isWhitespaceOrComments(data) {
buf.Reset()
continue
}
m := yaml.MapSlice{}
err = yaml.Unmarshal(data, &m)
namespace := getYamlValueString(&m, "metadata", "namespace")
if namespace == "" {
namespace = defaultNamespace
}
if err != nil {
return make([]string, 0), errors.Wrapf(err, "Failed to parse the following YAML from inputFile '%s':\n%s", inputFile, buf.String())
}
if len(m) == 0 {
buf.Reset()
continue
}
partFile, err := writeObjectInFile(&buf, baseDir, relativePath, namespace, fileName, count)
if err != nil {
return result, errors.Wrapf(err, "saving object")
}
result = append(result, partFile)
buf.Reset()
count += count + 1
} else {
_, err := buf.WriteString(line)
if err != nil {
return result, errors.Wrapf(err, "writing line from inputFile %q into a buffer", inputFile)
}
_, err = buf.WriteString("\n")
if err != nil {
return result, errors.Wrapf(err, "writing a new line in the buffer")
}
}
}
if buf.Len() > 0 && !isWhitespaceOrComments(buf.Bytes()) {
data := buf.Bytes()
m := yaml.MapSlice{}
err = yaml.Unmarshal(data, &m)
namespace := getYamlValueString(&m, "metadata", "namespace")
if namespace == "" {
namespace = defaultNamespace
}
partFile, err := writeObjectInFile(&buf, baseDir, relativePath, namespace, fileName, count)
if err != nil {
return result, errors.Wrapf(err, "saving object")
}
result = append(result, partFile)
}
return result, nil
}
// isWhitespaceOrComments returns true if the data is empty, whitespace or comments only
func isWhitespaceOrComments(data []byte) bool {
if len(data) == 0 {
return true
}
lines := strings.Split(string(data), "\n")
for _, line := range lines {
t := strings.TrimSpace(line)
if t != "" && !strings.HasPrefix(t, "#") {
return false
}
}
return true
}
func writeObjectInFile(buf io.WriterTo, baseDir string, relativePath, namespace string, fileName string, count int) (string, error) {
relativeDir := filepath.Dir(relativePath)
const filePrefix = "part"
partFile := fmt.Sprintf("%s%d-%s", filePrefix, count, fileName)
absFile := filepath.Join(baseDir, "namespaces", namespace, relativeDir, partFile)
absFileDir := filepath.Dir(absFile)
log.Logger().Debugf("creating file: %s", absFile)
err := os.MkdirAll(absFileDir, os.ModePerm)
if err != nil {
return "", errors.Wrapf(err, "creating directory %q", absFileDir)
}
file, err := os.Create(absFile)
if err != nil {
return "", errors.Wrapf(err, "creating file %q", absFile)
}
log.Logger().Debugf("writing data to %s", absFile)
defer file.Close()
_, err = buf.WriteTo(file)
if err != nil {
return "", errors.Wrapf(err, "writing object to file %q", absFile)
}
return absFile, nil
}
func addLabelsToChartYaml(basedir string, hooksDir string, chart string, releaseName string, version string, metadata *chart.Metadata, ns string) ([]*HelmHook, error) {
helmHooks := []*HelmHook{}
log.Logger().Debugf("Searching for yaml files from basedir %s", basedir)
err := filepath.Walk(basedir, func(path string, f os.FileInfo, err error) error {
ext := filepath.Ext(path)
if ext == ".yaml" {
file := path
relativePath, err := filepath.Rel(basedir, file)
if err != nil {
return errors.Wrapf(err, "unable to determine relative path %q", file)
}
partFiles, err := splitObjectsInFiles(file, basedir, relativePath, ns)
if err != nil {
return errors.Wrapf(err, "splitting objects from file %q", file)
}
log.Logger().Debugf("part files list: %v", partFiles)
for _, partFile := range partFiles {
log.Logger().Debugf("processing part file: %s", partFile)
data, err := ioutil.ReadFile(partFile)
if err != nil {
return errors.Wrapf(err, "Failed to load partFile %s", partFile)
}
m := yaml.MapSlice{}
err = yaml.Unmarshal(data, &m)
if err != nil {
return errors.Wrapf(err, "Failed to parse YAML of partFile %s", partFile)
}
kind := getYamlValueString(&m, "kind")
helmHookType := getYamlValueString(&m, "metadata", "annotations", "helm.sh/hook")
err = processChartResource(partFile, data, kind, ns, releaseName, &m, metadata, version, chart, helmHookType != "")
if err != nil {
return errors.Wrap(err, fmt.Sprintf("when processing chart resource '%s'", partFile))
}
if helmHookType != "" {
helmHook, err := getHelmHookFromFile(basedir, path, hooksDir, helmHookType, kind, &m, partFile)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("when getting helm hook from part file '%s'", partFile))
}
helmHooks = append(helmHooks, helmHook)
}
}
}
return nil
})
return helmHooks, err
}
func getHelmHookFromFile(basedir string, path string, hooksDir string, helmHook string, kind string, m *yaml.MapSlice, partFile string) (*HelmHook, error) {
// lets move any helm hooks to the new partFile
relPath, err := filepath.Rel(basedir, path)
if err != nil {
return &HelmHook{}, err
}
if relPath == "" {
return &HelmHook{}, fmt.Errorf("Failed to find relative path of basedir %s and path %s", basedir, partFile)
}
// add the hook type into the directory structure
newPath := filepath.Join(hooksDir, relPath)
newDir, _ := filepath.Split(newPath)
err = os.MkdirAll(newDir, util.DefaultWritePermissions)
if err != nil {
return &HelmHook{}, errors.Wrap(err, fmt.Sprintf("when creating '%s'", newDir))
}
// copy the hook part file to the hooks path
_, hookFileName := filepath.Split(partFile)
hookFile := filepath.Join(newDir, hookFileName)
err = os.Rename(partFile, hookFile)
if err != nil {
return &HelmHook{}, errors.Wrap(err, fmt.Sprintf("when copying from '%s' to '%s'", partFile, hookFile))
}
name := getYamlValueString(m, "metadata", "name")
helmDeletePolicy := getYamlValueString(m, "metadata", "annotations", "helm.sh/hook-delete-policy")
log.Logger().Debugf("processed hook %s (%s | %s) to file: %s", kind, helmHook, helmDeletePolicy, hookFile)
return NewHelmHook(kind, name, hookFile, helmHook, helmDeletePolicy), nil
}
func processChartResource(partFile string, data []byte, kind string, ns string, releaseName string, m *yaml.MapSlice, metadata *chart.Metadata, version string, chart string, hook bool) error {
err := setYamlValue(m, releaseName, "metadata", "labels", LabelReleaseName)
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of partFile %s", partFile)
}
if !isClusterKind(kind) {
err = setYamlValue(m, ns, "metadata", "labels", LabelNamespace)
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of partFile %s", partFile)
}
}
if hook {
err = setYamlValue(m, version, "metadata", "labels", LabelReleaseHookChartVersion)
} else {
err = setYamlValue(m, version, "metadata", "labels", LabelReleaseChartVersion)
}
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of partFile %s", partFile)
}
chartName := ""
if metadata != nil {
chartName = metadata.GetName()
appVersion := metadata.GetAppVersion()
if appVersion != "" {
err = setYamlValue(m, appVersion, "metadata", "annotations", AnnotationAppVersion)
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of partFile %s", partFile)
}
}
}
if chartName == "" {
chartName = chart
}
err = setYamlValue(m, chartName, "metadata", "annotations", AnnotationChartName)
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of partFile %s", partFile)
}
data, err = yaml.Marshal(m)
if err != nil {
return errors.Wrapf(err, "Failed to marshal YAML of partFile %s", partFile)
}
err = ioutil.WriteFile(partFile, data, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "Failed to write YAML partFile %s", partFile)
}
log.Logger().Debugf("processed resource %s in part file: %s", kind, partFile)
return nil
}
func getYamlValueString(mapSlice *yaml.MapSlice, keys ...string) string {
value := getYamlValue(mapSlice, keys...)
answer, ok := value.(string)
if ok {
return answer
}
return ""
}
func getYamlValue(mapSlice *yaml.MapSlice, keys ...string) interface{} {
if mapSlice == nil {
return nil
}
if mapSlice == nil {
return fmt.Errorf("No map input!")
}
m := mapSlice
lastIdx := len(keys) - 1
for idx, k := range keys {
last := idx >= lastIdx
found := false
for _, mi := range *m {
if mi.Key == k {
found = true
if last {
return mi.Value
} else {
value := mi.Value
if value == nil {
return nil
} else {
v, ok := value.(yaml.MapSlice)
if ok {
m = &v
} else {
v2, ok := value.(*yaml.MapSlice)
if ok {
m = v2
} else {
return nil
}
}
}
}
}
}
if !found {
return nil
}
}
return nil
}
// setYamlValue navigates through the YAML object structure lazily creating or inserting new values
func setYamlValue(mapSlice *yaml.MapSlice, value string, keys ...string) error {
if mapSlice == nil {
return fmt.Errorf("No map input!")
}
m := mapSlice
lastIdx := len(keys) - 1
for idx, k := range keys {
last := idx >= lastIdx
found := false
for i, mi := range *m {
if mi.Key == k {
found = true
if last {
(*m)[i].Value = value
} else if i < len(*m) {
value := (*m)[i].Value
if value == nil {
v := &yaml.MapSlice{}
(*m)[i].Value = v
m = v