Skip to content

Commit

Permalink
Merge branch 'main' into chore/acceptance-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jromero committed Sep 15, 2022
2 parents a520115 + 100f656 commit 8330cb9
Show file tree
Hide file tree
Showing 12 changed files with 254 additions and 42 deletions.
27 changes: 27 additions & 0 deletions acceptance/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,6 +1281,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
9 changes: 4 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/buildpacks/pack
require (
github.com/BurntSushi/toml v1.2.0
github.com/Masterminds/semver v1.5.0
github.com/Microsoft/go-winio v0.5.2
github.com/apex/log v1.9.0
github.com/buildpacks/imgutil v0.0.0-20220527150729-7a271a852e31
github.com/buildpacks/lifecycle v0.14.1
github.com/docker/cli v20.10.17+incompatible
github.com/docker/docker v20.10.17+incompatible
github.com/docker/cli v20.10.18+incompatible
github.com/docker/docker v20.10.18+incompatible
github.com/docker/go-connections v0.4.0
github.com/dustin/go-humanize v1.0.0
github.com/gdamore/tcell/v2 v2.5.3
Expand All @@ -32,7 +33,6 @@ require (
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4
golang.org/x/oauth2 v0.0.0-20220718184931-c8730f7fcb92
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce
gopkg.in/yaml.v3 v3.0.1
)

Expand All @@ -47,7 +47,6 @@ require (
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Microsoft/hcsshim v0.9.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20210428141323-04723f9f07d7 // indirect
github.com/acomagu/bufpipe v1.0.3 // indirect
Expand Down Expand Up @@ -108,7 +107,7 @@ require (
github.com/xanzy/ssh-agent v0.3.1 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220722155237-a158d28d115b // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand Down
9 changes: 4 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -461,17 +461,19 @@ github.com/dimchansky/utfbom v1.1.1/go.mod h1:SxdoEBH5qIqFocHMyGOXVAybYJdr71b1Q/
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
github.com/docker/cli v0.0.0-20191017083524-a8ff7f821017/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli v20.10.16+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli v20.10.17+incompatible h1:eO2KS7ZFeov5UJeaDmIs1NFEDRf32PaqRpvoEkKBy5M=
github.com/docker/cli v20.10.17+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/cli v20.10.18+incompatible h1:f/GQLsVpo10VvToRay2IraVA1wHz9KktZyjev3SIVDU=
github.com/docker/cli v20.10.18+incompatible/go.mod h1:JLrzqnKDaYBop7H2jaqPtU4hHvMKP+vjCwu2uszcLI8=
github.com/docker/distribution v0.0.0-20190905152932-14b96e55d84c/go.mod h1:0+TTO4EOBfRPhZXAeF1Vu+W3hHZ8eLp8PgKVZlcvtFY=
github.com/docker/distribution v2.7.1-0.20190205005809-0d3efadf0154+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/distribution v2.7.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/distribution v2.8.1+incompatible h1:Q50tZOPR6T/hjNsyc9g8/syEs6bk8XXApsHjKukMl68=
github.com/docker/distribution v2.8.1+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=
github.com/docker/docker v1.4.2-0.20190924003213-a8608b5b67c7/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v20.10.16+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v20.10.17+incompatible h1:JYCuMrWaVNophQTOrMMoSwudOVEfcegoZZrleKc1xwE=
github.com/docker/docker v20.10.17+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker v20.10.18+incompatible h1:SN84VYXTBNGn92T/QwIRPlum9zfemfitN7pbsp26WSc=
github.com/docker/docker v20.10.18+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/docker-credential-helpers v0.6.3/go.mod h1:WRaJzqw3CTB9bk10avuGsjVBZsD05qeibJ1/TYlvc0Y=
github.com/docker/docker-credential-helpers v0.6.4 h1:axCks+yV+2MR3/kZhAmy07yC56WZ2Pwu/fKWtKuZB0o=
github.com/docker/docker-credential-helpers v0.6.4/go.mod h1:ofX3UI0Gz1TteYBjtgs07O36Pyasyp66D2uKT7H8W1c=
Expand Down Expand Up @@ -1617,7 +1619,6 @@ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220513210516-0976fa681c29/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f h1:Ax0t5p6N38Ga0dThY21weqDEyz2oklo4IvDkpigvkD8=
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 h1:uVc8UZUe6tr40fFVnUP5Oj+veunVezqYl9z7DYw9xzw=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand Down Expand Up @@ -2120,8 +2121,6 @@ gopkg.in/ini.v1 v1.51.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.62.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/ini.v1 v1.63.2/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.0.0/go.mod h1:l0ndWWf7gzL7RNwBG7wST/UCcT4T24xpD6X8LsfU/+k=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce h1:+JknDZhAj8YMt7GC73Ei8pv4MzjDUNPHgQWJdtMAaDU=
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce/go.mod h1:5AcXVHNjg+BDxry382+8OKon8SEWiKktQR07RKPsv1c=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/square/go-jose.v2 v2.2.2/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
gopkg.in/square/go-jose.v2 v2.3.1/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI=
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

0 comments on commit 8330cb9

Please sign in to comment.