Skip to content

Commit

Permalink
feat(kuma-dp): detect memory limit only on linux (#4715)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Dyszkiewicz <jakub.dyszkiewicz@gmail.com>
  • Loading branch information
jakubdyszkiewicz authored Aug 1, 2022
1 parent 9837334 commit d43e390
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 39 deletions.
5 changes: 5 additions & 0 deletions app/kuma-dp/pkg/dataplane/envoy/memory_limit_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package envoy

func DetectMaxMemory() uint64 {
return 0
}
50 changes: 50 additions & 0 deletions app/kuma-dp/pkg/dataplane/envoy/memory_limit_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package envoy

import (
"os"
"strconv"
"strings"

"github.com/containerd/cgroups"
)

type UIntOrString struct {
Type string
UInt uint64
String string
}

func DetectMaxMemory() uint64 {
switch cgroups.Mode() {
case cgroups.Legacy:
res := maybeReadAsBytes("/sys/fs/cgroup/memory.limit_in_bytes")
if res != nil && res.Type == "int" {
return res.UInt
}
case cgroups.Hybrid, cgroups.Unified:
res := maybeReadAsBytes("/sys/fs/cgroup/memory.max")
if res != nil && res.Type == "int" {
return res.UInt
}
}
return 0
}

func maybeReadAsBytes(path string) *UIntOrString {
byteContents, err := os.ReadFile(path)
if err == nil {
contents := strings.TrimSpace(string(byteContents))
bytes, err := strconv.ParseUint(contents, 10, 64)
if err != nil {
return &UIntOrString{
Type: "string",
String: contents,
}
}
return &UIntOrString{
Type: "int",
UInt: bytes,
}
}
return nil
}
5 changes: 5 additions & 0 deletions app/kuma-dp/pkg/dataplane/envoy/memory_limit_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package envoy

func DetectMaxMemory() uint64 {
return 0
}
40 changes: 1 addition & 39 deletions app/kuma-dp/pkg/dataplane/envoy/remote_bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ import (
"net/http"
net_url "net/url"
"os"
"strconv"
"strings"

"github.com/containerd/cgroups"
envoy_bootstrap_v3 "github.com/envoyproxy/go-control-plane/envoy/config/bootstrap/v3"
"github.com/pkg/errors"
"github.com/sethvargo/go-retry"
Expand Down Expand Up @@ -111,47 +109,11 @@ func (b *remoteBootstrap) Generate(ctx context.Context, url string, cfg kuma_dp.
return envoyBootstrap, &bootstrap.KumaSidecarConfiguration, nil
}

type UIntOrString struct {
Type string
UInt uint64
String string
}

func maybeReadAsBytes(path string) *UIntOrString {
byteContents, err := os.ReadFile(path)
if err == nil {
contents := strings.TrimSpace(string(byteContents))
bytes, err := strconv.ParseUint(contents, 10, 64)
if err != nil {
return &UIntOrString{
Type: "string",
String: contents,
}
}
return &UIntOrString{
Type: "int",
UInt: bytes,
}
}
return nil
}

func (b *remoteBootstrap) resourceMetadata(cfg kuma_dp.DataplaneResources) (types.ProxyResources, error) {
var maxMemory uint64

if cfg.MaxMemoryBytes == 0 {
switch cgroups.Mode() {
case cgroups.Legacy:
res := maybeReadAsBytes("/sys/fs/cgroup/memory.limit_in_bytes")
if res != nil && res.Type == "int" {
maxMemory = res.UInt
}
case cgroups.Hybrid, cgroups.Unified:
res := maybeReadAsBytes("/sys/fs/cgroup/memory.max")
if res != nil && res.Type == "int" {
maxMemory = res.UInt
}
}
maxMemory = DetectMaxMemory()
} else {
maxMemory = cfg.MaxMemoryBytes
}
Expand Down

0 comments on commit d43e390

Please sign in to comment.