Repository navigation
feat: add GOMEMLIMIT env var to Helm chart derived from memory resource limit [backport #1061] - #1064
Conversation
…ce limit (#1061) Set GOMEMLIMIT automatically from resources.limits.memory in both Deployment and StatefulSet modes using the Kubernetes Downward API resourceFieldRef. This keeps the Go runtime soft memory limit in sync with the container's memory limit, causing GC to run more aggressively as memory approaches the limit and preventing OOM kills. The env var is only injected when resources.limits.memory is set, matching the condition under which a memory limit is meaningful. Both deployment and statefulset templates are updated identically. Four Helm unit tests are added to deployment_test.yaml covering: - GOMEMLIMIT present in Deployment when memory limit is set - GOMEMLIMIT absent in Deployment when no memory limit - GOMEMLIMIT present in StatefulSet when memory limit is set - GOMEMLIMIT absent in StatefulSet when no memory limit (cherry picked from commit 6cdc72d)
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the Helm chart by automatically configuring the GOMEMLIMIT environment variable for Go applications running in Kubernetes. This ensures that the Go runtime's soft memory limit is synchronized with the container's memory limit, leading to more efficient garbage collection and reduced risk of out-of-memory terminations. The change applies to both Deployment and StatefulSet modes, leveraging the Kubernetes Downward API, and is supported by new unit tests and updated documentation. Highlights
Changelog
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds a useful feature to automatically set GOMEMLIMIT based on container memory limits, which is a great practice for Go applications in Kubernetes. The implementation is sound and includes tests and documentation.
My review includes a few suggestions for improvement:
- The Helm template logic for checking if
GOMEMLIMITis already defined can be simplified in bothdeployment.yamlandstatefulset.yamlby using thewherefunction. - The tests should be expanded to cover the case where a user explicitly sets
GOMEMLIMITviaextraEnvVars, ensuring the chart does not override it.
These changes will improve the maintainability and robustness of the chart. Overall, this is a solid contribution.
| {{- $scratch := dict "gomemlimitDefined" false }} | ||
| {{- range .Values.extraEnvVars }} | ||
| {{- if eq .name "GOMEMLIMIT" }} | ||
| {{- $_ := set $scratch "gomemlimitDefined" true }} | ||
| {{- end }} | ||
| {{- end }} | ||
| {{- if and .Values.resources .Values.resources.limits .Values.resources.limits.memory (not (get $scratch "gomemlimitDefined")) }} | ||
| - name: GOMEMLIMIT | ||
| valueFrom: | ||
| resourceFieldRef: | ||
| resource: limits.memory | ||
| {{- end }} |
There was a problem hiding this comment.
The logic to check if GOMEMLIMIT is already defined in extraEnvVars can be simplified. Instead of using a $scratch dictionary and ranging over the list, you can use the where function to make the template more concise and readable.
{{- $gomemlimitInExtra := where .Values.extraEnvVars "name" "GOMEMLIMIT" }}
{{- if and .Values.resources .Values.resources.limits .Values.resources.limits.memory (not $gomemlimitInExtra) }}
- name: GOMEMLIMIT
valueFrom:
resourceFieldRef:
resource: limits.memory
{{- end }}| {{- $scratch := dict "gomemlimitDefined" false }} | ||
| {{- range .Values.extraEnvVars }} | ||
| {{- if eq .name "GOMEMLIMIT" }} | ||
| {{- $_ := set $scratch "gomemlimitDefined" true }} | ||
| {{- end }} | ||
| {{- end }} | ||
| {{- if and .Values.resources .Values.resources.limits .Values.resources.limits.memory (not (get $scratch "gomemlimitDefined")) }} | ||
| - name: GOMEMLIMIT | ||
| valueFrom: | ||
| resourceFieldRef: | ||
| resource: limits.memory | ||
| {{- end }} |
There was a problem hiding this comment.
Similar to deployment.yaml, the logic to check if GOMEMLIMIT is already defined in extraEnvVars can be simplified. Using the where function will make the template more concise and readable.
{{- $gomemlimitInExtra := where .Values.extraEnvVars "name" "GOMEMLIMIT" }}
{{- if and .Values.resources .Values.resources.limits .Values.resources.limits.memory (not $gomemlimitInExtra) }}
- name: GOMEMLIMIT
valueFrom:
resourceFieldRef:
resource: limits.memory
{{- end }}| # GOMEMLIMIT tests | ||
| - it: should set GOMEMLIMIT from memory limit in Deployment | ||
| template: deployment.yaml | ||
| set: | ||
| mode: deployment | ||
| config.hostname: test.example.com | ||
| config.storage.type: s3 | ||
| config.storage.s3.bucket: my-bucket | ||
| config.storage.s3.endpoint: https://s3.amazonaws.com | ||
| config.storage.s3.accessKeyId: test-key | ||
| config.storage.s3.secretAccessKey: test-secret | ||
| resources: | ||
| limits: | ||
| memory: 4Gi | ||
| asserts: | ||
| - contains: | ||
| path: spec.template.spec.containers[0].env | ||
| content: | ||
| name: GOMEMLIMIT | ||
| valueFrom: | ||
| resourceFieldRef: | ||
| resource: limits.memory | ||
|
|
||
| - it: should not set GOMEMLIMIT when no memory limit in Deployment | ||
| template: deployment.yaml | ||
| set: | ||
| mode: deployment | ||
| config.hostname: test.example.com | ||
| config.storage.type: s3 | ||
| config.storage.s3.bucket: my-bucket | ||
| config.storage.s3.endpoint: https://s3.amazonaws.com | ||
| config.storage.s3.accessKeyId: test-key | ||
| config.storage.s3.secretAccessKey: test-secret | ||
| asserts: | ||
| - notContains: | ||
| path: spec.template.spec.containers[0].env | ||
| content: | ||
| name: GOMEMLIMIT | ||
|
|
||
| - it: should set GOMEMLIMIT from memory limit in StatefulSet | ||
| template: statefulset.yaml | ||
| set: | ||
| mode: statefulset | ||
| config.hostname: test.example.com | ||
| resources: | ||
| limits: | ||
| memory: 8Gi | ||
| asserts: | ||
| - contains: | ||
| path: spec.template.spec.containers[0].env | ||
| content: | ||
| name: GOMEMLIMIT | ||
| valueFrom: | ||
| resourceFieldRef: | ||
| resource: limits.memory | ||
|
|
||
| - it: should not set GOMEMLIMIT when no memory limit in StatefulSet | ||
| template: statefulset.yaml | ||
| set: | ||
| mode: statefulset | ||
| config.hostname: test.example.com | ||
| asserts: | ||
| - notContains: | ||
| path: spec.template.spec.containers[0].env | ||
| content: | ||
| name: GOMEMLIMIT |
There was a problem hiding this comment.
The new tests cover cases where GOMEMLIMIT is set or not set based on memory limits. However, there's no test to verify that a user-defined GOMEMLIMIT in extraEnvVars is respected and not overridden by the chart's logic. It would be good to add test cases for this scenario for both Deployment and StatefulSet to ensure the override protection works as expected.
For example, you could add a test case like this for the Deployment:
- it: should not override GOMEMLIMIT if set in extraEnvVars for Deployment
template: deployment.yaml
set:
mode: deployment
config.hostname: test.example.com
resources:
limits:
memory: 4Gi
extraEnvVars:
- name: GOMEMLIMIT
value: "2Gi"
asserts:
- contains:
path: spec.template.spec.containers[0].env
content:
name: GOMEMLIMIT
value: "2Gi"
- notContains:
path: spec.template.spec.containers[0].env
content:
name: GOMEMLIMIT
valueFrom:
resourceFieldRef:
resource: limits.memoryA similar test should be added for the StatefulSet as well.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## release-0.9 #1064 +/- ##
===========================================
Coverage 5.04% 5.04%
===========================================
Files 6 6
Lines 436 436
===========================================
Hits 22 22
Misses 410 410
Partials 4 4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Bot-based backport to
release-0.9, triggered by a label in #1061.Set GOMEMLIMIT automatically from resources.limits.memory in both
Deployment and StatefulSet modes using the Kubernetes Downward API
resourceFieldRef. This keeps the Go runtime soft memory limit in sync
with the container's memory limit, causing GC to run more aggressively
as memory approaches the limit and preventing OOM kills.
The env var is only injected when resources.limits.memory is set,
matching the condition under which a memory limit is meaningful. Both
deployment and statefulset templates are updated identically.
Four Helm unit tests are added to deployment_test.yaml covering: