-
Notifications
You must be signed in to change notification settings - Fork 303
/
tiltfile.go
275 lines (228 loc) · 8.18 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
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
package tiltfile
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
wmanalytics "github.com/tilt-dev/wmclient/pkg/analytics"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
"github.com/tilt-dev/tilt/internal/tiltfile/tiltextension"
"github.com/tilt-dev/tilt/internal/analytics"
"github.com/tilt-dev/tilt/internal/dockercompose"
"github.com/tilt-dev/tilt/internal/feature"
"github.com/tilt-dev/tilt/internal/k8s"
"github.com/tilt-dev/tilt/internal/ospath"
"github.com/tilt-dev/tilt/internal/sliceutils"
tiltfileanalytics "github.com/tilt-dev/tilt/internal/tiltfile/analytics"
"github.com/tilt-dev/tilt/internal/tiltfile/config"
"github.com/tilt-dev/tilt/internal/tiltfile/dockerprune"
"github.com/tilt-dev/tilt/internal/tiltfile/io"
"github.com/tilt-dev/tilt/internal/tiltfile/k8scontext"
"github.com/tilt-dev/tilt/internal/tiltfile/metrics"
"github.com/tilt-dev/tilt/internal/tiltfile/secretsettings"
"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
"github.com/tilt-dev/tilt/internal/tiltfile/telemetry"
"github.com/tilt-dev/tilt/internal/tiltfile/updatesettings"
"github.com/tilt-dev/tilt/internal/tiltfile/value"
"github.com/tilt-dev/tilt/internal/tiltfile/version"
"github.com/tilt-dev/tilt/internal/tiltfile/watch"
"github.com/tilt-dev/tilt/pkg/model"
)
const FileName = "Tiltfile"
func init() {
resolve.AllowLambda = true
resolve.AllowNestedDef = true
resolve.AllowGlobalReassign = true
resolve.AllowRecursion = true
}
type TiltfileLoadResult struct {
Manifests []model.Manifest
Tiltignore model.Dockerignore
ConfigFiles []string
FeatureFlags map[string]bool
TeamID string
TelemetrySettings model.TelemetrySettings
MetricsSettings model.MetricsSettings
Secrets model.SecretSet
Error error
DockerPruneSettings model.DockerPruneSettings
AnalyticsOpt wmanalytics.Opt
VersionSettings model.VersionSettings
UpdateSettings model.UpdateSettings
WatchSettings model.WatchSettings
// For diagnostic purposes only
BuiltinCalls []starkit.BuiltinCall `json:"-"`
}
func (r TiltfileLoadResult) Orchestrator() model.Orchestrator {
for _, manifest := range r.Manifests {
if manifest.IsK8s() {
return model.OrchestratorK8s
} else if manifest.IsDC() {
return model.OrchestratorDC
}
}
return model.OrchestratorUnknown
}
type TiltfileLoader interface {
// Load the Tiltfile.
//
// By design, Load() always returns a result.
// We want to be very careful not to treat non-zero exit codes like an error.
// Because even if the Tiltfile has errors, we might need to watch files
// or return partial results (like enabled features).
Load(ctx context.Context, filename string, userConfigState model.UserConfigState) TiltfileLoadResult
}
type FakeTiltfileLoader struct {
Result TiltfileLoadResult
userConfigState model.UserConfigState
}
var _ TiltfileLoader = &FakeTiltfileLoader{}
func NewFakeTiltfileLoader() *FakeTiltfileLoader {
return &FakeTiltfileLoader{}
}
func (tfl *FakeTiltfileLoader) Load(ctx context.Context, filename string, userConfigState model.UserConfigState) TiltfileLoadResult {
tfl.userConfigState = userConfigState
return tfl.Result
}
// the UserConfigState that was passed to the last invocation of Load
func (tfl *FakeTiltfileLoader) PassedUserConfigState() model.UserConfigState {
return tfl.userConfigState
}
func ProvideTiltfileLoader(
analytics *analytics.TiltAnalytics,
kCli k8s.Client,
k8sContextExt k8scontext.Extension,
versionExt version.Extension,
configExt *config.Extension,
dcCli dockercompose.DockerComposeClient,
webHost model.WebHost,
fDefaults feature.Defaults,
env k8s.Env) TiltfileLoader {
return tiltfileLoader{
analytics: analytics,
kCli: kCli,
k8sContextExt: k8sContextExt,
versionExt: versionExt,
configExt: configExt,
dcCli: dcCli,
webHost: webHost,
fDefaults: fDefaults,
env: env,
}
}
type tiltfileLoader struct {
analytics *analytics.TiltAnalytics
kCli k8s.Client
dcCli dockercompose.DockerComposeClient
webHost model.WebHost
k8sContextExt k8scontext.Extension
versionExt version.Extension
configExt *config.Extension
fDefaults feature.Defaults
env k8s.Env
}
var _ TiltfileLoader = &tiltfileLoader{}
// Load loads the Tiltfile in `filename`
func (tfl tiltfileLoader) Load(ctx context.Context, filename string, userConfigState model.UserConfigState) TiltfileLoadResult {
start := time.Now()
absFilename, err := ospath.RealAbs(filename)
if err != nil {
if os.IsNotExist(err) {
return TiltfileLoadResult{
ConfigFiles: []string{filename},
Error: fmt.Errorf("No Tiltfile found at paths '%s'. Check out https://docs.tilt.dev/tutorial.html", filename),
}
}
absFilename, _ = filepath.Abs(filename)
return TiltfileLoadResult{
ConfigFiles: []string{absFilename},
Error: err,
}
}
tiltignorePath := watch.TiltignorePath(absFilename)
tlr := TiltfileLoadResult{
ConfigFiles: []string{absFilename, tiltignorePath},
}
tiltignore, err := watch.ReadTiltignore(tiltignorePath)
// missing tiltignore is fine, but a filesystem error is not
if err != nil {
tlr.Error = err
return tlr
}
tlr.Tiltignore = tiltignore
localRegistry := tfl.kCli.LocalRegistry(ctx)
s := newTiltfileState(ctx, tfl.dcCli, tfl.webHost, tfl.k8sContextExt, tfl.versionExt, tfl.configExt, localRegistry, feature.FromDefaults(tfl.fDefaults))
manifests, result, err := s.loadManifests(absFilename, userConfigState)
tlr.BuiltinCalls = result.BuiltinCalls
ws, _ := watch.GetState(result)
tlr.WatchSettings = ws
// NOTE(maia): if/when add secret settings that affect the engine, add them to tlr here
ss, _ := secretsettings.GetState(result)
s.secretSettings = ss
ioState, _ := io.GetState(result)
tlr.ConfigFiles = append(tlr.ConfigFiles, ioState.Paths...)
tlr.ConfigFiles = append(tlr.ConfigFiles, s.postExecReadFiles...)
tlr.ConfigFiles = sliceutils.DedupedAndSorted(tlr.ConfigFiles)
dps, _ := dockerprune.GetState(result)
tlr.DockerPruneSettings = dps
aSettings, _ := tiltfileanalytics.GetState(result)
tlr.AnalyticsOpt = aSettings.Opt
tlr.Secrets = s.extractSecrets()
tlr.FeatureFlags = s.features.ToEnabled()
tlr.Error = err
tlr.Manifests = manifests
tlr.TeamID = s.teamID
vs, _ := version.GetState(result)
tlr.VersionSettings = vs
telemetrySettings, _ := telemetry.GetState(result)
tlr.TelemetrySettings = telemetrySettings
metricsSettings, _ := metrics.GetState(result)
tlr.MetricsSettings = metricsSettings
us, _ := updatesettings.GetState(result)
tlr.UpdateSettings = us
duration := time.Since(start)
if tlr.Error == nil {
s.logger.Infof("Successfully loaded Tiltfile (%s)", duration)
}
extState, _ := tiltextension.GetState(result)
tfl.reportTiltfileLoaded(s.builtinCallCounts, s.builtinArgCounts, duration, extState.ExtsLoaded)
reportTiltfileExecMetrics(ctx, duration, err != nil)
if len(aSettings.CustomTagsToReport) > 0 {
reportCustomTags(tfl.analytics, aSettings.CustomTagsToReport)
}
return tlr
}
func starlarkValueOrSequenceToSlice(v starlark.Value) []starlark.Value {
return value.ValueOrSequenceToSlice(v)
}
func reportCustomTags(a *analytics.TiltAnalytics, tags map[string]string) {
a.Incr("tiltfile.custom.report", tags)
}
func (tfl *tiltfileLoader) reportTiltfileLoaded(
callCounts map[string]int, argCounts map[string]map[string]int,
loadDur time.Duration, extensionsLoaded map[string]bool) {
tags := make(map[string]string)
// env should really be a global tag, but there's a circular dependency
// between the global tags and env initialization, so we add it manually.
tags["env"] = string(tfl.env)
for builtinName, count := range callCounts {
tags[fmt.Sprintf("tiltfile.invoked.%s", builtinName)] = strconv.Itoa(count)
}
for builtinName, counts := range argCounts {
for argName, count := range counts {
tags[fmt.Sprintf("tiltfile.invoked.%s.arg.%s", builtinName, argName)] = strconv.Itoa(count)
}
}
tfl.analytics.Incr("tiltfile.loaded", tags)
tfl.analytics.Timer("tiltfile.load", loadDur, nil)
for ext := range extensionsLoaded {
tags := map[string]string{
"env": string(tfl.env),
"ext_name": ext,
}
tfl.analytics.Incr("tiltfile.loaded.extension", tags)
}
}