Skip to content

Commit

Permalink
dockercompose: upgrade compose-go to v1.0.8 (#5233)
Browse files Browse the repository at this point in the history
The `compose-go` library reached v1! It's also got fixes for
(hopefully) all the big regressions from the Python version,
particularly environment variable resolution.

There are a couple changes to the client, predominantly to share
more logic around loader options between the three code paths
(`compose-go`, CLI fallback, fake/tests).

Importantly, the `compose-go` logic for `Dockerfile` paths changed,
such that they are no longer made to be absolute (unless an absolute
path is used, at which point it won't be touched). They're assumed
to be relative to the `context` in the general case. This actually
only affects the assertions in the tests - Compose has flip-flopped
behavior here multiple times, so Tilt already has logic to join the
`dockerfile` path with the `context` path if the former is relative.
(In theory, this is now the final behavior, as it was restoring v1
semantics to address compatibility with WSL, but I think I said the
same thing last time they changed the path resolution semantics!)
  • Loading branch information
milas committed Nov 30, 2021
1 parent c531e05 commit 30cd9b1
Show file tree
Hide file tree
Showing 30 changed files with 637 additions and 502 deletions.
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/akutz/memconn v0.1.0
github.com/alessio/shellescape v1.2.2
github.com/blang/semver v3.5.1+incompatible
github.com/compose-spec/compose-go v0.0.0-20210910070419-813d4ccb40f8
github.com/compose-spec/compose-go v1.0.8
github.com/containerd/continuity v0.0.0-20201208142359-180525291bb7
github.com/davecgh/go-spew v1.1.1
github.com/docker/cli v20.10.5+incompatible
Expand Down Expand Up @@ -104,6 +104,7 @@ require (
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/chai2010/gettext-go v0.0.0-20170215093142-bf70f2a70fb1 // indirect
github.com/cloudflare/cfssl v1.4.1 // indirect
github.com/compose-spec/godotenv v1.1.1 // indirect
github.com/containerd/containerd v1.4.4 // indirect
github.com/containerd/ttrpc v1.0.1 // indirect
github.com/containerd/typeurl v1.0.1 // indirect
Expand Down Expand Up @@ -155,7 +156,7 @@ require (
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/miekg/pkcs11 v0.0.0-20180817151620-df0db7a16a9e // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/mitchellh/mapstructure v1.4.2 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/sys/mount v0.1.0 // indirect
github.com/moby/sys/mountinfo v0.1.3 // indirect
Expand All @@ -178,7 +179,6 @@ require (
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/theupdateframework/notary v0.6.1 // indirect
github.com/ulyssessouza/godotenv v1.3.1-0.20210806120901-e417b721114e // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
Expand Down
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,10 @@ github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoC
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/codahale/hdrhistogram v0.0.0-20160425231609-f8ad88b59a58/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/compose-spec/compose-go v0.0.0-20210910070419-813d4ccb40f8 h1:lt9yXQhLnqqypjFXtBGKQHyyHNVsXIjC+z5wH6Ld5L0=
github.com/compose-spec/compose-go v0.0.0-20210910070419-813d4ccb40f8/go.mod h1:Hnmn5ZCVA3sSBN2urjCZNNIyNqCPayRGH7PmMSaV2Q0=
github.com/compose-spec/compose-go v1.0.8 h1:fgT7mYYu5Sp37i2lUIAAvwJpkAHk6dP5ITHy/LlutUk=
github.com/compose-spec/compose-go v1.0.8/go.mod h1:REnCbBugoIdHB7S1sfkN/aJ7AJpNApGNjNiVjA9L8x4=
github.com/compose-spec/godotenv v1.1.1 h1:lp+WpAInnw06YN9sV/XLUOV/9z4C+6wjJdWlrdVac7o=
github.com/compose-spec/godotenv v1.1.1/go.mod h1:zF/3BOa18Z24tts5qnO/E9YURQanJTBUf7nlcCTNsyc=
github.com/containerd/cgroups v0.0.0-20190919134610-bf292b21730f/go.mod h1:OApqhQ4XNSNC13gXIwDjhOQxjWa/NxkwZXJ1EvqT0ko=
github.com/containerd/cgroups v0.0.0-20200531161412-0dbf7f05ba59/go.mod h1:pA0z1pT8KYB3TCXK/ocprsh7MAkoW8bZVzPdih9snmM=
github.com/containerd/cgroups v0.0.0-20200710171044-318312a37340 h1:9atoWyI9RtXFwf7UDbme/6M8Ud0rFrx+Q3ZWgSnsxtw=
Expand Down Expand Up @@ -828,8 +830,9 @@ github.com/mitchellh/hashstructure v1.0.0/go.mod h1:QjSHrPWS+BGUVBYkbTZWEnOh3G1D
github.com/mitchellh/iochan v1.0.0/go.mod h1:JwYml1nuB7xOzsp52dPpHFffvOCDupsG0QubkSMEySY=
github.com/mitchellh/mapstructure v0.0.0-20160808181253-ca63d7c062ee/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/mitchellh/mapstructure v1.4.1 h1:CpVNEelQCZBooIPDn+AR3NpivK/TIKU8bDxdASFVQag=
github.com/mitchellh/mapstructure v1.4.1/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/mapstructure v1.4.2 h1:6h7AQ0yhTcIsmFmnAwQls75jp2Gzs4iB8W7pjMO+rqo=
github.com/mitchellh/mapstructure v1.4.2/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
github.com/mitchellh/reflectwalk v1.0.0/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
github.com/mitchellh/reflectwalk v1.0.1/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
Expand Down Expand Up @@ -1146,8 +1149,6 @@ github.com/uber/jaeger-client-go v2.25.0+incompatible/go.mod h1:WVhlPFC8FDjOFMMW
github.com/uber/jaeger-lib v2.2.0+incompatible/go.mod h1:ComeNDZlWwrWnDv8aPp0Ba6+uUTzImX/AauajbLI56U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/ulyssessouza/godotenv v1.3.1-0.20210806120901-e417b721114e h1:byEYm3QADv5mDUesYKstWwRodf2RoxxC/YuGOxtdqJw=
github.com/ulyssessouza/godotenv v1.3.1-0.20210806120901-e417b721114e/go.mod h1:9JN/BuU6Agy5aHyEoA5EIPkBsYbk0+2R42zJgYi/SlI=
github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
Expand Down
30 changes: 24 additions & 6 deletions internal/dockercompose/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ import (
// (See TestParseComposeVersionOutput for various cases.)
var versionRegex = regexp.MustCompile(`(?mi)^docker[ -]compose(?: version)?:? v?([^\s,]+),?(?: build ([a-z0-9-]+))?`)

// dcProjectOptions are used when loading Docker Compose projects via the Go library.
//
// See also: dcLoaderOption which is used for loading projects from the CLI fallback and for tests, which should
// be kept in sync behavior-wise.
var dcProjectOptions = []compose.ProjectOptionsFn{
compose.WithResolvedPaths(true),
compose.WithNormalization(true),
compose.WithOsEnv,
compose.WithDotEnv,
}

type DockerComposeClient interface {
Up(ctx context.Context, spec model.DockerComposeUpSpec, shouldBuild bool, stdout, stderr io.Writer) error
Down(ctx context.Context, spec model.DockerComposeProject, stdout, stderr io.Writer) error
Expand Down Expand Up @@ -255,9 +266,8 @@ func (c *cmdDCClient) Version(ctx context.Context) (string, string, error) {
}

func (c *cmdDCClient) loadProjectNative(configPaths []string) (*types.Project, error) {
// NOTE: take care to keep relevant options in sync with FakeDCClient::Project() and cmdDCClient::loadProjectCLI()
// which work differently so cannot directly share options but need to behave similarly
opts, err := compose.NewProjectOptions(configPaths, compose.WithOsEnv, compose.WithResolvedPaths(true), compose.WithDotEnv)
// NOTE: take care to keep behavior in sync with loadProjectCLI()
opts, err := compose.NewProjectOptions(configPaths, dcProjectOptions...)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -293,9 +303,17 @@ func (c *cmdDCClient) loadProjectCLI(ctx context.Context, proj model.DockerCompo
},
},
// no environment specified because the CLI call will already have resolved all variables
}, func(options *loader.Options) {
options.ResolvePaths = true
})
}, dcLoaderOption)
}

// dcLoaderOption is used when loading Docker Compose projects via the CLI and fallback and for tests.
//
// See also: dcProjectOptions which is used for loading projects from the Go library, which should
// be kept in sync behavior-wise.
func dcLoaderOption(opts *loader.Options) {
opts.ResolvePaths = true
opts.SkipNormalization = false
opts.SkipInterpolation = false
}

func dcExecutablePath() string {
Expand Down
2 changes: 1 addition & 1 deletion internal/dockercompose/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestVariableInterpolation(t *testing.T) {
if assert.NotNil(t, svc.Build) {
assert.Equal(t, f.tmpdir.JoinPath("0"), svc.Build.Context)
// resolved Dockerfile path is relative to the context
assert.Equal(t, f.tmpdir.JoinPath("0", "Y"), svc.Build.Dockerfile)
assert.Equal(t, "Y", svc.Build.Dockerfile)
}
if assert.Len(t, svc.Ports, 1) {
assert.Equal(t, 8081, int(svc.Ports[0].Target))
Expand Down
6 changes: 2 additions & 4 deletions internal/dockercompose/fake_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (c *FakeDCClient) Project(_ context.Context, m model.DockerComposeProject)
// this is a dummy ProjectOptions that lets us use compose's logic to apply options
// for consistency, but we have to then pull the data out ourselves since we're calling
// loader.Load ourselves
opts, err := compose.NewProjectOptions(nil, compose.WithDotEnv, compose.WithOsEnv)
opts, err := compose.NewProjectOptions(nil, dcProjectOptions...)
if err != nil {
return nil, err
}
Expand All @@ -156,9 +156,7 @@ func (c *FakeDCClient) Project(_ context.Context, m model.DockerComposeProject)
},
},
Environment: opts.Environment,
}, func(options *loader.Options) {
options.ResolvePaths = true
})
}, dcLoaderOption)
return p, err
}

Expand Down
12 changes: 6 additions & 6 deletions internal/tiltfile/tiltfile_docker_compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ services:
func (f *fixture) simpleConfigAfterParse() string {
return fmt.Sprintf(`build:
context: %s
dockerfile: %s
dockerfile: Dockerfile
command:
- sleep
- "100"
Expand All @@ -82,7 +82,7 @@ ports:
- mode: ingress
target: 80
published: 12312
protocol: tcp`, f.JoinPath("foo"), f.JoinPath("foo", "Dockerfile"))
protocol: tcp`, f.JoinPath("foo"))
}

func TestDockerComposeManifest(t *testing.T) {
Expand Down Expand Up @@ -153,10 +153,10 @@ services:

expectedYAML := fmt.Sprintf(`build:
context: %s
dockerfile: %s
dockerfile: alternate-Dockerfile
networks:
default: null`,
f.JoinPath("baz"), f.JoinPath("baz", "alternate-Dockerfile"))
f.JoinPath("baz"))

f.load("baz")
f.assertDcManifest("baz",
Expand Down Expand Up @@ -220,10 +220,10 @@ services:

expectedYAML := fmt.Sprintf(`build:
context: %s
dockerfile: %s
dockerfile: alternate-Dockerfile
networks:
default: null`,
f.JoinPath("baz"), f.JoinPath("baz", "alternate-Dockerfile"))
f.JoinPath("baz"))

f.load("baz")
f.assertDcManifest("baz",
Expand Down
34 changes: 26 additions & 8 deletions vendor/github.com/compose-spec/compose-go/cli/options.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 30cd9b1

Please sign in to comment.