Skip to content
Permalink
Browse files
fix(buildah): Buildah mode autodetection
  • Loading branch information
ilya-lesikov committed Dec 9, 2021
1 parent 7e21c0c commit 80b9e903b61c391dacfaee674f953b4bd591c443
Showing with 20 additions and 22 deletions.
  1. +2 −4 cmd/werf/common/container_runtime.go
  2. +3 −8 pkg/buildah/common.go
  3. +15 −10 pkg/util/linux_container.go
@@ -38,10 +38,8 @@ func GetBuildahMode() (*buildah.Mode, *thirdparty.Isolation, error) {
modeRaw := os.Getenv("WERF_BUILDAH_MODE")
switch modeRaw {
case "native-rootless":
if isInContainer, err := util.IsInContainer(); err != nil {
return nil, nil, fmt.Errorf("unable to determine if is in container: %s", err)
} else if isInContainer {
return nil, nil, fmt.Errorf("native rootless mode is not available in containers: %s", err)
if util.IsInContainer() {
return nil, nil, fmt.Errorf("native rootless mode is not available in containers")
}
mode = buildah.ModeNative
isolation = thirdparty.IsolationOCIRootless
@@ -183,23 +183,18 @@ func GetOverlayOptions() ([]string, error) {

result := []string{fmt.Sprintf("overlay.mount_program=%s", fuseOverlayBinPath)}

if isInContainer, err := util.IsInContainer(); err != nil {
return nil, fmt.Errorf("unable to determine whether we are in the container: %s", err)
} else if isInContainer {
if util.IsInContainer() {
result = append(result, fmt.Sprintf("overlay.mountopt=%s", "nodev,fsync=0"))
}

return result, nil
}

func GetDefaultIsolation() (thirdparty.Isolation, error) {
if isInContainer, err := util.IsInContainer(); err != nil {
return 0, fmt.Errorf("unable to determine if is in container: %s", err)
} else if isInContainer {
if util.IsInContainer() {
return thirdparty.IsolationChroot, nil
} else {
return thirdparty.IsolationOCIRootless, nil
}
return thirdparty.IsolationOCIRootless, nil
}

func debug() bool {
@@ -3,6 +3,7 @@ package util
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

@@ -36,18 +37,22 @@ func ToLinuxContainerPath(path string) string {
)
}

func IsInContainer() (bool, error) {
if dockerEnvExist, err := RegularFileExists("/.dockerenv"); err != nil {
return false, fmt.Errorf("unable to check for /.dockerenv existence: %s", err)
} else if dockerEnvExist {
return true, nil
func IsInContainer() bool {
// Docker-daemon
if isInContainer, err := RegularFileExists("/.dockerenv"); err == nil && isInContainer {
return true
}

if containerEnvExist, err := RegularFileExists("/run/.containerenv"); err != nil {
return false, fmt.Errorf("unable to check for /run/.containerenv existence: %s", err)
} else if containerEnvExist {
return true, nil
// Podman, CRI-O
if isInContainer, err := RegularFileExists("/run/.containerenv"); err == nil && isInContainer {
return true
}

return false, nil
// containerd without Docker-daemon
if cgroupsData, err := os.ReadFile("/proc/1/cgroup"); err == nil &&
strings.Contains(string(cgroupsData), "/cri-containerd-") {
return true
}

return false
}

0 comments on commit 80b9e90

Please sign in to comment.