Skip to content

Commit cd93a76

Browse files
committed
Simplify docker related code.
Now that `skaffold docker` commands are removed, the code can be simplified. Signed-off-by: David Gageot <[email protected]>
1 parent 9e4c746 commit cd93a76

File tree

12 files changed

+75
-90
lines changed

12 files changed

+75
-90
lines changed

pkg/skaffold/bazel/bazel.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ func query(target string) string {
3535
}
3636

3737
// GetDependencies finds the sources dependencies for the given bazel artifact.
38-
func GetDependencies(a *v1alpha2.Artifact) ([]string, error) {
39-
cmd := exec.Command("bazel", "query", query(a.BazelArtifact.BuildTarget), "--noimplicit_deps", "--order_output=no")
40-
cmd.Dir = a.Workspace
38+
// All paths are relative to the workspace.
39+
func GetDependencies(workspace string, a *v1alpha2.BazelArtifact) ([]string, error) {
40+
cmd := exec.Command("bazel", "query", query(a.BuildTarget), "--noimplicit_deps", "--order_output=no")
41+
cmd.Dir = workspace
4142
stdout, err := util.RunCmdOut(cmd)
4243
if err != nil {
4344
return nil, errors.Wrap(err, "getting bazel dependencies")
@@ -59,7 +60,7 @@ func GetDependencies(a *v1alpha2.Artifact) ([]string, error) {
5960
deps = append(deps, depToPath(l))
6061
}
6162

62-
if _, err := os.Stat(filepath.Join(a.Workspace, "WORKSPACE")); err == nil {
63+
if _, err := os.Stat(filepath.Join(workspace, "WORKSPACE")); err == nil {
6364
deps = append(deps, "WORKSPACE")
6465
}
6566

pkg/skaffold/build/deps.go

-41
This file was deleted.

pkg/skaffold/build/gcb/container_builder.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func (b *Builder) buildArtifact(ctx context.Context, out io.Writer, tagger tag.T
8787
}
8888

8989
fmt.Fprintf(out, "Pushing code to gs://%s/%s\n", cbBucket, buildObject)
90-
if err := docker.UploadContextToGCS(ctx, artifact.Workspace, artifact.DockerArtifact.DockerfilePath, cbBucket, buildObject); err != nil {
90+
if err := docker.UploadContextToGCS(ctx, artifact.Workspace, artifact.DockerArtifact, cbBucket, buildObject); err != nil {
9191
return "", errors.Wrap(err, "uploading source tarball")
9292
}
9393

pkg/skaffold/build/kaniko/run.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func runKaniko(ctx context.Context, out io.Writer, artifact *v1alpha2.Artifact,
4444

4545
initialTag := util.RandomID()
4646
tarName := fmt.Sprintf("context-%s.tar.gz", initialTag)
47-
if err := docker.UploadContextToGCS(ctx, artifact.Workspace, dockerfilePath, cfg.GCSBucket, tarName); err != nil {
47+
if err := docker.UploadContextToGCS(ctx, artifact.Workspace, artifact.DockerArtifact, cfg.GCSBucket, tarName); err != nil {
4848
return "", errors.Wrap(err, "uploading tar to gcs")
4949
}
5050
defer gcsDelete(ctx, cfg.GCSBucket, tarName)

pkg/skaffold/build/local/docker.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import (
2626
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
2727
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
2828
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
29-
"github.com/docker/docker/api/types"
3029
"github.com/pkg/errors"
3130
)
3231

@@ -56,13 +55,7 @@ func (b *Builder) buildDocker(ctx context.Context, out io.Writer, workspace stri
5655
return "", errors.Wrap(err, "running build")
5756
}
5857
} else {
59-
err := docker.RunBuild(ctx, out, b.api, workspace, types.ImageBuildOptions{
60-
Tags: []string{initialTag},
61-
Dockerfile: a.DockerfilePath,
62-
BuildArgs: a.BuildArgs,
63-
CacheFrom: a.CacheFrom,
64-
})
65-
if err != nil {
58+
if err := docker.BuildArtifact(ctx, out, b.api, workspace, a, initialTag); err != nil {
6659
return "", errors.Wrap(err, "running build")
6760
}
6861
}

pkg/skaffold/docker/context.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"strings"
2424

2525
cstorage "cloud.google.com/go/storage"
26+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
2627
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
2728
"github.com/pkg/errors"
2829
)
@@ -39,37 +40,41 @@ func NormalizeDockerfilePath(context, dockerfile string) (string, error) {
3940
return filepath.Abs(dockerfile)
4041
}
4142

42-
func CreateDockerTarContext(buildArgs map[string]*string, w io.Writer, context, dockerfilePath string) error {
43-
paths, err := GetDependencies(buildArgs, context, dockerfilePath)
43+
func CreateDockerTarContext(w io.Writer, workspace string, a *v1alpha2.DockerArtifact) error {
44+
paths, err := GetDependencies(workspace, a)
4445
if err != nil {
4546
return errors.Wrap(err, "getting relative tar paths")
4647
}
47-
if err := util.CreateTar(w, context, paths); err != nil {
48+
49+
if err := util.CreateTar(w, workspace, paths); err != nil {
4850
return errors.Wrap(err, "creating tar gz")
4951
}
52+
5053
return nil
5154
}
5255

53-
func CreateDockerTarGzContext(buildArgs map[string]*string, w io.Writer, context, dockerfilePath string) error {
54-
paths, err := GetDependencies(buildArgs, context, dockerfilePath)
56+
func CreateDockerTarGzContext(w io.Writer, workspace string, a *v1alpha2.DockerArtifact) error {
57+
paths, err := GetDependencies(workspace, a)
5558
if err != nil {
5659
return errors.Wrap(err, "getting relative tar paths")
5760
}
58-
if err := util.CreateTarGz(w, context, paths); err != nil {
61+
62+
if err := util.CreateTarGz(w, workspace, paths); err != nil {
5963
return errors.Wrap(err, "creating tar gz")
6064
}
65+
6166
return nil
6267
}
6368

64-
func UploadContextToGCS(ctx context.Context, context, dockerfilePath, bucket, objectName string) error {
69+
func UploadContextToGCS(ctx context.Context, workspace string, a *v1alpha2.DockerArtifact, bucket, objectName string) error {
6570
c, err := cstorage.NewClient(ctx)
6671
if err != nil {
6772
return err
6873
}
6974
defer c.Close()
7075

7176
w := c.Bucket(bucket).Object(objectName).NewWriter(ctx)
72-
if err := CreateDockerTarGzContext(map[string]*string{}, w, context, dockerfilePath); err != nil {
77+
if err := CreateDockerTarGzContext(w, workspace, a); err != nil {
7378
return errors.Wrap(err, "uploading targz to google storage")
7479
}
7580
return w.Close()

pkg/skaffold/docker/context_test.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"path/filepath"
2525
"testing"
2626

27+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
2728
"github.com/GoogleContainerTools/skaffold/testutil"
2829
)
2930

@@ -36,6 +37,11 @@ func TestDockerContext(t *testing.T) {
3637
RetrieveImage = retrieveImage
3738
}()
3839

40+
artifact := &v1alpha2.DockerArtifact{
41+
DockerfilePath: "Dockerfile",
42+
BuildArgs: map[string]*string{},
43+
}
44+
3945
os.Mkdir(filepath.Join(tmpDir, "files"), 0750)
4046
ioutil.WriteFile(filepath.Join(tmpDir, "files", "ignored.txt"), []byte(""), 0644)
4147
ioutil.WriteFile(filepath.Join(tmpDir, "files", "included.txt"), []byte(""), 0644)
@@ -45,7 +51,7 @@ func TestDockerContext(t *testing.T) {
4551
ioutil.WriteFile(filepath.Join(tmpDir, "alsoignored.txt"), []byte(""), 0644)
4652
reader, writer := io.Pipe()
4753
go func() {
48-
err := CreateDockerTarContext(map[string]*string{}, writer, tmpDir, "Dockerfile")
54+
err := CreateDockerTarContext(writer, tmpDir, artifact)
4955
if err != nil {
5056
writer.CloseWithError(err)
5157
} else {

pkg/skaffold/docker/image.go

+12-6
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,17 @@ import (
3939
"github.com/sirupsen/logrus"
4040
)
4141

42-
// RunBuild performs a docker build and returns nothing
43-
func RunBuild(ctx context.Context, out io.Writer, cli APIClient, workspace string, opts types.ImageBuildOptions) error {
44-
logrus.Debugf("Running docker build: context: %s, dockerfile: %s", workspace, opts.Dockerfile)
42+
// BuildArtifact performs a docker build and returns nothing
43+
func BuildArtifact(ctx context.Context, out io.Writer, cli APIClient, workspace string, a *v1alpha2.DockerArtifact, initialTag string) error {
44+
logrus.Debugf("Running docker build: context: %s, dockerfile: %s", workspace, a.DockerfilePath)
4545

4646
// Like `docker build`, we ignore the errors
4747
// See https://github.com/docker/cli/blob/75c1bb1f33d7cedbaf48404597d5bf9818199480/cli/command/image/build.go#L364
4848
authConfigs, _ := DefaultAuthHelper.GetAllAuthConfigs()
49-
opts.AuthConfigs = authConfigs
5049

5150
buildCtx, buildCtxWriter := io.Pipe()
5251
go func() {
53-
err := CreateDockerTarContext(opts.BuildArgs, buildCtxWriter, workspace, opts.Dockerfile)
52+
err := CreateDockerTarContext(buildCtxWriter, workspace, a)
5453
if err != nil {
5554
buildCtxWriter.CloseWithError(errors.Wrap(err, "creating docker context"))
5655
return
@@ -61,11 +60,18 @@ func RunBuild(ctx context.Context, out io.Writer, cli APIClient, workspace strin
6160
progressOutput := streamformatter.NewProgressOutput(out)
6261
body := progress.NewProgressReader(buildCtx, progressOutput, 0, "", "Sending build context to Docker daemon")
6362

64-
resp, err := cli.ImageBuild(ctx, body, opts)
63+
resp, err := cli.ImageBuild(ctx, body, types.ImageBuildOptions{
64+
Tags: []string{initialTag},
65+
Dockerfile: a.DockerfilePath,
66+
BuildArgs: a.BuildArgs,
67+
CacheFrom: a.CacheFrom,
68+
AuthConfigs: authConfigs,
69+
})
6570
if err != nil {
6671
return errors.Wrap(err, "docker build")
6772
}
6873
defer resp.Body.Close()
74+
6975
return StreamDockerMessages(out, resp.Body)
7076
}
7177

pkg/skaffold/docker/image_test.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,11 @@ import (
2222
"fmt"
2323
"io/ioutil"
2424
"os"
25-
"path/filepath"
2625
"testing"
2726

2827
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
2928
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
3029
"github.com/GoogleContainerTools/skaffold/testutil"
31-
"github.com/docker/docker/api/types"
3230
"github.com/google/go-cmp/cmp"
3331
)
3432

@@ -96,7 +94,7 @@ func TestRunPush(t *testing.T) {
9694
}
9795
}
9896

99-
func TestRunBuild(t *testing.T) {
97+
func TestRunBuildArtifact(t *testing.T) {
10098
var tests = []testImageAPI{
10199
{
102100
description: "build",
@@ -122,16 +120,9 @@ func TestRunBuild(t *testing.T) {
122120
}
123121
for _, test := range tests {
124122
t.Run(test.description, func(t *testing.T) {
125-
tmp, cleanup := testutil.TempDir(t)
126-
defer cleanup()
127-
128-
ioutil.WriteFile(filepath.Join(tmp, "Dockerfile"), []byte{}, os.ModePerm)
129-
130123
api := testutil.NewFakeImageAPIClient(test.tagToImageID, test.testOpts)
131-
err := RunBuild(context.Background(), ioutil.Discard, api, tmp, types.ImageBuildOptions{
132-
Dockerfile: "Dockerfile",
133-
Tags: []string{"finalimage"},
134-
})
124+
125+
err := BuildArtifact(context.Background(), ioutil.Discard, api, ".", &v1alpha2.DockerArtifact{}, "finalimage")
135126

136127
testutil.CheckError(t, test.shouldErr, err)
137128
})

pkg/skaffold/docker/parse.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strings"
2727
"sync"
2828

29+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
2930
"github.com/docker/docker/builder/dockerignore"
3031
"github.com/docker/docker/pkg/fileutils"
3132
"github.com/google/go-containerregistry/pkg/v1"
@@ -167,13 +168,15 @@ func readDockerfile(workspace, absDockerfilePath string, buildArgs map[string]*s
167168
return deps, nil
168169
}
169170

170-
func GetDependencies(buildArgs map[string]*string, workspace, dockerfilePath string) ([]string, error) {
171-
absDockerfilePath, err := NormalizeDockerfilePath(workspace, dockerfilePath)
171+
// GetDependencies finds the sources dependencies for the given docker artifact.
172+
// All paths are relative to the workspace.
173+
func GetDependencies(workspace string, a *v1alpha2.DockerArtifact) ([]string, error) {
174+
absDockerfilePath, err := NormalizeDockerfilePath(workspace, a.DockerfilePath)
172175
if err != nil {
173176
return nil, errors.Wrap(err, "normalizing dockerfile path")
174177
}
175178

176-
deps, err := readDockerfile(workspace, absDockerfilePath, buildArgs)
179+
deps, err := readDockerfile(workspace, absDockerfilePath, a.BuildArgs)
177180
if err != nil {
178181
return nil, err
179182
}
@@ -225,8 +228,8 @@ func GetDependencies(buildArgs map[string]*string, workspace, dockerfilePath str
225228
}
226229

227230
// Always add dockerfile even if it's .dockerignored. The daemon will need it anyways.
228-
if !filepath.IsAbs(dockerfilePath) {
229-
files[dockerfilePath] = true
231+
if !filepath.IsAbs(a.DockerfilePath) {
232+
files[a.DockerfilePath] = true
230233
} else {
231234
files[absDockerfilePath] = true
232235
}

pkg/skaffold/docker/parse_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"path/filepath"
2424
"testing"
2525

26+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
2627
"github.com/GoogleContainerTools/skaffold/testutil"
2728
"github.com/google/go-containerregistry/pkg/v1"
2829
)
@@ -307,7 +308,11 @@ func TestGetDependencies(t *testing.T) {
307308
ioutil.WriteFile(filepath.Join(workspace, ".dockerignore"), []byte(test.ignore), 0644)
308309
}
309310

310-
deps, err := GetDependencies(test.buildArgs, workspace, "Dockerfile")
311+
deps, err := GetDependencies(workspace, &v1alpha2.DockerArtifact{
312+
BuildArgs: test.buildArgs,
313+
DockerfilePath: "Dockerfile",
314+
})
315+
311316
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, deps)
312317
})
313318
}

pkg/skaffold/watch/artifacts.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ package watch
1818

1919
import (
2020
"context"
21+
"fmt"
2122
"path/filepath"
2223
"strings"
2324
"time"
2425

25-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
26+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/bazel"
27+
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
2628
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
2729
"github.com/pkg/errors"
2830
"github.com/sirupsen/logrus"
@@ -112,7 +114,7 @@ func matchesAny(artifact *v1alpha2.Artifact, files []string) (bool, error) {
112114
return false, nil
113115
}
114116

115-
deps, err := build.DependenciesForArtifact(artifact)
117+
deps, err := dependenciesForArtifact(artifact)
116118
if err != nil {
117119
return false, errors.Wrap(err, "getting dependencies for artifact")
118120
}
@@ -128,6 +130,20 @@ func matchesAny(artifact *v1alpha2.Artifact, files []string) (bool, error) {
128130
return false, nil
129131
}
130132

133+
// All paths are relative to the workspace.
134+
func dependenciesForArtifact(a *v1alpha2.Artifact) ([]string, error) {
135+
switch {
136+
case a.DockerArtifact != nil:
137+
return docker.GetDependencies(a.Workspace, a.DockerArtifact)
138+
139+
case a.BazelArtifact != nil:
140+
return bazel.GetDependencies(a.Workspace, a.BazelArtifact)
141+
142+
default:
143+
return nil, fmt.Errorf("undefined artifact type: %+v", a.ArtifactType)
144+
}
145+
}
146+
131147
// inWorkspace filters out the paths that are not in the artifact's workspace.
132148
func inWorkspace(artifact *v1alpha2.Artifact, files []string) ([]string, error) {
133149
var inWorkspace []string

0 commit comments

Comments
 (0)