Skip to content

Commit

Permalink
Merge pull request #1069 from crazy-max/compose-build-secrets
Browse files Browse the repository at this point in the history
bake: support compose build secrets
  • Loading branch information
crazy-max authored Apr 14, 2022
2 parents 951201a + c0f8a83 commit a2d5bc7
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 69 deletions.
29 changes: 29 additions & 0 deletions bake/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ func ParseCompose(dt []byte) (*Config, error) {
dockerfilePath := s.Build.Dockerfile
dockerfilePathP = &dockerfilePath
}

var secrets []string
for _, bs := range s.Build.Secrets {
secret, err := composeToBuildkitSecret(bs, cfg.Secrets[bs.Source])
if err != nil {
return nil, err
}
secrets = append(secrets, secret)
}

g.Targets = append(g.Targets, s.Name)
t := &Target{
Name: s.Name,
Expand All @@ -89,6 +99,7 @@ func ParseCompose(dt []byte) (*Config, error) {
})),
CacheFrom: s.Build.CacheFrom,
NetworkMode: &s.Build.Network,
Secrets: secrets,
}
if err = t.composeExtTarget(s.Build.Extensions); err != nil {
return nil, err
Expand Down Expand Up @@ -209,3 +220,21 @@ func (t *Target) composeExtTarget(exts map[string]interface{}) error {
}
return nil
}

// composeToBuildkitSecret converts secret from compose format to buildkit's
// csv format.
func composeToBuildkitSecret(inp compose.ServiceSecretConfig, psecret compose.SecretConfig) (string, error) {
if psecret.External.External {
return "", errors.Errorf("unsupported external secret %s", psecret.Name)
}

var bkattrs []string
if inp.Source != "" {
bkattrs = append(bkattrs, "id="+inp.Source)
}
if psecret.File != "" {
bkattrs = append(bkattrs, "src="+psecret.File)
}

return strings.Join(bkattrs, ","), nil
}
11 changes: 11 additions & 0 deletions bake/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ services:
none
args:
buildno: 123
secrets:
- ENV_TOKEN
- aws
secrets:
ENV_TOKEN: {}
aws:
file: /root/.aws/credentials
`)

c, err := ParseCompose(dt)
Expand All @@ -46,6 +53,10 @@ services:
require.Equal(t, 1, len(c.Targets[1].Args))
require.Equal(t, "123", c.Targets[1].Args["buildno"])
require.Equal(t, "none", *c.Targets[1].NetworkMode)
require.Equal(t, []string{
"id=ENV_TOKEN",
"id=aws,src=/root/.aws/credentials",
}, c.Targets[1].Secrets)
}

func TestNoBuildOutOfTreeService(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/bugsnag/panicwrap v1.2.0 // indirect
github.com/cenkalti/backoff v2.1.1+incompatible // indirect
github.com/cloudflare/cfssl v0.0.0-20181213083726-b94e044bb51e // indirect
github.com/compose-spec/compose-go v1.2.1
github.com/compose-spec/compose-go v1.2.4
github.com/containerd/console v1.0.3
github.com/containerd/containerd v1.6.3-0.20220401172941-5ff8fce1fcc6
github.com/docker/cli v20.10.13+incompatible
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@ github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
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/compose-spec/compose-go v1.2.1 h1:8+DAP7Mt/Ohl5y6YbZdilLMvIhMxvuSZcNZyywjQmJE=
github.com/compose-spec/compose-go v1.2.1/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
github.com/compose-spec/compose-go v1.2.4 h1:nzTFqM8+2J7Veao5Pq5U451thinv3U1wChIvcjX59/A=
github.com/compose-spec/compose-go v1.2.4/go.mod h1:pAy7Mikpeft4pxkFU565/DRHEbDfR84G6AQuiL+Hdg8=
github.com/containerd/aufs v0.0.0-20200908144142-dab0cbea06f4/go.mod h1:nukgQABAEopAHvB6j7cnP5zJ+/3aVcE7hCYqvIwAHyE=
github.com/containerd/aufs v0.0.0-20201003224125-76a6863f2989/go.mod h1:AkGGQs9NM2vtYHaUen+NljV0/baGCAPELGm2q9ZXpWU=
github.com/containerd/aufs v0.0.0-20210316121734-20793ff83c97/go.mod h1:kL5kd6KM5TzQjR79jljyi4olc1Vrx6XBlcyj3gNv2PU=
Expand Down

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

42 changes: 34 additions & 8 deletions vendor/github.com/compose-spec/compose-go/loader/loader.go

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

8 changes: 5 additions & 3 deletions vendor/github.com/compose-spec/compose-go/loader/validate.go

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

66 changes: 25 additions & 41 deletions vendor/github.com/compose-spec/compose-go/schema/compose-spec.json

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

5 changes: 5 additions & 0 deletions vendor/github.com/compose-spec/compose-go/types/project.go

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

46 changes: 33 additions & 13 deletions vendor/github.com/compose-spec/compose-go/types/types.go

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ github.com/cenkalti/backoff/v4
github.com/cespare/xxhash/v2
# github.com/cloudflare/cfssl v0.0.0-20181213083726-b94e044bb51e
## explicit
# github.com/compose-spec/compose-go v1.2.1
# github.com/compose-spec/compose-go v1.2.4
## explicit
github.com/compose-spec/compose-go/consts
github.com/compose-spec/compose-go/dotenv
Expand Down

0 comments on commit a2d5bc7

Please sign in to comment.