-
Notifications
You must be signed in to change notification settings - Fork 303
/
tiltfile.go
221 lines (181 loc) · 5.67 KB
/
tiltfile.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
package tiltfile
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"github.com/pkg/errors"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"github.com/windmilleng/wmclient/pkg/analytics"
"github.com/windmilleng/tilt/internal/dockercompose"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/internal/model"
"github.com/windmilleng/tilt/internal/ospath"
)
const FileName = "Tiltfile"
const TiltIgnoreFileName = ".tiltignore"
func init() {
resolve.AllowLambda = true
resolve.AllowNestedDef = true
resolve.AllowGlobalReassign = true
}
type TiltfileLoadResult struct {
Manifests []model.Manifest
ConfigFiles []string
Warnings []string
TiltIgnoreContents string
}
type TiltfileLoader interface {
Load(ctx context.Context, filename string, matching map[string]bool) (TiltfileLoadResult, error)
}
type FakeTiltfileLoader struct {
Manifests []model.Manifest
ConfigFiles []string
Warnings []string
Err error
}
var _ TiltfileLoader = &FakeTiltfileLoader{}
func NewFakeTiltfileLoader() *FakeTiltfileLoader {
return &FakeTiltfileLoader{}
}
func (tfl *FakeTiltfileLoader) Load(ctx context.Context, filename string, matching map[string]bool) (TiltfileLoadResult, error) {
return TiltfileLoadResult{
Manifests: tfl.Manifests,
ConfigFiles: tfl.ConfigFiles,
Warnings: tfl.Warnings,
}, tfl.Err
}
func ProvideTiltfileLoader(analytics analytics.Analytics, dcCli dockercompose.DockerComposeClient, kubeContext k8s.KubeContext) TiltfileLoader {
return tiltfileLoader{analytics: analytics, dcCli: dcCli, kubeContext: kubeContext}
}
type tiltfileLoader struct {
analytics analytics.Analytics
dcCli dockercompose.DockerComposeClient
kubeContext k8s.KubeContext
}
var _ TiltfileLoader = &tiltfileLoader{}
func printWarnings(s *tiltfileState) {
for _, w := range s.warnings {
s.logger.Infof("WARNING: %s\n", w)
}
}
// Load loads the Tiltfile in `filename`, and returns the manifests matching `matching`.
func (tfl tiltfileLoader) Load(ctx context.Context, filename string, matching map[string]bool) (tlr TiltfileLoadResult, err error) {
absFilename, err := ospath.RealAbs(filename)
if err != nil {
if os.IsNotExist(err) {
return TiltfileLoadResult{ConfigFiles: []string{filename}}, fmt.Errorf("No Tiltfile found at path '%s'. Check out https://docs.tilt.dev/tutorial.html", filename)
}
absFilename, _ = filepath.Abs(filename)
return TiltfileLoadResult{ConfigFiles: []string{absFilename}}, err
}
s := newTiltfileState(ctx, tfl.dcCli, absFilename, tfl.kubeContext)
printedWarnings := false
defer func() {
tlr.ConfigFiles = s.configFiles
tlr.Warnings = s.warnings
if !printedWarnings {
printWarnings(s)
}
}()
s.logger.Infof("Beginning Tiltfile execution")
if err := s.exec(); err != nil {
if err, ok := err.(*starlark.EvalError); ok {
return TiltfileLoadResult{}, errors.New(err.Backtrace())
}
return TiltfileLoadResult{}, err
}
resources, unresourced, err := s.assemble()
if err != nil {
return TiltfileLoadResult{}, err
}
var manifests []model.Manifest
if len(resources.k8s) > 0 {
manifests, err = s.translateK8s(resources.k8s)
if err != nil {
return TiltfileLoadResult{}, err
}
} else {
manifests, err = s.translateDC(resources.dc)
if err != nil {
return TiltfileLoadResult{}, err
}
}
err = s.checkForUnconsumedLiveUpdateSteps()
if err != nil {
return TiltfileLoadResult{}, err
}
manifests, err = match(manifests, matching)
if err != nil {
return TiltfileLoadResult{}, err
}
yamlManifest := model.Manifest{}
if len(unresourced) > 0 {
yamlManifest, err = k8s.NewK8sOnlyManifest(model.UnresourcedYAMLManifestName, unresourced)
if err != nil {
return TiltfileLoadResult{}, err
}
manifests = append(manifests, yamlManifest)
}
printWarnings(s)
printedWarnings = true
s.logger.Infof("Successfully loaded Tiltfile")
tfl.reportTiltfileLoaded(s.builtinCallCounts)
tiltIgnoreContents, err := s.readFile(s.localPathFromString(tiltIgnorePath(filename)))
// missing tiltignore is fine
if os.IsNotExist(err) {
err = nil
} else if err != nil {
return TiltfileLoadResult{}, errors.Wrapf(err, "error reading %s", tiltIgnorePath(filename))
}
return TiltfileLoadResult{manifests, s.configFiles, s.warnings, string(tiltIgnoreContents)}, err
}
// .tiltignore sits next to Tiltfile
func tiltIgnorePath(tiltfilePath string) string {
return filepath.Join(filepath.Dir(tiltfilePath), TiltIgnoreFileName)
}
func skylarkStringDictToGoMap(d *starlark.Dict) (map[string]string, error) {
r := map[string]string{}
for _, tuple := range d.Items() {
kV, ok := tuple[0].(starlark.String)
if !ok {
return nil, fmt.Errorf("key is not a string: %T (%v)", tuple[0], tuple[0])
}
k := string(kV)
vV, ok := tuple[1].(starlark.String)
if !ok {
return nil, fmt.Errorf("value is not a string: %T (%v)", tuple[1], tuple[1])
}
v := string(vV)
r[k] = v
}
return r, nil
}
// If `v` is a `starlark.Sequence`, return a slice of its elements
// Otherwise, return it as a single-element slice
// For functions that take `Union[List[T], T]`
func starlarkValueOrSequenceToSlice(v starlark.Value) []starlark.Value {
if seq, ok := v.(starlark.Sequence); ok {
var ret []starlark.Value
it := seq.Iterate()
defer it.Done()
var i starlark.Value
for it.Next(&i) {
ret = append(ret, i)
}
return ret
} else if v == nil || v == starlark.None {
return nil
} else {
return []starlark.Value{v}
}
}
func (tfl *tiltfileLoader) reportTiltfileLoaded(counts map[string]int) {
tags := make(map[string]string)
for builtinName, count := range counts {
tags[fmt.Sprintf("tiltfile.invoked.%s", builtinName)] = strconv.Itoa(count)
}
tfl.analytics.Incr("tiltfile.loaded", tags)
}