-
Notifications
You must be signed in to change notification settings - Fork 310
/
docker_compose.go
379 lines (318 loc) · 10.6 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
package tiltfile
import (
"context"
"crypto/sha256"
"fmt"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/compose-spec/compose-go/types"
// DANGER: some compose-go types are not friendly to being marshaled with gopkg.in/yaml.v3
// and will trigger a stack overflow panic
// see https://github.com/tilt-dev/tilt/issues/4797
composeyaml "gopkg.in/yaml.v2"
"github.com/docker/distribution/reference"
"github.com/pkg/errors"
"go.starlark.net/starlark"
"github.com/tilt-dev/tilt/internal/container"
"github.com/tilt-dev/tilt/internal/dockercompose"
"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/model"
)
// dcResourceSet represents a single docker-compose config file and all its associated services
type dcResourceSet struct {
Project v1alpha1.DockerComposeProject
configPaths []string
services []*dcService
tiltfilePath string
}
func (dc dcResourceSet) Empty() bool { return reflect.DeepEqual(dc, dcResourceSet{}) }
func (s *tiltfileState) dockerCompose(thread *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var configPaths starlark.Value
envFile := value.NewLocalPathUnpacker(thread)
err := s.unpackArgs(fn.Name(), args, kwargs, "configPaths", &configPaths, "env_file?", &envFile)
if err != nil {
return nil, err
}
paths := starlarkValueOrSequenceToSlice(configPaths)
if len(paths) == 0 {
return nil, fmt.Errorf("Nothing to compose")
}
dc := s.dc
currentTiltfilePath := starkit.CurrentExecPath(thread)
if dc.tiltfilePath != "" && dc.tiltfilePath != currentTiltfilePath {
return starlark.None, fmt.Errorf("Cannot load docker-compose files from two different Tiltfiles.\n"+
"docker-compose must have a single working directory:\n"+
"(%s, %s)", dc.tiltfilePath, currentTiltfilePath)
}
project := v1alpha1.DockerComposeProject{
ConfigPaths: dc.configPaths,
ProjectPath: dc.Project.ProjectPath,
Name: model.NormalizeName(filepath.Base(filepath.Dir(currentTiltfilePath))),
EnvFile: envFile.Value,
}
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 to dir of first compose file, like DC CLI does
if project.ProjectPath == "" {
project.ProjectPath = filepath.Dir(path)
}
project.ConfigPaths = append(project.ConfigPaths, path)
err = io.RecordReadPath(thread, io.WatchFileOnly, path)
if err != nil {
return nil, err
}
}
}
// Set to tiltfile directory for YAML blob tempfiles
if project.ProjectPath == "" {
project.ProjectPath = filepath.Dir(currentTiltfilePath)
}
// NOTE(nick): We currently merge with the existing dockercompose project,
// so remove the current services from the name-reservation map.
for _, svc := range s.dc.services {
delete(s.dcByName, svc.Name)
}
services, err := parseDCConfig(s.ctx, s.dcCli, project)
if err != nil {
return nil, err
}
for _, svc := range services {
err := s.checkResourceConflict(svc.Name)
if err != nil {
return nil, err
}
s.dcByName[svc.Name] = svc
}
s.dc = dcResourceSet{
Project: project,
configPaths: project.ConfigPaths,
services: services,
tiltfilePath: currentTiltfilePath,
}
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 imageVal starlark.Value
var triggerMode triggerMode
var resourceDepsVal starlark.Sequence
var links links.LinkList
var labels value.LabelSet
var autoInit = value.BoolOrNone{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,
); 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)
}
svc, err := s.getDCService(name)
if err != nil {
return nil, err
}
if triggerMode != TriggerModeUnset {
svc.TriggerMode = triggerMode
}
svc.Links = append(svc.Links, links.Links...)
svc.Labels = labels.Values
if imageRefAsStr != nil {
normalized, err := container.ParseNamed(*imageRefAsStr)
if err != nil {
return nil, err
}
svc.imageRefFromUser = normalized
}
rds, err := value.SequenceToStringSlice(resourceDepsVal)
if err != nil {
return nil, errors.Wrapf(err, "%s: resource_deps", fn.Name())
}
svc.resourceDeps = append(svc.resourceDeps, rds...)
svc.AutoInit = autoInit
return starlark.None, nil
}
func (s *tiltfileState) getDCService(name string) (*dcService, error) {
allNames := make([]string, len(s.dc.services))
for i, svc := range s.dc.services {
if svc.Name == name {
return svc, nil
}
allNames[i] = svc.Name
}
return nil, fmt.Errorf("no Docker Compose service found with name '%s'. "+
"Found these instead:\n\t%s", name, strings.Join(allNames, "; "))
}
// A docker-compose service, according to Tilt.
type dcService struct {
Name 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
imageRefFromUser reference.Named // set via dc_resource
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
TriggerMode triggerMode
Links []model.Link
AutoInit value.BoolOrNone
Labels map[string]string
resourceDeps []string
}
func (svc dcService) ImageRef() reference.Named {
if svc.imageRefFromUser != nil {
return svc.imageRefFromUser
}
return svc.imageRefFromConfig
}
func dockerComposeConfigToService(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 {
if portSpec.Published != 0 {
publishedPorts = append(publishedPorts, int(portSpec.Published))
}
}
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)
}
svc := dcService{
Name: svcConfig.Name,
ServiceConfig: svcConfig,
MountedLocalDirs: mountedLocalDirs,
ServiceYAML: rawConfig,
PublishedPorts: publishedPorts,
imageRefFromConfig: imageRef,
}
return svc, nil
}
func parseDCConfig(ctx context.Context, dcc dockercompose.DockerComposeClient, spec v1alpha1.DockerComposeProject) ([]*dcService, error) {
proj, err := dcc.Project(ctx, spec)
if err != nil {
return nil, err
}
var services []*dcService
err = proj.WithServices(proj.ServiceNames(), func(svcConfig types.ServiceConfig) error {
svc, err := dockerComposeConfigToService(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) {
dcInfo := model.DockerComposeTarget{
Name: model.TargetName(service.Name),
Spec: v1alpha1.DockerComposeServiceSpec{
Service: service.Name,
Project: dcSet.Project,
},
ServiceYAML: string(service.ServiceYAML),
Links: service.Links,
LocalVolumePaths: service.MountedLocalDirs,
}.WithImageMapDeps(model.FilterLiveUpdateOnly(service.ImageMapDeps, iTargets)).
WithPublishedPorts(service.PublishedPorts)
autoInit := true
if service.AutoInit.IsSet {
autoInit = service.AutoInit.Value
}
um, err := starlarkTriggerModeToModel(s.triggerModeForResource(service.TriggerMode), autoInit)
if err != nil {
return model.Manifest{}, err
}
var mds []model.ManifestName
for _, md := range service.resourceDeps {
mds = append(mds, model.ManifestName(md))
}
m := model.Manifest{
Name: model.ManifestName(service.Name),
TriggerMode: um,
ResourceDependencies: mds,
}.WithDeployTarget(dcInfo).
WithLabels(service.Labels).
WithImageTargets(iTargets)
return m, nil
}