Skip to content

Commit 245093b

Browse files
authored
Merge pull request #2945 from tonistiigi/v0.20.1-cherry-picks
[v0.20.1] cherry picks
2 parents bd7090b + e2ed15f commit 245093b

File tree

10 files changed

+176
-55
lines changed

10 files changed

+176
-55
lines changed

.github/workflows/docs-upstream.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ jobs:
6565
retention-days: 1
6666

6767
validate:
68-
uses: docker/docs/.github/workflows/validate-upstream.yml@6b73b05acb21edf7995cc5b3c6672d8e314cee7a # pin for artifact v4 support: https://github.com/docker/docs/pull/19220
68+
uses: docker/docs/.github/workflows/validate-upstream.yml@main
6969
needs:
7070
- docs-yaml
7171
with:

bake/bake.go

Lines changed: 2 additions & 37 deletions
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

Lines changed: 22 additions & 0 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 5 additions & 2 deletions
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 {

docs/bake-reference.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ The following table shows the complete list of attributes that you can assign to
221221
| [`attest`](#targetattest) | List | Build attestations |
222222
| [`cache-from`](#targetcache-from) | List | External cache sources |
223223
| [`cache-to`](#targetcache-to) | List | External cache destinations |
224+
| [`call`](#targetcall) | String | Specify the frontend method to call for the target. |
224225
| [`context`](#targetcontext) | String | Set of files located in the specified path or URL |
225226
| [`contexts`](#targetcontexts) | Map | Additional build contexts |
227+
| [`description`](#targetdescription) | String | Description of a target |
226228
| [`dockerfile-inline`](#targetdockerfile-inline) | String | Inline Dockerfile string |
227229
| [`dockerfile`](#targetdockerfile) | String | Dockerfile location |
228230
| [`inherits`](#targetinherits) | List | Inherit attributes from other targets |
@@ -371,6 +373,13 @@ target "app" {
371373
}
372374
```
373375

376+
Supported values are:
377+
378+
- `build` builds the target (default)
379+
- `check`: evaluates [build checks](https://docs.docker.com/build/checks/) for the target
380+
- `outline`: displays the target's build arguments and their default values if available
381+
- `targets`: lists all Bake targets in the loaded definition, along with its [description](#targetdescription).
382+
374383
For more information about frontend methods, refer to the CLI reference for
375384
[`docker buildx build --call`](https://docs.docker.com/reference/cli/docker/buildx/build/#call).
376385

@@ -481,6 +490,25 @@ FROM baseapp
481490
RUN echo "Hello world"
482491
```
483492

493+
### `target.description`
494+
495+
Defines a human-readable description for the target, clarifying its purpose or
496+
functionality.
497+
498+
```hcl
499+
target "lint" {
500+
description = "Runs golangci-lint to detect style errors"
501+
args = {
502+
GOLANGCI_LINT_VERSION = null
503+
}
504+
dockerfile = "lint.Dockerfile"
505+
}
506+
```
507+
508+
This attribute is useful when combined with the `docker buildx bake --list=targets`
509+
option, providing a more informative output when listing the available build
510+
targets in a Bake file.
511+
484512
### `target.dockerfile-inline`
485513

486514
Uses the string value as an inline Dockerfile for the build target.

docs/reference/buildx_bake.md

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Build from a file
1515

1616
| Name | Type | Default | Description |
1717
|:------------------------------------|:--------------|:--------|:-------------------------------------------------------------------------------------------------------------|
18-
| `--allow` | `stringArray` | | Allow build to access specified resources |
18+
| [`--allow`](#allow) | `stringArray` | | Allow build to access specified resources |
1919
| [`--builder`](#builder) | `string` | | Override the configured builder instance |
2020
| [`--call`](#call) | `string` | `build` | Set method for evaluating build (`check`, `outline`, `targets`) |
2121
| [`--check`](#check) | `bool` | | Shorthand for `--call=check` |
@@ -51,6 +51,80 @@ guide for introduction to writing bake files.
5151
5252
## Examples
5353

54+
### <a name="allow"></a> Allow extra privileged entitlement (--allow)
55+
56+
```text
57+
--allow=ENTITLEMENT[=VALUE]
58+
```
59+
60+
Entitlements are designed to provide controlled access to privileged
61+
operations. By default, Buildx and BuildKit operates with restricted
62+
permissions to protect users and their systems from unintended side effects or
63+
security risks. The `--allow` flag explicitly grants access to additional
64+
entitlements, making it clear when a build or bake operation requires elevated
65+
privileges.
66+
67+
In addition to BuildKit's `network.host` and `security.insecure` entitlements
68+
(see [`docker buildx build --allow`](https://docs.docker.com/reference/cli/docker/buildx/build/#allow),
69+
Bake supports file system entitlements that grant granular control over file
70+
system access. These are particularly useful when working with builds that need
71+
access to files outside the default working directory.
72+
73+
Bake supports the following filesystem entitlements:
74+
75+
- `--allow fs=<path|*>` - Grant read and write access to files outside of the
76+
working directory.
77+
- `--allow fs.read=<path|*>` - Grant read access to files outside of the
78+
working directory.
79+
- `--allow fs.write=<path|*>` - Grant write access to files outside of the
80+
working directory.
81+
82+
The `fs` entitlements take a path value (relative or absolute) to a directory
83+
on the filesystem. Alternatively, you can pass a wildcard (`*`) to allow Bake
84+
to access the entire filesystem.
85+
86+
### Example: fs.read
87+
88+
Given the following Bake configuration, Bake would need to access the parent
89+
directory, relative to the Bake file.
90+
91+
```hcl
92+
target "app" {
93+
context = "../src"
94+
}
95+
```
96+
97+
Assuming `docker buildx bake app` is executed in the same directory as the
98+
`docker-bake.hcl` file, you would need to explicitly allow Bake to read from
99+
the `../src` directory. In this case, the following invocations all work:
100+
101+
```console
102+
$ docker buildx bake --allow fs.read=* app
103+
$ docker buildx bake --allow fs.read=../src app
104+
$ docker buildx bake --allow fs=* app
105+
```
106+
107+
### Example: fs.write
108+
109+
The following `docker-bake.hcl` file requires write access to the `/tmp`
110+
directory.
111+
112+
```hcl
113+
target "app" {
114+
output = "/tmp"
115+
}
116+
```
117+
118+
Assuming `docker buildx bake app` is executed outside of the `/tmp` directory,
119+
you would need to allow the `fs.write` entitlement, either by specifying the
120+
path or using a wildcard:
121+
122+
```console
123+
$ docker buildx bake --allow fs=/tmp app
124+
$ docker buildx bake --allow fs.write=/tmp app
125+
$ docker buildx bake --allow fs.write=* app
126+
```
127+
54128
### <a name="builder"></a> Override the configured builder instance (--builder)
55129

56130
Same as [`buildx --builder`](buildx.md#builder).

util/buildflags/cache.go

Lines changed: 19 additions & 2 deletions
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

Lines changed: 10 additions & 7 deletions
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

Lines changed: 10 additions & 1 deletion
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)