Skip to content

Commit

Permalink
Added in cache-from and cache-to support. (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
swrap authored Sep 2, 2020
1 parent 26feed9 commit cd9273d
Show file tree
Hide file tree
Showing 20 changed files with 345 additions and 71 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,18 @@ build - Build an image from a Dockerfile
Usage: img build [OPTIONS] PATH

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

Global Flags:
-b, --backend string backend for snapshots ([auto native overlayfs]) (default "auto")
Expand Down
74 changes: 58 additions & 16 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"errors"

"fmt"
Expand Down Expand Up @@ -46,6 +47,7 @@ func newBuildCommand() *cobra.Command {
labels: newListValue(),
platforms: newListValue(),
cacheFrom: newListValue(),
cacheTo: newListValue(),
}

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

Expand All @@ -84,6 +87,7 @@ type buildCommand struct {
platforms *listValue
output string
cacheFrom *listValue
cacheTo *listValue
bkoutput bkclient.ExportEntry

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

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

var cacheImports []*controlapi.CacheOptionsEntry
for _, cacheTarget := range cmd.cacheFrom.GetAll() {
cacheImports = append(cacheImports, &controlapi.CacheOptionsEntry{
Type: "registry",
Attrs: map[string]string{
"ref": cacheTarget,
},
for _, cacheToItem := range parsedCacheToList {
cacheToList = append(cacheToList, &controlapi.CacheOptionsEntry{
Type: cacheToItem.Type,
Attrs: cacheToItem.Attrs,
})
}
} else {
cacheToList = append(cacheToList, &controlapi.CacheOptionsEntry{
Type: "inline",
})
}

if cmd.cacheFrom.Len() > 0 {
frontendAttrs["cache-from"] = strings.Join(cmd.cacheFrom.GetAll(), ",")
//create cacheFrom list for buildlkit's import-cache
var cacheFromList []*controlapi.CacheOptionsEntry
strCacheFromList := make([]string, 0)
for _, cacheFrom := range cmd.cacheFrom.GetAll() {
if !strings.Contains(cacheFrom, "type=") {
//append early to not trigger warning in ParseImportCache func below
cacheFromList = append(cacheFromList, &controlapi.CacheOptionsEntry{
Type: "registry",
Attrs: map[string]string{
"ref": cacheFrom,
},
})
} else {
strCacheFromList = append(strCacheFromList, cacheFrom)
}
}

//parse the remainder of the cacheFrom list
parsedCacheFromList, err := build.ParseImportCache(strCacheFromList)
if err != nil {
return fmt.Errorf("error parsing import cache: %v", err)
}
for _, cacheFromItem := range parsedCacheFromList {
cacheFromList = append(cacheFromList, &controlapi.CacheOptionsEntry{
Type: cacheFromItem.Type,
Attrs: cacheFromItem.Attrs,
})
}

if len(cacheFromList) > 0 {
cacheImportMarshalled, err := json.Marshal(cacheFromList)
if err != nil {
return fmt.Errorf("failed to marshal cache-imports: %v", err)
}
frontendAttrs["cache-imports"] = string(cacheImportMarshalled)
}

// Solve the dockerfile.
Expand All @@ -288,8 +330,8 @@ func (cmd *buildCommand) Run(args []string) (err error) {
Frontend: "dockerfile.v0",
FrontendAttrs: frontendAttrs,
Cache: controlapi.CacheOptions{
Exports: cacheExports,
Imports: cacheImports,
Exports: cacheToList,
Imports: cacheFromList,
},
}, ch)
})
Expand Down
10 changes: 7 additions & 3 deletions client/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package client

import (
"fmt"
"path/filepath"

"github.com/containerd/containerd/remotes/docker"
"github.com/moby/buildkit/cache/remotecache"
inlineremotecache "github.com/moby/buildkit/cache/remotecache/inline"
localremotecache "github.com/moby/buildkit/cache/remotecache/local"
registryremotecache "github.com/moby/buildkit/cache/remotecache/registry"
"github.com/moby/buildkit/control"
"github.com/moby/buildkit/frontend"
Expand All @@ -14,7 +17,6 @@ import (
"github.com/moby/buildkit/solver/bboltcachestorage"
"github.com/moby/buildkit/worker"
"github.com/moby/buildkit/worker/base"
"path/filepath"
)

func (c *Client) createController() error {
Expand Down Expand Up @@ -52,9 +54,12 @@ func (c *Client) createController() error {
}

remoteCacheExporterFuncs := map[string]remotecache.ResolveCacheExporterFunc{
"inline": inlineremotecache.ResolveCacheExporterFunc(),
"inline": inlineremotecache.ResolveCacheExporterFunc(),
"local": localremotecache.ResolveCacheExporterFunc(sm),
"registry": registryremotecache.ResolveCacheExporterFunc(sm, docker.ConfigureDefaultRegistries()),
}
remoteCacheImporterFuncs := map[string]remotecache.ResolveCacheImporterFunc{
"local": localremotecache.ResolveCacheImporterFunc(sm),
"registry": registryremotecache.ResolveCacheImporterFunc(sm, opt.ContentStore, docker.ConfigureDefaultRegistries()),
}

Expand All @@ -66,7 +71,6 @@ func (c *Client) createController() error {
ResolveCacheExporterFuncs: remoteCacheExporterFuncs,
ResolveCacheImporterFuncs: remoteCacheImporterFuncs,
CacheKeyStorage: cacheStorage,
// No cache importer/exporter
})
if err != nil {
return fmt.Errorf("creating new controller failed: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/docker/go-units v0.4.0
github.com/genuinetools/reg v0.16.0
github.com/mitchellh/hashstructure v1.0.0 // indirect
github.com/moby/buildkit v0.7.1
github.com/moby/buildkit v0.7.2
github.com/opencontainers/image-spec v1.0.1
github.com/opencontainers/runc v1.0.0-rc9.0.20200221051241-688cf6d43cc4
github.com/opentracing-contrib/go-stdlib v0.0.0-20180702182724-07a764486eb1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh
github.com/mitchellh/osext v0.0.0-20151018003038-5e2d6d41470f/go.mod h1:OkQIRizQZAeMln+1tSwduZz7+Af5oFlKirV/MSYes2A=
github.com/moby/buildkit v0.7.1 h1:aPnJdFwfTVuV/+MpOiBu0rTWi0FnRoUYX/WI+8ZJQeU=
github.com/moby/buildkit v0.7.1/go.mod h1:D3DN/Nl4DyMH1LkwpRUJuoghqdigdXd1A6HXt5aZS40=
github.com/moby/buildkit v0.7.2 h1:wp4R0QMXSqwjTJKhhWlJNOCSQ/OVPnsCf3N8rs09+vQ=
github.com/moby/buildkit v0.7.2/go.mod h1:D3DN/Nl4DyMH1LkwpRUJuoghqdigdXd1A6HXt5aZS40=
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c h1:nXxl5PrvVm2L/wCy8dQu6DMTwH4oIuGN8GJDAlqDdVE=
github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/ncw/swift v1.0.47/go.mod h1:23YIA4yWVnGwv2dQlN4bB7egfYX6YLn0Yo/S6zZO/ZM=
Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/moby/buildkit/cache/contenthash/filehash.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion vendor/github.com/moby/buildkit/cache/refs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions vendor/github.com/moby/buildkit/cache/remotecache/local/local.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit cd9273d

Please sign in to comment.