Skip to content

Specifying artifact location i.e locally or remote #2958

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

Merged
merged 5 commits into from
Oct 2, 2019
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
49 changes: 39 additions & 10 deletions pkg/skaffold/build/cache/details.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

type cacheDetails interface {
Hash() string
Location() string
}

// Failed: couldn't lookup cache
Expand All @@ -36,6 +37,10 @@ func (d failed) Hash() string {
return ""
}

func (d failed) Location() string {
return ""
}

// Not found, needs building
type needsBuilding struct {
hash string
Expand All @@ -45,61 +50,85 @@ func (d needsBuilding) Hash() string {
return d.hash
}

func (d needsBuilding) Location() string {
return ""
}

// Found in cache
type found struct {
hash string
hash string
location string
}

func (d found) Hash() string {
return d.hash
}

func (d found) Location() string {
return d.location
}

type needsTagging interface {
Tag(context.Context, *cache) error
}

// Found locally with wrong tag. Needs retagging
type needsLocalTagging struct {
hash string
tag string
imageID string
hash string
tag string
imageID string
location string
}

func (d needsLocalTagging) Hash() string {
return d.hash
}

func (d needsLocalTagging) Location() string {
return d.location
}

func (d needsLocalTagging) Tag(ctx context.Context, c *cache) error {
return c.client.Tag(ctx, d.imageID, d.tag)
}

// Found remotely with wrong tag. Needs retagging
type needsRemoteTagging struct {
hash string
tag string
digest string
hash string
tag string
digest string
location string
}

func (d needsRemoteTagging) Hash() string {
return d.hash
}

func (d needsRemoteTagging) Location() string {
return d.location
}

func (d needsRemoteTagging) Tag(ctx context.Context, c *cache) error {
fqn := d.tag + "@" + d.digest // Tag is not important. We just need the registry and the digest to locate the image.
return docker.AddRemoteTag(fqn, d.tag, c.insecureRegistries)
}

// Found locally. Needs pushing
type needsPushing struct {
hash string
tag string
imageID string
hash string
tag string
imageID string
location string
}

func (d needsPushing) Hash() string {
return d.hash
}

func (d needsPushing) Location() string {
return d.location
}

func (d needsPushing) Push(ctx context.Context, out io.Writer, c *cache) error {
if err := c.client.Tag(ctx, d.imageID, d.tag); err != nil {
return err
Expand Down
10 changes: 5 additions & 5 deletions pkg/skaffold/build/cache/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,12 @@ func (c *cache) lookupLocal(ctx context.Context, hash, tag string, entry ImageDe

// Image exists locally with the same tag
if idForTag == entry.ID {
return found{hash: hash}
return found{hash: hash, location: "locally"}
}

// Image exists locally with a different tag
if c.client.ImageExists(ctx, entry.ID) {
return needsLocalTagging{hash: hash, tag: tag, imageID: entry.ID}
return needsLocalTagging{hash: hash, tag: tag, imageID: entry.ID, location: "locally"}
}

return needsBuilding{hash: hash}
Expand All @@ -97,21 +97,21 @@ func (c *cache) lookupRemote(ctx context.Context, hash, tag string, entry ImageD
if remoteDigest, err := docker.RemoteDigest(tag, c.insecureRegistries); err == nil {
// Image exists remotely with the same tag and digest
if remoteDigest == entry.Digest {
return found{hash: hash}
return found{hash: hash, location: "remote"}
}
}

// Image exists remotely with a different tag
fqn := tag + "@" + entry.Digest // Actual tag will be ignored but we need the registry and the digest part of it.
if remoteDigest, err := docker.RemoteDigest(fqn, c.insecureRegistries); err == nil {
if remoteDigest == entry.Digest {
return needsRemoteTagging{hash: hash, tag: tag, digest: entry.Digest}
return needsRemoteTagging{hash: hash, tag: tag, digest: entry.Digest, location: "remote"}
}
}

// Image exists locally
if entry.ID != "" && c.client != nil && c.client.ImageExists(ctx, entry.ID) {
return needsPushing{hash: hash, tag: tag, imageID: entry.ID}
return needsPushing{hash: hash, tag: tag, imageID: entry.ID, location: "remote"}
}

return needsBuilding{hash: hash}
Expand Down
10 changes: 5 additions & 5 deletions pkg/skaffold/build/cache/lookup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func TestLookupLocal(t *testing.T) {
"hash": {ID: "imageID"},
},
api: (&testutil.FakeAPIClient{}).Add("tag", "imageID"),
expected: found{hash: "hash"},
expected: found{hash: "hash", location: "locally"},
},
{
description: "hit but different tag",
Expand All @@ -89,7 +89,7 @@ func TestLookupLocal(t *testing.T) {
"hash": {ID: "imageID"},
},
api: (&testutil.FakeAPIClient{}).Add("tag", "otherImageID").Add("othertag", "imageID"),
expected: needsLocalTagging{hash: "hash", tag: "tag", imageID: "imageID"},
expected: needsLocalTagging{hash: "hash", tag: "tag", imageID: "imageID", location: "locally"},
},
{
description: "hit but imageID not found",
Expand Down Expand Up @@ -147,15 +147,15 @@ func TestLookupRemote(t *testing.T) {
cache: map[string]ImageDetails{
"hash": {Digest: "digest"},
},
expected: found{hash: "hash"},
expected: found{hash: "hash", location: "remote"},
},
{
description: "hit with different tag",
hasher: mockHasher("hash"),
cache: map[string]ImageDetails{
"hash": {Digest: "otherdigest"},
},
expected: needsRemoteTagging{hash: "hash", tag: "tag", digest: "otherdigest"},
expected: needsRemoteTagging{hash: "hash", tag: "tag", digest: "otherdigest", location: "remote"},
},
{
description: "found locally",
Expand All @@ -164,7 +164,7 @@ func TestLookupRemote(t *testing.T) {
"hash": {ID: "imageID"},
},
api: (&testutil.FakeAPIClient{}).Add("tag", "imageID"),
expected: needsPushing{hash: "hash", tag: "tag", imageID: "imageID"},
expected: needsPushing{hash: "hash", tag: "tag", imageID: "imageID", location: "remote"},
},
{
description: "not found",
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/build/cache/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c *cache) Build(ctx context.Context, out io.Writer, tags tag.ImageTags, ar
}

default:
color.Green.Fprintln(out, "Found")
color.Green.Fprintln(out, "Found cached artifact at ", result.Location())
}

// Image is already built
Expand Down