Skip to content

Commit

Permalink
Pre-pull cache-from images
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Jan 20, 2019
1 parent 51bcca6 commit 88f1247
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
4 changes: 2 additions & 2 deletions integration/examples/annotated-skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ build:
key2: "value2"
# Images to consider as cache sources
cacheFrom:
- image1
- image2
- golang:1.10.1-alpine3.7
- alpine:3.7
# Dockerfile target name to build.
# target: stageName

Expand Down
27 changes: 27 additions & 0 deletions pkg/skaffold/build/local/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import (
)

func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace string, a *latest.DockerArtifact) (string, error) {
if err := b.pullCacheFromImages(ctx, out, a); err != nil {
return "", errors.Wrap(err, "pulling cache-from images")
}

initialTag := util.RandomID()

if b.cfg.UseDockerCLI || b.cfg.UseBuildkit {
Expand Down Expand Up @@ -56,3 +60,26 @@ func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace stri

return b.localDocker.Build(ctx, out, workspace, a, initialTag)
}

func (b *Builder) pullCacheFromImages(ctx context.Context, out io.Writer, a *latest.DockerArtifact) error {
if len(a.CacheFrom) == 0 {
return nil
}

for _, image := range a.CacheFrom {
imageID, err := b.localDocker.ImageID(ctx, image)
if err != nil {
return errors.Wrapf(err, "getting imageID for %s", image)
}
if imageID != "" {
// already pulled
continue
}

if err := b.localDocker.Pull(ctx, out, image); err != nil {
return errors.Wrapf(err, "pulling image %s", image)
}
}

return nil
}
71 changes: 71 additions & 0 deletions pkg/skaffold/build/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,77 @@ func TestLocalRun(t *testing.T) {
tagger: &FakeTagger{Err: fmt.Errorf("")},
shouldErr: true,
},
{
description: "cache-from images already pulled",
artifacts: []*latest.Artifact{{
ImageName: "gcr.io/test/image",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
CacheFrom: []string{"pull1", "pull2"},
},
}},
},
api: testutil.FakeAPIClient{
TagToImageID: map[string]string{
"pull1": "imageID1",
"pull2": "imageID2",
},
},
tagger: &FakeTagger{Out: "gcr.io/test/image:tag"},
expected: []build.Artifact{{
ImageName: "gcr.io/test/image",
Tag: "gcr.io/test/image:tag",
}},
},
{
description: "pull cache-from images",
artifacts: []*latest.Artifact{{
ImageName: "gcr.io/test/image",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
CacheFrom: []string{"pull1", "pull2"},
},
}},
},
api: testutil.FakeAPIClient{},
tagger: &FakeTagger{Out: "gcr.io/test/image:tag"},
expected: []build.Artifact{{
ImageName: "gcr.io/test/image",
Tag: "gcr.io/test/image:tag",
}},
},
{
description: "pull error",
artifacts: []*latest.Artifact{{
ImageName: "gcr.io/test/image",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
CacheFrom: []string{"pull1"},
},
}},
},
api: testutil.FakeAPIClient{
ErrImagePull: true,
},
tagger: &FakeTagger{Out: "gcr.io/test/image:tag"},
shouldErr: true,
},
{
description: "inspect error",
artifacts: []*latest.Artifact{{
ImageName: "gcr.io/test/image",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
CacheFrom: []string{"pull1"},
},
}},
},
api: testutil.FakeAPIClient{
ErrImageInspect: true,
},
tagger: &FakeTagger{Out: "gcr.io/test/image:tag"},
shouldErr: true,
},
}

for _, test := range tests {
Expand Down
19 changes: 19 additions & 0 deletions pkg/skaffold/docker/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type LocalDaemon interface {
ConfigFile(ctx context.Context, image string) (*v1.ConfigFile, error)
Build(ctx context.Context, out io.Writer, workspace string, a *latest.DockerArtifact, ref string) (string, error)
Push(ctx context.Context, out io.Writer, ref string) (string, error)
Pull(ctx context.Context, out io.Writer, ref string) error
Load(ctx context.Context, out io.Writer, input io.Reader, ref string) (string, error)
Tag(ctx context.Context, image, ref string) error
ImageID(ctx context.Context, ref string) (string, error)
Expand Down Expand Up @@ -222,6 +223,24 @@ func (l *localDaemon) Push(ctx context.Context, out io.Writer, ref string) (stri
return digest, nil
}

// Pull pulls an image reference from a registry.
func (l *localDaemon) Pull(ctx context.Context, out io.Writer, ref string) error {
registryAuth, err := l.encodedRegistryAuth(ctx, DefaultAuthHelper, ref)
if err != nil {
return errors.Wrapf(err, "getting auth config for %s", ref)
}

rc, err := l.apiClient.ImagePull(ctx, ref, types.ImagePullOptions{
RegistryAuth: registryAuth,
})
if err != nil {
return errors.Wrap(err, "pulling image from repository")
}
defer rc.Close()

return streamDockerMessages(out, rc, nil)
}

// Load loads an image from a tar file. Returns the imageID for the loaded image.
func (l *localDaemon) Load(ctx context.Context, out io.Writer, input io.Reader, ref string) (string, error) {
resp, err := l.apiClient.ImageLoad(ctx, input, false)
Expand Down
9 changes: 9 additions & 0 deletions testutil/fake_image_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type FakeAPIClient struct {
ErrImageInspect bool
ErrImageTag bool
ErrImagePush bool
ErrImagePull bool
ErrStream bool
}

Expand Down Expand Up @@ -120,6 +121,14 @@ func (f *FakeAPIClient) ImagePush(_ context.Context, ref string, _ types.ImagePu
return f.body(digest), nil
}

func (f *FakeAPIClient) ImagePull(_ context.Context, ref string, _ types.ImagePullOptions) (io.ReadCloser, error) {
if f.ErrImagePull {
return nil, fmt.Errorf("")
}

return f.body(""), nil
}

func (f *FakeAPIClient) Info(context.Context) (types.Info, error) {
return types.Info{
IndexServerAddress: registry.IndexServer,
Expand Down

0 comments on commit 88f1247

Please sign in to comment.