Skip to content

Commit

Permalink
Merge pull request #1453 from dgageot/error-gcb
Browse files Browse the repository at this point in the history
Clear error message for unsupported artifact on GCB
  • Loading branch information
dgageot authored Jan 14, 2019
2 parents 40c6426 + 30b9e6f commit 967c35d
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 77 deletions.
6 changes: 5 additions & 1 deletion pkg/skaffold/build/gcb/cloud_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,16 @@ func (b *Builder) buildArtifactWithCloudBuild(ctx context.Context, out io.Writer
return "", errors.Wrap(err, "checking bucket is in correct project")
}

desc, err := b.buildDescription(artifact, cbBucket, buildObject)
if err != nil {
return "", errors.Wrap(err, "could not create build description")
}

color.Default.Fprintf(out, "Pushing code to gs://%s/%s\n", cbBucket, buildObject)
if err := docker.UploadContextToGCS(ctx, artifact.Workspace, artifact.DockerArtifact, cbBucket, buildObject); err != nil {
return "", errors.Wrap(err, "uploading source tarball")
}

desc := b.buildDescription(artifact, cbBucket, buildObject)
call := cbclient.Projects.Builds.Create(projectID, desc)
op, err := call.Context(ctx).Do()
if err != nil {
Expand Down
44 changes: 26 additions & 18 deletions pkg/skaffold/build/gcb/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,19 @@ limitations under the License.
package gcb

import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"errors"
"fmt"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

func (b *Builder) buildDescription(artifact *latest.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},
})
func (b *Builder) buildDescription(artifact *latest.Artifact, bucket, object string) (*cloudbuild.Build, error) {
steps, err := b.buildSteps(artifact)
if err != nil {
return nil, err
}

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{
Expand All @@ -56,5 +45,24 @@ func (b *Builder) buildDescription(artifact *latest.Artifact, bucket, object str
MachineType: b.MachineType,
},
Timeout: b.Timeout,
}, nil
}

func (b *Builder) buildSteps(artifact *latest.Artifact) ([]*cloudbuild.BuildStep, error) {
switch {
case artifact.DockerArtifact != nil:
return b.dockerBuildSteps(artifact.ImageName, artifact.DockerArtifact), nil

case artifact.BazelArtifact != nil:
return nil, errors.New("skaffold can't build a bazel artifact with Google Cloud Build")

case artifact.JibMavenArtifact != nil:
return nil, errors.New("skaffold can't build a jib maven artifact with Google Cloud Build")

case artifact.JibGradleArtifact != nil:
return nil, errors.New("skaffold can't build a jib gradle artifact with Google Cloud Build")

default:
return nil, fmt.Errorf("undefined artifact type: %+v", artifact.ArtifactType)
}
}
80 changes: 22 additions & 58 deletions pkg/skaffold/build/gcb/desc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,86 +20,50 @@ import (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

func TestBuildDescription(t *testing.T) {
func TestBuildJibMavenDescriptionFail(t *testing.T) {
artifact := &latest.Artifact{
ImageName: "nginx",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: "Dockerfile",
BuildArgs: map[string]*string{
"arg1": util.StringPtr("value1"),
"arg2": nil,
},
},
JibMavenArtifact: &latest.JibMavenArtifact{},
},
}

builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{
DockerImage: "docker/docker",
DiskSizeGb: 100,
MachineType: "n1-standard-1",
Timeout: "10m",
},
GoogleCloudBuild: &latest.GoogleCloudBuild{},
}
desc := builder.buildDescription(artifact, "bucket", "object")
_, err := builder.buildDescription(artifact, "bucket", "object")

expected := cloudbuild.Build{
LogsBucket: "bucket",
Source: &cloudbuild.Source{
StorageSource: &cloudbuild.StorageSource{
Bucket: "bucket",
Object: "object",
},
},
Steps: []*cloudbuild.BuildStep{{
Name: "docker/docker",
Args: []string{"build", "--tag", "nginx", "-f", "Dockerfile", "--build-arg", "arg1=value1", "--build-arg", "arg2", "."},
}},
Images: []string{artifact.ImageName},
Options: &cloudbuild.BuildOptions{
DiskSizeGb: 100,
MachineType: "n1-standard-1",
testutil.CheckError(t, true, err)
}

func TestBuildJibGradleDescriptionFail(t *testing.T) {
artifact := &latest.Artifact{
ArtifactType: latest.ArtifactType{
JibGradleArtifact: &latest.JibGradleArtifact{},
},
Timeout: "10m",
}

testutil.CheckDeepEqual(t, expected, *desc)
builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{},
}
_, err := builder.buildDescription(artifact, "bucket", "object")

testutil.CheckError(t, true, err)
}

func TestPullCacheFrom(t *testing.T) {
func TestBuildBazelDescriptionFail(t *testing.T) {
artifact := &latest.Artifact{
ImageName: "nginx",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: "Dockerfile",
CacheFrom: []string{"from/image1", "from/image2"},
},
BazelArtifact: &latest.BazelArtifact{},
},
}

builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{
DockerImage: "docker/docker",
},
GoogleCloudBuild: &latest.GoogleCloudBuild{},
}
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", "."},
}}
_, err := builder.buildDescription(artifact, "bucket", "object")

testutil.CheckDeepEqual(t, expected, desc.Steps)
testutil.CheckError(t, true, err)
}
43 changes: 43 additions & 0 deletions pkg/skaffold/build/gcb/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
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/latest"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

func (b *Builder) dockerBuildSteps(imageName string, artifact *latest.DockerArtifact) []*cloudbuild.BuildStep {
var steps []*cloudbuild.BuildStep

for _, cacheFrom := range artifact.CacheFrom {
steps = append(steps, &cloudbuild.BuildStep{
Name: b.DockerImage,
Args: []string{"pull", cacheFrom},
})
}

args := append([]string{"build", "--tag", imageName, "-f", artifact.DockerfilePath})
args = append(args, docker.GetBuildArgs(artifact)...)
args = append(args, ".")

return append(steps, &cloudbuild.BuildStep{
Name: b.DockerImage,
Args: args,
})
}
100 changes: 100 additions & 0 deletions pkg/skaffold/build/gcb/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
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 (
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/testutil"
cloudbuild "google.golang.org/api/cloudbuild/v1"
)

func TestDockerBuildDescription(t *testing.T) {
artifact := &latest.Artifact{
ImageName: "nginx",
ArtifactType: latest.ArtifactType{
DockerArtifact: &latest.DockerArtifact{
DockerfilePath: "Dockerfile",
BuildArgs: map[string]*string{
"arg1": util.StringPtr("value1"),
"arg2": nil,
},
},
},
}

builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{
DockerImage: "docker/docker",
DiskSizeGb: 100,
MachineType: "n1-standard-1",
Timeout: "10m",
},
}
desc, err := builder.buildDescription(artifact, "bucket", "object")

expected := cloudbuild.Build{
LogsBucket: "bucket",
Source: &cloudbuild.Source{
StorageSource: &cloudbuild.StorageSource{
Bucket: "bucket",
Object: "object",
},
},
Steps: []*cloudbuild.BuildStep{{
Name: "docker/docker",
Args: []string{"build", "--tag", "nginx", "-f", "Dockerfile", "--build-arg", "arg1=value1", "--build-arg", "arg2", "."},
}},
Images: []string{artifact.ImageName},
Options: &cloudbuild.BuildOptions{
DiskSizeGb: 100,
MachineType: "n1-standard-1",
},
Timeout: "10m",
}

testutil.CheckErrorAndDeepEqual(t, false, err, expected, *desc)
}

func TestPullCacheFrom(t *testing.T) {
artifact := &latest.DockerArtifact{
DockerfilePath: "Dockerfile",
CacheFrom: []string{"from/image1", "from/image2"},
}

builder := Builder{
GoogleCloudBuild: &latest.GoogleCloudBuild{
DockerImage: "docker/docker",
},
}
steps := builder.dockerBuildSteps("nginx2", artifact)

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", "nginx2", "-f", "Dockerfile", "--cache-from", "from/image1", "--cache-from", "from/image2", "."},
}}

testutil.CheckDeepEqual(t, expected, steps)
}

0 comments on commit 967c35d

Please sign in to comment.