-
Notifications
You must be signed in to change notification settings - Fork 303
/
tiltfile.go
205 lines (170 loc) · 5.82 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
package tiltfile
import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"time"
"go.starlark.net/resolve"
"go.starlark.net/starlark"
wmanalytics "github.com/windmilleng/wmclient/pkg/analytics"
"github.com/windmilleng/tilt/internal/analytics"
"github.com/windmilleng/tilt/internal/dockercompose"
"github.com/windmilleng/tilt/internal/feature"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/internal/ospath"
"github.com/windmilleng/tilt/internal/sliceutils"
tiltfileanalytics "github.com/windmilleng/tilt/internal/tiltfile/analytics"
"github.com/windmilleng/tilt/internal/tiltfile/dockerprune"
"github.com/windmilleng/tilt/internal/tiltfile/io"
"github.com/windmilleng/tilt/internal/tiltfile/k8scontext"
"github.com/windmilleng/tilt/internal/tiltfile/value"
"github.com/windmilleng/tilt/pkg/model"
)
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
FeatureFlags map[string]bool
TeamName string
Secrets model.SecretSet
Error error
DockerPruneSettings model.DockerPruneSettings
AnalyticsOpt wmanalytics.Opt
}
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, requestedManifests []model.ManifestName) TiltfileLoadResult
}
type FakeTiltfileLoader struct {
Result TiltfileLoadResult
}
var _ TiltfileLoader = &FakeTiltfileLoader{}
func NewFakeTiltfileLoader() *FakeTiltfileLoader {
return &FakeTiltfileLoader{}
}
func (tfl *FakeTiltfileLoader) Load(ctx context.Context, filename string, requestedManifests []model.ManifestName) TiltfileLoadResult {
return tfl.Result
}
func ProvideTiltfileLoader(
analytics *analytics.TiltAnalytics,
kCli k8s.Client,
k8sContextExt k8scontext.Extension,
dcCli dockercompose.DockerComposeClient,
fDefaults feature.Defaults) TiltfileLoader {
return tiltfileLoader{
analytics: analytics,
kCli: kCli,
k8sContextExt: k8sContextExt,
dcCli: dcCli,
fDefaults: fDefaults,
}
}
type tiltfileLoader struct {
analytics *analytics.TiltAnalytics
kCli k8s.Client
dcCli dockercompose.DockerComposeClient
k8sContextExt k8scontext.Extension
fDefaults feature.Defaults
}
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`
func (tfl tiltfileLoader) Load(ctx context.Context, filename string, requestedManifests []model.ManifestName) 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 := tiltIgnorePath(absFilename)
tlr := TiltfileLoadResult{
ConfigFiles: []string{absFilename, tiltIgnorePath},
}
tiltIgnoreContents, err := ioutil.ReadFile(tiltIgnorePath)
// missing tiltignore is fine, but a filesystem error is not
if err != nil && !os.IsNotExist(err) {
tlr.Error = err
return tlr
}
tlr.TiltIgnoreContents = string(tiltIgnoreContents)
privateRegistry := tfl.kCli.PrivateRegistry(ctx)
s := newTiltfileState(ctx, tfl.dcCli, tfl.k8sContextExt, privateRegistry, feature.FromDefaults(tfl.fDefaults))
manifests, result, err := s.loadManifests(absFilename, requestedManifests)
ioState, _ := io.GetState(result)
tlr.ConfigFiles = sliceutils.AppendWithoutDupes(ioState.Files, s.postExecReadFiles...)
dps, _ := dockerprune.GetState(result)
tlr.DockerPruneSettings = dps
aSettings, _ := tiltfileanalytics.GetState(result)
tlr.AnalyticsOpt = aSettings.Opt
tlr.Secrets = s.extractSecrets()
tlr.Warnings = s.warnings
tlr.FeatureFlags = s.features.ToEnabled()
tlr.Error = err
tlr.Manifests = manifests
tlr.TeamName = s.teamName
printWarnings(s)
duration := time.Since(start)
s.logger.Infof("Successfully loaded Tiltfile (%s)", duration)
tfl.reportTiltfileLoaded(s.builtinCallCounts, s.builtinArgCounts, duration)
return tlr
}
// .tiltignore sits next to Tiltfile
func tiltIgnorePath(tiltfilePath string) string {
return filepath.Join(filepath.Dir(tiltfilePath), TiltIgnoreFileName)
}
func starlarkValueOrSequenceToSlice(v starlark.Value) []starlark.Value {
return value.ValueOrSequenceToSlice(v)
}
func (tfl *tiltfileLoader) reportTiltfileLoaded(callCounts map[string]int,
argCounts map[string]map[string]int, loadDur time.Duration) {
tags := make(map[string]string)
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)
}