From fe76a1b17945e9aa5cdc7f4b128e5b2255f375d5 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Mon, 5 Dec 2022 22:32:55 +0100 Subject: [PATCH] bake: support null label value Signed-off-by: CrazyMax --- bake/bake.go | 21 ++++++++++++++++----- bake/bake_test.go | 8 ++++++++ bake/compose.go | 8 +++++++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/bake/bake.go b/bake/bake.go index db03e05e..06efe9a3 100644 --- a/bake/bake.go +++ b/bake/bake.go @@ -564,7 +564,7 @@ type Target struct { Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional" cty:"dockerfile"` DockerfileInline *string `json:"dockerfile-inline,omitempty" hcl:"dockerfile-inline,optional" cty:"dockerfile-inline"` Args map[string]*string `json:"args,omitempty" hcl:"args,optional" cty:"args"` - Labels map[string]string `json:"labels,omitempty" hcl:"labels,optional" cty:"labels"` + Labels map[string]*string `json:"labels,omitempty" hcl:"labels,optional" cty:"labels"` Tags []string `json:"tags,omitempty" hcl:"tags,optional" cty:"tags"` CacheFrom []string `json:"cache-from,omitempty" hcl:"cache-from,optional" cty:"cache-from"` CacheTo []string `json:"cache-to,omitempty" hcl:"cache-to,optional" cty:"cache-to"` @@ -630,8 +630,11 @@ func (t *Target) Merge(t2 *Target) { t.Contexts[k] = v } for k, v := range t2.Labels { + if v == nil { + continue + } if t.Labels == nil { - t.Labels = map[string]string{} + t.Labels = map[string]*string{} } t.Labels[k] = v } @@ -707,9 +710,9 @@ func (t *Target) AddOverrides(overrides map[string]Override) error { return errors.Errorf("labels require name") } if t.Labels == nil { - t.Labels = map[string]string{} + t.Labels = map[string]*string{} } - t.Labels[keys[1]] = value + t.Labels[keys[1]] = &value case "tags": t.Tags = o.ArrValue case "cache-from": @@ -893,6 +896,14 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { args[k] = *v } + labels := map[string]string{} + for k, v := range t.Labels { + if v == nil { + continue + } + labels[k] = *v + } + noCache := false if t.NoCache != nil { noCache = *t.NoCache @@ -934,7 +945,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) { Inputs: bi, Tags: t.Tags, BuildArgs: args, - Labels: t.Labels, + Labels: labels, NoCache: noCache, NoCacheFilter: t.NoCacheFilter, Pull: pull, diff --git a/bake/bake_test.go b/bake/bake_test.go index 50950814..4311b252 100644 --- a/bake/bake_test.go +++ b/bake/bake_test.go @@ -1296,11 +1296,18 @@ func TestHCLNullVars(t *testing.T) { `variable "FOO" { default = null } + variable "BAR" { + default = null + } target "default" { args = { foo = FOO bar = "baz" } + labels = { + "com.docker.app.bar" = BAR + "com.docker.app.baz" = "foo" + } }`), } @@ -1315,6 +1322,7 @@ func TestHCLNullVars(t *testing.T) { _, err = TargetsToBuildOpt(m, &Input{}) require.NoError(t, err) require.Equal(t, map[string]*string{"bar": ptrstr("baz")}, m["default"].Args) + require.Equal(t, map[string]*string{"com.docker.app.baz": ptrstr("foo")}, m["default"].Labels) } func TestJSONNullVars(t *testing.T) { diff --git a/bake/compose.go b/bake/compose.go index ea8eab65..990fcee1 100644 --- a/bake/compose.go +++ b/bake/compose.go @@ -75,13 +75,19 @@ func ParseCompose(cfgs []compose.ConfigFile, envs map[string]string) (*Config, e secrets = append(secrets, secret) } + // compose does not support nil values for labels + labels := map[string]*string{} + for k, v := range s.Build.Labels { + labels[k] = &v + } + g.Targets = append(g.Targets, targetName) t := &Target{ Name: targetName, Context: contextPathP, Dockerfile: dockerfilePathP, Tags: s.Build.Tags, - Labels: s.Build.Labels, + Labels: labels, Args: flatten(s.Build.Args.Resolve(func(val string) (string, bool) { if val, ok := s.Environment[val]; ok && val != nil { return *val, true