Skip to content
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

Remove PreBuiltImageBuilder and make it a function instead. #1986

Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions examples/kustomize/patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ spec:
command:
- sleep
- "3600"
- name: kustomize-test1
image: index.docker.io/library/nginx
3 changes: 2 additions & 1 deletion integration/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ func TestDeploy(t *testing.T) {
ns, client, deleteNs := SetupNamespace(t)
defer deleteNs()

skaffold.Deploy("--images", "index.docker.io/library/busybox:1").InDir("examples/kustomize").InNs(ns.Name).RunOrFail(t)
skaffold.Deploy("--images", "index.docker.io/library/busybox:1", "--images", "index.docker.io/library/nginx:2").InDir("examples/kustomize").InNs(ns.Name).RunOrFail(t)

dep := client.GetDeployment("kustomize-test")
testutil.CheckDeepEqual(t, "index.docker.io/library/busybox:1", dep.Spec.Template.Spec.Containers[0].Image)
testutil.CheckDeepEqual(t, "index.docker.io/library/nginx:2", dep.Spec.Template.Spec.Containers[1].Image)

skaffold.Delete().InDir("examples/kustomize").InNs(ns.Name).RunOrFail(t)
}
2 changes: 2 additions & 0 deletions integration/examples/kustomize/patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ spec:
command:
- sleep
- "3600"
- name: kustomize-test1
image: index.docker.io/library/nginx
95 changes: 0 additions & 95 deletions pkg/skaffold/build/prebuilt.go

This file was deleted.

38 changes: 38 additions & 0 deletions pkg/skaffold/runner/prebuilt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
Copyright 2019 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 runner

import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
)

func convertImagesToArtifact(images []string) ([]build.Artifact, error) {
var artifacts []build.Artifact

for _, tag := range images {
parsed, err := docker.ParseReference(tag)
if err != nil {
return nil, err
}
artifacts = append(artifacts, build.Artifact{
ImageName: parsed.BaseName,
Tag: tag,
})
}
return artifacts, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,20 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package build
package runner

import (
"context"
"io/ioutil"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
runcontext "github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/context"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestPreBuiltImagesBuilder(t *testing.T) {
var tests = []struct {
description string
images []string
artifacts []*latest.Artifact
expected []Artifact
expected []build.Artifact
shouldErr bool
}{
{
Expand All @@ -41,67 +36,27 @@ func TestPreBuiltImagesBuilder(t *testing.T) {
"skaffold/image1:tag1",
"skaffold/image2:tag2",
},
artifacts: []*latest.Artifact{
{ImageName: "skaffold/image1"},
{ImageName: "skaffold/image2"},
},
expected: []Artifact{
{ImageName: "skaffold/image1", Tag: "skaffold/image1:tag1"},
{ImageName: "skaffold/image2", Tag: "skaffold/image2:tag2"},
},
},
{
description: "images in reverse order",
images: []string{
"skaffold/image2:tag2",
"skaffold/image1:tag1",
},
artifacts: []*latest.Artifact{
{ImageName: "skaffold/image1"},
{ImageName: "skaffold/image2"},
},
expected: []Artifact{
expected: []build.Artifact{
{ImageName: "skaffold/image1", Tag: "skaffold/image1:tag1"},
{ImageName: "skaffold/image2", Tag: "skaffold/image2:tag2"},
},
},
{
description: "missing image",
images: []string{
"skaffold/image1:tag1",
},
artifacts: []*latest.Artifact{
{ImageName: "skaffold/image1"},
{ImageName: "skaffold/image2"},
},
shouldErr: true,
},
{
// Should we support that? It is used in kustomize example.
description: "additional image",
images: []string{
"busybox:1",
"skaffold/image1:tag1",
},
artifacts: []*latest.Artifact{
{ImageName: "skaffold/image1"},
},
expected: []Artifact{
{ImageName: "skaffold/image1", Tag: "skaffold/image1:tag1"},
expected: []build.Artifact{
{ImageName: "busybox", Tag: "busybox:1"},
{ImageName: "skaffold/image1", Tag: "skaffold/image1:tag1"},
},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
builder := NewPreBuiltImagesBuilder(&runcontext.RunContext{
Opts: &config.SkaffoldOptions{
PreBuiltImages: test.images,
},
})

bRes, err := builder.Build(context.Background(), ioutil.Discard, nil, test.artifacts)

bRes, err := convertImagesToArtifact(test.images)
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, bRes)
})
}
Expand Down
13 changes: 9 additions & 4 deletions pkg/skaffold/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,6 @@ func NewForConfig(opts *config.SkaffoldOptions, cfg *latest.SkaffoldConfig) (*Sk

func getBuilder(runCtx *runcontext.RunContext) (build.Builder, error) {
switch {
case len(runCtx.Opts.PreBuiltImages) > 0:
logrus.Debugln("Using pre-built images")
return build.NewPreBuiltImagesBuilder(runCtx), nil

case runCtx.Cfg.Build.LocalBuild != nil:
logrus.Debugln("Using builder: local")
return local.NewBuilder(runCtx)
Expand Down Expand Up @@ -331,6 +327,15 @@ func (r *SkaffoldRunner) BuildAndTest(ctx context.Context, out io.Writer, artifa

// Deploy deploys the given artifacts
func (r *SkaffoldRunner) Deploy(ctx context.Context, out io.Writer, artifacts []build.Artifact) error {
if len(r.runCtx.Opts.PreBuiltImages) > 0 {
logrus.Debugln("Deploy using pre-built images")
pBuilts, err := convertImagesToArtifact(r.runCtx.Opts.PreBuiltImages)
if err != nil {
return errors.Wrap(err, "deploy failed")
}
// Merge current build artifacts with previously built passed in --images.
artifacts = build.MergeWithPreviousBuilds(artifacts, pBuilts)
}
err := r.Deployer.Deploy(ctx, out, artifacts, r.labellers)
r.hasDeployed = true
return err
Expand Down