Skip to content

Commit

Permalink
configure the go runtime to honour the k8s memory and cpu resource li…
Browse files Browse the repository at this point in the history
…mits
  • Loading branch information
rgl committed Jun 8, 2024
1 parent 66eba59 commit 8e89201
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ It will:
* Show the containers running inside the container pod.
* Show the container memory limits.
* Show the container cgroups.
* Configure [the Go runtime](https://pkg.go.dev/runtime) to honour the defined kubernetes container memory and cpu resource limits with the `GOMEMLIMIT` and `GOMAXPROCS` environment variables.
* Expose as a Kubernetes `LoadBalancer` `Service`.
* Note that this results in the creation of an [EC2 Classic Load Balancer (CLB)](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/introduction.html).
* Use [Role and RoleBinding](https://kubernetes.io/docs/reference/access-authn-authz/rbac/).
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ table > tbody > tr:hover {
<tr><th>Hostname</th><td>{{.Hostname}}</td></tr>
<tr><th>Pod Containers</th><td>{{.PodContainers}}</td></tr>
<tr><th>Cgroup</th><td>{{.Cgroup}}</td></tr>
<tr><th>Go Max CPUs</th><td>{{.GoMaxCPUs}}</td></tr>
<tr><th>Memory Limit</th><td>{{.MemoryLimit}}</td></tr>
<tr><th>Memory Usage</th><td>{{.MemoryUsage}}</td></tr>
<tr><th>Os</th><td>{{.Os}}</td></tr>
Expand Down Expand Up @@ -297,6 +298,7 @@ type indexData struct {
Gid int
PodContainers string
Cgroup string
GoMaxCPUs int
MemoryLimit uint64
MemoryUsage uint64
Request string
Expand Down Expand Up @@ -493,6 +495,7 @@ func main() {
Gid: os.Getgid(),
PodContainers: podContainers,
Cgroup: string(cgroup),
GoMaxCPUs: runtime.GOMAXPROCS(0),
MemoryLimit: memoryInfo.Limit,
MemoryUsage: memoryInfo.Usage,
Request: fmt.Sprintf("%s %s%s", r.Method, r.Host, r.URL),
Expand Down
16 changes: 16 additions & 0 deletions resources.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,22 @@ spec:
memory: 20Mi
cpu: '0.1'
env:
# configure the go runtime to honour the k8s memory and cpu
# resource limits.
# NB resourceFieldRef will cast the limits to bytes and integer
# number of cpus (rounding up to the nearest integer).
# see https://pkg.go.dev/runtime
# see https://www.riverphillips.dev/blog/go-cfs/
# see https://github.com/golang/go/issues/33803
# see https://github.com/traefik/traefik-helm-chart/pull/1029
- name: GOMEMLIMIT
valueFrom:
resourceFieldRef:
resource: limits.memory
- name: GOMAXPROCS
valueFrom:
resourceFieldRef:
resource: limits.cpu
# see https://github.com/kubernetes/kubernetes/blob/master/test/e2e/common/downward_api.go
- name: POD_UID
valueFrom:
Expand Down

0 comments on commit 8e89201

Please sign in to comment.