Skip to content

Commit dca8dcc

Browse files
committed
fix(kustomize): Improved file handling when mirroring files in Windows systems
1 parent 71312e1 commit dca8dcc

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

pkg/skaffold/render/renderer/kustomize/fs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (f TmpFSReal) GetPath(path string) (string, error) {
6262
}
6363

6464
func (f TmpFSReal) validate(path string) error {
65-
if path != f.root && !strings.HasPrefix(path, f.root+"/") {
65+
if path != f.root && !strings.HasPrefix(path, f.root+string(filepath.Separator)) {
6666
return fmt.Errorf("temporary file system operation is out of boundary, root: %s, trying to access %s", f.root, path)
6767
}
6868
return nil

pkg/skaffold/render/renderer/kustomize/kustomize.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ func (k Kustomize) render(ctx context.Context, kustomizePath string, useKubectlK
130130
defer fs.Cleanup()
131131

132132
if err := k.mirror(kustomizePath, fs); err == nil {
133-
kustomizePath = filepath.Join(temp, kustomizePath)
133+
kustomizePath = filepath.Join(temp, strings.TrimPrefix(kustomizePath, filepath.VolumeName(kustomizePath)))
134134
} else {
135135
return out, err
136136
}
@@ -167,7 +167,9 @@ func (k Kustomize) mirror(kusDir string, fs TmpFS) error {
167167
return err
168168
}
169169

170-
if err := fs.WriteTo(kFile, bytes); err != nil {
170+
pFile := strings.TrimPrefix(kFile, filepath.VolumeName(kFile))
171+
172+
if err := fs.WriteTo(pFile, bytes); err != nil {
171173
return err
172174
}
173175

@@ -314,15 +316,18 @@ func (k Kustomize) mirrorFile(kusDir string, fs TmpFS, path string) error {
314316
if sUtil.IsURL(path) {
315317
return nil
316318
}
317-
pFile := filepath.Join(kusDir, path)
318-
bytes, err := os.ReadFile(pFile)
319+
320+
sourceFile := filepath.Join(kusDir, path)
321+
targetFile := strings.TrimPrefix(sourceFile, filepath.VolumeName(sourceFile))
322+
323+
bytes, err := os.ReadFile(sourceFile)
319324
if err != nil {
320325
return err
321326
}
322-
if err := fs.WriteTo(pFile, bytes); err != nil {
327+
if err := fs.WriteTo(targetFile, bytes); err != nil {
323328
return err
324329
}
325-
fsPath, err := fs.GetPath(pFile)
330+
fsPath, err := fs.GetPath(targetFile)
326331

327332
if err != nil {
328333
return err
@@ -336,7 +341,7 @@ func (k Kustomize) mirrorFile(kusDir string, fs TmpFS, path string) error {
336341

337342
err = k.applySetters.ApplyPath(fsPath)
338343
if err != nil {
339-
return fmt.Errorf("failed to apply setter to file %s, err: %v", pFile, err)
344+
return fmt.Errorf("failed to apply setter to file %s, err: %v", sourceFile, err)
340345
}
341346
}
342347
return nil

pkg/skaffold/render/renderer/kustomize/kustomize_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package kustomize
1818

1919
import (
20+
"path/filepath"
2021
"slices"
22+
"strings"
2123
"testing"
2224

2325
"github.com/GoogleContainerTools/skaffold/v2/pkg/skaffold/render"
@@ -165,16 +167,27 @@ secretGenerator:
165167
t_err := k.mirror(sourceDir.Root(), fs)
166168

167169
// Gather a list of the expected files
168-
expectedFiles, err := sourceDir.List()
170+
var expectedFiles []string
171+
172+
sourceFileList, err := sourceDir.List()
169173
t.CheckNoError(err)
170174

175+
sourceVolName := filepath.VolumeName(sourceDir.Root())
176+
if sourceVolName != "" {
177+
for _, f := range sourceFileList {
178+
expectedFiles = append(expectedFiles, strings.TrimPrefix(f, sourceVolName))
179+
}
180+
} else {
181+
expectedFiles = sourceFileList
182+
}
183+
171184
slices.Sort(expectedFiles)
172185

173186
// Gather a list of files that have been created by the mirror function
174187
targetFileList, err := targetDir.List()
175188
t.CheckNoError(err)
176189

177-
minPrefixLen := len(targetDir.Root()) + len(sourceDir.Root())
190+
minPrefixLen := len(targetDir.Root()) + len(sourceDir.Root()) - len(sourceVolName)
178191
prefixLen := len(targetDir.Root())
179192

180193
var mirroredFiles []string

0 commit comments

Comments
 (0)