-
Notifications
You must be signed in to change notification settings - Fork 288
/
image_build_and_deployer.go
340 lines (284 loc) · 9.69 KB
/
image_build_and_deployer.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
339
340
package engine
import (
context "context"
"fmt"
"time"
"github.com/windmilleng/tilt/internal/dockerfile"
"github.com/windmilleng/tilt/internal/logger"
"github.com/windmilleng/tilt/internal/store"
"github.com/pkg/errors"
"github.com/docker/distribution/reference"
opentracing "github.com/opentracing/opentracing-go"
"github.com/windmilleng/tilt/internal/build"
"github.com/windmilleng/tilt/internal/ignore"
"github.com/windmilleng/tilt/internal/k8s"
"github.com/windmilleng/tilt/internal/model"
"github.com/windmilleng/tilt/internal/synclet/sidecar"
"github.com/windmilleng/wmclient/pkg/analytics"
v1 "k8s.io/api/core/v1"
)
var _ BuildAndDeployer = &ImageBuildAndDeployer{}
type ImageBuildAndDeployer struct {
b build.ImageBuilder
cacheBuilder build.CacheBuilder
k8sClient k8s.Client
env k8s.Env
analytics analytics.Analytics
updateMode UpdateMode
injectSynclet bool
clock build.Clock
}
func NewImageBuildAndDeployer(
b build.ImageBuilder,
cacheBuilder build.CacheBuilder,
k8sClient k8s.Client,
env k8s.Env,
analytics analytics.Analytics,
updateMode UpdateMode,
c build.Clock) *ImageBuildAndDeployer {
return &ImageBuildAndDeployer{
b: b,
cacheBuilder: cacheBuilder,
k8sClient: k8sClient,
env: env,
analytics: analytics,
updateMode: updateMode,
clock: c,
}
}
// Turn on synclet injection. Should be called before any builds.
func (ibd *ImageBuildAndDeployer) SetInjectSynclet(inject bool) {
ibd.injectSynclet = inject
}
func (ibd *ImageBuildAndDeployer) BuildAndDeploy(ctx context.Context, manifest model.Manifest, state store.BuildState) (br store.BuildResult, err error) {
if manifest.IsDC() {
return store.BuildResult{}, RedirectToNextBuilderf("dc manifest")
}
span, ctx := opentracing.StartSpanFromContext(ctx, "daemon-ImageBuildAndDeployer-BuildAndDeploy")
defer span.Finish()
startTime := time.Now()
defer func() {
incremental := "0"
if state.HasImage() {
incremental = "1"
}
tags := map[string]string{"incremental": incremental}
ibd.analytics.Timer("build.image", time.Since(startTime), tags)
}()
numStages := 2
ps := build.NewPipelineState(ctx, numStages, ibd.clock)
defer func() { ps.End(ctx, err) }()
err = manifest.Validate()
if err != nil {
return store.BuildResult{}, err
}
ref, err := ibd.build(ctx, manifest.ImageTarget, state, ps)
if err != nil {
return store.BuildResult{}, err
}
if !manifest.IsK8s() {
// If a non-yaml manifest reaches this code, something is wrong.
// If we change BaD structure such that that might reasonably happen,
// this should be a `RedirectToNextBuilder` error.
return store.BuildResult{}, fmt.Errorf("manifest %s has no k8s deploy info", manifest.Name)
}
err = ibd.deploy(ctx, ps, manifest.K8sTarget(), ref)
if err != nil {
return store.BuildResult{}, err
}
return store.BuildResult{
Image: ref,
}, nil
}
func (ibd *ImageBuildAndDeployer) build(ctx context.Context, image model.ImageTarget, state store.BuildState, ps *build.PipelineState) (reference.NamedTagged, error) {
var n reference.NamedTagged
ref := image.Ref
cacheRef, err := ibd.fetchCache(ctx, ref, image.CachePaths())
if err != nil {
return nil, err
}
switch bd := image.BuildDetails.(type) {
case model.StaticBuild:
ps.StartPipelineStep(ctx, "Building Dockerfile: [%s]", ref)
defer ps.EndPipelineStep(ctx)
df := ibd.staticDockerfile(image, cacheRef)
ref, err := ibd.b.BuildDockerfile(ctx, ps, ref, df, bd.BuildPath, ignore.CreateBuildContextFilter(image), bd.BuildArgs)
if err != nil {
return nil, err
}
n = ref
go ibd.maybeCreateCacheFrom(ctx, ref, state, image, cacheRef)
case model.FastBuild:
if !state.HasImage() || ibd.updateMode == UpdateModeNaive {
// No existing image to build off of, need to build from scratch
ps.StartPipelineStep(ctx, "Building from scratch: [%s]", ref)
defer ps.EndPipelineStep(ctx)
df := ibd.baseDockerfile(bd, cacheRef, image.CachePaths())
steps := bd.Steps
ref, err := ibd.b.BuildImageFromScratch(ctx, ps, ref, df, bd.Mounts, ignore.CreateBuildContextFilter(image), steps, bd.Entrypoint)
if err != nil {
return nil, err
}
n = ref
go ibd.maybeCreateCacheFrom(ctx, ref, state, image, cacheRef)
} else {
// We have an existing image, can do an iterative build
changed, err := state.FilesChangedSinceLastResultImage()
if err != nil {
return nil, err
}
cf, err := build.FilesToPathMappings(changed, bd.Mounts)
if err != nil {
return nil, err
}
ps.StartPipelineStep(ctx, "Building from existing: [%s]", ref)
defer ps.EndPipelineStep(ctx)
steps := bd.Steps
ref, err := ibd.b.BuildImageFromExisting(ctx, ps, state.LastResult.Image, cf, ignore.CreateBuildContextFilter(image), steps)
if err != nil {
return nil, err
}
n = ref
}
default:
// Theoretically this should never trip b/c we `validate` the manifest beforehand...?
// If we get here, something is very wrong.
return nil, fmt.Errorf("image %q has no valid buildDetails (neither StaticBuildInfo nor FastBuildInfo)", image.Ref)
}
if !ibd.canSkipPush() {
var err error
n, err = ibd.b.PushImage(ctx, n, ps.Writer(ctx))
if err != nil {
return nil, err
}
}
return n, nil
}
// Returns: the entities deployed and the namespace of the pod with the given image name/tag.
func (ibd *ImageBuildAndDeployer) deploy(ctx context.Context, ps *build.PipelineState, k8sInfo model.K8sTarget, ref reference.NamedTagged) error {
ps.StartPipelineStep(ctx, "Deploying")
defer ps.EndPipelineStep(ctx)
ps.StartBuildStep(ctx, "Parsing Kubernetes config YAML")
// TODO(nick): The parsed YAML should probably be a part of the model?
// It doesn't make much sense to re-parse it and inject labels on every deploy.
entities, err := k8s.ParseYAMLFromString(k8sInfo.YAML)
if err != nil {
return err
}
replacedAny := false
newK8sEntities := []k8s.K8sEntity{}
for _, e := range entities {
e, err = k8s.InjectLabels(e, []k8s.LabelPair{TiltRunLabel(), {Key: ManifestNameLabel, Value: k8sInfo.Name.String()}})
if err != nil {
return errors.Wrap(err, "deploy")
}
// For development, image pull policy should never be set to "Always",
// even if it might make sense to use "Always" in prod. People who
// set "Always" for development are shooting their own feet.
e, err = k8s.InjectImagePullPolicy(e, v1.PullIfNotPresent)
if err != nil {
return err
}
// When working with a local k8s cluster, we set the pull policy to Never,
// to ensure that k8s fails hard if the image is missing from docker.
policy := v1.PullIfNotPresent
if ibd.canSkipPush() {
policy = v1.PullNever
}
if ref != nil {
var replaced bool
e, replaced, err = k8s.InjectImageDigest(e, ref, policy)
if err != nil {
return err
}
if replaced {
replacedAny = true
if ibd.injectSynclet {
var sidecarInjected bool
e, sidecarInjected, err = sidecar.InjectSyncletSidecar(e, ref)
if err != nil {
return err
}
if !sidecarInjected {
return fmt.Errorf("Could not inject synclet: %v", e)
}
}
}
}
newK8sEntities = append(newK8sEntities, e)
}
if ref != nil && !replacedAny {
return fmt.Errorf("Docker image missing from yaml: %s", ref)
}
err = ibd.k8sClient.Upsert(ctx, newK8sEntities)
if err != nil {
return err
}
return nil
}
// If we're using docker-for-desktop as our k8s backend,
// we don't need to push to the central registry.
// The k8s will use the image already available
// in the local docker daemon.
func (ibd *ImageBuildAndDeployer) canSkipPush() bool {
return ibd.env.IsLocalCluster()
}
func (ibd *ImageBuildAndDeployer) fetchCache(ctx context.Context, ref reference.Named, cachePaths []string) (reference.NamedTagged, error) {
return ibd.cacheBuilder.FetchCache(ctx, ref, cachePaths)
}
func (ibd *ImageBuildAndDeployer) maybeCreateCacheFrom(ctx context.Context, sourceRef reference.NamedTagged, state store.BuildState, image model.ImageTarget, oldCacheRef reference.NamedTagged) {
// Only create the cache the first time we build the image.
if !state.LastResult.IsEmpty() {
return
}
// Only create the cache if there is no existing cache
if oldCacheRef != nil {
return
}
baseDockerfile := dockerfile.Dockerfile(image.FastBuildInfo().BaseDockerfile)
var buildArgs model.DockerBuildArgs
if sbInfo, ok := image.BuildDetails.(model.StaticBuild); ok {
staticDockerfile := dockerfile.Dockerfile(sbInfo.Dockerfile)
ok := true
baseDockerfile, _, ok = staticDockerfile.SplitIntoBaseDockerfile()
if !ok {
return
}
buildArgs = sbInfo.BuildArgs
}
err := ibd.cacheBuilder.CreateCacheFrom(ctx, baseDockerfile, sourceRef,
image.CachePaths(), buildArgs)
if err != nil {
logger.Get(ctx).Debugf("Could not create cache: %v", err)
}
}
func (ibd *ImageBuildAndDeployer) staticDockerfile(image model.ImageTarget, cacheRef reference.NamedTagged) dockerfile.Dockerfile {
df := dockerfile.Dockerfile(image.StaticBuildInfo().Dockerfile)
if cacheRef == nil {
return df
}
if len(image.CachePaths()) == 0 {
return df
}
_, restDf, ok := df.SplitIntoBaseDockerfile()
if !ok {
return df
}
// Replace all the lines before the ADD with a load from the Tilt cache.
return dockerfile.FromExisting(cacheRef).
WithLabel(build.CacheImage, "0"). // sadly there's no way to unset a label :sob:
Append(restDf)
}
func (ibd *ImageBuildAndDeployer) baseDockerfile(fbInfo model.FastBuild,
cacheRef reference.NamedTagged, cachePaths []string) dockerfile.Dockerfile {
df := dockerfile.Dockerfile(fbInfo.BaseDockerfile)
if cacheRef == nil {
return df
}
if len(cachePaths) == 0 {
return df
}
// Use the cache as the new base dockerfile.
return dockerfile.FromExisting(cacheRef).
WithLabel(build.CacheImage, "0") // sadly there's no way to unset a label :sob:
}