Skip to content

Commit

Permalink
Merge pull request #1039 from buildpacks/317_labels_to_volumes
Browse files Browse the repository at this point in the history
Add labels to cache volumes
Signed-off-by: David Freilich <[email protected]>
  • Loading branch information
dfreilich committed Jan 27, 2021
2 parents ddaff10 + 62d3ce9 commit c911002
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
11 changes: 10 additions & 1 deletion internal/cache/volume_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"crypto/sha256"
"fmt"
"strings"

"github.com/docker/docker/client"
"github.com/google/go-containerregistry/pkg/name"
Expand All @@ -19,7 +20,7 @@ type VolumeCache struct {
func NewVolumeCache(imageRef name.Reference, suffix string, dockerClient client.CommonAPIClient) *VolumeCache {
sum := sha256.Sum256([]byte(imageRef.Name()))

vol := paths.FilterReservedNames(fmt.Sprintf("%x", sum[:6]))
vol := paths.FilterReservedNames(fmt.Sprintf("%s-%x", sanitizedRef(imageRef), sum[:6]))
return &VolumeCache{
volume: fmt.Sprintf("pack-cache-%s.%s", vol, suffix),
docker: dockerClient,
Expand All @@ -41,3 +42,11 @@ func (c *VolumeCache) Clear(ctx context.Context) error {
func (c *VolumeCache) Type() Type {
return Volume
}

// note image names and volume names are validated using the same restrictions:
// see https://github.com/moby/moby/blob/f266f13965d5bfb1825afa181fe6c32f3a597fa3/daemon/names/names.go#L5
func sanitizedRef(ref name.Reference) string {
result := strings.TrimPrefix(ref.Context().String(), ref.Context().RegistryStr()+"/")
result = strings.ReplaceAll(result, "/", "_")
return fmt.Sprintf("%s_%s", result, ref.Identifier())
}
11 changes: 11 additions & 0 deletions internal/cache/volume_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/api/types/volume"
"github.com/docker/docker/client"
"github.com/docker/docker/daemon/names"
"github.com/google/go-containerregistry/pkg/name"
"github.com/heroku/color"
"github.com/sclevine/spec"
Expand Down Expand Up @@ -108,6 +109,16 @@ func testCache(t *testing.T, when spec.G, it spec.S) {
expected := cache.NewVolumeCache(ref, "some-suffix", dockerClient)
h.AssertEq(t, subject.Name(), expected.Name())
})

it("includes human readable information", func() {
ref, err := name.ParseReference("myregistryhost:5000/fedora/httpd:version1.0", name.WeakValidation)
h.AssertNil(t, err)

subject := cache.NewVolumeCache(ref, "some-suffix", dockerClient)

h.AssertContains(t, subject.Name(), "fedora_httpd_version1.0")
h.AssertTrue(t, names.RestrictedNamePattern.MatchString(subject.Name()))
})
})

when("#Clear", func() {
Expand Down

0 comments on commit c911002

Please sign in to comment.