diff --git a/pkg/skaffold/build/cache/lookup.go b/pkg/skaffold/build/cache/lookup.go index abc96c5c715..e526ebeee13 100644 --- a/pkg/skaffold/build/cache/lookup.go +++ b/pkg/skaffold/build/cache/lookup.go @@ -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" @@ -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 { @@ -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 +} diff --git a/pkg/skaffold/build/cache/lookup_test.go b/pkg/skaffold/build/cache/lookup_test.go index ea20f8c7942..b1c9916fa2c 100644 --- a/pkg/skaffold/build/cache/lookup_test.go +++ b/pkg/skaffold/build/cache/lookup_test.go @@ -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"}, }, { @@ -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"}, }, {