Skip to content

Commit da246c6

Browse files
committed
Setting up initial tests for docker
1 parent f860bdb commit da246c6

File tree

20 files changed

+340
-63
lines changed

20 files changed

+340
-63
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -239,16 +239,19 @@ 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 Images to consider as cache sources
244+
--export-cache list Image cache export location (synonymous to buildkit export-cache)
245+
-f, --file string Name of the Dockerfile (Default is 'PATH/Dockerfile')
246+
-h, --help help for build
247+
--import-cache list Image cache import location (synonymous to buildkit import-cache) takes precedence over 'cache-from'
248+
--label list Set metadata for an image
249+
--no-cache Do not use cache when building the image
250+
--no-console Use non-console progress UI
251+
-o, --output string BuildKit output specification (e.g. type=tar,dest=build.tar)
252+
--platform list Set platforms for which the image should be built
253+
-t, --tag list Name and optionally a tag in the 'name:tag' format
254+
--target string Set the target build stage to build
252255

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

build.go

Lines changed: 52 additions & 8 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"
@@ -26,6 +27,7 @@ import (
2627
controlapi "github.com/moby/buildkit/api/services/control"
2728
bkclient "github.com/moby/buildkit/client"
2829
"github.com/moby/buildkit/cmd/buildctl/build"
30+
bkbuild "github.com/moby/buildkit/cmd/buildctl/build"
2931
"github.com/moby/buildkit/identity"
3032
"github.com/moby/buildkit/session"
3133
"github.com/moby/buildkit/session/filesync"
@@ -41,11 +43,13 @@ const buildUsageLongHelp = `Build an image from a Dockerfile`
4143
func newBuildCommand() *cobra.Command {
4244

4345
build := &buildCommand{
44-
tags: newListValue().WithValidator(validateTag),
45-
buildArgs: newListValue(),
46-
labels: newListValue(),
47-
platforms: newListValue(),
48-
cacheFrom: newListValue(),
46+
tags: newListValue().WithValidator(validateTag),
47+
buildArgs: newListValue(),
48+
labels: newListValue(),
49+
platforms: newListValue(),
50+
cacheFrom: newListValue(),
51+
exportCache: newListValue(),
52+
importCache: newListValue(),
4953
}
5054

5155
cmd := &cobra.Command{
@@ -72,6 +76,8 @@ func newBuildCommand() *cobra.Command {
7276
fs.BoolVar(&build.noCache, "no-cache", false, "Do not use cache when building the image")
7377
fs.StringVarP(&build.output, "output", "o", "", "BuildKit output specification (e.g. type=tar,dest=build.tar)")
7478
fs.Var(build.cacheFrom, "cache-from", "Images to consider as cache sources")
79+
fs.Var(build.exportCache, "export-cache", "Image cache export location (synonymous to buildkit export-cache)")
80+
fs.Var(build.importCache, "import-cache", "Image cache import location (synonymous to buildkit import-cache) takes precedence over 'cache-from'")
7581
return cmd
7682
}
7783

@@ -84,6 +90,8 @@ type buildCommand struct {
8490
platforms *listValue
8591
output string
8692
cacheFrom *listValue
93+
exportCache *listValue
94+
importCache *listValue
8795
bkoutput bkclient.ExportEntry
8896

8997
contextDir string
@@ -259,11 +267,39 @@ func (cmd *buildCommand) Run(args []string) (err error) {
259267
})
260268

261269
var cacheExports []*controlapi.CacheOptionsEntry
262-
cacheExports = append(cacheExports, &controlapi.CacheOptionsEntry{
263-
Type: "inline",
264-
})
270+
if cmdExportCaches := cmd.exportCache.GetAll(); len(cmdExportCaches) > 0 {
271+
parsedCacheExports, err := bkbuild.ParseExportCache(cmdExportCaches, []string{})
272+
if err != nil {
273+
return fmt.Errorf("error parsing export cache: %v", err)
274+
}
275+
276+
for _, exportCacheItem := range parsedCacheExports {
277+
cacheExports = append(cacheExports, &controlapi.CacheOptionsEntry{
278+
Type: exportCacheItem.Type,
279+
Attrs: exportCacheItem.Attrs,
280+
})
281+
}
282+
} else {
283+
cacheExports = append(cacheExports, &controlapi.CacheOptionsEntry{
284+
Type: "inline",
285+
})
286+
}
265287

266288
var cacheImports []*controlapi.CacheOptionsEntry
289+
if cmdImportCaches := cmd.importCache.GetAll(); len(cmdImportCaches) > 0 {
290+
parsedCacheImport, err := bkbuild.ParseImportCache(cmdImportCaches)
291+
if err != nil {
292+
return fmt.Errorf("error parsing import cache: %v", err)
293+
}
294+
295+
for _, importCacheItem := range parsedCacheImport {
296+
cacheImports = append(cacheImports, &controlapi.CacheOptionsEntry{
297+
Type: importCacheItem.Type,
298+
Attrs: importCacheItem.Attrs,
299+
})
300+
}
301+
}
302+
267303
for _, cacheTarget := range cmd.cacheFrom.GetAll() {
268304
cacheImports = append(cacheImports, &controlapi.CacheOptionsEntry{
269305
Type: "registry",
@@ -277,6 +313,14 @@ func (cmd *buildCommand) Run(args []string) (err error) {
277313
frontendAttrs["cache-from"] = strings.Join(cmd.cacheFrom.GetAll(), ",")
278314
}
279315

316+
if len(cacheImports) > 0 {
317+
cacheImportMarshalled, err := json.Marshal(cacheImports)
318+
if err != nil {
319+
return fmt.Errorf("failed to marshal cache-imports: %v", err)
320+
}
321+
frontendAttrs["cache-imports"] = string(cacheImportMarshalled)
322+
}
323+
280324
// Solve the dockerfile.
281325
eg.Go(func() error {
282326
defer sess.Close()

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)