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

Add labels to cache volumes #1039

Merged
merged 3 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
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