-
Notifications
You must be signed in to change notification settings - Fork 303
/
tiltfile.go
297 lines (253 loc) · 9.39 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package tiltfile
import (
"context"
"fmt"
"os"
"path/filepath"
"strconv"
"time"
"go.starlark.net/starlark"
"github.com/tilt-dev/clusterid"
"github.com/tilt-dev/tilt/internal/analytics"
"github.com/tilt-dev/tilt/internal/controllers/apiset"
"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/localexec"
"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/cisettings"
"github.com/tilt-dev/tilt/internal/tiltfile/config"
"github.com/tilt-dev/tilt/internal/tiltfile/dockerprune"
"github.com/tilt-dev/tilt/internal/tiltfile/hasher"
"github.com/tilt-dev/tilt/internal/tiltfile/io"
"github.com/tilt-dev/tilt/internal/tiltfile/k8scontext"
"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/tiltextension"
"github.com/tilt-dev/tilt/internal/tiltfile/updatesettings"
"github.com/tilt-dev/tilt/internal/tiltfile/v1alpha1"
"github.com/tilt-dev/tilt/internal/tiltfile/value"
"github.com/tilt-dev/tilt/internal/tiltfile/version"
"github.com/tilt-dev/tilt/internal/tiltfile/watch"
corev1alpha1 "github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/model"
wmanalytics "github.com/tilt-dev/wmclient/pkg/analytics"
)
const FileName = "Tiltfile"
type TiltfileLoadResult struct {
Manifests []model.Manifest
EnabledManifests []model.ManifestName
Tiltignore model.Dockerignore
ConfigFiles []string
FeatureFlags map[string]bool
TeamID string
TelemetrySettings model.TelemetrySettings
Secrets model.SecretSet
Error error
DockerPruneSettings model.DockerPruneSettings
AnalyticsOpt wmanalytics.Opt
VersionSettings model.VersionSettings
UpdateSettings model.UpdateSettings
WatchSettings model.WatchSettings
DefaultRegistry *corev1alpha1.RegistryHosting
ObjectSet apiset.ObjectSet
Hashes hasher.Hashes
CISettings *corev1alpha1.SessionCISpec
// For diagnostic purposes only
BuiltinCalls []starkit.BuiltinCall `json:"-"`
}
func (r TiltfileLoadResult) HasOrchestrator(orc model.Orchestrator) bool {
for _, manifest := range r.Manifests {
if manifest.IsK8s() && orc == model.OrchestratorK8s {
return true
} else if manifest.IsDC() && orc == model.OrchestratorDC {
return true
}
}
return false
}
func (r TiltfileLoadResult) WithAllManifestsEnabled() TiltfileLoadResult {
r.EnabledManifests = nil
for _, m := range r.Manifests {
r.EnabledManifests = append(r.EnabledManifests, m.Name)
}
return r
}
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, tf *corev1alpha1.Tiltfile, prevResult *TiltfileLoadResult) TiltfileLoadResult
}
func ProvideTiltfileLoader(
analytics *analytics.TiltAnalytics,
k8sContextPlugin k8scontext.Plugin,
versionPlugin version.Plugin,
configPlugin *config.Plugin,
extensionPlugin *tiltextension.Plugin,
ciSettingsPlugin cisettings.Plugin,
dcCli dockercompose.DockerComposeClient,
webHost model.WebHost,
execer localexec.Execer,
fDefaults feature.Defaults,
env clusterid.Product) TiltfileLoader {
return tiltfileLoader{
analytics: analytics,
k8sContextPlugin: k8sContextPlugin,
versionPlugin: versionPlugin,
configPlugin: configPlugin,
extensionPlugin: extensionPlugin,
ciSettingsPlugin: ciSettingsPlugin,
dcCli: dcCli,
webHost: webHost,
execer: execer,
fDefaults: fDefaults,
env: env,
}
}
type tiltfileLoader struct {
analytics *analytics.TiltAnalytics
dcCli dockercompose.DockerComposeClient
webHost model.WebHost
execer localexec.Execer
k8sContextPlugin k8scontext.Plugin
versionPlugin version.Plugin
configPlugin *config.Plugin
extensionPlugin *tiltextension.Plugin
ciSettingsPlugin cisettings.Plugin
fDefaults feature.Defaults
env clusterid.Product
}
var _ TiltfileLoader = &tiltfileLoader{}
// Load loads the Tiltfile in `filename`
func (tfl tiltfileLoader) Load(ctx context.Context, tf *corev1alpha1.Tiltfile, prevResult *TiltfileLoadResult) TiltfileLoadResult {
start := time.Now()
filename := tf.Spec.Path
absFilename, err := ospath.RealAbs(tf.Spec.Path)
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
s := newTiltfileState(ctx, tfl.dcCli, tfl.webHost, tfl.execer, tfl.k8sContextPlugin, tfl.versionPlugin,
tfl.configPlugin, tfl.extensionPlugin, tfl.ciSettingsPlugin, feature.FromDefaults(tfl.fDefaults))
manifests, result, err := s.loadManifests(tf)
tlr.BuiltinCalls = result.BuiltinCalls
tlr.DefaultRegistry = s.defaultReg
// All data models are loaded with GetState. We ignore the error if the state
// isn't properly loaded. This is necessary for handling partial Tiltfile
// execution correctly, where some state is correctly assembled but other
// state is not (and should be assumed empty).
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
objectSet, _ := v1alpha1.GetState(result)
tlr.ObjectSet = objectSet
vs, _ := version.GetState(result)
tlr.VersionSettings = vs
telemetrySettings, _ := telemetry.GetState(result)
tlr.TelemetrySettings = telemetrySettings
us, _ := updatesettings.GetState(result)
tlr.UpdateSettings = us
ci, _ := cisettings.GetState(result)
tlr.CISettings = ci
configSettings, _ := config.GetState(result)
if tlr.Error == nil {
tlr.EnabledManifests, tlr.Error = configSettings.EnabledResources(tf, manifests)
}
duration := time.Since(start)
if tlr.Error == nil {
s.logger.Infof("Successfully loaded Tiltfile (%s)", duration)
}
extState, _ := tiltextension.GetState(result)
hashState, _ := hasher.GetState(result)
var prevHashes hasher.Hashes
if prevResult != nil {
prevHashes = prevResult.Hashes
}
tlr.Hashes = hashState.GetHashes()
tfl.reportTiltfileLoaded(s.builtinCallCounts, s.builtinArgCounts, duration,
extState.ExtsLoaded, prevHashes, tlr.Hashes)
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,
pluginsLoaded map[string]bool,
prevHashes hasher.Hashes,
currHashes hasher.Hashes,
) {
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"] = k8s.AnalyticsEnv(tfl.env)
tags["tiltfile.changed"] = strconv.FormatBool(prevHashes.TiltfileSHA256 != "" && prevHashes.TiltfileSHA256 != currHashes.TiltfileSHA256)
tags["allfiles.changed"] = strconv.FormatBool(prevHashes.AllFilesSHA256 != "" && prevHashes.AllFilesSHA256 != currHashes.AllFilesSHA256)
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 pluginsLoaded {
tags := map[string]string{
"env": k8s.AnalyticsEnv(tfl.env),
"ext_name": ext,
}
tfl.analytics.Incr("tiltfile.loaded.plugin", tags)
}
}