This repository has been archived by the owner on Jul 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker.go
444 lines (398 loc) · 13.1 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
package builder
import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/url"
"os"
"path/filepath"
"strings"
"time"
dockercmd "github.com/docker/docker/builder/command"
"github.com/docker/docker/builder/parser"
docker "github.com/fsouza/go-dockerclient"
"github.com/golang/glog"
kapi "k8s.io/kubernetes/pkg/api"
"github.com/openshift/origin/pkg/build/api"
"github.com/openshift/origin/pkg/build/builder/cmd/dockercfg"
"github.com/openshift/source-to-image/pkg/git"
"github.com/openshift/source-to-image/pkg/tar"
"github.com/openshift/source-to-image/pkg/util"
)
const (
// urlCheckTimeout is the timeout used to check the source URL
// If fetching the URL exceeds the timeout, then the build will
// not proceed further and stop
urlCheckTimeout = 16 * time.Second
// noOutputDefaultTag is used as the tag name for docker built images that will
// not be pushed because no Output value was defined in the BuildConfig
noOutputDefaultTag = "no_repo/no_output_default_tag"
)
// DockerBuilder builds Docker images given a git repository URL
type DockerBuilder struct {
dockerClient DockerClient
git git.Git
tar tar.Tar
build *api.Build
urlTimeout time.Duration
}
// MetaInstuction represent an Docker instruction used for adding metadata
// to Dockerfile
type MetaInstruction string
const (
// Label represents the LABEL Docker instruction
Label MetaInstruction = "LABEL"
// Env represents the ENV Docker instruction
Env MetaInstruction = "ENV"
)
// NewDockerBuilder creates a new instance of DockerBuilder
func NewDockerBuilder(dockerClient DockerClient, build *api.Build) *DockerBuilder {
return &DockerBuilder{
dockerClient: dockerClient,
build: build,
git: git.New(),
tar: tar.New(),
urlTimeout: urlCheckTimeout,
}
}
// Build executes a Docker build
func (d *DockerBuilder) Build() error {
buildDir, err := ioutil.TempDir("", "docker-build")
if err != nil {
return err
}
if err = d.fetchSource(buildDir); err != nil {
return err
}
if err = d.addBuildParameters(buildDir); err != nil {
return err
}
glog.V(4).Infof("Starting Docker build from %s/%s BuildConfig ...", d.build.Namespace, d.build.Name)
var push bool
// if there is no output target, set one up so the docker build logic
// will still work, but we won't push it at the end.
if d.build.Spec.Output.To == nil || len(d.build.Spec.Output.To.Name) == 0 {
d.build.Spec.Output.To = &kapi.ObjectReference{
Kind: "DockerImage",
Name: noOutputDefaultTag,
}
push = false
} else {
push = true
}
if err = d.dockerBuild(buildDir); err != nil {
return err
}
defer removeImage(d.dockerClient, d.build.Spec.Output.To.Name)
if push {
// Get the Docker push authentication
pushAuthConfig, authPresent := dockercfg.NewHelper().GetDockerAuth(
d.build.Spec.Output.To.Name,
dockercfg.PushAuthType,
)
if authPresent {
glog.Infof("Using provided push secret for pushing %s image", d.build.Spec.Output.To.Name)
}
glog.Infof("Pushing %s image ...", d.build.Spec.Output.To.Name)
if err := pushImage(d.dockerClient, d.build.Spec.Output.To.Name, pushAuthConfig); err != nil {
return fmt.Errorf("Failed to push image: %v", err)
}
glog.Infof("Successfully pushed %s", d.build.Spec.Output.To.Name)
}
return nil
}
// checkSourceURI performs a check on the URI associated with the build
// to make sure that it is live before proceeding with the build.
func (d *DockerBuilder) checkSourceURI() error {
rawurl := d.build.Spec.Source.Git.URI
if !d.git.ValidCloneSpec(rawurl) {
return fmt.Errorf("Invalid git source url: %s", rawurl)
}
if strings.HasPrefix(rawurl, "git://") || strings.HasPrefix(rawurl, "git@") {
return nil
}
if !strings.HasPrefix(rawurl, "http://") && !strings.HasPrefix(rawurl, "https://") {
rawurl = fmt.Sprintf("https://%s", rawurl)
}
srcURL, err := url.Parse(rawurl)
if err != nil {
return err
}
host := srcURL.Host
if strings.Index(host, ":") == -1 {
switch srcURL.Scheme {
case "http":
host += ":80"
case "https":
host += ":443"
}
}
dialer := net.Dialer{Timeout: d.urlTimeout}
conn, err := dialer.Dial("tcp", host)
if err != nil {
return err
}
return conn.Close()
}
// fetchSource retrieves the git source from the repository. If a commit ID
// is included in the build revision, that commit ID is checked out. Otherwise
// if a ref is included in the source definition, that ref is checked out.
func (d *DockerBuilder) fetchSource(dir string) error {
if err := d.checkSourceURI(); err != nil {
return err
}
origProxy := make(map[string]string)
var setHttp, setHttps bool
// set the http proxy to be used by the git clone performed by S2I
if len(d.build.Spec.Source.Git.HTTPSProxy) != 0 {
glog.V(2).Infof("Setting https proxy variables for Git to %s", d.build.Spec.Source.Git.HTTPSProxy)
origProxy["HTTPS_PROXY"] = os.Getenv("HTTPS_PROXY")
origProxy["https_proxy"] = os.Getenv("https_proxy")
os.Setenv("HTTPS_PROXY", d.build.Spec.Source.Git.HTTPSProxy)
os.Setenv("https_proxy", d.build.Spec.Source.Git.HTTPSProxy)
setHttps = true
}
if len(d.build.Spec.Source.Git.HTTPProxy) != 0 {
glog.V(2).Infof("Setting http proxy variables for Git to %s", d.build.Spec.Source.Git.HTTPProxy)
origProxy["HTTP_PROXY"] = os.Getenv("HTTP_PROXY")
origProxy["http_proxy"] = os.Getenv("http_proxy")
os.Setenv("HTTP_PROXY", d.build.Spec.Source.Git.HTTPProxy)
os.Setenv("http_proxy", d.build.Spec.Source.Git.HTTPProxy)
setHttp = true
}
defer func() {
// reset http proxy env variables to original value
if setHttps {
glog.V(4).Infof("Resetting HTTPS_PROXY variable for Git to %s", origProxy["HTTPS_PROXY"])
os.Setenv("HTTPS_PROXY", origProxy["HTTPS_PROXY"])
glog.V(4).Infof("Resetting https_proxy variable for Git to %s", origProxy["https_proxy"])
os.Setenv("https_proxy", origProxy["https_proxy"])
}
if setHttp {
glog.V(4).Infof("Resetting HTTP_PROXY variable for Git to %s", origProxy["HTTP_PROXY"])
os.Setenv("HTTP_PROXY", origProxy["HTTP_PROXY"])
glog.V(4).Infof("Resetting http_proxy variable for Git to %s", origProxy["http_proxy"])
os.Setenv("http_proxy", origProxy["http_proxy"])
}
}()
if err := d.git.Clone(d.build.Spec.Source.Git.URI, dir); err != nil {
return err
}
if d.build.Spec.Source.Git.Ref == "" &&
(d.build.Spec.Revision == nil ||
d.build.Spec.Revision.Git == nil ||
d.build.Spec.Revision.Git.Commit == "") {
return nil
}
if d.build.Spec.Revision != nil &&
d.build.Spec.Revision.Git != nil &&
d.build.Spec.Revision.Git.Commit != "" {
return d.git.Checkout(dir, d.build.Spec.Revision.Git.Commit)
}
return d.git.Checkout(dir, d.build.Spec.Source.Git.Ref)
}
// addBuildParameters checks if a Image is set to replace the default base image.
// If that's the case then change the Dockerfile to make the build with the given image.
// Also append the environment variables and labels in the Dockerfile.
func (d *DockerBuilder) addBuildParameters(dir string) error {
dockerfilePath := filepath.Join(dir, "Dockerfile")
if d.build.Spec.Strategy.DockerStrategy != nil && len(d.build.Spec.Source.ContextDir) > 0 {
dockerfilePath = filepath.Join(dir, d.build.Spec.Source.ContextDir, "Dockerfile")
}
fileStat, err := os.Lstat(dockerfilePath)
if err != nil {
return err
}
filePerm := fileStat.Mode()
fileData, err := ioutil.ReadFile(dockerfilePath)
if err != nil {
return err
}
var newFileData string
if d.build.Spec.Strategy.DockerStrategy.From != nil && d.build.Spec.Strategy.DockerStrategy.From.Kind == "DockerImage" {
newFileData, err = replaceValidCmd(dockercmd.From, d.build.Spec.Strategy.DockerStrategy.From.Name, fileData)
if err != nil {
return err
}
} else {
newFileData = newFileData + string(fileData)
}
envVars := getBuildEnvVars(d.build)
newFileData = appendMetadata(Env, newFileData, envVars)
labels := map[string]string{}
sourceInfo := d.git.GetInfo(dir)
if len(d.build.Spec.Source.ContextDir) > 0 {
sourceInfo.ContextDir = d.build.Spec.Source.ContextDir
}
labels = util.GenerateLabelsFromSourceInfo(labels, sourceInfo, api.DefaultDockerLabelNamespace)
newFileData = appendMetadata(Label, newFileData, labels)
if ioutil.WriteFile(dockerfilePath, []byte(newFileData), filePerm); err != nil {
return err
}
return nil
}
// appendMetadata appends a Docker instruction that adds metadata values
// to a string containing a valid Dockerfile.
func appendMetadata(inst MetaInstruction, fileData string, envVars map[string]string) string {
if !strings.HasSuffix(fileData, "\n") {
fileData += "\n"
}
first := true
for k, v := range envVars {
if first {
fileData += fmt.Sprintf("%s %s=\"%s\"", inst, k, v)
first = false
} else {
fileData += fmt.Sprintf(" \\\n\t%s=\"%s\"", k, v)
}
}
fileData += "\n"
return fileData
}
// invalidCmdErr represents an error returned from replaceValidCmd
// when an invalid Dockerfile command has been passed to
// replaceValidCmd
var invalidCmdErr = errors.New("invalid Dockerfile command")
// replaceCmdErr represents an error returned from replaceValidCmd
// when a command which has more than one valid occurrences inside
// a Dockerfile has been passed or the specified command cannot
// be found
var replaceCmdErr = errors.New("cannot replace given Dockerfile command")
// replaceValidCmd replaces the valid occurrence of a command
// in a Dockerfile with the given replaceArgs
func replaceValidCmd(cmd, replaceArgs string, fileData []byte) (string, error) {
if _, ok := dockercmd.Commands[cmd]; !ok {
return "", invalidCmdErr
}
buf := bytes.NewBuffer(fileData)
// Parse with Docker parser
node, err := parser.Parse(buf)
if err != nil {
return "", errors.New("cannot parse Dockerfile: " + err.Error())
}
pos := traverseAST(cmd, node)
if pos == 0 {
return "", replaceCmdErr
}
// Re-initialize the buffer
buf = bytes.NewBuffer(fileData)
var newFileData string
var index int
var replaceNextLn bool
for {
line, err := buf.ReadString('\n')
if err != nil && err != io.EOF {
return "", err
}
line = strings.TrimSpace(line)
// The current line starts with the specified command (cmd)
if strings.HasPrefix(strings.ToUpper(line), strings.ToUpper(cmd)) {
index++
// The current line finishes on a backslash.
// All we need to do is replace the next line
// with our specified replaceArgs
if line[len(line)-1:] == "\\" && index == pos {
replaceNextLn = true
args := strings.Split(line, " ")
if len(args) > 2 {
// Keep just our Dockerfile command and the backslash
newFileData += args[0] + " \\" + "\n"
} else {
newFileData += line + "\n"
}
continue
}
// Normal ending line
if index == pos {
line = fmt.Sprintf("%s %s", strings.ToUpper(cmd), replaceArgs)
}
}
// Previous line ended on a backslash
// This line contains command arguments
if replaceNextLn {
if line[len(line)-1:] == "\\" {
// Ignore all successive lines terminating on a backslash
// since they all are going to be replaced by replaceArgs
continue
}
replaceNextLn = false
line = replaceArgs
}
if err == io.EOF {
// Otherwise, the new Dockerfile will have one newline
// more in the end
newFileData += line
break
}
newFileData += line + "\n"
}
// Parse output for validation
buf = bytes.NewBuffer([]byte(newFileData))
if _, err := parser.Parse(buf); err != nil {
return "", errors.New("cannot parse new Dockerfile: " + err.Error())
}
return newFileData, nil
}
// traverseAST traverses the Abstract Syntax Tree output
// from the Docker parser and returns the valid position
// of the command it was requested to look for.
//
// Note that this function is intended to be used with
// Dockerfile commands that should be specified only once
// in a Dockerfile (FROM, CMD, ENTRYPOINT)
func traverseAST(cmd string, node *parser.Node) int {
switch cmd {
case dockercmd.From, dockercmd.Entrypoint, dockercmd.Cmd:
default:
return 0
}
index := 0
if node.Value == cmd {
index++
}
for _, n := range node.Children {
index += traverseAST(cmd, n)
}
if node.Next != nil {
for n := node.Next; n != nil; n = n.Next {
if len(n.Children) > 0 {
index += traverseAST(cmd, n)
} else if n.Value == cmd {
index++
}
}
}
return index
}
// setupPullSecret provides a Docker authentication configuration when the
// PullSecret is specified.
func (d *DockerBuilder) setupPullSecret() (*docker.AuthConfigurations, error) {
if len(os.Getenv(dockercfg.PullAuthType)) == 0 {
return nil, nil
}
r, err := os.Open(os.Getenv(dockercfg.PullAuthType))
if err != nil {
return nil, fmt.Errorf("'%s': %s", os.Getenv(dockercfg.PullAuthType), err)
}
return docker.NewAuthConfigurations(r)
}
// dockerBuild performs a docker build on the source that has been retrieved
func (d *DockerBuilder) dockerBuild(dir string) error {
var noCache bool
var forcePull bool
if d.build.Spec.Strategy.DockerStrategy != nil {
if d.build.Spec.Source.ContextDir != "" {
dir = filepath.Join(dir, d.build.Spec.Source.ContextDir)
}
noCache = d.build.Spec.Strategy.DockerStrategy.NoCache
forcePull = d.build.Spec.Strategy.DockerStrategy.ForcePull
}
auth, err := d.setupPullSecret()
if err != nil {
return err
}
return buildImage(d.dockerClient, dir, noCache, d.build.Spec.Output.To.Name, d.tar, auth, forcePull)
}