Skip to content

Commit

Permalink
feat(buildah): auto Buildah Ulimits from current user ulimits
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Lesikov <ilya@lesikov.com>
  • Loading branch information
ilya-lesikov committed Jul 25, 2022
1 parent 734963a commit 28d4d28
Showing 1 changed file with 58 additions and 4 deletions.
62 changes: 58 additions & 4 deletions pkg/buildah/native_linux.go
Expand Up @@ -10,7 +10,9 @@ import (
"io"
"os"
"path/filepath"
"strconv"
"strings"
"syscall"
"time"

"github.com/containers/buildah"
Expand Down Expand Up @@ -113,12 +115,20 @@ func NewNativeBuildah(commonOpts CommonBuildahOpts, opts NativeModeOpts) (*Nativ
}
}

b.DefaultCommonBuildOptions = define.CommonBuildOptions{
ShmSize: DefaultShmSize,
var ulimit []string
if ulmt := os.Getenv("WERF_BUILDAH_ULIMIT"); ulmt != "" {
ulimit = strings.Split(ulmt, ",")
} else {
rlimits, err := currentRlimits()
if err != nil {
return nil, fmt.Errorf("error getting current rlimits: %w", err)
}
ulimit = rlimitsToBuildahUlimits(rlimits)
}

if ulimit := os.Getenv("WERF_BUILDAH_ULIMIT"); ulimit != "" {
b.DefaultCommonBuildOptions.Ulimit = strings.Split(ulimit, ",")
b.DefaultCommonBuildOptions = define.CommonBuildOptions{
ShmSize: DefaultShmSize,
Ulimit: ulimit,
}

imgstor.Transport.SetStore(b.Store)
Expand Down Expand Up @@ -632,3 +642,47 @@ func newHealthConfigFromString(healthcheck string) (*docker.HealthConfig, error)

return healthConfig, nil
}

func currentRlimits() (map[int]*syscall.Rlimit, error) {
result := map[int]*syscall.Rlimit{
syscall.RLIMIT_CORE: {},
syscall.RLIMIT_CPU: {},
syscall.RLIMIT_DATA: {},
syscall.RLIMIT_FSIZE: {},
syscall.RLIMIT_NOFILE: {},
syscall.RLIMIT_STACK: {},
}

for k, v := range result {
if err := syscall.Getrlimit(k, v); err != nil {
return nil, fmt.Errorf("error getting rlimit: %w", err)
}
}

return result, nil
}

func rlimitsToBuildahUlimits(rlimits map[int]*syscall.Rlimit) []string {
rlimitToBuildahUlimitFn := func(rlimitKey int, buildahKey string) string {
rlimitUintToStrFn := func(val uint64) string {
if int64(val) < 0 {
return strconv.FormatInt(int64(val), 10)
} else {
return strconv.FormatUint(val, 10)
}
}

cur := rlimitUintToStrFn(rlimits[rlimitKey].Cur)
max := rlimitUintToStrFn(rlimits[rlimitKey].Max)
return fmt.Sprintf("%s=%s:%s", buildahKey, cur, max)
}

return []string{
rlimitToBuildahUlimitFn(syscall.RLIMIT_CORE, "core"),
rlimitToBuildahUlimitFn(syscall.RLIMIT_CPU, "cpu"),
rlimitToBuildahUlimitFn(syscall.RLIMIT_DATA, "data"),
rlimitToBuildahUlimitFn(syscall.RLIMIT_FSIZE, "fsize"),
rlimitToBuildahUlimitFn(syscall.RLIMIT_NOFILE, "nofile"),
rlimitToBuildahUlimitFn(syscall.RLIMIT_STACK, "stack"),
}
}

0 comments on commit 28d4d28

Please sign in to comment.