Skip to content

Commit

Permalink
Try to import docker images before falling back to building
Browse files Browse the repository at this point in the history
  • Loading branch information
arlyon committed Jun 25, 2020
1 parent fc1b5da commit 7c34330
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
39 changes: 38 additions & 1 deletion pkg/skaffold/build/cache/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ package cache
import (
"context"
"fmt"
"io/ioutil"
"sync"

"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
Expand Down Expand Up @@ -52,7 +55,10 @@ func (c *cache) lookup(ctx context.Context, a *latest.Artifact, tag string) cach

entry, cacheHit := c.artifactCache[hash]
if !cacheHit {
return needsBuilding{hash: hash}
if entry, err = c.tryImport(ctx, a, tag, hash); err != nil {
logrus.Debugf("Could not import artifact from Docker, building instead (%s)", err)
return needsBuilding{hash: hash}
}
}

if c.imagesAreLocal {
Expand Down Expand Up @@ -108,3 +114,34 @@ func (c *cache) lookupRemote(ctx context.Context, hash, tag string, entry ImageD

return needsBuilding{hash: hash}
}

func (c *cache) tryImport(ctx context.Context, a *latest.Artifact, tag string, hash string) (ImageDetails, error) {
entry := ImageDetails{}

if !c.client.ImageExists(ctx, tag) {
logrus.Debugf("Importing artifact %s from docker registry", tag)
err := c.client.Pull(ctx, ioutil.Discard, tag)
if err != nil {
return entry, err
}
} else {
logrus.Debugf("Importing artifact %s from local docker", tag)
}

imageID, err := c.client.ImageID(ctx, a.ImageName)
if err != nil {
return entry, err
}

if imageID != "" {
entry.ID = imageID
}

if digest, err := docker.RemoteDigest(tag, c.insecureRegistries); err == nil {
logrus.Debugf("Added digest for %s to cache entry", tag)
entry.Digest = digest
}

c.artifactCache[hash] = entry
return entry, nil
}
4 changes: 4 additions & 0 deletions pkg/skaffold/build/cache/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func TestLookupLocal(t *testing.T) {
{
description: "miss",
hasher: mockHasher("thehash"),
api: &testutil.FakeAPIClient{},
cache: map[string]ImageDetails{},
expected: needsBuilding{hash: "thehash"},
},
{
Expand Down Expand Up @@ -132,6 +134,8 @@ func TestLookupRemote(t *testing.T) {
{
description: "miss",
hasher: mockHasher("hash"),
api: &testutil.FakeAPIClient{ErrImagePull: true},
cache: map[string]ImageDetails{},
expected: needsBuilding{hash: "hash"},
},
{
Expand Down

0 comments on commit 7c34330

Please sign in to comment.