@@ -22,14 +22,12 @@ import (
22
22
"fmt"
23
23
"io"
24
24
"os"
25
- "path"
26
25
"path/filepath"
27
26
"sort"
28
27
"strings"
29
28
"sync"
30
29
31
30
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/v1alpha2"
32
- "github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
33
31
"github.com/google/go-containerregistry/v1"
34
32
35
33
"github.com/docker/docker/builder/dockerignore"
@@ -57,11 +55,9 @@ func (d *DockerfileDepResolver) GetDependencies(a *v1alpha2.Artifact) ([]string,
57
55
return GetDockerfileDependencies (a .DockerArtifact .DockerfilePath , a .Workspace )
58
56
}
59
57
60
- // GetDockerfileDependencies parses a dockerfile and returns the full paths
61
- // of all the source files that the resulting docker image depends on.
62
- func GetDockerfileDependencies (dockerfilePath , workspace string ) ([]string , error ) {
58
+ func readDockerfile (workspace , dockerfilePath string ) ([]string , error ) {
63
59
path := filepath .Join (workspace , dockerfilePath )
64
- f , err := util . Fs .Open (path )
60
+ f , err := os .Open (path )
65
61
if err != nil {
66
62
return nil , errors .Wrapf (err , "opening dockerfile: %s" , path )
67
63
}
@@ -91,7 +87,7 @@ func GetDockerfileDependencies(dockerfilePath, workspace string) ([]string, erro
91
87
for _ , value := range r .AST .Children {
92
88
switch value .Value {
93
89
case add , copy :
94
- processCopy (workspace , value , depMap , envs )
90
+ processCopy (value , depMap , envs )
95
91
case env :
96
92
envs [value .Next .Value ] = value .Next .Next .Value
97
93
}
@@ -109,30 +105,86 @@ func GetDockerfileDependencies(dockerfilePath, workspace string) ([]string, erro
109
105
110
106
dispatchInstructions (res )
111
107
112
- deps := []string {}
108
+ var deps []string
113
109
for dep := range depMap {
114
110
deps = append (deps , dep )
115
111
}
116
112
logrus .Infof ("Found dependencies for dockerfile %s" , deps )
117
113
118
- expandedDeps , err := util .ExpandPaths (workspace , deps )
114
+ return deps , nil
115
+ }
116
+
117
+ func GetDockerfileDependencies (dockerfilePath , workspace string ) ([]string , error ) {
118
+ deps , err := readDockerfile (workspace , dockerfilePath )
119
119
if err != nil {
120
- return nil , errors . Wrap ( err , "expanding dockerfile paths" )
120
+ return nil , err
121
121
}
122
- logrus .Infof ("deps %s" , expandedDeps )
123
122
124
- if ! util .StrSliceContains (expandedDeps , path ) {
125
- expandedDeps = append (expandedDeps , path )
123
+ // Read patterns to ignore
124
+ var excludes []string
125
+ dockerignorePath := filepath .Join (workspace , ".dockerignore" )
126
+ if _ , err := os .Stat (dockerignorePath ); ! os .IsNotExist (err ) {
127
+ r , err := os .Open (dockerignorePath )
128
+ if err != nil {
129
+ return nil , err
130
+ }
131
+ defer r .Close ()
132
+
133
+ excludes , err = dockerignore .ReadAll (r )
134
+ if err != nil {
135
+ return nil , err
136
+ }
137
+ }
138
+
139
+ // Walk the workspace
140
+ files := make (map [string ]bool )
141
+ for _ , dep := range deps {
142
+ filepath .Walk (filepath .Join (workspace , dep ), func (fpath string , info os.FileInfo , err error ) error {
143
+ if err != nil {
144
+ return err
145
+ }
146
+
147
+ relPath , err := filepath .Rel (workspace , fpath )
148
+ if err != nil {
149
+ return err
150
+ }
151
+
152
+ ignored , err := fileutils .Matches (relPath , excludes )
153
+ if err != nil {
154
+ return err
155
+ }
156
+
157
+ if info .IsDir () && ignored {
158
+ return filepath .SkipDir
159
+ }
160
+
161
+ if ! info .IsDir () && ! ignored {
162
+ files [relPath ] = true
163
+ }
164
+
165
+ return nil
166
+ })
126
167
}
127
168
128
- // Look for .dockerignore.
129
- ignorePath := filepath .Join (workspace , ".dockerignore" )
130
- filteredDeps , err := ApplyDockerIgnore (expandedDeps , ignorePath )
169
+ // Add dockerfile?
170
+ m , err := fileutils .Matches (dockerfilePath , excludes )
131
171
if err != nil {
132
- return nil , errors .Wrap (err , "applying dockerignore" )
172
+ return nil , err
173
+ }
174
+ if ! m {
175
+ files [dockerfilePath ] = true
176
+ }
177
+
178
+ // Ignore .dockerignore
179
+ delete (files , ".dockerignore" )
180
+
181
+ var dependencies []string
182
+ for file := range files {
183
+ dependencies = append (dependencies , file )
133
184
}
185
+ sort .Strings (dependencies )
134
186
135
- return filteredDeps , nil
187
+ return dependencies , nil
136
188
}
137
189
138
190
func PortsFromDockerfile (r io.Reader ) ([]string , error ) {
@@ -242,7 +294,7 @@ func retrieveRemoteConfig(identifier string) (*v1.ConfigFile, error) {
242
294
return img .ConfigFile ()
243
295
}
244
296
245
- func processCopy (workspace string , value * parser.Node , paths map [string ]struct {}, envs map [string ]string ) error {
297
+ func processCopy (value * parser.Node , paths map [string ]struct {}, envs map [string ]string ) error {
246
298
slex := shell .NewLex ('\\' )
247
299
for {
248
300
// Skip last node, since it is the destination, and stop if we arrive at a comment
@@ -259,8 +311,7 @@ func processCopy(workspace string, value *parser.Node, paths map[string]struct{}
259
311
return nil
260
312
}
261
313
if ! strings .HasPrefix (src , "http://" ) && ! strings .HasPrefix (src , "https://" ) {
262
- dep := path .Join (workspace , src )
263
- paths [dep ] = struct {}{}
314
+ paths [src ] = struct {}{}
264
315
} else {
265
316
logrus .Debugf ("Skipping watch on remote dependency %s" , src )
266
317
}
@@ -286,42 +337,3 @@ func hasMultiStageFlag(flags []string) bool {
286
337
}
287
338
return false
288
339
}
289
-
290
- func ApplyDockerIgnore (paths []string , dockerIgnorePath string ) ([]string , error ) {
291
- absPaths , err := util .RelPathToAbsPath (paths )
292
- if err != nil {
293
- return nil , errors .Wrap (err , "getting absolute path of dependencies" )
294
- }
295
- excludes := []string {}
296
- if _ , err := util .Fs .Stat (dockerIgnorePath ); ! os .IsNotExist (err ) {
297
- r , err := util .Fs .Open (dockerIgnorePath )
298
- if err != nil {
299
- return nil , err
300
- }
301
- defer r .Close ()
302
-
303
- excludes , err = dockerignore .ReadAll (r )
304
- if err != nil {
305
- return nil , err
306
- }
307
- excludes = append (excludes , ".dockerignore" )
308
- }
309
-
310
- absPathExcludes , err := util .RelPathToAbsPath (excludes )
311
- if err != nil {
312
- return nil , errors .Wrap (err , "getting absolute path of docker ignored paths" )
313
- }
314
-
315
- filteredDeps := []string {}
316
- for _ , d := range absPaths {
317
- m , err := fileutils .Matches (d , absPathExcludes )
318
- if err != nil {
319
- return nil , err
320
- }
321
- if ! m {
322
- filteredDeps = append (filteredDeps , d )
323
- }
324
- }
325
- sort .Strings (filteredDeps )
326
- return filteredDeps , nil
327
- }
0 commit comments