@@ -4,13 +4,11 @@ import (
4
4
"bytes"
5
5
"errors"
6
6
"fmt"
7
- "io"
8
7
"io/ioutil"
9
8
"net/url"
10
9
"os"
11
10
"path/filepath"
12
11
"strings"
13
- "time"
14
12
15
13
utilruntime "k8s.io/kubernetes/pkg/util/runtime"
16
14
@@ -57,10 +55,9 @@ func (_ runtimeConfigValidator) ValidateConfig(config *s2iapi.Config) []validati
57
55
58
56
// S2IBuilder performs an STI build given the build object
59
57
type S2IBuilder struct {
60
- builder builderFactory
61
- validator validator
62
- gitClient GitClient
63
-
58
+ builder builderFactory
59
+ validator validator
60
+ gitClient GitClient
64
61
dockerClient DockerClient
65
62
dockerSocket string
66
63
build * api.Build
@@ -98,10 +95,6 @@ func (s *S2IBuilder) Build() error {
98
95
return errors .New ("the source to image builder must be used with the source strategy" )
99
96
}
100
97
101
- contextDir := filepath .Clean (s .build .Spec .Source .ContextDir )
102
- if contextDir == "." || contextDir == "/" {
103
- contextDir = ""
104
- }
105
98
buildDir , err := ioutil .TempDir ("" , "s2i-build" )
106
99
if err != nil {
107
100
return err
@@ -110,20 +103,6 @@ func (s *S2IBuilder) Build() error {
110
103
if err = os .MkdirAll (srcDir , os .ModePerm ); err != nil {
111
104
return err
112
105
}
113
- tmpDir := filepath .Join (buildDir , "tmp" )
114
- if err = os .MkdirAll (tmpDir , os .ModePerm ); err != nil {
115
- return err
116
- }
117
-
118
- download := & downloader {
119
- s : s ,
120
- in : os .Stdin ,
121
- timeout : initialURLCheckTimeout ,
122
-
123
- dir : srcDir ,
124
- contextDir : contextDir ,
125
- tmpDir : tmpDir ,
126
- }
127
106
128
107
var push bool
129
108
// if there is no output target, set one up so the docker build logic
@@ -134,20 +113,34 @@ func (s *S2IBuilder) Build() error {
134
113
push = true
135
114
}
136
115
pushTag := s .build .Status .OutputDockerImageReference
137
- git := s .build .Spec .Source .Git
138
-
139
- var ref string
140
- if s .build .Spec .Revision != nil && s .build .Spec .Revision .Git != nil &&
141
- len (s .build .Spec .Revision .Git .Commit ) != 0 {
142
- ref = s .build .Spec .Revision .Git .Commit
143
- } else if git != nil && len (git .Ref ) != 0 {
144
- ref = git .Ref
145
- }
146
116
147
- sourceURI := & url.URL {
148
- Scheme : "file" ,
149
- Path : srcDir ,
150
- Fragment : ref ,
117
+ // fetch source
118
+ sourceInfo , err := fetchSource (s .dockerClient , srcDir , s .build , initialURLCheckTimeout , os .Stdin , s .gitClient )
119
+ if err != nil {
120
+ s .build .Status .Reason = api .StatusReasonFetchSourceFailed
121
+ s .build .Status .Message = api .StatusMessageFetchSourceFailed
122
+ if updateErr := retryBuildStatusUpdate (s .build , s .client , nil ); updateErr != nil {
123
+ utilruntime .HandleError (fmt .Errorf ("error occured while updating the build status: %v" , updateErr ))
124
+ }
125
+ return err
126
+ }
127
+ if len (s .build .Spec .Source .ContextDir ) > 0 {
128
+ contextDir := filepath .Clean (s .build .Spec .Source .ContextDir )
129
+ if contextDir == "." || contextDir == "/" {
130
+ contextDir = ""
131
+ }
132
+ if sourceInfo != nil {
133
+ sourceInfo .ContextDir = s .build .Spec .Source .ContextDir
134
+ }
135
+ srcDir = filepath .Join (srcDir , s .build .Spec .Source .ContextDir )
136
+ }
137
+ download := & downloader {}
138
+ if sourceInfo != nil {
139
+ download .sourceInfo = & sourceInfo .SourceInfo
140
+ revision := updateBuildRevision (s .build , sourceInfo )
141
+ if updateErr := retryBuildStatusUpdate (s .build , s .client , revision ); updateErr != nil {
142
+ utilruntime .HandleError (fmt .Errorf ("error occured while updating the build status: %v" , updateErr ))
143
+ }
151
144
}
152
145
153
146
injections := s2iapi.VolumeList {}
@@ -176,10 +169,12 @@ func (s *S2IBuilder) Build() error {
176
169
incremental = * s .build .Spec .Strategy .SourceStrategy .Incremental
177
170
}
178
171
config := & s2iapi.Config {
179
- WorkingDir : buildDir ,
180
- DockerConfig : & s2iapi.DockerConfig {Endpoint : s .dockerSocket },
181
- DockerCfgPath : os .Getenv (dockercfg .PullAuthType ),
182
- LabelNamespace : api .DefaultDockerLabelNamespace ,
172
+ // Save some processing time by not cleaning up (the container will go away anyway)
173
+ PreserveWorkingDir : true ,
174
+ WorkingDir : buildDir ,
175
+ DockerConfig : & s2iapi.DockerConfig {Endpoint : s .dockerSocket },
176
+ DockerCfgPath : os .Getenv (dockercfg .PullAuthType ),
177
+ LabelNamespace : api .DefaultDockerLabelNamespace ,
183
178
184
179
ScriptsURL : s .build .Spec .Strategy .SourceStrategy .Scripts ,
185
180
@@ -191,11 +186,13 @@ func (s *S2IBuilder) Build() error {
191
186
Labels : buildLabels (s .build ),
192
187
DockerNetworkMode : getDockerNetworkMode (),
193
188
194
- Source : sourceURI .String (),
195
- Tag : buildTag ,
196
- ContextDir : s .build .Spec .Source .ContextDir ,
189
+ Source : srcDir ,
190
+ ForceCopy : true ,
191
+ Injections : injections ,
192
+
193
+ Tag : buildTag ,
194
+
197
195
CGroupLimits : s .cgLimits ,
198
- Injections : injections ,
199
196
ScriptDownloadProxyConfig : scriptDownloadProxyConfig ,
200
197
BlockOnBuild : true ,
201
198
}
@@ -256,7 +253,7 @@ func (s *S2IBuilder) Build() error {
256
253
buildInfo .FailureReason .Message ,
257
254
)
258
255
if updateErr := retryBuildStatusUpdate (s .build , s .client , nil ); updateErr != nil {
259
- utilruntime .HandleError (fmt .Errorf ("error: An error occured while updating the build status: %v" , updateErr ))
256
+ utilruntime .HandleError (fmt .Errorf ("error occured while updating the build status: %v" , updateErr ))
260
257
}
261
258
return err
262
259
}
@@ -270,7 +267,7 @@ func (s *S2IBuilder) Build() error {
270
267
)
271
268
272
269
if updateErr := retryBuildStatusUpdate (s .build , s .client , nil ); updateErr != nil {
273
- utilruntime .HandleError (fmt .Errorf ("error: An error occured while updating the build status: %v" , updateErr ))
270
+ utilruntime .HandleError (fmt .Errorf ("error occured while updating the build status: %v" , updateErr ))
274
271
}
275
272
return err
276
273
}
@@ -280,7 +277,7 @@ func (s *S2IBuilder) Build() error {
280
277
s .build .Status .Reason = api .StatusReasonPostCommitHookFailed
281
278
s .build .Status .Message = api .StatusMessagePostCommitHookFailed
282
279
if updateErr := retryBuildStatusUpdate (s .build , s .client , nil ); updateErr != nil {
283
- utilruntime .HandleError (fmt .Errorf ("error: An error occured while updating the build status: %v" , updateErr ))
280
+ utilruntime .HandleError (fmt .Errorf ("error occured while updating the build status: %v" , updateErr ))
284
281
}
285
282
return err
286
283
}
@@ -311,7 +308,7 @@ func (s *S2IBuilder) Build() error {
311
308
s .build .Status .Reason = api .StatusReasonPushImageToRegistryFailed
312
309
s .build .Status .Message = api .StatusMessagePushImageToRegistryFailed
313
310
if updateErr := retryBuildStatusUpdate (s .build , s .client , nil ); updateErr != nil {
314
- utilruntime .HandleError (fmt .Errorf ("error: An error occured while updating the build status: %v" , updateErr ))
311
+ utilruntime .HandleError (fmt .Errorf ("error occured while updating the build status: %v" , updateErr ))
315
312
}
316
313
return reportPushFailure (err , authPresent , pushAuthConfig )
317
314
}
@@ -321,57 +318,15 @@ func (s *S2IBuilder) Build() error {
321
318
}
322
319
323
320
type downloader struct {
324
- s * S2IBuilder
325
- in io.Reader
326
- timeout time.Duration
327
-
328
- dir string
329
- contextDir string
330
- tmpDir string
321
+ sourceInfo * s2iapi.SourceInfo
331
322
}
332
323
324
+ // Download no-ops (because we already downloaded the source to the right location)
325
+ // and returns the previously computed sourceInfo for the source.
333
326
func (d * downloader ) Download (config * s2iapi.Config ) (* s2iapi.SourceInfo , error ) {
334
- var targetDir string
335
- if len (d .contextDir ) > 0 {
336
- targetDir = d .tmpDir
337
- } else {
338
- targetDir = d .dir
339
- }
340
-
341
- // fetch source
342
- sourceInfo , err := fetchSource (d .s .dockerClient , targetDir , d .s .build , d .timeout , d .in , d .s .gitClient )
343
- if err != nil {
344
- d .s .build .Status .Reason = api .StatusReasonFetchSourceFailed
345
- d .s .build .Status .Message = api .StatusMessageFetchSourceFailed
346
- if updateErr := retryBuildStatusUpdate (d .s .build , d .s .client , nil ); updateErr != nil {
347
- utilruntime .HandleError (fmt .Errorf ("error: An error occured while updating the build status: %v" , updateErr ))
348
- }
349
- return nil , err
350
- }
351
- if sourceInfo != nil {
352
- revision := updateBuildRevision (d .s .build , sourceInfo )
353
- if updateErr := retryBuildStatusUpdate (d .s .build , d .s .client , revision ); updateErr != nil {
354
- utilruntime .HandleError (fmt .Errorf ("error: An error occured while updating the build status: %v" , updateErr ))
355
- }
356
- }
357
- if sourceInfo != nil {
358
- sourceInfo .ContextDir = config .ContextDir
359
- }
327
+ config .WorkingSourceDir = config .Source
360
328
361
- // if a context dir is provided, move the context dir contents into the src location
362
- if len (d .contextDir ) > 0 {
363
- srcDir := filepath .Join (targetDir , d .contextDir )
364
- if err := os .Remove (d .dir ); err != nil {
365
- return nil , err
366
- }
367
- if err := os .Rename (srcDir , d .dir ); err != nil {
368
- return nil , err
369
- }
370
- }
371
- if sourceInfo != nil {
372
- return & sourceInfo .SourceInfo , nil
373
- }
374
- return nil , nil
329
+ return d .sourceInfo , nil
375
330
}
376
331
377
332
// buildEnvVars returns a map with build metadata to be inserted into Docker
0 commit comments