-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
helm.go
143 lines (124 loc) · 3.64 KB
/
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
package helm
import (
"fmt"
"io/ioutil"
"net/url"
"os/exec"
"path"
"strings"
"github.com/ghodss/yaml"
"github.com/argoproj/argo-cd/util/config"
executil "github.com/argoproj/argo-cd/util/exec"
)
type HelmRepository struct {
Creds
Name string
Repo string
}
// Helm provides wrapper functionality around the `helm` command.
type Helm interface {
// Template returns a list of unstructured objects from a `helm template` command
Template(opts *TemplateOpts) (string, error)
// GetParameters returns a list of chart parameters taking into account values in provided YAML files.
GetParameters(valuesFiles []string) (map[string]string, error)
// DependencyBuild runs `helm dependency build` to download a chart's dependencies
DependencyBuild() error
// Init runs `helm init --client-only`
Init() error
// Dispose deletes temp resources
Dispose()
}
// NewHelmApp create a new wrapper to run commands on the `helm` command-line tool.
func NewHelmApp(workDir string, repos []HelmRepository, isLocal bool) (Helm, error) {
cmd, err := NewCmd(workDir)
if err != nil {
return nil, err
}
cmd.IsLocal = isLocal
return &helm{repos: repos, cmd: *cmd}, nil
}
type helm struct {
cmd Cmd
repos []HelmRepository
}
// IsMissingDependencyErr tests if the error is related to a missing chart dependency
func IsMissingDependencyErr(err error) bool {
return strings.Contains(err.Error(), "found in requirements.yaml, but missing in charts") ||
strings.Contains(err.Error(), "found in Chart.yaml, but missing in charts/ directory")
}
func (h *helm) Template(templateOpts *TemplateOpts) (string, error) {
out, err := h.cmd.template(".", templateOpts)
if err != nil {
return "", err
}
return out, nil
}
func (h *helm) DependencyBuild() error {
for _, repo := range h.repos {
_, err := h.cmd.RepoAdd(repo.Name, repo.Repo, repo.Creds)
if err != nil {
return err
}
}
h.repos = nil
_, err := h.cmd.dependencyBuild()
return err
}
func (h *helm) Init() error {
_, err := h.cmd.Init()
return err
}
func (h *helm) Dispose() {
h.cmd.Close()
}
func Version() (string, error) {
cmd := exec.Command("helm", "version", "--client")
version, err := executil.RunWithRedactor(cmd, redactor)
if err != nil {
return "", fmt.Errorf("could not get helm version: %s", err)
}
return strings.TrimSpace(version), nil
}
func (h *helm) GetParameters(valuesFiles []string) (map[string]string, error) {
out, err := h.cmd.inspectValues(".")
if err != nil {
return nil, err
}
values := []string{out}
for _, file := range valuesFiles {
var fileValues []byte
parsedURL, err := url.ParseRequestURI(file)
if err == nil && (parsedURL.Scheme == "http" || parsedURL.Scheme == "https") {
fileValues, err = config.ReadRemoteFile(file)
} else {
fileValues, err = ioutil.ReadFile(path.Join(h.cmd.WorkDir, file))
}
if err != nil {
return nil, fmt.Errorf("failed to read value file %s: %s", file, err)
}
values = append(values, string(fileValues))
}
output := map[string]string{}
for _, file := range values {
values := map[string]interface{}{}
if err = yaml.Unmarshal([]byte(file), &values); err != nil {
return nil, fmt.Errorf("failed to parse values: %s", err)
}
flatVals(values, output)
}
return output, nil
}
func flatVals(input interface{}, output map[string]string, prefixes ...string) {
switch i := input.(type) {
case map[string]interface{}:
for k, v := range i {
flatVals(v, output, append(prefixes, k)...)
}
case []interface{}:
for j, v := range i {
flatVals(v, output, append(prefixes[0:len(prefixes)-1], fmt.Sprintf("%s[%v]", prefixes[len(prefixes)-1], j))...)
}
default:
output[strings.Join(prefixes, ".")] = fmt.Sprintf("%v", i)
}
}