Skip to content

Commit 11c8497

Browse files
committed
buildflags: fix ref only format for command line and bake
Signed-off-by: Jonathan A. Sternberg <[email protected]>
1 parent b2bbb33 commit 11c8497

File tree

7 files changed

+72
-53
lines changed

7 files changed

+72
-53
lines changed

bake/bake.go

+2-37
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"github.com/moby/buildkit/session/auth/authprovider"
3030
"github.com/moby/buildkit/util/entitlements"
3131
"github.com/pkg/errors"
32-
"github.com/tonistiigi/go-csvvalue"
3332
"github.com/zclconf/go-cty/cty"
3433
"github.com/zclconf/go-cty/cty/convert"
3534
)
@@ -900,7 +899,7 @@ func (t *Target) AddOverrides(overrides map[string]Override, ent *EntitlementCon
900899
case "tags":
901900
t.Tags = o.ArrValue
902901
case "cache-from":
903-
cacheFrom, err := parseCacheArrValues(o.ArrValue)
902+
cacheFrom, err := buildflags.ParseCacheEntry(o.ArrValue)
904903
if err != nil {
905904
return err
906905
}
@@ -913,7 +912,7 @@ func (t *Target) AddOverrides(overrides map[string]Override, ent *EntitlementCon
913912
}
914913
}
915914
case "cache-to":
916-
cacheTo, err := parseCacheArrValues(o.ArrValue)
915+
cacheTo, err := buildflags.ParseCacheEntry(o.ArrValue)
917916
if err != nil {
918917
return err
919918
}
@@ -1585,37 +1584,3 @@ func parseArrValue[T any, PT arrValue[T]](s []string) ([]*T, error) {
15851584
}
15861585
return outputs, nil
15871586
}
1588-
1589-
func parseCacheArrValues(s []string) (buildflags.CacheOptions, error) {
1590-
var outs buildflags.CacheOptions
1591-
for _, in := range s {
1592-
if in == "" {
1593-
continue
1594-
}
1595-
1596-
if !strings.Contains(in, "=") {
1597-
// This is ref only format. Each field in the CSV is its own entry.
1598-
fields, err := csvvalue.Fields(in, nil)
1599-
if err != nil {
1600-
return nil, err
1601-
}
1602-
1603-
for _, field := range fields {
1604-
out := buildflags.CacheOptionsEntry{}
1605-
if err := out.UnmarshalText([]byte(field)); err != nil {
1606-
return nil, err
1607-
}
1608-
outs = append(outs, &out)
1609-
}
1610-
continue
1611-
}
1612-
1613-
// Normal entry.
1614-
out := buildflags.CacheOptionsEntry{}
1615-
if err := out.UnmarshalText([]byte(in)); err != nil {
1616-
return nil, err
1617-
}
1618-
outs = append(outs, &out)
1619-
}
1620-
return outs, nil
1621-
}

bake/bake_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"testing"
1111

12+
"github.com/docker/buildx/util/buildflags"
1213
"github.com/moby/buildkit/util/entitlements"
1314
"github.com/stretchr/testify/assert"
1415
"github.com/stretchr/testify/require"
@@ -1759,6 +1760,27 @@ func TestAnnotations(t *testing.T) {
17591760
require.Equal(t, "bar", bo["app"].Exports[0].Attrs["annotation-manifest[linux/amd64].foo"])
17601761
}
17611762

1763+
func TestRefOnlyCacheOptions(t *testing.T) {
1764+
fp := File{
1765+
Name: "docker-bake.hcl",
1766+
Data: []byte(
1767+
`target "app" {
1768+
output = ["type=image,name=foo"]
1769+
cache-from = ["ref1,ref2"]
1770+
}`),
1771+
}
1772+
ctx := context.TODO()
1773+
m, _, err := ReadTargets(ctx, []File{fp}, []string{"app"}, nil, nil, &EntitlementConf{})
1774+
require.NoError(t, err)
1775+
1776+
require.Len(t, m, 1)
1777+
require.Contains(t, m, "app")
1778+
require.Equal(t, buildflags.CacheOptions{
1779+
{Type: "registry", Attrs: map[string]string{"ref": "ref1"}},
1780+
{Type: "registry", Attrs: map[string]string{"ref": "ref2"}},
1781+
}, m["app"].CacheFrom)
1782+
}
1783+
17621784
func TestHCLEntitlements(t *testing.T) {
17631785
fp := File{
17641786
Name: "docker-bake.hcl",

bake/compose.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,12 @@ func ParseCompose(cfgs []composetypes.ConfigFile, envs map[string]string) (*Conf
145145
labels[k] = &v
146146
}
147147

148-
cacheFrom, err := parseCacheArrValues(s.Build.CacheFrom)
148+
cacheFrom, err := buildflags.ParseCacheEntry(s.Build.CacheFrom)
149149
if err != nil {
150150
return nil, err
151151
}
152152

153-
cacheTo, err := parseCacheArrValues(s.Build.CacheTo)
153+
cacheTo, err := buildflags.ParseCacheEntry(s.Build.CacheTo)
154154
if err != nil {
155155
return nil, err
156156
}
@@ -349,14 +349,14 @@ func (t *Target) composeExtTarget(exts map[string]interface{}) error {
349349
t.Tags = dedupSlice(append(t.Tags, xb.Tags...))
350350
}
351351
if len(xb.CacheFrom) > 0 {
352-
cacheFrom, err := parseCacheArrValues(xb.CacheFrom)
352+
cacheFrom, err := buildflags.ParseCacheEntry(xb.CacheFrom)
353353
if err != nil {
354354
return err
355355
}
356356
t.CacheFrom = t.CacheFrom.Merge(cacheFrom)
357357
}
358358
if len(xb.CacheTo) > 0 {
359-
cacheTo, err := parseCacheArrValues(xb.CacheTo)
359+
cacheTo, err := buildflags.ParseCacheEntry(xb.CacheTo)
360360
if err != nil {
361361
return err
362362
}

commands/build.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,17 @@ func (o *buildOptions) toControllerOptions() (*controllerapi.BuildOptions, error
183183
}
184184
}
185185

186-
opts.CacheFrom, err = buildflags.ParseCacheEntry(o.cacheFrom)
186+
cacheFrom, err := buildflags.ParseCacheEntry(o.cacheFrom)
187187
if err != nil {
188188
return nil, err
189189
}
190-
opts.CacheTo, err = buildflags.ParseCacheEntry(o.cacheTo)
190+
opts.CacheFrom = cacheFrom.ToPB()
191+
192+
cacheTo, err := buildflags.ParseCacheEntry(o.cacheTo)
191193
if err != nil {
192194
return nil, err
193195
}
196+
opts.CacheTo = cacheTo.ToPB()
194197

195198
opts.Secrets, err = buildflags.ParseSecretSpecs(o.secrets)
196199
if err != nil {

util/buildflags/cache.go

+19-2
Original file line numberDiff line numberDiff line change
@@ -167,20 +167,37 @@ func (e *CacheOptionsEntry) validate(gv interface{}) error {
167167
return nil
168168
}
169169

170-
func ParseCacheEntry(in []string) ([]*controllerapi.CacheOptionsEntry, error) {
170+
func ParseCacheEntry(in []string) (CacheOptions, error) {
171171
if len(in) == 0 {
172172
return nil, nil
173173
}
174174

175175
opts := make(CacheOptions, 0, len(in))
176176
for _, in := range in {
177+
if !strings.Contains(in, "=") {
178+
// This is ref only format. Each field in the CSV is its own entry.
179+
fields, err := csvvalue.Fields(in, nil)
180+
if err != nil {
181+
return nil, err
182+
}
183+
184+
for _, field := range fields {
185+
opt := CacheOptionsEntry{}
186+
if err := opt.UnmarshalText([]byte(field)); err != nil {
187+
return nil, err
188+
}
189+
opts = append(opts, &opt)
190+
}
191+
continue
192+
}
193+
177194
var out CacheOptionsEntry
178195
if err := out.UnmarshalText([]byte(in)); err != nil {
179196
return nil, err
180197
}
181198
opts = append(opts, &out)
182199
}
183-
return opts.ToPB(), nil
200+
return opts, nil
184201
}
185202

186203
func addGithubToken(ci *controllerapi.CacheOptionsEntry) {

util/buildflags/cache_cty.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ func (o *CacheOptions) fromCtyValue(in cty.Value, p cty.Path) error {
3030
continue
3131
}
3232

33+
// Special handling for a string type to handle ref only format.
34+
if value.Type() == cty.String {
35+
entries, err := ParseCacheEntry([]string{value.AsString()})
36+
if err != nil {
37+
return err
38+
}
39+
*o = append(*o, entries...)
40+
continue
41+
}
42+
3343
entry := &CacheOptionsEntry{}
3444
if err := entry.FromCtyValue(value, p); err != nil {
3545
return err
@@ -52,13 +62,6 @@ func (o CacheOptions) ToCtyValue() cty.Value {
5262
}
5363

5464
func (o *CacheOptionsEntry) FromCtyValue(in cty.Value, p cty.Path) error {
55-
if in.Type() == cty.String {
56-
if err := o.UnmarshalText([]byte(in.AsString())); err != nil {
57-
return p.NewError(err)
58-
}
59-
return nil
60-
}
61-
6265
conv, err := convert.Convert(in, cty.Map(cty.String))
6366
if err != nil {
6467
return err

util/buildflags/cache_test.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func TestCacheOptions_DerivedVars(t *testing.T) {
3737
"session_token": "not_a_mitm_attack",
3838
},
3939
},
40-
}, cacheFrom)
40+
}, cacheFrom.ToPB())
4141
}
4242

4343
func TestCacheOptions(t *testing.T) {
@@ -109,3 +109,12 @@ func TestCacheOptions(t *testing.T) {
109109
require.True(t, result.True())
110110
})
111111
}
112+
113+
func TestCacheOptions_RefOnlyFormat(t *testing.T) {
114+
opts, err := ParseCacheEntry([]string{"ref1", "ref2"})
115+
require.NoError(t, err)
116+
require.Equal(t, CacheOptions{
117+
{Type: "registry", Attrs: map[string]string{"ref": "ref1"}},
118+
{Type: "registry", Attrs: map[string]string{"ref": "ref2"}},
119+
}, opts)
120+
}

0 commit comments

Comments
 (0)