-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
docker.go
197 lines (166 loc) · 6.63 KB
/
docker.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright 2020 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 docker
import (
"bytes"
"context"
"fmt"
"io"
"os"
"os/exec"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/output/log"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/platform"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/util/stringslice"
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/warnings"
)
func (b *Builder) SupportedPlatforms() platform.Matcher {
return platform.All
}
func (b *Builder) Build(ctx context.Context, out io.Writer, a *latest.Artifact, tag string, matcher platform.Matcher) (string, error) {
var pl v1.Platform
if len(matcher.Platforms) == 1 {
pl = util.ConvertToV1Platform(matcher.Platforms[0])
}
a = adjustCacheFrom(a, tag)
instrumentation.AddAttributesToCurrentSpanFromContext(ctx, map[string]string{
"BuildType": "docker",
"Context": instrumentation.PII(a.Workspace),
"Destination": instrumentation.PII(tag),
})
// Fail fast if the Dockerfile can't be found.
dockerfile, err := docker.NormalizeDockerfilePath(a.Workspace, a.DockerArtifact.DockerfilePath)
if err != nil {
return "", dockerfileNotFound(fmt.Errorf("normalizing dockerfile path: %w", err), a.ImageName)
}
if _, err := os.Stat(dockerfile); os.IsNotExist(err) {
return "", dockerfileNotFound(err, a.ImageName)
}
if err := b.pullCacheFromImages(ctx, out, a.ArtifactType.DockerArtifact, pl); err != nil {
return "", cacheFromPullErr(err, a.ImageName)
}
opts := docker.BuildOptions{Tag: tag, Mode: b.cfg.Mode(), ExtraBuildArgs: docker.ResolveDependencyImages(a.Dependencies, b.artifacts, true)}
var imageID string
// ignore useCLI boolean if buildkit is enabled since buildkit is only implemented for docker CLI at the moment in skaffold.
// we might consider a different approach in the future.
// use CLI for cross-platform builds
if b.useCLI || (b.useBuildKit != nil && *b.useBuildKit) || len(a.DockerArtifact.CliFlags) > 0 || matcher.IsCrossPlatform() {
imageID, err = b.dockerCLIBuild(ctx, output.GetUnderlyingWriter(out), a.ImageName, a.Workspace, dockerfile, a.ArtifactType.DockerArtifact, opts, pl)
} else {
imageID, err = b.localDocker.Build(ctx, out, a.Workspace, a.ImageName, a.ArtifactType.DockerArtifact, opts)
}
if err != nil {
return "", newBuildError(err, b.cfg)
}
if b.pushImages {
// TODO (tejaldesai) Remove https://github.com/GoogleContainerTools/skaffold/blob/main/pkg/skaffold/errors/err_map.go#L56
// and instead define a pushErr() method here.
return b.localDocker.Push(ctx, out, tag)
}
return imageID, nil
}
func (b *Builder) dockerCLIBuild(ctx context.Context, out io.Writer, name string, workspace string, dockerfilePath string, a *latest.DockerArtifact, opts docker.BuildOptions, pl v1.Platform) (string, error) {
args := []string{"build", workspace, "--file", dockerfilePath, "-t", opts.Tag}
imgRef, err := docker.ParseReference(opts.Tag)
if err != nil {
return "", fmt.Errorf("couldn't parse image tag: %w", err)
}
imageInfoEnv := map[string]string{
"IMAGE_REPO": imgRef.Repo,
"IMAGE_NAME": imgRef.Name,
"IMAGE_TAG": imgRef.Tag,
}
ba, err := docker.EvalBuildArgsWithEnv(b.cfg.Mode(), workspace, a.DockerfilePath, a.BuildArgs, opts.ExtraBuildArgs, imageInfoEnv)
if err != nil {
return "", fmt.Errorf("unable to evaluate build args: %w", err)
}
cliArgs, err := docker.ToCLIBuildArgs(a, ba)
if err != nil {
return "", fmt.Errorf("getting docker build args: %w", err)
}
args = append(args, cliArgs...)
if b.cfg.Prune() {
args = append(args, "--force-rm")
}
if pl.String() != "" {
args = append(args, "--platform", pl.String())
}
if b.useBuildKit != nil && *b.useBuildKit && !b.pushImages {
args = append(args, "--load")
}
cmd := exec.CommandContext(ctx, "docker", args...)
cmd.Env = append(util.OSEnviron(), b.localDocker.ExtraEnv()...)
if b.useBuildKit != nil {
if *b.useBuildKit {
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=1")
} else {
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=0")
}
} else if pl.String() != "" { // cross-platform builds require buildkit
log.Entry(ctx).Debugf("setting DOCKER_BUILDKIT=1 for docker build for artifact %q since it targets platform %q", name, pl.String())
cmd.Env = append(cmd.Env, "DOCKER_BUILDKIT=1")
}
cmd.Stdout = out
var errBuffer bytes.Buffer
stderr := io.MultiWriter(out, &errBuffer)
cmd.Stderr = stderr
if err := util.RunCmd(ctx, cmd); err != nil {
return "", tryExecFormatErr(fmt.Errorf("running build: %w", err), errBuffer)
}
return b.localDocker.ImageID(ctx, opts.Tag)
}
func (b *Builder) pullCacheFromImages(ctx context.Context, out io.Writer, a *latest.DockerArtifact, pl v1.Platform) error {
if len(a.CacheFrom) == 0 {
return nil
}
for _, image := range a.CacheFrom {
imageID, err := b.localDocker.ImageID(ctx, image)
if err != nil {
return fmt.Errorf("getting imageID for %q: %w", image, err)
}
if imageID != "" {
// already pulled
continue
}
if err := b.localDocker.Pull(ctx, out, image, pl); err != nil {
warnings.Printf("cacheFrom image %q couldn't be pulled for platform %q\n", image, pl)
}
}
return nil
}
// adjustCacheFrom returns an artifact where any cache references from the artifactImage is changed to the tagged built image name instead.
func adjustCacheFrom(a *latest.Artifact, artifactTag string) *latest.Artifact {
if os.Getenv("SKAFFOLD_DISABLE_DOCKER_CACHE_ADJUSTMENT") != "" {
// allow this behaviour to be disabled
return a
}
if !stringslice.Contains(a.DockerArtifact.CacheFrom, a.ImageName) {
return a
}
cf := make([]string, 0, len(a.DockerArtifact.CacheFrom))
for _, image := range a.DockerArtifact.CacheFrom {
if image == a.ImageName {
cf = append(cf, artifactTag)
} else {
cf = append(cf, image)
}
}
copy := *a
copy.DockerArtifact.CacheFrom = cf
return ©
}