@@ -21,12 +21,12 @@ import (
21
21
"fmt"
22
22
"os/exec"
23
23
"path/filepath"
24
- "strings"
25
24
26
25
"github.com/bmatcuk/doublestar"
27
26
"github.com/sirupsen/logrus"
28
27
29
28
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build"
29
+ "github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
30
30
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
31
31
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
32
32
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
@@ -36,6 +36,11 @@ import (
36
36
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
37
37
)
38
38
39
+ var (
40
+ // For testing
41
+ WorkingDir = retrieveWorkingDir
42
+ )
43
+
39
44
type Syncer interface {
40
45
Sync (context.Context , * Item ) error
41
46
}
@@ -52,12 +57,22 @@ func NewItem(a *latest.Artifact, e watch.Events, builds []build.Artifact) (*Item
52
57
return nil , nil
53
58
}
54
59
55
- toCopy , err := intersect (a .Workspace , a .Sync , append (e .Added , e .Modified ... ))
60
+ tag := latestTag (a .ImageName , builds )
61
+ if tag == "" {
62
+ return nil , fmt .Errorf ("could not find latest tag for image %s in builds: %v" , a .ImageName , builds )
63
+ }
64
+
65
+ wd , err := WorkingDir (tag )
66
+ if err != nil {
67
+ return nil , errors .Wrapf (err , "retrieving working dir for %s" , tag )
68
+ }
69
+
70
+ toCopy , err := intersect (a .Workspace , a .Sync , append (e .Added , e .Modified ... ), wd )
56
71
if err != nil {
57
72
return nil , errors .Wrap (err , "intersecting sync map and added, modified files" )
58
73
}
59
74
60
- toDelete , err := intersect (a .Workspace , a .Sync , e .Deleted )
75
+ toDelete , err := intersect (a .Workspace , a .Sync , e .Deleted , wd )
61
76
if err != nil {
62
77
return nil , errors .Wrap (err , "intersecting sync map and deleted files" )
63
78
}
@@ -67,18 +82,24 @@ func NewItem(a *latest.Artifact, e watch.Events, builds []build.Artifact) (*Item
67
82
return nil , nil
68
83
}
69
84
70
- tag := latestTag (a .ImageName , builds )
71
- if tag == "" {
72
- return nil , fmt .Errorf ("could not find latest tag for image %s in builds: %v" , a .ImageName , builds )
73
- }
74
-
75
85
return & Item {
76
86
Image : tag ,
77
87
Copy : toCopy ,
78
88
Delete : toDelete ,
79
89
}, nil
80
90
}
81
91
92
+ func retrieveWorkingDir (image string ) (string , error ) {
93
+ cf , err := docker .RetrieveRemoteConfig (image )
94
+ if err != nil {
95
+ return "" , errors .Wrap (err , "retrieving remote config" )
96
+ }
97
+ if cf .Config .WorkingDir == "" {
98
+ return "/" , nil
99
+ }
100
+ return cf .Config .WorkingDir , nil
101
+ }
102
+
82
103
func latestTag (image string , builds []build.Artifact ) string {
83
104
for _ , build := range builds {
84
105
if build .ImageName == image {
@@ -88,7 +109,7 @@ func latestTag(image string, builds []build.Artifact) string {
88
109
return ""
89
110
}
90
111
91
- func intersect (context string , syncMap map [string ]string , files []string ) (map [string ]string , error ) {
112
+ func intersect (context string , syncMap map [string ]string , files []string , workingDir string ) (map [string ]string , error ) {
92
113
ret := map [string ]string {}
93
114
94
115
for _ , f := range files {
@@ -103,18 +124,14 @@ func intersect(context string, syncMap map[string]string, files []string) (map[s
103
124
return nil , errors .Wrapf (err , "pattern error for %s" , relPath )
104
125
}
105
126
if match {
106
- staticPath := strings .Split (filepath .FromSlash (p ), "*" )[0 ]
127
+ if filepath .IsAbs (dst ) {
128
+ dst = filepath .ToSlash (filepath .Join (dst , filepath .Base (relPath )))
129
+ } else {
130
+ dst = filepath .ToSlash (filepath .Join (workingDir , dst , filepath .Base (relPath )))
131
+ }
107
132
// Every file must match at least one sync pattern, if not we'll have to
108
133
// skip the entire sync
109
134
matches = true
110
- // If the source has special match characters,
111
- // the destination must be a directory
112
- // The path package must be used here to enforce slashes,
113
- // since the destination is always a linux filesystem.
114
- if util .HasMeta (p ) {
115
- relPathDynamic := strings .TrimPrefix (relPath , staticPath )
116
- dst = filepath .ToSlash (filepath .Join (dst , filepath .Base (relPathDynamic )))
117
- }
118
135
ret [f ] = dst
119
136
}
120
137
}
0 commit comments