From bdb0a09d79334bc3d4f90ce7ae35d02de634dadf Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 7 Sep 2018 14:02:44 +0200 Subject: [PATCH] Pull cache-from images on gcb Fixes #936 Signed-off-by: David Gageot --- pkg/skaffold/build/gcb/cloud_build.go | 26 -------- pkg/skaffold/build/gcb/desc.go | 60 +++++++++++++++++++ .../gcb/{cloud_build_test.go => desc_test.go} | 32 ++++++++++ 3 files changed, 92 insertions(+), 26 deletions(-) create mode 100644 pkg/skaffold/build/gcb/desc.go rename pkg/skaffold/build/gcb/{cloud_build_test.go => desc_test.go} (69%) diff --git a/pkg/skaffold/build/gcb/cloud_build.go b/pkg/skaffold/build/gcb/cloud_build.go index aa5ef05aaf5..7adcd2ad15c 100644 --- a/pkg/skaffold/build/gcb/cloud_build.go +++ b/pkg/skaffold/build/gcb/cloud_build.go @@ -158,32 +158,6 @@ watch: return newTag, nil } -func (b *Builder) buildDescription(artifact *v1alpha2.Artifact, bucket, object string) *cloudbuild.Build { - args := append([]string{"build", "--tag", artifact.ImageName, "-f", artifact.DockerArtifact.DockerfilePath}) - args = append(args, docker.GetBuildArgs(artifact.DockerArtifact)...) - args = append(args, ".") - - return &cloudbuild.Build{ - LogsBucket: bucket, - Source: &cloudbuild.Source{ - StorageSource: &cloudbuild.StorageSource{ - Bucket: bucket, - Object: object, - }, - }, - Steps: []*cloudbuild.BuildStep{{ - Name: b.DockerImage, - Args: args, - }}, - Images: []string{artifact.ImageName}, - Options: &cloudbuild.BuildOptions{ - DiskSizeGb: b.DiskSizeGb, - MachineType: b.MachineType, - }, - Timeout: b.Timeout, - } -} - func getBuildID(op *cloudbuild.Operation) (string, error) { if op.Metadata == nil { return "", errors.New("missing Metadata in operation") diff --git a/pkg/skaffold/build/gcb/desc.go b/pkg/skaffold/build/gcb/desc.go new file mode 100644 index 00000000000..9ced4e2a5bd --- /dev/null +++ b/pkg/skaffold/build/gcb/desc.go @@ -0,0 +1,60 @@ +/* +Copyright 2018 The Skaffold Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package gcb + +import ( + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker" + "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2" + cloudbuild "google.golang.org/api/cloudbuild/v1" +) + +func (b *Builder) buildDescription(artifact *v1alpha2.Artifact, bucket, object string) *cloudbuild.Build { + var steps []*cloudbuild.BuildStep + + for _, cacheFrom := range artifact.DockerArtifact.CacheFrom { + steps = append(steps, &cloudbuild.BuildStep{ + Name: b.DockerImage, + Args: []string{"pull", cacheFrom}, + }) + } + + args := append([]string{"build", "--tag", artifact.ImageName, "-f", artifact.DockerArtifact.DockerfilePath}) + args = append(args, docker.GetBuildArgs(artifact.DockerArtifact)...) + args = append(args, ".") + + steps = append(steps, &cloudbuild.BuildStep{ + Name: b.DockerImage, + Args: args, + }) + + return &cloudbuild.Build{ + LogsBucket: bucket, + Source: &cloudbuild.Source{ + StorageSource: &cloudbuild.StorageSource{ + Bucket: bucket, + Object: object, + }, + }, + Steps: steps, + Images: []string{artifact.ImageName}, + Options: &cloudbuild.BuildOptions{ + DiskSizeGb: b.DiskSizeGb, + MachineType: b.MachineType, + }, + Timeout: b.Timeout, + } +} diff --git a/pkg/skaffold/build/gcb/cloud_build_test.go b/pkg/skaffold/build/gcb/desc_test.go similarity index 69% rename from pkg/skaffold/build/gcb/cloud_build_test.go rename to pkg/skaffold/build/gcb/desc_test.go index 0e1c3062b7b..ab51863302a 100644 --- a/pkg/skaffold/build/gcb/cloud_build_test.go +++ b/pkg/skaffold/build/gcb/desc_test.go @@ -71,3 +71,35 @@ func TestBuildDescription(t *testing.T) { testutil.CheckDeepEqual(t, expected, *desc) } + +func TestPullCacheFrom(t *testing.T) { + artifact := &v1alpha2.Artifact{ + ImageName: "nginx", + ArtifactType: v1alpha2.ArtifactType{ + DockerArtifact: &v1alpha2.DockerArtifact{ + DockerfilePath: "Dockerfile", + CacheFrom: []string{"from/image1", "from/image2"}, + }, + }, + } + + builder := Builder{ + GoogleCloudBuild: &v1alpha2.GoogleCloudBuild{ + DockerImage: "docker/docker", + }, + } + desc := builder.buildDescription(artifact, "bucket", "object") + + expected := []*cloudbuild.BuildStep{{ + Name: "docker/docker", + Args: []string{"pull", "from/image1"}, + }, { + Name: "docker/docker", + Args: []string{"pull", "from/image2"}, + }, { + Name: "docker/docker", + Args: []string{"build", "--tag", "nginx", "-f", "Dockerfile", "--cache-from", "from/image1", "--cache-from", "from/image2", "."}, + }} + + testutil.CheckDeepEqual(t, expected, desc.Steps) +}