-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
dependencies.go
192 lines (161 loc) · 4.98 KB
/
dependencies.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
/*
Copyright 2019 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 (
"context"
"os"
"path/filepath"
"sort"
"strings"
"github.com/docker/docker/builder/dockerignore"
"github.com/docker/docker/pkg/fileutils"
"github.com/karrick/godirwalk"
"github.com/pkg/errors"
)
// NormalizeDockerfilePath returns the absolute path to the dockerfile.
func NormalizeDockerfilePath(context, dockerfile string) (string, error) {
if filepath.IsAbs(dockerfile) {
return dockerfile, nil
}
if !strings.HasPrefix(dockerfile, context) {
dockerfile = filepath.Join(context, dockerfile)
}
return filepath.Abs(dockerfile)
}
// GetDependencies finds the sources dependencies for the given docker artifact.
// All paths are relative to the workspace.
func GetDependencies(ctx context.Context, workspace string, dockerfilePath string, buildArgs map[string]*string, insecureRegistries map[string]bool) ([]string, error) {
absDockerfilePath, err := NormalizeDockerfilePath(workspace, dockerfilePath)
if err != nil {
return nil, errors.Wrap(err, "normalizing dockerfile path")
}
fts, err := readCopyCmdsFromDockerfile(false, absDockerfilePath, workspace, buildArgs, insecureRegistries)
if err != nil {
return nil, err
}
excludes, err := readDockerignore(workspace)
if err != nil {
return nil, errors.Wrap(err, "reading .dockerignore")
}
deps := make([]string, 0, len(fts))
for _, ft := range fts {
deps = append(deps, ft.from)
}
files, err := WalkWorkspace(workspace, excludes, deps)
if err != nil {
return nil, errors.Wrap(err, "walking workspace")
}
// Always add dockerfile even if it's .dockerignored. The daemon will need it anyways.
if !filepath.IsAbs(dockerfilePath) {
files[dockerfilePath] = true
} else {
files[absDockerfilePath] = true
}
// Ignore .dockerignore
delete(files, ".dockerignore")
var dependencies []string
for file := range files {
dependencies = append(dependencies, file)
}
sort.Strings(dependencies)
return dependencies, nil
}
// readDockerignore reads patterns to ignore
func readDockerignore(workspace string) ([]string, error) {
var excludes []string
dockerignorePath := filepath.Join(workspace, ".dockerignore")
if _, err := os.Stat(dockerignorePath); !os.IsNotExist(err) {
r, err := os.Open(dockerignorePath)
if err != nil {
return nil, err
}
defer r.Close()
excludes, err = dockerignore.ReadAll(r)
if err != nil {
return nil, err
}
}
return excludes, nil
}
// WalkWorkspace walks the given host directories and records all files found.
// Note: if you change this function, you might also want to modify `walkWorkspaceWithDestinations`.
func WalkWorkspace(workspace string, excludes, deps []string) (map[string]bool, error) {
pExclude, err := fileutils.NewPatternMatcher(excludes)
if err != nil {
return nil, errors.Wrap(err, "invalid exclude patterns")
}
// Walk the workspace
files := make(map[string]bool)
for _, dep := range deps {
dep = filepath.Clean(dep)
absDep := filepath.Join(workspace, dep)
fi, err := os.Stat(absDep)
if err != nil {
return nil, errors.Wrapf(err, "stating file %s", absDep)
}
switch mode := fi.Mode(); {
case mode.IsDir():
if err := godirwalk.Walk(absDep, &godirwalk.Options{
Unsorted: true,
Callback: func(fpath string, info *godirwalk.Dirent) error {
if fpath == absDep {
return nil
}
relPath, err := filepath.Rel(workspace, fpath)
if err != nil {
return err
}
ignored, err := pExclude.Matches(relPath)
if err != nil {
return err
}
if info.IsDir() {
if !ignored {
return nil
}
// exclusion handling closely follows vendor/github.com/docker/docker/pkg/archive/archive.go
// No exceptions (!...) in patterns so just skip dir
if !pExclude.Exclusions() {
return filepath.SkipDir
}
dirSlash := relPath + string(filepath.Separator)
for _, pat := range pExclude.Patterns() {
if !pat.Exclusion() {
continue
}
if strings.HasPrefix(pat.String()+string(filepath.Separator), dirSlash) {
// found a match - so can't skip this dir
return nil
}
}
return filepath.SkipDir
} else if !ignored {
files[relPath] = true
}
return nil
},
}); err != nil {
return nil, errors.Wrapf(err, "walking folder %s", absDep)
}
case mode.IsRegular():
ignored, err := pExclude.Matches(dep)
if err != nil {
return nil, err
}
if !ignored {
files[dep] = true
}
}
}
return files, nil
}