mirror of https://github.com/containers/podman.git
pkg/specgen: use fileutils.(Le|E)xists
Signed-off-by: Giuseppe Scrivano <gscrivan@redhat.com>
This commit is contained in:
parent
aab06ac445
commit
1991990d5a
|
@ -14,6 +14,7 @@ import (
|
||||||
"github.com/containers/podman/v5/libpod/define"
|
"github.com/containers/podman/v5/libpod/define"
|
||||||
"github.com/containers/podman/v5/pkg/rootless"
|
"github.com/containers/podman/v5/pkg/rootless"
|
||||||
"github.com/containers/podman/v5/pkg/util"
|
"github.com/containers/podman/v5/pkg/util"
|
||||||
|
"github.com/containers/storage/pkg/fileutils"
|
||||||
|
|
||||||
spec "github.com/opencontainers/runtime-spec/specs-go"
|
spec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/opencontainers/runtime-tools/generate"
|
"github.com/opencontainers/runtime-tools/generate"
|
||||||
|
@ -133,7 +134,7 @@ func addDevice(g *generate.Generator, device string) error {
|
||||||
return fmt.Errorf("%s is not a valid device: %w", src, err)
|
return fmt.Errorf("%s is not a valid device: %w", src, err)
|
||||||
}
|
}
|
||||||
if rootless.IsRootless() {
|
if rootless.IsRootless() {
|
||||||
if _, err := os.Stat(src); err != nil {
|
if err := fileutils.Exists(src); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
perm := "ro"
|
perm := "ro"
|
||||||
|
|
|
@ -5,12 +5,14 @@ package kube
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/containers/common/pkg/parse"
|
"github.com/containers/common/pkg/parse"
|
||||||
"github.com/containers/common/pkg/secrets"
|
"github.com/containers/common/pkg/secrets"
|
||||||
"github.com/containers/podman/v5/libpod"
|
"github.com/containers/podman/v5/libpod"
|
||||||
v1 "github.com/containers/podman/v5/pkg/k8s.io/api/core/v1"
|
v1 "github.com/containers/podman/v5/pkg/k8s.io/api/core/v1"
|
||||||
|
"github.com/containers/storage/pkg/fileutils"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"sigs.k8s.io/yaml"
|
"sigs.k8s.io/yaml"
|
||||||
|
@ -69,7 +71,7 @@ func VolumeFromHostPath(hostPath *v1.HostPathVolumeSource, mountLabel string) (*
|
||||||
return nil, fmt.Errorf("giving %s a label: %w", hostPath.Path, err)
|
return nil, fmt.Errorf("giving %s a label: %w", hostPath.Path, err)
|
||||||
}
|
}
|
||||||
case v1.HostPathFileOrCreate:
|
case v1.HostPathFileOrCreate:
|
||||||
if _, err := os.Stat(hostPath.Path); os.IsNotExist(err) {
|
if err := fileutils.Exists(hostPath.Path); errors.Is(err, fs.ErrNotExist) {
|
||||||
f, err := os.OpenFile(hostPath.Path, os.O_RDONLY|os.O_CREATE, kubeFilePermission)
|
f, err := os.OpenFile(hostPath.Path, os.O_RDONLY|os.O_CREATE, kubeFilePermission)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("creating HostPath: %w", err)
|
return nil, fmt.Errorf("creating HostPath: %w", err)
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/containers/podman/v5/libpod"
|
"github.com/containers/podman/v5/libpod"
|
||||||
"github.com/containers/podman/v5/libpod/define"
|
"github.com/containers/podman/v5/libpod/define"
|
||||||
"github.com/containers/podman/v5/pkg/specgen"
|
"github.com/containers/podman/v5/pkg/specgen"
|
||||||
|
"github.com/containers/storage/pkg/fileutils"
|
||||||
spec "github.com/opencontainers/runtime-spec/specs-go"
|
spec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/opencontainers/runtime-tools/generate"
|
"github.com/opencontainers/runtime-tools/generate"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
@ -18,7 +19,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
|
||||||
// PID
|
// PID
|
||||||
switch s.PidNS.NSMode {
|
switch s.PidNS.NSMode {
|
||||||
case specgen.Path:
|
case specgen.Path:
|
||||||
if _, err := os.Stat(s.PidNS.Value); err != nil {
|
if err := fileutils.Exists(s.PidNS.Value); err != nil {
|
||||||
return fmt.Errorf("cannot find specified PID namespace path: %w", err)
|
return fmt.Errorf("cannot find specified PID namespace path: %w", err)
|
||||||
}
|
}
|
||||||
if err := g.AddOrReplaceLinuxNamespace(string(spec.PIDNamespace), s.PidNS.Value); err != nil {
|
if err := g.AddOrReplaceLinuxNamespace(string(spec.PIDNamespace), s.PidNS.Value); err != nil {
|
||||||
|
@ -37,7 +38,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
|
||||||
// IPC
|
// IPC
|
||||||
switch s.IpcNS.NSMode {
|
switch s.IpcNS.NSMode {
|
||||||
case specgen.Path:
|
case specgen.Path:
|
||||||
if _, err := os.Stat(s.IpcNS.Value); err != nil {
|
if err := fileutils.Exists(s.IpcNS.Value); err != nil {
|
||||||
return fmt.Errorf("cannot find specified IPC namespace path: %w", err)
|
return fmt.Errorf("cannot find specified IPC namespace path: %w", err)
|
||||||
}
|
}
|
||||||
if err := g.AddOrReplaceLinuxNamespace(string(spec.IPCNamespace), s.IpcNS.Value); err != nil {
|
if err := g.AddOrReplaceLinuxNamespace(string(spec.IPCNamespace), s.IpcNS.Value); err != nil {
|
||||||
|
@ -56,7 +57,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
|
||||||
// UTS
|
// UTS
|
||||||
switch s.UtsNS.NSMode {
|
switch s.UtsNS.NSMode {
|
||||||
case specgen.Path:
|
case specgen.Path:
|
||||||
if _, err := os.Stat(s.UtsNS.Value); err != nil {
|
if err := fileutils.Exists(s.UtsNS.Value); err != nil {
|
||||||
return fmt.Errorf("cannot find specified UTS namespace path: %w", err)
|
return fmt.Errorf("cannot find specified UTS namespace path: %w", err)
|
||||||
}
|
}
|
||||||
if err := g.AddOrReplaceLinuxNamespace(string(spec.UTSNamespace), s.UtsNS.Value); err != nil {
|
if err := g.AddOrReplaceLinuxNamespace(string(spec.UTSNamespace), s.UtsNS.Value); err != nil {
|
||||||
|
@ -114,7 +115,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
|
||||||
// Cgroup
|
// Cgroup
|
||||||
switch s.CgroupNS.NSMode {
|
switch s.CgroupNS.NSMode {
|
||||||
case specgen.Path:
|
case specgen.Path:
|
||||||
if _, err := os.Stat(s.CgroupNS.Value); err != nil {
|
if err := fileutils.Exists(s.CgroupNS.Value); err != nil {
|
||||||
return fmt.Errorf("cannot find specified cgroup namespace path: %w", err)
|
return fmt.Errorf("cannot find specified cgroup namespace path: %w", err)
|
||||||
}
|
}
|
||||||
if err := g.AddOrReplaceLinuxNamespace(string(spec.CgroupNamespace), s.CgroupNS.Value); err != nil {
|
if err := g.AddOrReplaceLinuxNamespace(string(spec.CgroupNamespace), s.CgroupNS.Value); err != nil {
|
||||||
|
@ -133,7 +134,7 @@ func specConfigureNamespaces(s *specgen.SpecGenerator, g *generate.Generator, rt
|
||||||
// Net
|
// Net
|
||||||
switch s.NetNS.NSMode {
|
switch s.NetNS.NSMode {
|
||||||
case specgen.Path:
|
case specgen.Path:
|
||||||
if _, err := os.Stat(s.NetNS.Value); err != nil {
|
if err := fileutils.Exists(s.NetNS.Value); err != nil {
|
||||||
return fmt.Errorf("cannot find specified network namespace path: %w", err)
|
return fmt.Errorf("cannot find specified network namespace path: %w", err)
|
||||||
}
|
}
|
||||||
if err := g.AddOrReplaceLinuxNamespace(string(spec.NetworkNamespace), s.NetNS.Value); err != nil {
|
if err := g.AddOrReplaceLinuxNamespace(string(spec.NetworkNamespace), s.NetNS.Value); err != nil {
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"io/fs"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -18,6 +18,7 @@ import (
|
||||||
"github.com/containers/podman/v5/libpod/define"
|
"github.com/containers/podman/v5/libpod/define"
|
||||||
"github.com/containers/podman/v5/pkg/specgen"
|
"github.com/containers/podman/v5/pkg/specgen"
|
||||||
"github.com/containers/podman/v5/pkg/util"
|
"github.com/containers/podman/v5/pkg/util"
|
||||||
|
"github.com/containers/storage/pkg/fileutils"
|
||||||
spec "github.com/opencontainers/runtime-spec/specs-go"
|
spec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
@ -391,7 +392,7 @@ func addContainerInitBinary(s *specgen.SpecGenerator, path string) (spec.Mount,
|
||||||
if s.Systemd == "always" {
|
if s.Systemd == "always" {
|
||||||
return mount, errors.New("cannot use container-init binary with systemd=always")
|
return mount, errors.New("cannot use container-init binary with systemd=always")
|
||||||
}
|
}
|
||||||
if _, err := os.Stat(path); os.IsNotExist(err) {
|
if err := fileutils.Exists(path); errors.Is(err, fs.ErrNotExist) {
|
||||||
return mount, fmt.Errorf("container-init binary not found on the host: %w", err)
|
return mount, fmt.Errorf("container-init binary not found on the host: %w", err)
|
||||||
}
|
}
|
||||||
return mount, nil
|
return mount, nil
|
||||||
|
|
|
@ -13,6 +13,7 @@ import (
|
||||||
"github.com/containers/common/pkg/sysinfo"
|
"github.com/containers/common/pkg/sysinfo"
|
||||||
"github.com/containers/podman/v5/pkg/rootless"
|
"github.com/containers/podman/v5/pkg/rootless"
|
||||||
"github.com/containers/podman/v5/pkg/specgen"
|
"github.com/containers/podman/v5/pkg/specgen"
|
||||||
|
"github.com/containers/storage/pkg/fileutils"
|
||||||
"github.com/opencontainers/runtime-spec/specs-go"
|
"github.com/opencontainers/runtime-spec/specs-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -191,8 +192,8 @@ func verifyContainerResourcesCgroupV2(s *specgen.SpecGenerator) ([]string, error
|
||||||
|
|
||||||
memoryMax := filepath.Join("/sys/fs/cgroup", own, "memory.max")
|
memoryMax := filepath.Join("/sys/fs/cgroup", own, "memory.max")
|
||||||
memorySwapMax := filepath.Join("/sys/fs/cgroup", own, "memory.swap.max")
|
memorySwapMax := filepath.Join("/sys/fs/cgroup", own, "memory.swap.max")
|
||||||
_, errMemoryMax := os.Stat(memoryMax)
|
errMemoryMax := fileutils.Exists(memoryMax)
|
||||||
_, errMemorySwapMax := os.Stat(memorySwapMax)
|
errMemorySwapMax := fileutils.Exists(memorySwapMax)
|
||||||
// Differently than cgroup v1, the memory.*max files are not present in the
|
// Differently than cgroup v1, the memory.*max files are not present in the
|
||||||
// root directory, so we cannot query directly that, so as best effort use
|
// root directory, so we cannot query directly that, so as best effort use
|
||||||
// the current cgroup.
|
// the current cgroup.
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/containers/common/libnetwork/types"
|
"github.com/containers/common/libnetwork/types"
|
||||||
|
@ -13,6 +12,7 @@ import (
|
||||||
"github.com/containers/podman/v5/pkg/namespaces"
|
"github.com/containers/podman/v5/pkg/namespaces"
|
||||||
"github.com/containers/podman/v5/pkg/rootless"
|
"github.com/containers/podman/v5/pkg/rootless"
|
||||||
"github.com/containers/podman/v5/pkg/util"
|
"github.com/containers/podman/v5/pkg/util"
|
||||||
|
"github.com/containers/storage/pkg/fileutils"
|
||||||
storageTypes "github.com/containers/storage/types"
|
storageTypes "github.com/containers/storage/types"
|
||||||
spec "github.com/opencontainers/runtime-spec/specs-go"
|
spec "github.com/opencontainers/runtime-spec/specs-go"
|
||||||
"github.com/opencontainers/runtime-tools/generate"
|
"github.com/opencontainers/runtime-tools/generate"
|
||||||
|
@ -483,7 +483,7 @@ func SetupUserNS(idmappings *storageTypes.IDMappingOptions, userns Namespace, g
|
||||||
var user string
|
var user string
|
||||||
switch userns.NSMode {
|
switch userns.NSMode {
|
||||||
case Path:
|
case Path:
|
||||||
if _, err := os.Stat(userns.Value); err != nil {
|
if err := fileutils.Exists(userns.Value); err != nil {
|
||||||
return user, fmt.Errorf("cannot find specified user namespace path: %w", err)
|
return user, fmt.Errorf("cannot find specified user namespace path: %w", err)
|
||||||
}
|
}
|
||||||
if err := g.AddOrReplaceLinuxNamespace(string(spec.UserNamespace), userns.Value); err != nil {
|
if err := g.AddOrReplaceLinuxNamespace(string(spec.UserNamespace), userns.Value); err != nil {
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
package specgen
|
package specgen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/containers/common/pkg/machine"
|
"github.com/containers/common/pkg/machine"
|
||||||
|
"github.com/containers/storage/pkg/fileutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
func shouldResolveWinPaths() bool {
|
func shouldResolveWinPaths() bool {
|
||||||
|
@ -11,8 +10,7 @@ func shouldResolveWinPaths() bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
func shouldResolveUnixWinVariant(path string) bool {
|
func shouldResolveUnixWinVariant(path string) bool {
|
||||||
_, err := os.Stat(path)
|
return fileutils.Exists(path) != nil
|
||||||
return err != nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func resolveRelativeOnWindows(path string) string {
|
func resolveRelativeOnWindows(path string) string {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package specgen
|
package specgen
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/containers/storage/pkg/fileutils"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -26,6 +26,5 @@ func resolveRelativeOnWindows(path string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func winPathExists(path string) bool {
|
func winPathExists(path string) bool {
|
||||||
_, err := os.Stat(path)
|
return fileutils.Exists(path) == nil
|
||||||
return err == nil
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -981,7 +981,7 @@ EXPOSE 2004-2005/tcp`, ALPINE)
|
||||||
session := podmanTest.Podman([]string{"run", "-dt", "--net", "ns:/run/netns/xxy", ALPINE, "wget", "www.redhat.com"})
|
session := podmanTest.Podman([]string{"run", "-dt", "--net", "ns:/run/netns/xxy", ALPINE, "wget", "www.redhat.com"})
|
||||||
session.Wait(90)
|
session.Wait(90)
|
||||||
Expect(session).To(ExitWithError())
|
Expect(session).To(ExitWithError())
|
||||||
Expect(session.ErrorToString()).To(ContainSubstring("stat /run/netns/xxy: no such file or directory"))
|
Expect(session.ErrorToString()).To(ContainSubstring("faccessat /run/netns/xxy: no such file or directory"))
|
||||||
})
|
})
|
||||||
|
|
||||||
It("podman run in custom CNI network with --static-ip", func() {
|
It("podman run in custom CNI network with --static-ip", func() {
|
||||||
|
|
|
@ -419,7 +419,7 @@ EOF
|
||||||
|
|
||||||
myvolume=myvol$(random_string)
|
myvolume=myvol$(random_string)
|
||||||
run_podman 125 volume create -o type=bind -o device=/bogus $myvolume
|
run_podman 125 volume create -o type=bind -o device=/bogus $myvolume
|
||||||
is "$output" "Error: invalid volume option device for driver 'local': stat /bogus: no such file or directory" "should fail with bogus directory not existing"
|
is "$output" "Error: invalid volume option device for driver 'local': faccessat /bogus: no such file or directory" "should fail with bogus directory not existing"
|
||||||
|
|
||||||
run_podman volume create -o type=bind -o device=/$myvoldir $myvolume
|
run_podman volume create -o type=bind -o device=/$myvoldir $myvolume
|
||||||
is "$output" "$myvolume" "should successfully create myvolume"
|
is "$output" "$myvolume" "should successfully create myvolume"
|
||||||
|
|
Loading…
Reference in New Issue