[ISSUE-2830] Let yaml override inherited containerTemplate fields - #2855
[ISSUE-2830] Let yaml override inherited containerTemplate fields#2855RajeshRajendiran wants to merge 4 commits into
Conversation
When a pod template uses inheritFrom, a container that only exists because it was inherited from a parent's containerTemplate was always preferred over a same-named container defined in the child's own yaml block, regardless of yamlMergeStrategy. containerTemplate overrides worked correctly since they're merged by name earlier, in the PodTemplate-level combine, so the two mechanisms behaved inconsistently for the same use case. Track which container names are declared directly on a template (not inherited) and, only for containers that are purely inherited, let yaml take precedence over the inherited containerTemplate fields during the final pod merge. Containers declared locally via containerTemplate keep taking priority over yaml, unchanged.
Text block opener must stay on the same line as setYaml(, per the project's spotless style (google-java-format).
| PodTemplateUtilsTest.assertQuantity("1", jnlp.getResources().getLimits().get("cpu")); | ||
| assertEquals("jenkins-jnlp-override", jnlp.getImage()); | ||
| PodTemplateUtilsTest.assertQuantity("2", jnlp.getResources().getLimits().get("cpu")); |
There was a problem hiding this comment.
So, changing tested behavior dating to #372 (eight years ago) and potentially breaking working setups.
There was a problem hiding this comment.
Fair concern — this does flip behavior that's been pinned since #372. To be precise about the blast radius: the change only affects containers that are purely inherited from a parent's containerTemplate with no local
override at all.
If a template redeclares the container via its own containerTemplate (the common override pattern), that still wins over yaml, unchanged.
So the only setups affected are ones using inheritFrom + a yaml block to override a container that already exists in the parent template — which per #2830 currently silently does nothing, so I'd guess most of those configs are unintentionally inert today rather than intentionally relying on the old precedence. But I don't have visibility into how common that pattern is in the wild.
Would you be more comfortable with this landing as a straight fix, or would you rather see it gated behind an opt-in flag (e.g. on PodTemplate) so existing behavior is preserved by default and users opt into the corrected merge order? Happy to go either way, just don't want to guess wrong on compat expectations here.
Summary
Fixes #2830.
When a pod template uses
inheritFrom, a container defined in the parent'scontainerTemplatewas always preferred over a same-named container definedin the child's own
yamlblock — even though the child'syamlis the morespecific, local override. This happened regardless of
yamlMergeStrategy.containerTemplate-based overrides worked correctly, because they're mergedby container name earlier, during the
PodTemplate-levelcombine().yamloverrides were merged later at the
Podlevel, where the already-mergedcontainerTemplate-derived container list unconditionally won overyamlfor any matching container name — so the two override mechanisms behaved
inconsistently for what should be the same use case.
Fix
PodTemplatenow tracks which container names were declared directly onthat template (as opposed to only present via inheritance).
PodTemplateBuilder, containers that arepurely inherited (not redeclared locally) now let
yamltake precedence,matching how
containerTemplateoverrides already behaved.containerTemplateare unaffected andkeep taking priority over
yaml, exactly as before.This is intentionally scoped to the
inheritFromcase only. Precedencebetween
containerTemplateandyamlwithin a single (non-inherited)template is unchanged.
Test plan
PodTemplateBuilderTest#testInheritsFromWithYaml, whichpreviously pinned the old (documented as "counter intuitive")
behavior, to assert the corrected precedence.
PodTemplateBuilderTest#yamlOverridesInheritedContainerTemplate,reproducing the exact scenario from container in declarative pipeline
yamlignored whilecontainerTemplateworks #2830 and asserting thatcontainerTemplateandyamloverrides now behave identically.