forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helm_template.go
734 lines (650 loc) · 20.7 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
package helm
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/log"
"github.com/jenkins-x/jx/pkg/util"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
const (
// LabelReleaseName stores the chart release name
LabelReleaseName = "jenkins.io/chart-release"
// LabelReleaseChartVersion stores the version of a chart installation in a label
LabelReleaseChartVersion = "jenkins.io/version"
hookFailed = "hook-failed"
hookSucceeded = "hook-succeeded"
)
// 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.Command
KubectlValidate bool
KubeClient kubernetes.Interface
}
// NewHelmTemplate creates a new HelmTemplate instance configured to the given client side Helmer
func NewHelmTemplate(client *HelmCLI, workDir string, kubeClient kubernetes.Interface) *HelmTemplate {
cli := &HelmTemplate{
Client: client,
WorkDir: workDir,
Runner: client.Runner,
Binary: "kubectl",
CWD: client.CWD,
KubectlValidate: false,
KubeClient: kubeClient,
}
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)
}
// 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 string, URL string) error {
return h.Client.AddRepo(repo, URL)
}
// 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) ([]ChartSummary, error) {
return h.Client.SearchCharts(filter)
}
// IsRepoMissing checks if the repository with the given URL is missing from helm
func (h *HelmTemplate) IsRepoMissing(URL string) (bool, 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()
}
// ListCharts execute the helm list command and returns its output
func (h *HelmTemplate) ListCharts() (string, error) {
return h.Client.ListCharts()
}
// SearchChartVersions search all version of the given chart
func (h *HelmTemplate) SearchChartVersions(chart string) ([]string, error) {
return h.Client.SearchChartVersions(chart)
}
// 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() (string, error) {
return h.Client.Lint()
}
// 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.VersionWithArgs(tls, "--client")
}
// Mutation API
// 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, valueFiles []string) error {
err := h.clearOutputDir(releaseName)
if err != nil {
return err
}
outputDir, _, chartsDir, err := h.getDirectories(releaseName)
chartDir, err := h.chartNameToFolder(chart, chartsDir)
if err != nil {
return err
}
err = h.Client.Template(chartDir, releaseName, ns, outputDir, false, values, valueFiles)
if err != nil {
return err
}
_, versionText, err := h.getChartNameAndVersion(chartDir, version)
if err != nil {
return err
}
helmHooks, err := h.addLabelsToFiles(releaseName, versionText)
if err != nil {
return err
}
helmPrePhase := "pre-install"
helmPostPhase := "post-install"
wait := true
create := true
err = h.runHooks(helmHooks, helmPrePhase, ns, chart, releaseName, wait, create)
if err != nil {
return err
}
err = h.kubectlApply(ns, chart, releaseName, wait, create, outputDir)
if err != nil {
h.deleteHooks(helmHooks, helmPrePhase, hookFailed, ns)
return err
}
h.deleteHooks(helmHooks, helmPrePhase, hookSucceeded, ns)
err = h.runHooks(helmHooks, helmPostPhase, ns, chart, releaseName, wait, create)
if err != nil {
h.deleteHooks(helmHooks, helmPostPhase, hookFailed, ns)
return err
}
err = h.deleteHooks(helmHooks, helmPostPhase, hookSucceeded, ns)
err2 := h.deleteOldResources(ns, releaseName, versionText, wait)
return util.CombineErrors(err, err2)
}
// 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, valueFiles []string) error {
err := h.clearOutputDir(releaseName)
if err != nil {
return err
}
outputDir, _, chartsDir, err := h.getDirectories(releaseName)
chartDir, err := h.chartNameToFolder(chart, chartsDir)
if err != nil {
return err
}
err = h.Client.Template(chartDir, releaseName, ns, outputDir, false, values, valueFiles)
if err != nil {
return err
}
_, versionText, err := h.getChartNameAndVersion(chartDir, version)
if err != nil {
return err
}
helmHooks, err := h.addLabelsToFiles(releaseName, versionText)
if err != nil {
return err
}
helmPrePhase := "pre-upgrade"
helmPostPhase := "post-upgrade"
create := false
err = h.runHooks(helmHooks, helmPrePhase, ns, chart, releaseName, wait, create)
if err != nil {
return err
}
err = h.kubectlApply(ns, chart, releaseName, wait, create, outputDir)
if err != nil {
h.deleteHooks(helmHooks, helmPrePhase, hookFailed, ns)
return err
}
h.deleteHooks(helmHooks, helmPrePhase, hookSucceeded, ns)
err = h.runHooks(helmHooks, helmPostPhase, ns, chart, releaseName, wait, create)
if err != nil {
h.deleteHooks(helmHooks, helmPostPhase, hookFailed, ns)
return err
}
err = h.deleteHooks(helmHooks, helmPostPhase, hookSucceeded, ns)
err2 := h.deleteOldResources(ns, releaseName, versionText, wait)
return util.CombineErrors(err, err2)
}
func (h *HelmTemplate) kubectlApply(ns string, chart string, releaseName string, wait bool, create bool, dir string) error {
log.Infof("Applying generated chart %s YAML via kubectl in dir: %s\n", chart, dir)
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 !h.KubectlValidate {
args = append(args, "--validate=false")
}
return h.runKubectl(args...)
}
func (h *HelmTemplate) kubectlApplyFile(ns string, helmHook string, wait bool, create bool, file string) error {
log.Infof("Applying Helm hook %s YAML via kubectl in file: %s\n", 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 !h.KubectlValidate {
args = append(args, "--validate=false")
}
return h.runKubectl(args...)
}
func (h *HelmTemplate) kubectlDeleteFile(ns string, file string) error {
log.Infof("Deleting helm hook sources from file: %s\n", 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 + "!=" + versionText
log.Infof("Removing Kubernetes resources from older releases using selector: %s\n", util.ColorInfo(selector))
return h.deleteResourcesBySelector(ns, selector, wait)
}
func (h *HelmTemplate) deleteResourcesBySelector(ns string, selector string, wait bool) error {
args := []string{"delete", "all", "--ignore-not-found", "--namespace", ns, "-l", selector}
if wait {
args = append(args, "--wait")
}
err := h.runKubectl(args...)
if err != nil {
return err
}
// now lets delete resource CRDs
args = []string{"delete", "release", "--ignore-not-found", "--namespace", ns, "-l", selector}
if wait {
args = append(args, "--wait")
}
// lets ignore failures - probably due to CRD not yet existing
h.runKubectl(args...)
return nil
}
// DeleteRelease removes the given release
func (h *HelmTemplate) DeleteRelease(ns string, releaseName string, purge bool) error {
selector := LabelReleaseName + "=" + releaseName
log.Infof("Removing release %s using selector: %s\n", util.ColorInfo(releaseName), util.ColorInfo(selector))
return h.deleteResourcesBySelector(ns, selector, true)
}
// StatusRelease returns the output of the helm status command for a given release
func (h *HelmTemplate) StatusRelease(ns string, releaseName string) error {
// TODO
return nil
}
// StatusReleases returns the status of all installed releases
func (h *HelmTemplate) StatusReleases(ns string) (map[string]string, error) {
statusMap := map[string]string{}
if h.KubeClient == nil {
return statusMap, fmt.Errorf("No KubeClient configured!")
}
deployList, err := h.KubeClient.AppsV1beta1().Deployments(ns).List(metav1.ListOptions{})
if err != nil {
return statusMap, errors.Wrapf(err, "Failed to list Deployments in namespace %s", ns)
}
for _, deploy := range deployList.Items {
labels := deploy.Labels
if labels != nil {
release := labels[LabelReleaseName]
if release != "" {
statusMap[release] = "DEPLOYED"
}
}
}
return statusMap, nil
}
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) chartNameToFolder(chart string, dir string) (string, error) {
exists, err := util.FileExists(chart)
if err != nil {
return "", err
}
if exists {
return chart, nil
}
err = h.Client.runHelm("fetch", "-d", dir, "--untar", chart)
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.Infof("Fetched chart %s to dir %s\n", chart, answer)
return answer, nil
}
func (h *HelmTemplate) addLabelsToFiles(releaseName string, version string) ([]*HelmHook, error) {
dir, helmHookDir, _, err := h.getDirectories(releaseName)
if err != nil {
return nil, err
}
return addLabelsToChartYaml(dir, helmHookDir, releaseName, version)
}
func addLabelsToChartYaml(dir string, hooksDir string, releaseName string, version string) ([]*HelmHook, error) {
helmHooks := []*HelmHook{}
err := filepath.Walk(dir, func(path string, f os.FileInfo, err error) error {
ext := filepath.Ext(path)
if ext == ".yaml" {
file := path
data, err := ioutil.ReadFile(file)
if err != nil {
return errors.Wrapf(err, "Failed to load file %s", file)
}
m := yaml.MapSlice{}
err = yaml.Unmarshal(data, &m)
if err != nil {
return errors.Wrapf(err, "Failed to parse YAML of file %s", file)
}
helmHook := getYamlValueString(&m, "metadata", "annotations", "helm.sh/hook")
if helmHook != "" {
// lets move any helm hooks to the new path
relPath, err := filepath.Rel(dir, path)
if err != nil {
return err
}
if relPath == "" {
return fmt.Errorf("Failed to find relative path of dir %s and path %s", dir, path)
}
newPath := filepath.Join(hooksDir, relPath)
newDir, _ := filepath.Split(newPath)
err = os.MkdirAll(newDir, util.DefaultWritePermissions)
if err != nil {
return err
}
err = os.Rename(path, newPath)
if err != nil {
log.Warnf("Failed to move helm hook template %s to %s: %s", path, newPath, err)
return err
}
name := getYamlValueString(&m, "metadata", "name")
kind := getYamlValueString(&m, "kind")
helmDeletePolicy := getYamlValueString(&m, "metadata", "annotations", "helm.sh/hook-delete-policy")
helmHooks = append(helmHooks, NewHelmHook(kind, name, newPath, helmHook, helmDeletePolicy))
return nil
}
err = setYamlValue(&m, releaseName, "metadata", "labels", LabelReleaseName)
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of file %s", file)
}
err = setYamlValue(&m, version, "metadata", "labels", LabelReleaseChartVersion)
if err != nil {
return errors.Wrapf(err, "Failed to modify YAML of file %s", file)
}
data, err = yaml.Marshal(&m)
if err != nil {
return errors.Wrapf(err, "Failed to marshal YAML of file %s", file)
}
err = ioutil.WriteFile(file, data, util.DefaultWritePermissions)
if err != nil {
return errors.Wrapf(err, "Failed to write YAML file %s", file)
}
}
return nil
})
return helmHooks, err
}
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 {
value := (*m)[i].Value
if value == nil {
v := &yaml.MapSlice{}
(*m)[i].Value = v
m = v
} else {
v, ok := value.(yaml.MapSlice)
if ok {
m2 := &yaml.MapSlice{}
*m2 = append(*m2, v...)
(*m)[i].Value = m2
m = m2
} else {
v2, ok := value.(*yaml.MapSlice)
if ok {
m2 := &yaml.MapSlice{}
*m2 = append(*m2, *v2...)
(*m)[i].Value = m2
m = m2
} else {
return fmt.Errorf("Could not convert key %s value %#v to a yaml.MapSlice", k, value)
}
}
}
}
}
}
if !found {
if last {
*m = append(*m, yaml.MapItem{
Key: k,
Value: value,
})
} else {
m2 := &yaml.MapSlice{}
*m = append(*m, yaml.MapItem{
Key: k,
Value: m2,
})
m = m2
}
}
}
return nil
}
func (h *HelmTemplate) runKubectl(args ...string) error {
h.Runner.Name = h.Binary
h.Runner.Dir = h.CWD
h.Runner.Args = args
_, err := h.Runner.RunWithoutRetry()
return err
}
func (h *HelmTemplate) runKubectlWithOutput(args ...string) (string, error) {
h.Runner.Dir = h.CWD
h.Runner.Name = h.Binary
h.Runner.Args = args
return h.Runner.RunWithoutRetry()
}
// getChartNameAndVersion returns the chart name and version for the current chart folder
func (h *HelmTemplate) getChartNameAndVersion(chartDir string, version *string) (string, string, error) {
versionText := ""
file := filepath.Join(chartDir, "Chart.yaml")
if !filepath.IsAbs(chartDir) {
file = filepath.Join(h.Runner.Dir, file)
}
exists, err := util.FileExists(file)
if err != nil {
return "", versionText, err
}
if !exists {
return "", versionText, fmt.Errorf("No file %s found!", file)
}
chartName, versionText, err := LoadChartNameAndVersion(file)
if version != nil && *version != "" {
versionText = *version
}
return chartName, versionText, err
}
func (h *HelmTemplate) runHooks(hooks []*HelmHook, hookPhase string, ns string, chart string, releaseName string, wait bool, create bool) error {
matchingHooks := MatchingHooks(hooks, hookPhase, "")
for _, hook := range matchingHooks {
err := h.kubectlApplyFile(ns, hookPhase, wait, create, hook.File)
if err != nil {
return err
}
}
return nil
}
func (h *HelmTemplate) deleteHooks(hooks []*HelmHook, hookPhase string, hookDeletePolicy string, ns string) error {
matchingHooks := MatchingHooks(hooks, hookPhase, hookDeletePolicy)
for _, hook := range matchingHooks {
kind := hook.Kind
name := hook.Name
if kind == "Job" && name != "" {
log.Infof("Waiting for helm %s hook Job %s to complete before removing it\n", hookPhase, name)
err := kube.WaitForJobToTerminate(h.KubeClient, ns, name, time.Minute*10)
if err != nil {
log.Warnf("Job %s has not yet terminated for helm hook phase %s due to: %s so removing it anyway\n", name, hookPhase, err)
}
} else {
log.Warnf("Could not wait for hook resource to complete as it is kind %s and name %s for phase %s\n", kind, name, hookPhase)
}
// TODO wait for job to be complete
err := h.kubectlDeleteFile(ns, hook.File)
if err != nil {
return err
}
}
return nil
}
// NewHelmHook returns a newly created HelmHook
func NewHelmHook(kind string, name string, file string, hook string, hookDeletePolicy string) *HelmHook {
return &HelmHook{
Kind: kind,
Name: name,
File: file,
Hooks: strings.Split(hook, ","),
HookDeletePolicies: strings.Split(hookDeletePolicy, ","),
}
}
// MatchingHooks returns the matching files which have the given hook name and if hookPolicy is not blank the hook policy too
func MatchingHooks(hooks []*HelmHook, hook string, hookDeletePolicy string) []*HelmHook {
answer := []*HelmHook{}
for _, h := range hooks {
if util.StringArrayIndex(h.Hooks, hook) >= 0 &&
(hookDeletePolicy == "" || util.StringArrayIndex(h.HookDeletePolicies, hookDeletePolicy) >= 0) {
answer = append(answer, h)
}
}
return answer
}