-
Notifications
You must be signed in to change notification settings - Fork 25
/
helm.go
312 lines (243 loc) · 8.16 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
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
package helm
import (
"fmt"
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/suse-edge/edge-image-builder/pkg/fileio"
"github.com/suse-edge/edge-image-builder/pkg/image"
"go.uber.org/zap"
"gopkg.in/yaml.v3"
)
const (
templateLogFileName = "helm-template.log"
pullLogFileName = "helm-pull.log"
repoAddLogFileName = "helm-repo-add.log"
registryLoginFileName = "helm-registry-login.log"
outputFileFlags = os.O_APPEND | os.O_CREATE | os.O_WRONLY
)
type Helm struct {
outputDir string
certsDir string
}
func New(outputDir, certsDir string) *Helm {
return &Helm{
outputDir: outputDir,
certsDir: certsDir,
}
}
func chartPath(repoName, repoURL, chart string) string {
if strings.HasPrefix(repoURL, "http") {
return fmt.Sprintf("%s/%s", repoName, chart)
}
path, _ := url.JoinPath(repoURL, chart)
return path
}
func (h *Helm) AddRepo(repo *image.HelmRepository) error {
logFile := filepath.Join(h.outputDir, repoAddLogFileName)
file, err := os.OpenFile(logFile, outputFileFlags, fileio.NonExecutablePerms)
if err != nil {
return fmt.Errorf("opening log file: %w", err)
}
defer func() {
if err = file.Close(); err != nil {
zap.S().Warnf("Closing %s file failed: %s", logFile, err)
}
}()
cmd := addRepoCommand(repo, h.certsDir, file)
if _, err = fmt.Fprintf(file, "command: %s\n", cmd); err != nil {
return fmt.Errorf("writing command prefix to log file: %w", err)
}
return cmd.Run()
}
func addRepoCommand(repo *image.HelmRepository, certsDir string, output io.Writer) *exec.Cmd {
var args []string
args = append(args, "repo", "add", repo.Name, repo.URL)
if repo.Authentication.Username != "" && repo.Authentication.Password != "" {
args = append(args, "--username", repo.Authentication.Username, "--password", repo.Authentication.Password)
}
if repo.SkipTLSVerify {
args = append(args, "--insecure-skip-tls-verify")
} else if repo.CAFile != "" {
caFilePath := filepath.Join(certsDir, repo.CAFile)
args = append(args, "--ca-file", caFilePath)
}
cmd := exec.Command("helm", args...)
cmd.Stdout = output
cmd.Stderr = output
return cmd
}
func (h *Helm) RegistryLogin(repo *image.HelmRepository) error {
logFile := filepath.Join(h.outputDir, registryLoginFileName)
file, err := os.OpenFile(logFile, outputFileFlags, fileio.NonExecutablePerms)
if err != nil {
return fmt.Errorf("opening log file: %w", err)
}
defer func() {
if err = file.Close(); err != nil {
zap.S().Warnf("Closing %s file failed: %s", logFile, err)
}
}()
host, err := getHost(repo.URL)
if err != nil {
return fmt.Errorf("getting host url: %w", err)
}
cmd := registryLoginCommand(host, repo, h.certsDir, file)
if _, err = fmt.Fprintf(file, "command: %s\n", cmd); err != nil {
return fmt.Errorf("writing command prefix to log file: %w", err)
}
return cmd.Run()
}
func registryLoginCommand(host string, repo *image.HelmRepository, certsDir string, output io.Writer) *exec.Cmd {
var args []string
args = append(args, "registry", "login", host)
if repo.Authentication.Username != "" && repo.Authentication.Password != "" {
args = append(args, "--username", repo.Authentication.Username, "--password", repo.Authentication.Password)
}
if repo.SkipTLSVerify || repo.PlainHTTP {
args = append(args, "--insecure")
} else if repo.CAFile != "" {
caFilePath := filepath.Join(certsDir, repo.CAFile)
args = append(args, "--ca-file", caFilePath)
}
cmd := exec.Command("helm", args...)
cmd.Stdout = output
cmd.Stderr = output
return cmd
}
func (h *Helm) Pull(chart string, repo *image.HelmRepository, version, destDir string) (string, error) {
logFile := filepath.Join(h.outputDir, pullLogFileName)
file, err := os.OpenFile(logFile, outputFileFlags, fileio.NonExecutablePerms)
if err != nil {
return "", fmt.Errorf("opening log file: %w", err)
}
defer func() {
if err = file.Close(); err != nil {
zap.S().Warnf("Closing %s file failed: %s", logFile, err)
}
}()
chartDir := filepath.Join(destDir, chart)
if err = os.MkdirAll(chartDir, os.ModePerm); err != nil {
return "", fmt.Errorf("creating chart dir %q: %w", chartDir, err)
}
cmd := pullCommand(chart, repo, version, chartDir, h.certsDir, file)
if _, err = fmt.Fprintf(file, "command: %s\n", cmd); err != nil {
return "", fmt.Errorf("writing command prefix to log file: %w", err)
}
if err = cmd.Run(); err != nil {
return "", fmt.Errorf("executing command: %w", err)
}
chartPathPattern := fmt.Sprintf("%s/%s-*.tgz", chartDir, chart)
matches, err := filepath.Glob(chartPathPattern)
if err != nil {
return "", fmt.Errorf("looking for chart with pattern %s: %w", chartPathPattern, err)
} else if len(matches) != 1 {
return "", fmt.Errorf("unable to locate downloaded chart: %s", chart)
}
chartPath := matches[0]
return chartPath, nil
}
func pullCommand(chart string, repo *image.HelmRepository, version, destDir, certsDir string, output io.Writer) *exec.Cmd {
path := chartPath(repo.Name, repo.URL, chart)
var args []string
args = append(args, "pull", path)
if version != "" {
args = append(args, "--version", version)
}
if destDir != "" {
args = append(args, "--destination", destDir)
}
switch {
case repo.SkipTLSVerify:
args = append(args, "--insecure-skip-tls-verify")
case repo.PlainHTTP:
args = append(args, "--plain-http")
case repo.CAFile != "":
caFilePath := filepath.Join(certsDir, repo.CAFile)
args = append(args, "--ca-file", caFilePath)
}
cmd := exec.Command("helm", args...)
cmd.Stdout = output
cmd.Stderr = output
return cmd
}
func (h *Helm) Template(chart, repository, version, valuesFilePath, kubeVersion, targetNamespace string, apiVersions []string) ([]map[string]any, error) {
logFile := filepath.Join(h.outputDir, templateLogFileName)
file, err := os.OpenFile(logFile, outputFileFlags, fileio.NonExecutablePerms)
if err != nil {
return nil, fmt.Errorf("opening log file: %w", err)
}
defer func() {
if err = file.Close(); err != nil {
zap.S().Warnf("Closing %s file failed: %s", logFile, err)
}
}()
chartContentsBuffer := new(strings.Builder)
cmd := templateCommand(chart, repository, version, valuesFilePath, kubeVersion, targetNamespace, apiVersions, io.MultiWriter(file, chartContentsBuffer), file)
if _, err = fmt.Fprintf(file, "command: %s\n", cmd); err != nil {
return nil, fmt.Errorf("writing command prefix to log file: %w", err)
}
if err = cmd.Run(); err != nil {
return nil, fmt.Errorf("executing command: %w", err)
}
chartContents := chartContentsBuffer.String()
resources, err := parseChartContents(chartContents)
if err != nil {
return nil, fmt.Errorf("parsing chart contents: %w", err)
}
return resources, nil
}
func templateCommand(chart, repository, version, valuesFilePath, kubeVersion, targetNamespace string, apiVersions []string, stdout, stderr io.Writer) *exec.Cmd {
var args []string
args = append(args, "template", "--skip-crds", chart, repository)
if targetNamespace != "" {
args = append(args, "--namespace", targetNamespace)
}
if version != "" {
args = append(args, "--version", version)
}
if valuesFilePath != "" {
args = append(args, "-f", valuesFilePath)
}
if len(apiVersions) != 0 {
args = append(args, "--api-versions", strings.Join(apiVersions, ","))
}
args = append(args, "--kube-version", kubeVersion)
cmd := exec.Command("helm", args...)
cmd.Stdout = stdout
cmd.Stderr = stderr
return cmd
}
func parseChartContents(chartContents string) ([]map[string]any, error) {
var resources []map[string]any
for _, resource := range strings.Split(chartContents, "---\n") {
if resource == "" {
continue
}
resource = strings.TrimSpace(resource)
if !strings.HasPrefix(resource, "# Source") {
continue
}
source, content, found := strings.Cut(resource, "\n")
if !found {
zap.S().Warnf("Invalid Helm resource: %s", resource)
continue
}
var r map[string]any
if err := yaml.Unmarshal([]byte(content), &r); err != nil {
return nil, fmt.Errorf("decoding resource from source '%s': %w", source, err)
}
resources = append(resources, r)
}
return resources, nil
}
func getHost(repoURL string) (string, error) {
parsedURL, err := url.Parse(repoURL)
if err != nil {
return "", fmt.Errorf("parsing url %q: %w", repoURL, err)
}
return parsedURL.Host, nil
}