Skip to content

Commit 65af13b

Browse files
rhatdanRomain-Geissler-1A
authored andcommitted
Support podman --remote when Containerfile is not in context directory
Fixes: #18239 [NO NEW TESTS NEEDED] @test "podman build -f test" in test/system/070-build.bats Will test this. This was passing when run on a local system since the remote end was using the clients path to read the Containerfile The issue is it would not work in a podman machine since the Containerfile would/should be a different path. Signed-off-by: Daniel J Walsh <dwalsh@redhat.com> Signed-off-by: Romain Geissler <romain.geissler@amadeus.com>
1 parent ff30585 commit 65af13b

File tree

3 files changed

+28
-25
lines changed

3 files changed

+28
-25
lines changed

pkg/api/handlers/compat/images_build.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,11 @@ func BuildImage(w http.ResponseWriter, r *http.Request) {
220220
var m = []string{}
221221
if err := json.Unmarshal([]byte(query.Dockerfile), &m); err != nil {
222222
// it's not json, assume just a string
223-
m = []string{filepath.Join(contextDirectory, query.Dockerfile)}
223+
m = []string{query.Dockerfile}
224+
}
225+
for _, containerfile := range m {
226+
containerFiles = append(containerFiles, filepath.Join(contextDirectory, filepath.Clean(filepath.FromSlash(containerfile))))
224227
}
225-
containerFiles = m
226228
dockerFileSet = true
227229
}
228230
}

pkg/bindings/images/build.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -637,24 +637,24 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) {
637637
defer gw.Close()
638638
defer tw.Close()
639639
seen := make(map[devino]string)
640-
for _, src := range sources {
641-
s, err := filepath.Abs(src)
640+
for i, src := range sources {
641+
source, err := filepath.Abs(src)
642642
if err != nil {
643643
logrus.Errorf("Cannot stat one of source context: %v", err)
644644
merr = multierror.Append(merr, err)
645645
return
646646
}
647-
err = filepath.WalkDir(s, func(path string, d fs.DirEntry, err error) error {
647+
err = filepath.WalkDir(source, func(path string, dentry fs.DirEntry, err error) error {
648648
if err != nil {
649649
return err
650650
}
651651

652652
separator := string(filepath.Separator)
653653
// check if what we are given is an empty dir, if so then continue w/ it. Else return.
654654
// if we are given a file or a symlink, we do not want to exclude it.
655-
if s == path {
655+
if source == path {
656656
separator = ""
657-
if d.IsDir() {
657+
if dentry.IsDir() {
658658
var p *os.File
659659
p, err = os.Open(path)
660660
if err != nil {
@@ -669,8 +669,15 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) {
669669
}
670670
}
671671
}
672-
name := filepath.ToSlash(strings.TrimPrefix(path, s+separator))
673-
672+
var name string
673+
if i == 0 {
674+
name = filepath.ToSlash(strings.TrimPrefix(path, source+separator))
675+
} else {
676+
if !dentry.Type().IsRegular() {
677+
return fmt.Errorf("path %s must be a regular file", path)
678+
}
679+
name = filepath.ToSlash(path)
680+
}
674681
excluded, err := pm.Matches(name) //nolint:staticcheck
675682
if err != nil {
676683
return fmt.Errorf("checking if %q is excluded: %w", name, err)
@@ -682,8 +689,8 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) {
682689
return nil
683690
}
684691
switch {
685-
case d.Type().IsRegular(): // add file item
686-
info, err := d.Info()
692+
case dentry.Type().IsRegular(): // add file item
693+
info, err := dentry.Info()
687694
if err != nil {
688695
return err
689696
}
@@ -722,8 +729,8 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) {
722729
seen[di] = name
723730
}
724731
return err
725-
case d.IsDir(): // add folders
726-
info, err := d.Info()
732+
case dentry.IsDir(): // add folders
733+
info, err := dentry.Info()
727734
if err != nil {
728735
return err
729736
}
@@ -736,12 +743,12 @@ func nTar(excludes []string, sources ...string) (io.ReadCloser, error) {
736743
if lerr := tw.WriteHeader(hdr); lerr != nil {
737744
return lerr
738745
}
739-
case d.Type()&os.ModeSymlink != 0: // add symlinks as it, not content
746+
case dentry.Type()&os.ModeSymlink != 0: // add symlinks as it, not content
740747
link, err := os.Readlink(path)
741748
if err != nil {
742749
return err
743750
}
744-
info, err := d.Info()
751+
info, err := dentry.Info()
745752
if err != nil {
746753
return err
747754
}

test/e2e/build_test.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,14 +390,9 @@ RUN exit 5`, ALPINE)
390390
if IsRemote() {
391391
podmanTest.StopRemoteService()
392392
podmanTest.StartRemoteService()
393-
} else {
394-
Skip("Only valid at remote test, case works fine for regular podman and buildah")
395393
}
396-
cwd, err := os.Getwd()
397-
Expect(err).ToNot(HaveOccurred())
398-
399394
// Write target and fake files
400-
targetSubPath := filepath.Join(cwd, "emptydir")
395+
targetSubPath := filepath.Join(podmanTest.TempDir, "emptydir")
401396
if _, err = os.Stat(targetSubPath); err != nil {
402397
if os.IsNotExist(err) {
403398
err = os.Mkdir(targetSubPath, 0755)
@@ -406,13 +401,13 @@ RUN exit 5`, ALPINE)
406401
}
407402

408403
containerfile := fmt.Sprintf(`FROM %s
409-
COPY /* /dir`, ALPINE)
404+
COPY /emptydir/* /dir`, ALPINE)
410405

411-
containerfilePath := filepath.Join(cwd, "ContainerfilePathToCopier")
406+
containerfilePath := filepath.Join(podmanTest.TempDir, "ContainerfilePathToCopier")
412407
err = os.WriteFile(containerfilePath, []byte(containerfile), 0644)
413408
Expect(err).ToNot(HaveOccurred())
414409

415-
session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", "-f", "ContainerfilePathToCopier", targetSubPath})
410+
session := podmanTest.Podman([]string{"build", "--pull-never", "-t", "test", "-f", "ContainerfilePathToCopier", podmanTest.TempDir})
416411
session.WaitWithDefaultTimeout()
417412
// NOTE: Docker and buildah both should error when `COPY /* /dir` is done on emptydir
418413
// as context. However buildkit simply ignores this so when buildah also starts ignoring
@@ -621,7 +616,6 @@ subdir**`
621616
Expect(session).To(Exit(0))
622617

623618
output := session.OutputToString()
624-
Expect(output).NotTo(ContainSubstring("Containerfile"))
625619
Expect(output).To(ContainSubstring("/testfilter/expected"))
626620
Expect(output).NotTo(ContainSubstring("subdir"))
627621
})

0 commit comments

Comments
 (0)