bake: support null label value

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
pull/1449/head
CrazyMax 2 years ago
parent df4957307f
commit fe76a1b179
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7

@ -564,7 +564,7 @@ type Target struct {
Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional" cty:"dockerfile"` Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional" cty:"dockerfile"`
DockerfileInline *string `json:"dockerfile-inline,omitempty" hcl:"dockerfile-inline,optional" cty:"dockerfile-inline"` 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"` 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"` Tags []string `json:"tags,omitempty" hcl:"tags,optional" cty:"tags"`
CacheFrom []string `json:"cache-from,omitempty" hcl:"cache-from,optional" cty:"cache-from"` 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"` 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 t.Contexts[k] = v
} }
for k, v := range t2.Labels { for k, v := range t2.Labels {
if v == nil {
continue
}
if t.Labels == nil { if t.Labels == nil {
t.Labels = map[string]string{} t.Labels = map[string]*string{}
} }
t.Labels[k] = v t.Labels[k] = v
} }
@ -707,9 +710,9 @@ func (t *Target) AddOverrides(overrides map[string]Override) error {
return errors.Errorf("labels require name") return errors.Errorf("labels require name")
} }
if t.Labels == nil { 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": case "tags":
t.Tags = o.ArrValue t.Tags = o.ArrValue
case "cache-from": case "cache-from":
@ -893,6 +896,14 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
args[k] = *v args[k] = *v
} }
labels := map[string]string{}
for k, v := range t.Labels {
if v == nil {
continue
}
labels[k] = *v
}
noCache := false noCache := false
if t.NoCache != nil { if t.NoCache != nil {
noCache = *t.NoCache noCache = *t.NoCache
@ -934,7 +945,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
Inputs: bi, Inputs: bi,
Tags: t.Tags, Tags: t.Tags,
BuildArgs: args, BuildArgs: args,
Labels: t.Labels, Labels: labels,
NoCache: noCache, NoCache: noCache,
NoCacheFilter: t.NoCacheFilter, NoCacheFilter: t.NoCacheFilter,
Pull: pull, Pull: pull,

@ -1296,11 +1296,18 @@ func TestHCLNullVars(t *testing.T) {
`variable "FOO" { `variable "FOO" {
default = null default = null
} }
variable "BAR" {
default = null
}
target "default" { target "default" {
args = { args = {
foo = FOO foo = FOO
bar = "baz" 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{}) _, err = TargetsToBuildOpt(m, &Input{})
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, map[string]*string{"bar": ptrstr("baz")}, m["default"].Args) 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) { func TestJSONNullVars(t *testing.T) {

@ -75,13 +75,19 @@ func ParseCompose(cfgs []compose.ConfigFile, envs map[string]string) (*Config, e
secrets = append(secrets, secret) 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) g.Targets = append(g.Targets, targetName)
t := &Target{ t := &Target{
Name: targetName, Name: targetName,
Context: contextPathP, Context: contextPathP,
Dockerfile: dockerfilePathP, Dockerfile: dockerfilePathP,
Tags: s.Build.Tags, Tags: s.Build.Tags,
Labels: s.Build.Labels, Labels: labels,
Args: flatten(s.Build.Args.Resolve(func(val string) (string, bool) { Args: flatten(s.Build.Args.Resolve(func(val string) (string, bool) {
if val, ok := s.Environment[val]; ok && val != nil { if val, ok := s.Environment[val]; ok && val != nil {
return *val, true return *val, true

Loading…
Cancel
Save