Skip to content

Commit 7abeaff

Browse files
committed
Added in cache-from and cache-to support.
1 parent f860bdb commit 7abeaff

File tree

20 files changed

+345
-71
lines changed

20 files changed

+345
-71
lines changed

README.md

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,18 @@ build - Build an image from a Dockerfile
239239
Usage: img build [OPTIONS] PATH
240240

241241
Flags:
242-
--build-arg list Set build-time variables
243-
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
244-
-h, --help help for build
245-
--label list Set metadata for an image
246-
--no-cache Do not use cache when building the image
247-
--no-console Use non-console progress UI
248-
-o, --output string BuildKit output specification (e.g. type=tar,dest=build.tar)
249-
--platform list Set platforms for which the image should be built
250-
-t, --tag list Name and optionally a tag in the 'name:tag' format
251-
--target string Set the target build stage to build
242+
--build-arg list Set build-time variables
243+
--cache-from list Buildkit import-cache or Buildx cache-from specification
244+
--cache-to list Buildx cache-to specification
245+
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
246+
-h, --help help for build
247+
--label list Set metadata for an image
248+
--no-cache Do not use cache when building the image
249+
--no-console Use non-console progress UI
250+
-o, --output string BuildKit output specification (e.g. type=tar,dest=build.tar)
251+
--platform list Set platforms for which the image should be built
252+
-t, --tag list Name and optionally a tag in the 'name:tag' format
253+
--target string Set the target build stage to build
252254

253255
Global Flags:
254256
-b, --backend string backend for snapshots ([auto native overlayfs]) (default "auto")

build.go

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bytes"
77
"compress/gzip"
88
"context"
9+
"encoding/json"
910
"errors"
1011

1112
"fmt"
@@ -46,6 +47,7 @@ func newBuildCommand() *cobra.Command {
4647
labels: newListValue(),
4748
platforms: newListValue(),
4849
cacheFrom: newListValue(),
50+
cacheTo: newListValue(),
4951
}
5052

5153
cmd := &cobra.Command{
@@ -71,7 +73,8 @@ func newBuildCommand() *cobra.Command {
7173
fs.BoolVar(&build.noConsole, "no-console", false, "Use non-console progress UI")
7274
fs.BoolVar(&build.noCache, "no-cache", false, "Do not use cache when building the image")
7375
fs.StringVarP(&build.output, "output", "o", "", "BuildKit output specification (e.g. type=tar,dest=build.tar)")
74-
fs.Var(build.cacheFrom, "cache-from", "Images to consider as cache sources")
76+
fs.Var(build.cacheFrom, "cache-from", "Buildkit import-cache or Buildx cache-from specification")
77+
fs.Var(build.cacheTo, "cache-to", "Buildx cache-to specification")
7578
return cmd
7679
}
7780

@@ -84,6 +87,7 @@ type buildCommand struct {
8487
platforms *listValue
8588
output string
8689
cacheFrom *listValue
90+
cacheTo *listValue
8791
bkoutput bkclient.ExportEntry
8892

8993
contextDir string
@@ -258,23 +262,61 @@ func (cmd *buildCommand) Run(args []string) (err error) {
258262
return sess.Run(ctx, sessDialer)
259263
})
260264

261-
var cacheExports []*controlapi.CacheOptionsEntry
262-
cacheExports = append(cacheExports, &controlapi.CacheOptionsEntry{
263-
Type: "inline",
264-
})
265+
//create cacheTo list for buildlkit's export-cache
266+
var cacheToList []*controlapi.CacheOptionsEntry
267+
if cmdCacheToList := cmd.cacheTo.GetAll(); len(cmdCacheToList) > 0 {
268+
parsedCacheToList, err := build.ParseExportCache(cmdCacheToList, []string{})
269+
if err != nil {
270+
return fmt.Errorf("error parsing export cache: %v", err)
271+
}
265272

266-
var cacheImports []*controlapi.CacheOptionsEntry
267-
for _, cacheTarget := range cmd.cacheFrom.GetAll() {
268-
cacheImports = append(cacheImports, &controlapi.CacheOptionsEntry{
269-
Type: "registry",
270-
Attrs: map[string]string{
271-
"ref": cacheTarget,
272-
},
273+
for _, cacheToItem := range parsedCacheToList {
274+
cacheToList = append(cacheToList, &controlapi.CacheOptionsEntry{
275+
Type: cacheToItem.Type,
276+
Attrs: cacheToItem.Attrs,
277+
})
278+
}
279+
} else {
280+
cacheToList = append(cacheToList, &controlapi.CacheOptionsEntry{
281+
Type: "inline",
273282
})
274283
}
275284

276-
if cmd.cacheFrom.Len() > 0 {
277-
frontendAttrs["cache-from"] = strings.Join(cmd.cacheFrom.GetAll(), ",")
285+
//create cacheFrom list for buildlkit's import-cache
286+
var cacheFromList []*controlapi.CacheOptionsEntry
287+
strCacheFromList := make([]string, 0)
288+
for _, cacheFrom := range cmd.cacheFrom.GetAll() {
289+
if !strings.Contains(cacheFrom, "type=") {
290+
//append early to not trigger warning in ParseImportCache func below
291+
cacheFromList = append(cacheFromList, &controlapi.CacheOptionsEntry{
292+
Type: "registry",
293+
Attrs: map[string]string{
294+
"ref": cacheFrom,
295+
},
296+
})
297+
} else {
298+
strCacheFromList = append(strCacheFromList, cacheFrom)
299+
}
300+
}
301+
302+
//parse the remainder of the cacheFrom list
303+
parsedCacheFromList, err := build.ParseImportCache(strCacheFromList)
304+
if err != nil {
305+
return fmt.Errorf("error parsing import cache: %v", err)
306+
}
307+
for _, cacheFromItem := range parsedCacheFromList {
308+
cacheFromList = append(cacheFromList, &controlapi.CacheOptionsEntry{
309+
Type: cacheFromItem.Type,
310+
Attrs: cacheFromItem.Attrs,
311+
})
312+
}
313+
314+
if len(cacheFromList) > 0 {
315+
cacheImportMarshalled, err := json.Marshal(cacheFromList)
316+
if err != nil {
317+
return fmt.Errorf("failed to marshal cache-imports: %v", err)
318+
}
319+
frontendAttrs["cache-imports"] = string(cacheImportMarshalled)
278320
}
279321

280322
// Solve the dockerfile.
@@ -288,8 +330,8 @@ func (cmd *buildCommand) Run(args []string) (err error) {
288330
Frontend: "dockerfile.v0",
289331
FrontendAttrs: frontendAttrs,
290332
Cache: controlapi.CacheOptions{
291-
Exports: cacheExports,
292-
Imports: cacheImports,
333+
Exports: cacheToList,
334+
Imports: cacheFromList,
293335
},
294336
}, ch)
295337
})

client/controller.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package client
22

33
import (
44
"fmt"
5+
"path/filepath"
6+
57
"github.com/containerd/containerd/remotes/docker"
68
"github.com/moby/buildkit/cache/remotecache"
79
inlineremotecache "github.com/moby/buildkit/cache/remotecache/inline"
10+
localremotecache "github.com/moby/buildkit/cache/remotecache/local"
811
registryremotecache "github.com/moby/buildkit/cache/remotecache/registry"
912
"github.com/moby/buildkit/control"
1013
"github.com/moby/buildkit/frontend"
@@ -14,7 +17,6 @@ import (
1417
"github.com/moby/buildkit/solver/bboltcachestorage"
1518
"github.com/moby/buildkit/worker"
1619
"github.com/moby/buildkit/worker/base"
17-
"path/filepath"
1820
)
1921

2022
func (c *Client) createController() error {
@@ -52,9 +54,12 @@ func (c *Client) createController() error {
5254
}
5355

5456
remoteCacheExporterFuncs := map[string]remotecache.ResolveCacheExporterFunc{
55-
"inline": inlineremotecache.ResolveCacheExporterFunc(),
57+
"inline": inlineremotecache.ResolveCacheExporterFunc(),
58+
"local": localremotecache.ResolveCacheExporterFunc(sm),
59+
"registry": registryremotecache.ResolveCacheExporterFunc(sm, docker.ConfigureDefaultRegistries()),
5660
}
5761
remoteCacheImporterFuncs := map[string]remotecache.ResolveCacheImporterFunc{
62+
"local": localremotecache.ResolveCacheImporterFunc(sm),
5863
"registry": registryremotecache.ResolveCacheImporterFunc(sm, opt.ContentStore, docker.ConfigureDefaultRegistries()),
5964
}
6065

@@ -66,7 +71,6 @@ func (c *Client) createController() error {
6671
ResolveCacheExporterFuncs: remoteCacheExporterFuncs,
6772
ResolveCacheImporterFuncs: remoteCacheImporterFuncs,
6873
CacheKeyStorage: cacheStorage,
69-
// No cache importer/exporter
7074
})
7175
if err != nil {
7276
return fmt.Errorf("creating new controller failed: %v", err)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ require (
1818
github.com/docker/go-units v0.4.0
1919
github.com/genuinetools/reg v0.16.0
2020
github.com/mitchellh/hashstructure v1.0.0 // indirect
21-
github.com/moby/buildkit v0.7.1
21+
github.com/moby/buildkit v0.7.2
2222
github.com/opencontainers/image-spec v1.0.1
2323
github.com/opencontainers/runc v1.0.0-rc9.0.20200221051241-688cf6d43cc4
2424
github.com/opentracing-contrib/go-stdlib v0.0.0-20180702182724-07a764486eb1 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
221221
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
222222
github.com/moby/buildkit v0.7.1 h1:aPnJdFwfTVuV/+MpOiBu0rTWi0FnRoUYX/WI+8ZJQeU=
223223
github.com/moby/buildkit v0.7.1/go.mod h1:D3DN/Nl4DyMH1LkwpRUJuoghqdigdXd1A6HXt5aZS40=
224+
github.com/moby/buildkit v0.7.2 h1:wp4R0QMXSqwjTJKhhWlJNOCSQ/OVPnsCf3N8rs09+vQ=
225+
github.com/moby/buildkit v0.7.2/go.mod h1:D3DN/Nl4DyMH1LkwpRUJuoghqdigdXd1A6HXt5aZS40=
224226
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c h1:nXxl5PrvVm2L/wCy8dQu6DMTwH4oIuGN8GJDAlqDdVE=
225227
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
226228
github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=

vendor/github.com/moby/buildkit/cache/contenthash/filehash.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/buildkit/cache/contenthash/tarsum.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/buildkit/cache/refs.go

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/buildkit/cache/remotecache/inline/inline.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/moby/buildkit/cache/remotecache/local/local.go

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)