/
syncmap.go
139 lines (116 loc) · 4.06 KB
/
syncmap.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
/*
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"
"fmt"
"os"
"path"
"path/filepath"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/walk"
)
// SyncMap creates a map of syncable files by looking at the COPY/ADD commands in the Dockerfile.
// All keys are relative to the Skaffold root, the destinations are absolute container paths.
// TODO(corneliusweig) destinations are not resolved across stages in multistage dockerfiles. Is there a use-case for that?
func SyncMap(ctx context.Context, workspace string, dockerfilePath string, buildArgs map[string]*string, cfg Config) (map[string][]string, error) {
absDockerfilePath, err := NormalizeDockerfilePath(workspace, dockerfilePath)
if err != nil {
return nil, fmt.Errorf("normalizing dockerfile path: %w", err)
}
// only the COPY/ADD commands from the last image are syncable
fts, err := ReadCopyCmdsFromDockerfile(ctx, true, absDockerfilePath, workspace, buildArgs, cfg)
if err != nil {
return nil, err
}
excludes, err := readDockerignore(workspace, absDockerfilePath)
if err != nil {
return nil, fmt.Errorf("reading .dockerignore: %w", err)
}
srcByDest, err := walkWorkspaceWithDestinations(workspace, excludes, fts)
if err != nil {
return nil, fmt.Errorf("walking workspace: %w", err)
}
return invertMap(srcByDest), nil
}
// walkWorkspaceWithDestinations walks the given host directories and determines their
// location in the container. It returns a map of host path by container destination.
// Note: if you change this function, you might also want to modify `WalkWorkspace`.
func walkWorkspaceWithDestinations(workspace string, excludes []string, fts []FromTo) (map[string]string, error) {
dockerIgnored, err := NewDockerIgnorePredicate(workspace, excludes)
if err != nil {
return nil, err
}
// Walk the workspace
srcByDest := make(map[string]string)
for _, ft := range fts {
absFrom := filepath.Join(workspace, ft.From)
fi, err := os.Stat(absFrom)
if err != nil {
return nil, fmt.Errorf("stating file %q: %w", absFrom, err)
}
switch mode := fi.Mode(); {
case mode.IsDir():
keepFile := func(path string, info walk.Dirent) (bool, error) {
// Always keep root folders.
if info.IsDir() && path == absFrom {
return true, nil
}
ignored, err := dockerIgnored(path, info)
if err != nil {
return false, err
}
return !ignored, nil
}
if err := walk.From(absFrom).Unsorted().When(keepFile).WhenIsFile().Do(func(fpath string, info walk.Dirent) error {
relPath, err := filepath.Rel(workspace, fpath)
if err != nil {
return err
}
relBase, err := filepath.Rel(absFrom, fpath)
if err != nil {
return err
}
srcByDest[path.Join(ft.To, filepath.ToSlash(relBase))] = relPath
return nil
}); err != nil {
return nil, fmt.Errorf("walking %q: %w", absFrom, err)
}
case mode.IsRegular():
ignored, err := dockerIgnored(filepath.Join(workspace, ft.From), fi)
if err != nil {
return nil, err
}
if !ignored {
if ft.ToIsDir {
base := filepath.Base(ft.From)
srcByDest[path.Join(ft.To, base)] = ft.From
} else {
srcByDest[ft.To] = ft.From
}
}
}
}
return srcByDest, nil
}
func invertMap(kv map[string]string) map[string][]string {
// len(kv) is a good upper bound for the size, because most files will have exactly one destination
keysByValue := make(map[string][]string, len(kv))
for k, v := range kv {
if vs, ok := keysByValue[v]; ok {
keysByValue[v] = append(vs, k)
} else {
keysByValue[v] = []string{k}
}
}
return keysByValue
}