Skip to content

feat: add GOMEMLIMIT env var to Helm chart derived from memory resource limit [backport #1061] - #1064

Merged
kalbasit merged 1 commit into
release-0.9from
backport-1061-to-release-0.9
Mar 11, 2026
Merged

kalbasit merged 1 commit into
release-0.9from
backport-1061-to-release-0.9

Conversation

@kalbasit

Copy link
Copy Markdown
Owner

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:

  • 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

…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)
@kalbasit
kalbasit enabled auto-merge (squash) March 11, 2026 05:37
@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request helm labels Mar 11, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Automatic GOMEMLIMIT Configuration: The Helm chart now automatically injects the GOMEMLIMIT environment variable into Go applications, deriving its value from the container's memory resource limit using the Kubernetes Downward API.
  • Improved Go Memory Management: This change ensures the Go runtime's soft memory limit aligns with the container's limit, promoting more aggressive garbage collection and reducing the likelihood of Out-Of-Memory (OOM) kills.
  • Comprehensive Testing: New Helm unit tests have been added to validate the correct behavior of GOMEMLIMIT injection in both Deployment and StatefulSet configurations, covering scenarios with and without memory limits.
  • Updated Documentation: The Chart Reference documentation has been updated to include a detailed explanation of the GOMEMLIMIT feature, its benefits, and how to override its value if needed.
Changelog
  • charts/ncps/templates/deployment.yaml
    • Injected GOMEMLIMIT environment variable using resourceFieldRef for limits.memory when a memory limit is defined and GOMEMLIMIT is not already set via extraEnvVars.
  • charts/ncps/templates/statefulset.yaml
    • Injected GOMEMLIMIT environment variable using resourceFieldRef for limits.memory when a memory limit is defined and GOMEMLIMIT is not already set via extraEnvVars.
  • charts/ncps/tests/deployment_test.yaml
    • Added four new unit tests to verify the correct behavior of GOMEMLIMIT injection in both Deployment and StatefulSet configurations, covering cases where memory limits are set and not set.
  • docs/!!!meta.json
    • Updated the appVersion to "0.102.0".
    • Adjusted the order and positions of several internal links within the document metadata.
  • docs/docs/User Guide/Installation/Helm Chart/Chart Reference.md
    • Added a detailed note explaining the GOMEMLIMIT feature, its benefits, and how to override it.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@github-actions

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 GOMEMLIMIT is already defined can be simplified in both deployment.yaml and statefulset.yaml by using the where function.
  • The tests should be expanded to cover the case where a user explicitly sets GOMEMLIMIT via extraEnvVars, ensuring the chart does not override it.

These changes will improve the maintainability and robustness of the chart. Overall, this is a solid contribution.

Comment on lines +143 to +154
{{- $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 }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 }}

Comment on lines +134 to +145
{{- $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 }}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 }}

Comment on lines +507 to +572
# 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.memory

A similar test should be added for the StatefulSet as well.

@codecov

codecov Bot commented Mar 11, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 5.04%. Comparing base (422d695) to head (6a4e296).
⚠️ Report is 1 commits behind head on release-0.9.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@kalbasit
kalbasit merged commit f1cd76c into release-0.9 Mar 11, 2026
13 checks passed
@kalbasit
kalbasit deleted the backport-1061-to-release-0.9 branch March 11, 2026 05:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request helm size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant