-
Notifications
You must be signed in to change notification settings - Fork 298
/
files.go
338 lines (293 loc) · 9.57 KB
/
files.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
package tiltfile
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/tilt-dev/tilt/internal/k8s"
"github.com/tilt-dev/tilt/internal/localexec"
tiltfile_io "github.com/tilt-dev/tilt/internal/tiltfile/io"
"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
"github.com/tilt-dev/tilt/internal/tiltfile/value"
"github.com/tilt-dev/tilt/pkg/logger"
"github.com/tilt-dev/tilt/pkg/model"
"github.com/pkg/errors"
"go.starlark.net/starlark"
"github.com/tilt-dev/tilt/internal/kustomize"
)
const localLogPrefix = " → "
type execCommandOptions struct {
// logOutput writes stdout and stderr to logs if true.
logOutput bool
// logCommand writes the command being executed to logs if true.
logCommand bool
// logCommandPrefix is a custom prefix before the command (default: "Running: ") used if logCommand is true.
logCommandPrefix string
}
func (s *tiltfileState) local(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var commandValue, commandBatValue, commandDirValue starlark.Value
var commandEnv value.StringStringMap
quiet := false
echoOff := false
err := s.unpackArgs(fn.Name(), args, kwargs,
"command", &commandValue,
"quiet?", &quiet,
"command_bat", &commandBatValue,
"echo_off", &echoOff,
"env", &commandEnv,
"dir?", &commandDirValue,
)
if err != nil {
return nil, err
}
cmd, err := value.ValueGroupToCmdHelper(thread, commandValue, commandBatValue, commandDirValue, commandEnv)
if err != nil {
return nil, err
}
out, err := s.execLocalCmd(thread, cmd, execCommandOptions{
logOutput: !quiet,
logCommand: !echoOff,
logCommandPrefix: "local:",
})
if err != nil {
return nil, err
}
return tiltfile_io.NewBlob(out, fmt.Sprintf("local: %s", cmd)), nil
}
func (s *tiltfileState) execLocalCmd(t *starlark.Thread, cmd model.Cmd, options execCommandOptions) (string, error) {
var stdoutBuf, stderrBuf bytes.Buffer
ctx, err := starkit.ContextFromThread(t)
if err != nil {
return "", err
}
if options.logCommand {
prefix := options.logCommandPrefix
if prefix == "" {
prefix = "Running:"
}
s.logger.Infof("%s %s", prefix, cmd)
}
var runIO localexec.RunIO
if options.logOutput {
logOutput := logger.NewMutexWriter(logger.NewPrefixedLogger(localLogPrefix, s.logger).Writer(logger.InfoLvl))
runIO.Stdout = io.MultiWriter(&stdoutBuf, logOutput)
runIO.Stderr = io.MultiWriter(&stderrBuf, logOutput)
} else {
runIO.Stdout = &stdoutBuf
runIO.Stderr = &stderrBuf
}
// TODO(nick): Should this also inject any docker.Env overrides?
exitCode, err := s.execer.Run(ctx, cmd, runIO)
if err != nil || exitCode != 0 {
var errMessage strings.Builder
errMessage.WriteString(fmt.Sprintf("command %q failed.", cmd))
if err != nil {
errMessage.WriteString(fmt.Sprintf("\nerror: %v", err))
} else {
errMessage.WriteString(fmt.Sprintf("\nerror: exit status %d", exitCode))
}
if !options.logOutput {
// if we already logged the output, don't include it in the error message to prevent it from
// getting output 2x
errMessage.WriteString(fmt.Sprintf("\nstdout: %q\nstderr: %q",
stdoutBuf.String(), stderrBuf.String()))
}
return "", errors.New(errMessage.String())
}
// only show that there was no output if the command was echoed AND we wanted output logged
// otherwise, it's confusing to get "[no output]" without context of _what_ didn't have output
if options.logCommand && options.logOutput && stdoutBuf.Len() == 0 && stderrBuf.Len() == 0 {
s.logger.Infof("%s[no output]", localLogPrefix)
}
return stdoutBuf.String(), nil
}
func (s *tiltfileState) kustomize(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
path, kustomizeBin := value.NewLocalPathUnpacker(thread), value.NewLocalPathUnpacker(thread)
err := s.unpackArgs(fn.Name(), args, kwargs, "paths", &path, "kustomize_bin?", &kustomizeBin)
if err != nil {
return nil, err
}
kustomizeArgs := []string{"kustomize", "build"}
if kustomizeBin.Value != "" {
kustomizeArgs[0] = kustomizeBin.Value
}
_, err = exec.LookPath(kustomizeArgs[0])
if err != nil {
if kustomizeBin.Value != "" {
return nil, err
}
s.logger.Infof("Falling back to `kubectl kustomize` since `%s` was not found in PATH", kustomizeArgs[0])
kustomizeArgs = []string{"kubectl", "kustomize"}
}
// NOTE(nick): There's a bug in kustomize where it doesn't properly
// handle absolute paths. Convert to relative paths instead:
// https://github.com/kubernetes-sigs/kustomize/issues/2789
relKustomizePath, err := filepath.Rel(starkit.AbsWorkingDir(thread), path.Value)
if err != nil {
return nil, err
}
cmd := model.Cmd{Argv: append(kustomizeArgs, relKustomizePath), Dir: starkit.AbsWorkingDir(thread)}
yaml, err := s.execLocalCmd(thread, cmd, execCommandOptions{
logOutput: false,
logCommand: false,
})
if err != nil {
return nil, err
}
deps, err := kustomize.Deps(path.Value)
if err != nil {
return nil, fmt.Errorf("resolving deps: %v", err)
}
for _, d := range deps {
err := tiltfile_io.RecordReadPath(thread, tiltfile_io.WatchRecursive, d)
if err != nil {
return nil, err
}
}
return tiltfile_io.NewBlob(yaml, fmt.Sprintf("kustomize: %s", path.Value)), nil
}
func (s *tiltfileState) helm(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var path starlark.Value
var name string
var namespace string
var valueFiles value.StringOrStringList
var set value.StringOrStringList
err := s.unpackArgs(fn.Name(), args, kwargs,
"paths", &path,
"name?", &name,
"namespace?", &namespace,
"values?", &valueFiles,
"set?", &set)
if err != nil {
return nil, err
}
localPath, err := value.ValueToAbsPath(thread, path)
if err != nil {
return nil, fmt.Errorf("Argument 0 (paths): %v", err)
}
info, err := os.Stat(localPath)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("Could not read Helm chart directory %q: does not exist", localPath)
}
return nil, fmt.Errorf("Could not read Helm chart directory %q: %v", localPath, err)
} else if !info.IsDir() {
return nil, fmt.Errorf("helm() may only be called on directories with Chart.yaml: %q", localPath)
}
err = tiltfile_io.RecordReadPath(thread, tiltfile_io.WatchRecursive, localPath)
if err != nil {
return nil, err
}
deps, err := localSubchartDependenciesFromPath(localPath)
if err != nil {
return nil, err
}
for _, d := range deps {
err = tiltfile_io.RecordReadPath(thread, tiltfile_io.WatchRecursive, starkit.AbsPath(thread, d))
if err != nil {
return nil, err
}
}
version, err := getHelmVersion()
if err != nil {
return nil, err
}
var cmd []string
if name == "" {
// Use 'chart' as the release name, so that the release name is stable
// across Tiltfile loads.
// This looks like what helm does.
// https://github.com/helm/helm/blob/e672a42efae30d45ddd642a26557dcdbf5a9f5f0/pkg/action/install.go#L562
name = "chart"
}
if version == helmV3_1andAbove {
cmd = []string{"helm", "template", name, localPath, "--include-crds"}
} else if version == helmV3_0 {
cmd = []string{"helm", "template", name, localPath}
} else {
cmd = []string{"helm", "template", localPath, "--name", name}
}
if namespace != "" {
cmd = append(cmd, "--namespace", namespace)
}
for _, valueFile := range valueFiles.Values {
cmd = append(cmd, "--values", valueFile)
err := tiltfile_io.RecordReadPath(thread, tiltfile_io.WatchFileOnly, starkit.AbsPath(thread, valueFile))
if err != nil {
return nil, err
}
}
for _, setArg := range set.Values {
cmd = append(cmd, "--set", setArg)
}
stdout, err := s.execLocalCmd(thread, model.Cmd{Argv: cmd, Dir: starkit.AbsWorkingDir(thread)}, execCommandOptions{
logOutput: false,
logCommand: true,
})
if err != nil {
return nil, err
}
yaml := filterHelmTestYAML(stdout)
if version == helmV3_0 {
// Helm v3.0 has a bug where it doesn't include CRDs in the template output
// https://github.com/tilt-dev/tilt/issues/3605
crds, err := getHelmCRDs(localPath)
if err != nil {
return nil, err
}
yaml = strings.Join(append([]string{yaml}, crds...), "\n---\n")
}
if namespace != "" {
// helm template --namespace doesn't inject the namespace, nor provide
// YAML that defines the namespace, so we have to do both ourselves :\
// https://github.com/helm/helm/issues/5465
parsed, err := k8s.ParseYAMLFromString(yaml)
if err != nil {
return nil, err
}
for i, e := range parsed {
parsed[i] = e.WithNamespace(e.NamespaceOrDefault(namespace))
}
yaml, err = k8s.SerializeSpecYAML(parsed)
if err != nil {
return nil, err
}
}
return tiltfile_io.NewBlob(yaml, fmt.Sprintf("helm: %s", localPath)), nil
}
// NOTE(nick): This isn't perfect. For example, it doesn't handle chart deps
// properly. When possible, prefer Helm 3.1's --include-crds
func getHelmCRDs(path string) ([]string, error) {
crdPath := filepath.Join(path, "crds")
result := []string{}
err := filepath.Walk(crdPath, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
isYAML := info != nil && info.Mode().IsRegular() && hasYAMLExtension(path)
if !isYAML {
return nil
}
contents, err := ioutil.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
result = append(result, string(contents))
return nil
})
if err != nil && !os.IsNotExist(err) {
return nil, err
}
return result, nil
}
func hasYAMLExtension(fname string) bool {
ext := filepath.Ext(fname)
return strings.EqualFold(ext, ".yaml") || strings.EqualFold(ext, ".yml")
}