From 9aa8f09f14b66849657ff5c7108abbe004da328a Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Fri, 30 Jul 2021 17:56:40 +0200 Subject: [PATCH] Set `ConfigFile` to parse compose files with bake Signed-off-by: CrazyMax --- bake/compose.go | 30 +++++++++---------- bake/compose_test.go | 30 +++++++++++++++++++ go.mod | 2 +- go.sum | 4 +-- .../compose-spec/compose-go/loader/loader.go | 23 ++++++++++++-- .../compose-go/template/template.go | 9 ++++-- .../compose-spec/compose-go/types/config.go | 4 --- vendor/modules.txt | 2 +- 8 files changed, 77 insertions(+), 27 deletions(-) diff --git a/bake/compose.go b/bake/compose.go index 000451b3a5af..025dd810d07e 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -11,15 +11,10 @@ import ( ) func parseCompose(dt []byte) (*compose.Project, error) { - config, err := loader.ParseYAML(dt) - if err != nil { - return nil, err - } - return loader.Load(compose.ConfigDetails{ ConfigFiles: []compose.ConfigFile{ { - Config: config, + Content: dt, }, }, Environment: envMap(os.Environ()), @@ -80,8 +75,11 @@ func ParseCompose(dt []byte) (*Config, error) { Context: contextPathP, Dockerfile: dockerfilePathP, Labels: s.Build.Labels, - Args: toMap(s.Build.Args), - CacheFrom: s.Build.CacheFrom, + Args: flatten(s.Build.Args.Resolve(func(val string) (string, bool) { + val, ok := cfg.Environment[val] + return val, ok + })), + CacheFrom: s.Build.CacheFrom, // TODO: add platforms } if s.Build.Target != "" { @@ -100,14 +98,16 @@ func ParseCompose(dt []byte) (*Config, error) { return &c, nil } -func toMap(in compose.MappingWithEquals) map[string]string { - m := map[string]string{} +func flatten(in compose.MappingWithEquals) compose.Mapping { + if len(in) == 0 { + return nil + } + out := compose.Mapping{} for k, v := range in { - if v != nil { - m[k] = *v - } else { - m[k] = os.Getenv(k) + if v == nil { + continue } + out[k] = *v } - return m + return out } diff --git a/bake/compose_test.go b/bake/compose_test.go index 16d5e442cd0c..076b9248a4b5 100644 --- a/bake/compose_test.go +++ b/bake/compose_test.go @@ -1,6 +1,7 @@ package bake import ( + "os" "sort" "testing" @@ -107,6 +108,35 @@ services: require.Equal(t, "webapp", *c.Targets[1].Target) } +func TestBuildArgEnvCompose(t *testing.T) { + var dt = []byte(` +version: "3.8" +services: + example: + image: example + build: + context: . + dockerfile: Dockerfile + args: + FOO: + BAR: $ZZZ_BAR + BRB: FOO +`) + + os.Setenv("FOO", "bar") + defer os.Unsetenv("FOO") + os.Setenv("BAR", "foo") + defer os.Unsetenv("BAR") + os.Setenv("ZZZ_BAR", "zzz_foo") + defer os.Unsetenv("ZZZ_BAR") + + c, err := ParseCompose(dt) + require.NoError(t, err) + require.Equal(t, c.Targets[0].Args["FOO"], "bar") + require.Equal(t, c.Targets[0].Args["BAR"], "zzz_foo") + require.Equal(t, c.Targets[0].Args["BRB"], "FOO") +} + func TestBogusCompose(t *testing.T) { var dt = []byte(` services: diff --git a/go.mod b/go.mod index df073055f04e..e61db60dc951 100644 --- a/go.mod +++ b/go.mod @@ -9,7 +9,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 v0.0.0-20210706130854-69459d4976b5 + github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c github.com/containerd/console v1.0.2 github.com/containerd/containerd v1.5.4 github.com/denisenkom/go-mssqldb v0.0.0-20190315220205-a8ed825ac853 // indirect diff --git a/go.sum b/go.sum index 0508a7ea2d32..40751de777e0 100644 --- a/go.sum +++ b/go.sum @@ -138,8 +138,8 @@ github.com/cloudflare/cfssl v0.0.0-20181213083726-b94e044bb51e/go.mod h1:yMWuSON github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc= github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk= github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8= -github.com/compose-spec/compose-go v0.0.0-20210706130854-69459d4976b5 h1:PpI72CT1bcVPNZyqI1HI8UhQVRVtqLb2tdwi5WphN3c= -github.com/compose-spec/compose-go v0.0.0-20210706130854-69459d4976b5/go.mod h1:5V65rPnTvvQagtoMxTneJ2QicLq6ZRQQ7fOgPN226fo= +github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c h1:lSR4wokZlq+Q8uJpgZuFMs3VoLaYVV07cJOZHa1zRBg= +github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c/go.mod h1:5V65rPnTvvQagtoMxTneJ2QicLq6ZRQQ7fOgPN226fo= 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= diff --git a/vendor/github.com/compose-spec/compose-go/loader/loader.go b/vendor/github.com/compose-spec/compose-go/loader/loader.go index a9a70e78caeb..de145b5a9457 100644 --- a/vendor/github.com/compose-spec/compose-go/loader/loader.go +++ b/vendor/github.com/compose-spec/compose-go/loader/loader.go @@ -217,16 +217,35 @@ func Load(configDetails types.ConfigDetails, options ...func(*Options)) (*types. func parseConfig(b []byte, opts *Options) (map[string]interface{}, error) { if !opts.SkipInterpolation { - substitute, err := opts.Interpolate.Substitute(string(b), template.Mapping(opts.Interpolate.LookupValue)) + withoutComments, err := removeYamlComments(b) if err != nil { return nil, err } - b = []byte(substitute) + + substituted, err := opts.Interpolate.Substitute(string(withoutComments), template.Mapping(opts.Interpolate.LookupValue)) + if err != nil { + return nil, err + } + b = []byte(substituted) } return ParseYAML(b) } +// removeYamlComments drop all comments from the yaml file, so we don't try to apply string substitutions on irrelevant places +func removeYamlComments(b []byte) ([]byte, error) { + var cfg interface{} + err := yaml.Unmarshal(b, &cfg) + if err != nil { + return nil, err + } + b, err = yaml.Marshal(cfg) + if err != nil { + return nil, err + } + return b, nil +} + func groupXFieldsIntoExtensions(dict map[string]interface{}) map[string]interface{} { extras := map[string]interface{}{} for key, value := range dict { diff --git a/vendor/github.com/compose-spec/compose-go/template/template.go b/vendor/github.com/compose-spec/compose-go/template/template.go index b838151d1d77..d5ad0ae05fef 100644 --- a/vendor/github.com/compose-spec/compose-go/template/template.go +++ b/vendor/github.com/compose-spec/compose-go/template/template.go @@ -20,6 +20,8 @@ import ( "fmt" "regexp" "strings" + + "github.com/sirupsen/logrus" ) var delimiter = "\\$" @@ -61,7 +63,7 @@ type Mapping func(string) (string, bool) // the substitution and an error. type SubstituteFunc func(string, Mapping) (string, bool, error) -// SubstituteWith subsitute variables in the string with their values. +// SubstituteWith substitute variables in the string with their values. // It accepts additional substitute function. func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, subsFuncs ...SubstituteFunc) (string, error) { var err error @@ -97,7 +99,10 @@ func SubstituteWith(template string, mapping Mapping, pattern *regexp.Regexp, su return value } - value, _ := mapping(substitution) + value, ok := mapping(substitution) + if !ok { + logrus.Warnf("The %q variable is not set. Defaulting to a blank string.", substitution) + } return value }) diff --git a/vendor/github.com/compose-spec/compose-go/types/config.go b/vendor/github.com/compose-spec/compose-go/types/config.go index 16544127c950..21127f491747 100644 --- a/vendor/github.com/compose-spec/compose-go/types/config.go +++ b/vendor/github.com/compose-spec/compose-go/types/config.go @@ -20,7 +20,6 @@ import ( "encoding/json" "github.com/mitchellh/mapstructure" - "github.com/sirupsen/logrus" ) // ConfigDetails are the details about a group of ConfigFiles @@ -34,9 +33,6 @@ type ConfigDetails struct { // LookupEnv provides a lookup function for environment variables func (cd ConfigDetails) LookupEnv(key string) (string, bool) { v, ok := cd.Environment[key] - if !ok { - logrus.Warnf("The %s variable is not set. Defaulting to a blank string.", key) - } return v, ok } diff --git a/vendor/modules.txt b/vendor/modules.txt index 7dcf363ba086..6a5f99d51a79 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -23,7 +23,7 @@ github.com/beorn7/perks/quantile github.com/cenkalti/backoff/v4 # github.com/cespare/xxhash/v2 v2.1.1 github.com/cespare/xxhash/v2 -# github.com/compose-spec/compose-go v0.0.0-20210706130854-69459d4976b5 +# github.com/compose-spec/compose-go v0.0.0-20210729195839-de56f4f0cb3c github.com/compose-spec/compose-go/errdefs github.com/compose-spec/compose-go/interpolation github.com/compose-spec/compose-go/loader