Investigate memory limit 2766 #2790
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix memory limits not persisting after Jenkins restart
Problem
Memory limits configured for Kubernetes agent pods reset to default (1024 MB) after Jenkins restarts, even when explicitly set to higher values like 3072 MB in the cloud configuration.
Root Cause
The issue was in
PodTemplate.readResolve(). When users configure memory limits through the Jenkins UI on a PodTemplate, the deprecated settersetResourceLimitMemory()is called. This setter tries to apply the value to the first container usinggetFirstContainer().ifPresent().If the containers list is empty at that point (which is common in modern configs),
ifPresent()does nothing and the value only gets stored in the deprecatedPodTemplate.resourceLimitMemoryfield.After Jenkins restarts,
readResolve()runs to migrate old configs. However, it only checked forcontainers == null(legacy v0.8 configs), notcontainers.isEmpty(). So these deprecated field values were never migrated to actual container resource limits, and pods ended up with default memory limits.Solution
Extended the
readResolve()migration logic to handle two additional cases:This maintains backward compatibility with v0.8 configs while fixing the modern usage pattern.
Testing done
Unit Tests:
Added comprehensive test coverage in
ResourceLimitPersistenceTest.java:testContainerTemplateResourceLimitMemoryPersistsAfterXStreamSerialization- Verifies XStream serialization works correctlytestPodTemplateWithContainerResourceLimitsPersists- Verifies container-level limits persisttestPodTemplateLegacyFieldsMigrateToContainer- Reproduces and verifies the bug fix - simulates the exact scenario from issue [JENKINS-76163] Kubernetes plugin doesnt save memory limits #2766 with deprecated fields and empty containers listAll tests pass:
#2787 - Fixed counter leak with ephemeral templates after restart
#2785 - Fixed Reaper not detecting ImagePullBackOff state
Submitter checklist
Fixes #2766