-
Notifications
You must be signed in to change notification settings - Fork 303
/
docker_compose.go
535 lines (455 loc) · 14.5 KB
/
docker_compose.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
package tiltfile
import (
"context"
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"github.com/compose-spec/compose-go/consts"
"github.com/compose-spec/compose-go/loader"
"github.com/compose-spec/compose-go/types"
"github.com/distribution/reference"
"github.com/pkg/errors"
"go.starlark.net/starlark"
composeyaml "gopkg.in/yaml.v3"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/controllers/apis/liveupdate"
"github.com/tilt-dev/tilt/internal/dockercompose"
"github.com/tilt-dev/tilt/internal/sliceutils"
"github.com/tilt-dev/tilt/internal/tiltfile/io"
"github.com/tilt-dev/tilt/internal/tiltfile/links"
"github.com/tilt-dev/tilt/internal/tiltfile/starkit"
"github.com/tilt-dev/tilt/internal/tiltfile/value"
"github.com/tilt-dev/tilt/pkg/apis/core/v1alpha1"
"github.com/tilt-dev/tilt/pkg/logger"
"github.com/tilt-dev/tilt/pkg/model"
)
// dcResourceSet represents a single docker-compose config file and all its associated services
type dcResourceSet struct {
Project v1alpha1.DockerComposeProject
configPaths []string
tiltfilePath string
services map[string]*dcService
serviceNames []string
resOptions map[string]*dcResourceOptions
}
type dcResourceMap map[string]*dcResourceSet
func (dc dcResourceSet) Empty() bool { return reflect.DeepEqual(dc, dcResourceSet{}) }
func (dc dcResourceSet) ServiceCount() int { return len(dc.services) }
func (dcm dcResourceMap) ServiceCount() int {
svcCount := 0
for _, dc := range dcm {
svcCount += dc.ServiceCount()
}
return svcCount
}
func (s *tiltfileState) dockerCompose(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var configPaths starlark.Value
var projectName string
var profiles value.StringOrStringList
envFile := value.NewLocalPathUnpacker(thread)
err := s.unpackArgs(fn.Name(), args, kwargs,
"configPaths", &configPaths,
"env_file?", &envFile,
"project_name?", &projectName,
"profiles?", &profiles,
)
if err != nil {
return nil, err
}
paths := starlarkValueOrSequenceToSlice(configPaths)
if len(paths) == 0 {
return nil, fmt.Errorf("Nothing to compose")
}
project := v1alpha1.DockerComposeProject{
Name: projectName,
EnvFile: envFile.Value,
Profiles: profiles.Values,
}
if project.EnvFile != "" {
err = io.RecordReadPath(thread, io.WatchFileOnly, project.EnvFile)
if err != nil {
return nil, err
}
}
for _, val := range paths {
switch v := val.(type) {
case nil:
continue
case io.Blob:
yaml := v.String()
message := "unable to store yaml blob"
tmpdir, err := s.tempDir()
if err != nil {
return nil, errors.Wrap(err, message)
}
tmpfile, err := os.Create(filepath.Join(tmpdir.Path(), fmt.Sprintf("%x.yml", sha256.Sum256([]byte(yaml)))))
if err != nil {
return nil, errors.Wrap(err, message)
}
_, err = tmpfile.WriteString(yaml)
if err != nil {
tmpfile.Close()
return nil, errors.Wrap(err, message)
}
err = tmpfile.Close()
if err != nil {
return nil, errors.Wrap(err, message)
}
project.ConfigPaths = append(project.ConfigPaths, tmpfile.Name())
default:
path, err := value.ValueToAbsPath(thread, val)
if err != nil {
return starlark.None, fmt.Errorf("expected blob | path (string). Actual type: %T", val)
}
// Set project path/name to dir of first compose file, like DC CLI does
if project.ProjectPath == "" {
project.ProjectPath = filepath.Dir(path)
}
if project.Name == "" {
project.Name = loader.NormalizeProjectName(filepath.Base(filepath.Dir(path)))
}
project.ConfigPaths = append(project.ConfigPaths, path)
err = io.RecordReadPath(thread, io.WatchFileOnly, path)
if err != nil {
return nil, err
}
}
}
currentTiltfilePath := starkit.CurrentExecPath(thread)
if project.Name == "" {
project.Name = loader.NormalizeProjectName(filepath.Base(filepath.Dir(currentTiltfilePath)))
}
// Set to tiltfile directory for YAML blob tempfiles
if project.ProjectPath == "" {
project.ProjectPath = filepath.Dir(currentTiltfilePath)
}
if !profiles.IsSet && os.Getenv(consts.ComposeProfiles) != "" {
logger.Get(s.ctx).Infof("Compose project %q loading profiles from environment: %s",
project.Name, os.Getenv(consts.ComposeProfiles))
project.Profiles = strings.Split(os.Getenv(consts.ComposeProfiles), ",")
}
dc := s.dc[project.Name]
if dc == nil {
dc = &dcResourceSet{
Project: project,
services: make(map[string]*dcService),
resOptions: make(map[string]*dcResourceOptions),
configPaths: project.ConfigPaths,
tiltfilePath: currentTiltfilePath,
}
s.dc[project.Name] = dc
} else {
dc.configPaths = sliceutils.AppendWithoutDupes(dc.configPaths, project.ConfigPaths...)
dc.Project.ConfigPaths = dc.configPaths
if project.EnvFile != "" {
dc.Project.EnvFile = project.EnvFile
}
project = dc.Project
}
services, err := parseDCConfig(s.ctx, s.dcCli, dc)
if err != nil {
return nil, err
}
dc.services = make(map[string]*dcService)
dc.serviceNames = []string{}
for _, svc := range services {
err := s.checkResourceConflict(svc.Name)
if err != nil {
return nil, err
}
dc.serviceNames = append(dc.serviceNames, svc.Name)
for _, f := range svc.ServiceConfig.EnvFile {
if !filepath.IsAbs(f) {
f = filepath.Join(project.ProjectPath, f)
}
err = io.RecordReadPath(thread, io.WatchFileOnly, f)
if err != nil {
return nil, err
}
}
dc.services[svc.Name] = svc
}
return starlark.None, nil
}
// DCResource allows you to adjust specific settings on a DC resource that we assume
// to be defined in a `docker_compose.yml`
func (s *tiltfileState) dcResource(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var name string
var projectName string
var newName string
var imageVal starlark.Value
var triggerMode triggerMode
var resourceDepsVal starlark.Sequence
var links links.LinkList
var labels value.LabelSet
var autoInit = value.Optional[starlark.Bool]{Value: true}
if err := s.unpackArgs(fn.Name(), args, kwargs,
"name", &name,
// TODO(milas): this argument is undocumented and arguably unnecessary
// now that Tilt correctly infers the Docker Compose image ref format
"image?", &imageVal,
"trigger_mode?", &triggerMode,
"resource_deps?", &resourceDepsVal,
"links?", &links,
"labels?", &labels,
"auto_init?", &autoInit,
"project_name?", &projectName,
"new_name?", &newName,
); err != nil {
return nil, err
}
if name == "" {
return nil, fmt.Errorf("dc_resource: `name` must not be empty")
}
var imageRefAsStr *string
switch imageVal := imageVal.(type) {
case nil: // optional arg, this is fine
case starlark.String:
s := string(imageVal)
imageRefAsStr = &s
default:
return nil, fmt.Errorf("image arg must be a string; got %T", imageVal)
}
projectName, svc, err := s.getDCService(name, projectName)
if err != nil {
return nil, err
}
if newName != "" {
name, err = s.renameDCService(projectName, name, newName, svc)
if err != nil {
return nil, err
}
}
options := s.dc[projectName].resOptions[name]
if options == nil {
options = newDcResourceOptions()
}
if triggerMode != TriggerModeUnset {
options.TriggerMode = triggerMode
}
options.Links = append(options.Links, links.Links...)
for key, val := range labels.Values {
options.Labels[key] = val
}
if imageRefAsStr != nil {
normalized, err := container.ParseNamed(*imageRefAsStr)
if err != nil {
return nil, err
}
options.imageRefFromUser = normalized
}
rds, err := value.SequenceToStringSlice(resourceDepsVal)
if err != nil {
return nil, errors.Wrapf(err, "%s: resource_deps", fn.Name())
}
for _, dep := range rds {
options.resourceDeps = sliceutils.AppendWithoutDupes(options.resourceDeps, dep)
}
if autoInit.IsSet {
options.AutoInit = autoInit
}
s.dc[projectName].resOptions[name] = options
svc.Options = options
return starlark.None, nil
}
func (s *tiltfileState) getDCService(svcName, projName string) (string, *dcService, error) {
allNames := []string{}
foundProjName := ""
var foundSvc *dcService
for _, dc := range s.dc {
if projName != "" && dc.Project.Name != projName {
continue
}
for key, svc := range dc.services {
if key == svcName {
if foundSvc != nil {
return "", nil, fmt.Errorf("found multiple resources named %q, "+
"please specify which one with project_name= argument", svcName)
}
foundProjName = dc.Project.Name
foundSvc = svc
}
allNames = append(allNames, key)
}
}
if foundSvc == nil {
return "", nil, fmt.Errorf("no Docker Compose service found with name %q. "+
"Found these instead:\n\t%s", svcName, strings.Join(allNames, "; "))
}
return foundProjName, foundSvc, nil
}
func (s *tiltfileState) renameDCService(projectName, name, newName string, svc *dcService) (string, error) {
err := s.checkResourceConflict(newName)
if err != nil {
return "", err
}
s.dc[projectName].services[newName] = svc
delete(s.dc[projectName].services, name)
if opts, exists := s.dc[projectName].resOptions[name]; exists {
s.dc[projectName].resOptions[newName] = opts
delete(s.dc[projectName].resOptions, name)
}
index := -1
for i, n := range s.dc[projectName].serviceNames {
if n == name {
index = i
break
}
}
s.dc[projectName].serviceNames[index] = newName
svc.Name = newName
return newName, nil
}
// A docker-compose service, according to Tilt.
type dcService struct {
Name string
// Contains the name of the service as referenced in the compose file where it was loaded.
ServiceName string
// these are the host machine paths that DC will sync from the local volume into the container
// https://docs.docker.com/compose/compose-file/#volumes
MountedLocalDirs []string
// RefSelector of the image associated with this service
// The user-provided image ref overrides the config-provided image ref
imageRefFromConfig reference.Named // from docker-compose.yml `Image` field
ServiceConfig types.ServiceConfig
// Currently just use this to diff against when config files are edited to see if manifest has changed
ServiceYAML []byte
ImageMapDeps []string
PublishedPorts []int
Options *dcResourceOptions
}
// Options set via dc_resource
type dcResourceOptions struct {
imageRefFromUser reference.Named
TriggerMode triggerMode
Links []model.Link
AutoInit value.Optional[starlark.Bool]
Labels map[string]string
resourceDeps []string
}
func newDcResourceOptions() *dcResourceOptions {
return &dcResourceOptions{
Labels: make(map[string]string),
}
}
func (svc dcService) ImageRef() reference.Named {
if svc.Options != nil && svc.Options.imageRefFromUser != nil {
return svc.Options.imageRefFromUser
}
return svc.imageRefFromConfig
}
func dockerComposeConfigToService(dcrs *dcResourceSet, projectName string, svcConfig types.ServiceConfig) (dcService, error) {
var mountedLocalDirs []string
for _, v := range svcConfig.Volumes {
mountedLocalDirs = append(mountedLocalDirs, v.Source)
}
var publishedPorts []int
for _, portSpec := range svcConfig.Ports {
// a published port can be a string range of ports (e.g. "80-90")
// this case is unusual and unsupported/ignored by Tilt for now
publishedPort, err := strconv.Atoi(portSpec.Published)
if err == nil && publishedPort != 0 {
publishedPorts = append(publishedPorts, publishedPort)
}
}
rawConfig, err := composeyaml.Marshal(svcConfig)
if err != nil {
return dcService{}, err
}
imageName := svcConfig.Image
if imageName == "" {
// see https://github.com/docker/compose/blob/7b84f2c2a538a1241dcf65f4b2828232189ef0ad/pkg/compose/create.go#L221-L227
imageName = fmt.Sprintf("%s_%s", projectName, svcConfig.Name)
}
imageRef, err := container.ParseNamed(imageName)
if err != nil {
// TODO(nick): This doesn't seem like the right place to report this
// error, but we don't really have a better way right now.
return dcService{}, fmt.Errorf("Error parsing image name %q: %v", imageName, err)
}
options, exists := dcrs.resOptions[svcConfig.Name]
if !exists {
options = newDcResourceOptions()
dcrs.resOptions[svcConfig.Name] = options
}
for _, dep := range svcConfig.GetDependencies() {
options.resourceDeps = sliceutils.AppendWithoutDupes(options.resourceDeps, dep)
}
svc := dcService{
Name: svcConfig.Name,
ServiceName: svcConfig.Name,
ServiceConfig: svcConfig,
MountedLocalDirs: mountedLocalDirs,
ServiceYAML: rawConfig,
PublishedPorts: publishedPorts,
Options: options,
imageRefFromConfig: imageRef,
}
return svc, nil
}
func parseDCConfig(ctx context.Context, dcc dockercompose.DockerComposeClient, dcrs *dcResourceSet) ([]*dcService, error) {
proj, err := dcc.Project(ctx, dcrs.Project)
if err != nil {
return nil, err
}
var services []*dcService
err = proj.WithServices(proj.ServiceNames(), func(svcConfig types.ServiceConfig) error {
svc, err := dockerComposeConfigToService(dcrs, proj.Name, svcConfig)
if err != nil {
return errors.Wrapf(err, "getting service %s", svcConfig.Name)
}
services = append(services, &svc)
return nil
})
if err != nil {
return nil, err
}
return services, nil
}
func (s *tiltfileState) dcServiceToManifest(service *dcService, dcSet *dcResourceSet, iTargets []model.ImageTarget) (model.Manifest, error) {
options := service.Options
if options == nil {
options = newDcResourceOptions()
}
dcInfo := model.DockerComposeTarget{
Name: model.TargetName(service.Name),
Spec: v1alpha1.DockerComposeServiceSpec{
Service: service.ServiceName,
Project: dcSet.Project,
},
ServiceYAML: string(service.ServiceYAML),
Links: options.Links,
}.WithImageMapDeps(model.FilterLiveUpdateOnly(service.ImageMapDeps, iTargets)).
WithPublishedPorts(service.PublishedPorts)
autoInit := true
if options.AutoInit.IsSet {
autoInit = bool(options.AutoInit.Value)
}
um, err := starlarkTriggerModeToModel(s.triggerModeForResource(options.TriggerMode), autoInit)
if err != nil {
return model.Manifest{}, err
}
var mds []model.ManifestName
for _, md := range options.resourceDeps {
mds = append(mds, model.ManifestName(md))
}
for i, iTarget := range iTargets {
if liveupdate.IsEmptySpec(iTarget.LiveUpdateSpec) {
continue
}
iTarget.LiveUpdateReconciler = true
iTargets[i] = iTarget
}
m := model.Manifest{
Name: model.ManifestName(service.Name),
TriggerMode: um,
ResourceDependencies: mds,
}.WithDeployTarget(dcInfo).
WithLabels(options.Labels).
WithImageTargets(iTargets)
return m, nil
}