-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
helm.go
182 lines (159 loc) · 4.68 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
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
package helm
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"os/exec"
"path"
"strings"
"github.com/ghodss/yaml"
"github.com/argoproj/argo-cd/v2/util/config"
executil "github.com/argoproj/argo-cd/v2/util/exec"
pathutil "github.com/argoproj/argo-cd/v2/util/io/path"
)
type HelmRepository struct {
Creds
Name string
Repo string
EnableOci bool
}
// 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 []pathutil.ResolvedFilePath) (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, version string, proxy string) (Helm, error) {
cmd, err := NewCmd(workDir, version, proxy)
if err != nil {
return nil, err
}
cmd.IsLocal = isLocal
return &helm{repos: repos, cmd: *cmd}, nil
}
type helm struct {
cmd Cmd
repos []HelmRepository
}
var _ Helm = &helm{}
// 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 {
isHelmOci := h.cmd.IsHelmOci
defer func() {
h.cmd.IsHelmOci = isHelmOci
}()
for i := range h.repos {
repo := h.repos[i]
if repo.EnableOci {
h.cmd.IsHelmOci = true
if repo.Creds.Username != "" && repo.Creds.Password != "" {
_, err := h.cmd.Login(repo.Repo, repo.Creds)
defer func() {
_, _ = h.cmd.Logout(repo.Repo, repo.Creds)
}()
if err != nil {
return err
}
}
} else {
_, 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(shortForm bool) (string, error) {
executable := "helm"
cmdArgs := []string{"version", "--client"}
if shortForm {
cmdArgs = append(cmdArgs, "--short")
}
cmd := exec.Command(executable, cmdArgs...)
// example version output:
// long: "version.BuildInfo{Version:\"v3.3.1\", GitCommit:\"249e5215cde0c3fa72e27eb7a30e8d55c9696144\", GitTreeState:\"clean\", GoVersion:\"go1.14.7\"}"
// short: "v3.3.1+g249e521"
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 []pathutil.ResolvedFilePath) (map[string]string, error) {
out, err := h.cmd.inspectValues(".")
if err != nil {
return nil, err
}
values := []string{out}
for i := range valuesFiles {
file := string(valuesFiles[i])
var fileValues []byte
parsedURL, err := url.ParseRequestURI(file)
if err == nil && (parsedURL.Scheme == "http" || parsedURL.Scheme == "https") {
fileValues, err = config.ReadRemoteFile(file)
} else {
filePath := path.Join(h.cmd.WorkDir, file)
if _, err := os.Stat(filePath); os.IsNotExist(err) {
continue
}
fileValues, err = ioutil.ReadFile(filePath)
}
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)
}
}