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

Only go through images once for artifact caching #1743

Merged
merged 1 commit into from
Mar 6, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
15 changes: 3 additions & 12 deletions pkg/skaffold/build/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,21 +236,12 @@ func (c *Cache) retrieveCachedArtifactDetails(ctx context.Context, a *latest.Art
}

func (c *Cache) retrievePrebuiltImage(ctx context.Context, details ImageDetails) (string, error) {
// first, search for an image with the same image ID
img, err := c.client.FindImageByID(ctx, details.ID)
img, err := c.client.FindTaggedImage(ctx, details.ID, details.Digest)
if err != nil {
logrus.Debugf("error getting tagged image with id %s, checking digest: %v", details.ID, err)
}
if err == nil && img != "" {
return img, nil
}
// else, search for an image with the same digest
img, err = c.client.FindTaggedImageByDigest(ctx, details.Digest)
if err != nil {
return "", errors.Wrapf(err, "getting image from digest %s", details.Digest)
return "", errors.Wrap(err, "unable to find tagged image")
}
if img == "" {
return "", errors.New("no prebuilt image")
return img, errors.New("no prebuilt image")
}
return img, nil
}
Expand Down
23 changes: 14 additions & 9 deletions pkg/skaffold/build/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package build

import (
"context"
"fmt"
"io/ioutil"
"os"
"reflect"
Expand All @@ -31,6 +32,10 @@ import (
yaml "gopkg.in/yaml.v2"
)

var (
digest = "sha256:0000000000000000000000000000000000000000000000000000000000000000"
)

var defaultArtifactCache = ArtifactCache{"hash": ImageDetails{
Digest: "digest",
ID: "id",
Expand Down Expand Up @@ -313,16 +318,16 @@ func TestRetrieveCachedArtifactDetails(t *testing.T) {
api: testutil.FakeAPIClient{
ImageSummaries: []types.ImageSummary{
{
RepoDigests: []string{"digest"},
RepoDigests: []string{fmt.Sprintf("image@%s", digest)},
RepoTags: []string{"anotherimage:hash"},
},
},
},
cache: &Cache{
useCache: true,
artifactCache: ArtifactCache{"hash": ImageDetails{Digest: "digest"}},
artifactCache: ArtifactCache{"hash": ImageDetails{Digest: digest}},
},
digest: "digest",
digest: digest,
expected: &cachedArtifactDetails{
needsRetag: true,
needsPush: true,
Expand All @@ -338,16 +343,16 @@ func TestRetrieveCachedArtifactDetails(t *testing.T) {
api: testutil.FakeAPIClient{
ImageSummaries: []types.ImageSummary{
{
RepoDigests: []string{"digest"},
RepoDigests: []string{fmt.Sprintf("image@%s", digest)},
RepoTags: []string{"anotherimage:hash"},
},
},
},
cache: &Cache{
useCache: true,
artifactCache: ArtifactCache{"hash": ImageDetails{Digest: "digest"}},
artifactCache: ArtifactCache{"hash": ImageDetails{Digest: digest}},
},
digest: "digest",
digest: digest,
expected: &cachedArtifactDetails{
needsRetag: true,
prebuiltImage: "anotherimage:hash",
Expand All @@ -362,17 +367,17 @@ func TestRetrieveCachedArtifactDetails(t *testing.T) {
api: testutil.FakeAPIClient{
ImageSummaries: []types.ImageSummary{
{
RepoDigests: []string{"digest"},
RepoDigests: []string{fmt.Sprintf("image@%s", digest)},
RepoTags: []string{"anotherimage:hash"},
},
},
},
cache: &Cache{
useCache: true,
needsPush: true,
artifactCache: ArtifactCache{"hash": ImageDetails{Digest: "digest"}},
artifactCache: ArtifactCache{"hash": ImageDetails{Digest: digest}},
},
digest: "digest",
digest: digest,
expected: &cachedArtifactDetails{
needsRetag: true,
needsPush: true,
Expand Down
39 changes: 15 additions & 24 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ type LocalDaemon interface {
Tag(ctx context.Context, image, ref string) error
ImageID(ctx context.Context, ref string) (string, error)
RepoDigest(ctx context.Context, ref string) (string, error)
FindTaggedImageByDigest(ctx context.Context, id string) (string, error)
FindImageByID(ctx context.Context, id string) (string, error)
FindTaggedImage(ctx context.Context, id, digest string) (string, error)
ImageExists(ctx context.Context, ref string) bool
}

Expand Down Expand Up @@ -289,19 +288,30 @@ func (l *localDaemon) ImageID(ctx context.Context, ref string) (string, error) {
return image.ID, nil
}

// FindImageByID returns the name of an image with the given id
func (l *localDaemon) FindImageByID(ctx context.Context, id string) (string, error) {
// FindTaggedImage returns the name of an image with a matching id or digest
func (l *localDaemon) FindTaggedImage(ctx context.Context, id, digest string) (string, error) {
resp, err := l.apiClient.ImageList(ctx, types.ImageListOptions{})
if err != nil {
return "", errors.Wrap(err, "listing images")
}
for _, r := range resp {
if r.ID == id {
if r.ID == id && id != "" {
if len(r.RepoTags) == 0 {
return "", nil
}
return r.RepoTags[0], nil
}
if digest == "" {
continue
}
for _, d := range r.RepoDigests {
if getDigest(d) == digest {
// Return a tagged version of this image, since we can't retag an image in the image@sha256: format
if len(r.RepoTags) > 0 {
return r.RepoTags[0], nil
}
}
}
}
return "", nil
}
Expand All @@ -323,25 +333,6 @@ func (l *localDaemon) ImageExists(ctx context.Context, ref string) bool {
return err == nil
}

// FindTaggedImageByDigest returns the name of a tagged image with the given digest, if one exists
func (l *localDaemon) FindTaggedImageByDigest(ctx context.Context, digest string) (string, error) {
resp, err := l.apiClient.ImageList(ctx, types.ImageListOptions{})
if err != nil {
return "", errors.Wrap(err, "listing images")
}
for _, r := range resp {
for _, d := range r.RepoDigests {
if getDigest(d) == digest {
// Return a tagged version of this image, since we can't retag an image in the image@sha256: format
if len(r.RepoTags) > 0 {
return r.RepoTags[0], nil
}
}
}
}
return "", nil
}

func getDigest(img string) string {
ref, _ := name.NewDigest(img, name.WeakValidation)
return ref.DigestStr()
Expand Down
6 changes: 3 additions & 3 deletions pkg/skaffold/docker/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func TestFindTaggedImageByDigest(t *testing.T) {
apiClient: api,
}

actual, err := localDocker.FindTaggedImageByDigest(context.Background(), test.digest)
actual, err := localDocker.FindTaggedImage(context.Background(), "", test.digest)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, actual)
})
}
Expand Down Expand Up @@ -381,7 +381,7 @@ func TestRepoDigest(t *testing.T) {
})
}
}
func TestFindImageByID(t *testing.T) {
func TestFindTaggedImageByID(t *testing.T) {
tests := []struct {
name string
id string
Expand Down Expand Up @@ -444,7 +444,7 @@ func TestFindImageByID(t *testing.T) {
apiClient: api,
}

actual, err := localDocker.FindImageByID(context.Background(), test.id)
actual, err := localDocker.FindTaggedImage(context.Background(), test.id, "")
testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, actual)
})
}
Expand Down