Skip to content

Commit 80b9e90

Browse files
committed
fix(buildah): Buildah mode autodetection
1 parent 7e21c0c commit 80b9e90

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

cmd/werf/common/container_runtime.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ func GetBuildahMode() (*buildah.Mode, *thirdparty.Isolation, error) {
3838
modeRaw := os.Getenv("WERF_BUILDAH_MODE")
3939
switch modeRaw {
4040
case "native-rootless":
41-
if isInContainer, err := util.IsInContainer(); err != nil {
42-
return nil, nil, fmt.Errorf("unable to determine if is in container: %s", err)
43-
} else if isInContainer {
44-
return nil, nil, fmt.Errorf("native rootless mode is not available in containers: %s", err)
41+
if util.IsInContainer() {
42+
return nil, nil, fmt.Errorf("native rootless mode is not available in containers")
4543
}
4644
mode = buildah.ModeNative
4745
isolation = thirdparty.IsolationOCIRootless

pkg/buildah/common.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,23 +183,18 @@ func GetOverlayOptions() ([]string, error) {
183183

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

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

192190
return result, nil
193191
}
194192

195193
func GetDefaultIsolation() (thirdparty.Isolation, error) {
196-
if isInContainer, err := util.IsInContainer(); err != nil {
197-
return 0, fmt.Errorf("unable to determine if is in container: %s", err)
198-
} else if isInContainer {
194+
if util.IsInContainer() {
199195
return thirdparty.IsolationChroot, nil
200-
} else {
201-
return thirdparty.IsolationOCIRootless, nil
202196
}
197+
return thirdparty.IsolationOCIRootless, nil
203198
}
204199

205200
func debug() bool {

pkg/util/linux_container.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package util
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"path/filepath"
78
"strings"
89

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

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

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

52-
return false, nil
51+
// containerd without Docker-daemon
52+
if cgroupsData, err := os.ReadFile("/proc/1/cgroup"); err == nil &&
53+
strings.Contains(string(cgroupsData), "/cri-containerd-") {
54+
return true
55+
}
56+
57+
return false
5358
}

0 commit comments

Comments
 (0)