Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cache: Add support for bind cache #1497

Merged
merged 5 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,33 @@ func testAcceptance(
})
})

when("--cache with options for build cache as bind", func() {
var bindCacheDir, cacheFlags string
it.Before(func() {
h.SkipIf(t, !pack.SupportsFeature(invoke.Cache), "")
cacheBindName := fmt.Sprintf("%s-bind", repoName)
bindCacheDir, err := ioutil.TempDir("", cacheBindName)
assert.Nil(err)
cacheFlags = fmt.Sprintf("type=build;format=bind;source=%s", bindCacheDir)
})

it("creates image and cache image on the registry", func() {
buildArgs := []string{
repoName,
"-p", filepath.Join("testdata", "mock_app"),
"--cache",
cacheFlags,
}

output := pack.RunSuccessfully("build", buildArgs...)
assertions.NewOutputAssertionManager(t, output).ReportsSuccessfulImageBuild(repoName)

t.Log("checking that bind mount has cache contents")
assert.FileExists(fmt.Sprintf("%s/committed", bindCacheDir))
defer os.RemoveAll(bindCacheDir)
})
})

when("ctrl+c", func() {
it("stops the execution", func() {
var buf = new(bytes.Buffer)
Expand Down
12 changes: 9 additions & 3 deletions internal/build/lifecycle_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,16 @@ func (l *LifecycleExecution) Run(ctx context.Context, phaseFactoryCreator PhaseF
}
buildCache = cache.NewImageCache(cacheImage, l.docker)
} else {
buildCache = cache.NewVolumeCache(l.opts.Image, l.opts.Cache.Build, "build", l.docker)
switch l.opts.Cache.Build.Format {
case cache.CacheVolume:
buildCache = cache.NewVolumeCache(l.opts.Image, l.opts.Cache.Build, "build", l.docker)
l.logger.Debugf("Using build cache volume %s", style.Symbol(buildCache.Name()))
case cache.CacheBind:
buildCache = cache.NewBindCache(l.opts.Cache.Build, l.docker)
l.logger.Debugf("Using build cache dir %s", style.Symbol(buildCache.Name()))
}
}

l.logger.Debugf("Using build cache volume %s", style.Symbol(buildCache.Name()))
if l.opts.ClearCache {
if err := buildCache.Clear(ctx); err != nil {
return errors.Wrap(err, "clearing build cache")
Expand Down Expand Up @@ -251,7 +257,7 @@ func (l *LifecycleExecution) Create(ctx context.Context, publish bool, dockerHos
case cache.Image:
flags = append(flags, "-cache-image", buildCache.Name())
cacheOpts = WithBinds(volumes...)
case cache.Volume:
case cache.Volume, cache.Bind:
cacheOpts = WithBinds(append(volumes, fmt.Sprintf("%s:%s", buildCache.Name(), l.mountPaths.cacheDir()))...)
}

Expand Down
36 changes: 36 additions & 0 deletions internal/cache/bind_cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package cache

import (
"context"
"os"

"github.com/docker/docker/client"
)

type BindCache struct {
docker client.CommonAPIClient
bind string
}

func NewBindCache(cacheType CacheInfo, dockerClient client.CommonAPIClient) *BindCache {
return &BindCache{
bind: cacheType.Source,
docker: dockerClient,
}
}

func (c *BindCache) Name() string {
return c.bind
}

func (c *BindCache) Clear(ctx context.Context) error {
err := os.RemoveAll(c.bind)
if err != nil {
return err
}
return nil
}

func (c *BindCache) Type() Type {
return Bind
}
62 changes: 56 additions & 6 deletions internal/cache/cache_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cache
import (
"encoding/csv"
"fmt"
"path/filepath"
"strings"

"github.com/pkg/errors"
Expand All @@ -13,6 +14,7 @@ type CacheInfo struct {
Format Format
Source string
}

type CacheOpts struct {
Build CacheInfo
Launch CacheInfo
Expand All @@ -21,6 +23,7 @@ type CacheOpts struct {
const (
CacheVolume Format = iota
CacheImage
CacheBind
)

func (f Format) String() string {
Expand All @@ -29,6 +32,20 @@ func (f Format) String() string {
return "image"
case CacheVolume:
return "volume"
case CacheBind:
return "bind"
}
return ""
}

func (c *CacheInfo) SourceName() string {
switch c.Format {
case CacheImage:
fallthrough
case CacheVolume:
return "name"
case CacheBind:
return "source"
}
return ""
}
Expand Down Expand Up @@ -76,15 +93,19 @@ func (c *CacheOpts) Set(value string) error {
cache.Format = CacheImage
case "volume":
cache.Format = CacheVolume
case "bind":
cache.Format = CacheBind
default:
return errors.Errorf("invalid cache format '%s'", value)
}
case "name":
cache.Source = value
case "source":
cache.Source = value
}
}

err = populateMissing(c)
err = sanitize(c)
if err != nil {
return err
}
Expand All @@ -93,18 +114,47 @@ func (c *CacheOpts) Set(value string) error {

func (c *CacheOpts) String() string {
var cacheFlag string
cacheFlag = fmt.Sprintf("type=build;format=%s;name=%s;", c.Build.Format.String(), c.Build.Source)
cacheFlag += fmt.Sprintf("type=launch;format=%s;name=%s;", c.Launch.Format.String(), c.Launch.Source)
cacheFlag = fmt.Sprintf("type=build;format=%s;", c.Build.Format.String())
if c.Build.Source != "" {
cacheFlag += fmt.Sprintf("%s=%s;", c.Build.SourceName(), c.Build.Source)
}

cacheFlag += fmt.Sprintf("type=launch;format=%s;", c.Launch.Format.String())
if c.Launch.Source != "" {
cacheFlag += fmt.Sprintf("%s=%s;", c.Launch.SourceName(), c.Launch.Source)
}

return cacheFlag
}

func (c *CacheOpts) Type() string {
return "cache"
}

func populateMissing(c *CacheOpts) error {
if (c.Build.Source == "" && c.Build.Format == CacheImage) || (c.Launch.Source == "" && c.Launch.Format == CacheImage) {
return errors.Errorf("cache 'name' is required")
func sanitize(c *CacheOpts) error {
for _, v := range []CacheInfo{c.Build, c.Launch} {
// volume cache name can be auto-generated
if v.Format != CacheVolume && v.Source == "" {
return errors.Errorf("cache '%s' is required", v.SourceName())
}
}

if c.Build.Format == CacheBind || c.Launch.Format == CacheBind {
var (
resolvedPath string
err error
)
if c.Build.Format == CacheBind {
if resolvedPath, err = filepath.Abs(c.Build.Source); err != nil {
return errors.Wrap(err, "resolve absolute path")
}
c.Build.Source = filepath.Join(resolvedPath, "build-cache")
} else {
if resolvedPath, err = filepath.Abs(c.Launch.Source); err != nil {
return errors.Wrap(err, "resolve absolute path")
}
c.Launch.Source = filepath.Join(resolvedPath, "launch-cache")
}
}
return nil
}
Loading