Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kuma-dp): detect memory limit only on linux #4715

Merged
merged 1 commit into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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