Skip to content

Commit d03e42f

Browse files
committed
bake: migrate other buildflags to allow different types
This migrates the other buildflags to use the same method that cache options now uses. Signed-off-by: Jonathan A. Sternberg <[email protected]>
1 parent 59fa21a commit d03e42f

File tree

11 files changed

+445
-206
lines changed

11 files changed

+445
-206
lines changed

bake/bake.go

+35-41
Original file line numberDiff line numberDiff line change
@@ -698,30 +698,30 @@ type Target struct {
698698
// Inherits is the only field that cannot be overridden with --set
699699
Inherits []string `json:"inherits,omitempty" hcl:"inherits,optional" cty:"inherits"`
700700

701-
Annotations []string `json:"annotations,omitempty" hcl:"annotations,optional" cty:"annotations"`
702-
Attest []string `json:"attest,omitempty" hcl:"attest,optional" cty:"attest"`
703-
Context *string `json:"context,omitempty" hcl:"context,optional" cty:"context"`
704-
Contexts map[string]string `json:"contexts,omitempty" hcl:"contexts,optional" cty:"contexts"`
705-
Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional" cty:"dockerfile"`
706-
DockerfileInline *string `json:"dockerfile-inline,omitempty" hcl:"dockerfile-inline,optional" cty:"dockerfile-inline"`
707-
Args map[string]*string `json:"args,omitempty" hcl:"args,optional" cty:"args"`
708-
Labels map[string]*string `json:"labels,omitempty" hcl:"labels,optional" cty:"labels"`
709-
Tags []string `json:"tags,omitempty" hcl:"tags,optional" cty:"tags"`
710-
CacheFrom buildflags.CacheOptions `json:"cache-from,omitempty" hcl:"cache-from,optional" cty:"cache-from"`
711-
CacheTo buildflags.CacheOptions `json:"cache-to,omitempty" hcl:"cache-to,optional" cty:"cache-to"`
712-
Target *string `json:"target,omitempty" hcl:"target,optional" cty:"target"`
713-
Secrets []*buildflags.Secret `json:"secret,omitempty" hcl:"secret,optional" cty:"secret"`
714-
SSH []*buildflags.SSH `json:"ssh,omitempty" hcl:"ssh,optional" cty:"ssh"`
715-
Platforms []string `json:"platforms,omitempty" hcl:"platforms,optional" cty:"platforms"`
716-
Outputs []*buildflags.ExportEntry `json:"output,omitempty" hcl:"output,optional" cty:"output"`
717-
Pull *bool `json:"pull,omitempty" hcl:"pull,optional" cty:"pull"`
718-
NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional" cty:"no-cache"`
719-
NetworkMode *string `json:"network,omitempty" hcl:"network,optional" cty:"network"`
720-
NoCacheFilter []string `json:"no-cache-filter,omitempty" hcl:"no-cache-filter,optional" cty:"no-cache-filter"`
721-
ShmSize *string `json:"shm-size,omitempty" hcl:"shm-size,optional"`
722-
Ulimits []string `json:"ulimits,omitempty" hcl:"ulimits,optional"`
723-
Call *string `json:"call,omitempty" hcl:"call,optional" cty:"call"`
724-
Entitlements []string `json:"entitlements,omitempty" hcl:"entitlements,optional" cty:"entitlements"`
701+
Annotations []string `json:"annotations,omitempty" hcl:"annotations,optional" cty:"annotations"`
702+
Attest []string `json:"attest,omitempty" hcl:"attest,optional" cty:"attest"`
703+
Context *string `json:"context,omitempty" hcl:"context,optional" cty:"context"`
704+
Contexts map[string]string `json:"contexts,omitempty" hcl:"contexts,optional" cty:"contexts"`
705+
Dockerfile *string `json:"dockerfile,omitempty" hcl:"dockerfile,optional" cty:"dockerfile"`
706+
DockerfileInline *string `json:"dockerfile-inline,omitempty" hcl:"dockerfile-inline,optional" cty:"dockerfile-inline"`
707+
Args map[string]*string `json:"args,omitempty" hcl:"args,optional" cty:"args"`
708+
Labels map[string]*string `json:"labels,omitempty" hcl:"labels,optional" cty:"labels"`
709+
Tags []string `json:"tags,omitempty" hcl:"tags,optional" cty:"tags"`
710+
CacheFrom buildflags.CacheOptions `json:"cache-from,omitempty" hcl:"cache-from,optional" cty:"cache-from"`
711+
CacheTo buildflags.CacheOptions `json:"cache-to,omitempty" hcl:"cache-to,optional" cty:"cache-to"`
712+
Target *string `json:"target,omitempty" hcl:"target,optional" cty:"target"`
713+
Secrets buildflags.Secrets `json:"secret,omitempty" hcl:"secret,optional" cty:"secret"`
714+
SSH buildflags.SSHKeys `json:"ssh,omitempty" hcl:"ssh,optional" cty:"ssh"`
715+
Platforms []string `json:"platforms,omitempty" hcl:"platforms,optional" cty:"platforms"`
716+
Outputs buildflags.Exports `json:"output,omitempty" hcl:"output,optional" cty:"output"`
717+
Pull *bool `json:"pull,omitempty" hcl:"pull,optional" cty:"pull"`
718+
NoCache *bool `json:"no-cache,omitempty" hcl:"no-cache,optional" cty:"no-cache"`
719+
NetworkMode *string `json:"network,omitempty" hcl:"network,optional" cty:"network"`
720+
NoCacheFilter []string `json:"no-cache-filter,omitempty" hcl:"no-cache-filter,optional" cty:"no-cache-filter"`
721+
ShmSize *string `json:"shm-size,omitempty" hcl:"shm-size,optional"`
722+
Ulimits []string `json:"ulimits,omitempty" hcl:"ulimits,optional"`
723+
Call *string `json:"call,omitempty" hcl:"call,optional" cty:"call"`
724+
Entitlements []string `json:"entitlements,omitempty" hcl:"entitlements,optional" cty:"entitlements"`
725725
// IMPORTANT: if you add more fields here, do not forget to update newOverrides/AddOverrides and docs/bake-reference.md.
726726

727727
// linked is a private field to mark a target used as a linked one
@@ -739,12 +739,12 @@ func (t *Target) normalize() {
739739
t.Annotations = removeDupesStr(t.Annotations)
740740
t.Attest = removeAttestDupes(t.Attest)
741741
t.Tags = removeDupesStr(t.Tags)
742-
t.Secrets = removeDupes(t.Secrets)
742+
t.Secrets = t.Secrets.Normalize()
743743
t.SSH = removeDupes(t.SSH)
744744
t.Platforms = removeDupesStr(t.Platforms)
745745
t.CacheFrom = t.CacheFrom.Normalize()
746746
t.CacheTo = t.CacheTo.Normalize()
747-
t.Outputs = removeDupes(t.Outputs)
747+
t.Outputs = t.Outputs.Normalize()
748748
t.NoCacheFilter = removeDupesStr(t.NoCacheFilter)
749749
t.Ulimits = removeDupesStr(t.Ulimits)
750750

@@ -815,10 +815,10 @@ func (t *Target) Merge(t2 *Target) {
815815
t.Attest = removeAttestDupes(t.Attest)
816816
}
817817
if t2.Secrets != nil { // merge
818-
t.Secrets = append(t.Secrets, t2.Secrets...)
818+
t.Secrets = t.Secrets.Merge(t2.Secrets)
819819
}
820820
if t2.SSH != nil { // merge
821-
t.SSH = append(t.SSH, t2.SSH...)
821+
t.SSH = t.SSH.Merge(t2.SSH)
822822
}
823823
if t2.Platforms != nil { // no merge
824824
t.Platforms = t2.Platforms
@@ -1333,13 +1333,8 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
13331333
}
13341334
bo.Platforms = platforms
13351335

1336-
secrets := make([]*controllerapi.Secret, len(t.Secrets))
1337-
for i, s := range t.Secrets {
1338-
secrets[i] = s.ToPB()
1339-
}
1340-
bo.SecretSpecs = secrets
1341-
1342-
secretAttachment, err := controllerapi.CreateSecrets(secrets)
1336+
bo.SecretSpecs = t.Secrets.ToPB()
1337+
secretAttachment, err := controllerapi.CreateSecrets(bo.SecretSpecs)
13431338
if err != nil {
13441339
return nil, err
13451340
}
@@ -1379,12 +1374,7 @@ func toBuildOpt(t *Target, inp *Input) (*build.Options, error) {
13791374
bo.CacheTo = controllerapi.CreateCaches(t.CacheTo.ToPB())
13801375
}
13811376

1382-
outputs := make([]*controllerapi.ExportEntry, len(t.Outputs))
1383-
for i, output := range t.Outputs {
1384-
outputs[i] = output.ToPB()
1385-
}
1386-
1387-
bo.Exports, bo.ExportsLocalPathsTemporary, err = controllerapi.CreateExports(outputs)
1377+
bo.Exports, bo.ExportsLocalPathsTemporary, err = controllerapi.CreateExports(t.Outputs.ToPB())
13881378
if err != nil {
13891379
return nil, err
13901380
}
@@ -1611,6 +1601,10 @@ type arrValue[B any] interface {
16111601
func parseArrValue[T any, PT arrValue[T]](s []string) ([]*T, error) {
16121602
outputs := make([]*T, 0, len(s))
16131603
for _, text := range s {
1604+
if text == "" {
1605+
continue
1606+
}
1607+
16141608
output := new(T)
16151609
if err := PT(output).UnmarshalText([]byte(text)); err != nil {
16161610
return nil, err

bake/compose.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -367,14 +367,14 @@ func (t *Target) composeExtTarget(exts map[string]interface{}) error {
367367
if err != nil {
368368
return err
369369
}
370-
t.Secrets = removeDupes(append(t.Secrets, secrets...))
370+
t.Secrets = t.Secrets.Merge(secrets)
371371
}
372372
if len(xb.SSH) > 0 {
373373
ssh, err := parseArrValue[buildflags.SSH](xb.SSH)
374374
if err != nil {
375375
return err
376376
}
377-
t.SSH = removeDupes(append(t.SSH, ssh...))
377+
t.SSH = t.SSH.Merge(ssh)
378378
slices.SortFunc(t.SSH, func(a, b *buildflags.SSH) int {
379379
return a.Less(b)
380380
})
@@ -387,7 +387,7 @@ func (t *Target) composeExtTarget(exts map[string]interface{}) error {
387387
if err != nil {
388388
return err
389389
}
390-
t.Outputs = removeDupes(append(t.Outputs, outputs...))
390+
t.Outputs = t.Outputs.Merge(outputs)
391391
}
392392
if xb.Pull != nil {
393393
t.Pull = xb.Pull

bake/hcl_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
615615
616616
output = [
617617
{ type = "oci", dest = "../out.tar" },
618+
"type=local,dest=../out",
618619
]
619620
620621
secret = [
@@ -633,7 +634,7 @@ func TestHCLAttrsCapsuleType(t *testing.T) {
633634
require.NoError(t, err)
634635

635636
require.Equal(t, 1, len(c.Targets))
636-
require.Equal(t, []string{"type=oci,dest=../out.tar"}, stringify(c.Targets[0].Outputs))
637+
require.Equal(t, []string{"type=local,dest=../out", "type=oci,dest=../out.tar"}, stringify(c.Targets[0].Outputs))
637638
require.Equal(t, []string{"type=local,src=path/to/cache", "user/app:cache"}, stringify(c.Targets[0].CacheFrom))
638639
require.Equal(t, []string{"type=local,dest=path/to/cache"}, stringify(c.Targets[0].CacheTo))
639640
require.Equal(t, []string{"id=mysecret,src=/local/secret", "id=mysecret2,env=TOKEN"}, stringify(c.Targets[0].Secrets))
@@ -656,6 +657,7 @@ func TestHCLAttrsCapsuleTypeVars(t *testing.T) {
656657
657658
output = [
658659
{ type = "oci", dest = "../out.tar" },
660+
"type=local,dest=../out",
659661
]
660662
661663
secret = [
@@ -696,7 +698,7 @@ func TestHCLAttrsCapsuleTypeVars(t *testing.T) {
696698
}
697699

698700
app := findTarget(t, "app")
699-
require.Equal(t, []string{"type=oci,dest=../out.tar"}, stringify(app.Outputs))
701+
require.Equal(t, []string{"type=local,dest=../out", "type=oci,dest=../out.tar"}, stringify(app.Outputs))
700702
require.Equal(t, []string{"type=local,src=path/to/cache", "user/app:cache"}, stringify(app.CacheFrom))
701703
require.Equal(t, []string{"user/app:cache"}, stringify(app.CacheTo))
702704
require.Equal(t, []string{"id=mysecret,src=/local/secret"}, stringify(app.Secrets))

util/buildflags/cty.go

-157
This file was deleted.

0 commit comments

Comments
 (0)