Skip to content

fix: disable service links in bootstrap job pods - #8943

Open
usiegj00 wants to merge 2 commits into
cloudnative-pg:mainfrom
usiegj00:dev/6234
Open

usiegj00 wants to merge 2 commits into
cloudnative-pg:mainfrom
usiegj00:dev/6234

Conversation

@usiegj00

Copy link
Copy Markdown

Summary

This PR fixes the "argument list too long" error in bootstrap job pods that occurs in namespaces with many services, completing the fix started in PR #6323.

Problem

Issue #6234 reported that bootstrap controller containers fail with exec /manager: argument list too long in namespaces with many services (reported in production with 465 PostgreSQL databases).

Critical limitation: Even with the cnpg.io/podPatch annotation setting enableServiceLinks: false for regular cluster instance pods (fixed in PR #6323), clusters still cannot launch at this scale because bootstrap job pods were not covered by that fix.

Why job pods are affected:

  1. Bootstrap jobs are created separately in pkg/specs/jobs.go via CreatePrimaryJob()
  2. These jobs create their own pod specs that don't go through the buildInstance() function where the patch annotation is applied
  3. The bootstrap-controller init container in these job pods receives the same excessive environment variables from Kubernetes service links
  4. Since jobs must complete before the cluster can start, this failure prevents cluster creation entirely

Solution

Set enableServiceLinks: false directly in the job pod spec in pkg/specs/jobs.go. This affects all bootstrap job types:

  • initdb jobs (initialize new cluster)
  • join jobs (join replica to cluster)
  • pg_basebackup jobs
  • Recovery/restore jobs

Alternative Solutions Considered

We considered several approaches:

  • Extending the cnpg.io/podPatch annotation to cover job pods
  • Adding a new CRD field for enableServiceLinks
  • Using policy-based approaches (Kyverno, etc.)

However, setting enableServiceLinks: false directly in the job spec appears to have no negative side-effects. Bootstrap jobs do not require service discovery via environment variables as they use DNS or explicit connection strings from secrets, making this a safe and simple default that resolves this critical aspect of large-cluster scaling challenges.

Testing

  • Added unit test to verify EnableServiceLinks is set to false
  • All existing tests pass

Related Issues

Fixes #6234

@usiegj00
usiegj00 requested a review from a team October 25, 2025 15:38
@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Oct 25, 2025
@cnpg-bot cnpg-bot added backport-requested ◀️ This pull request should be backported to all supported releases release-1.25 labels Oct 25, 2025
@github-actions

Copy link
Copy Markdown
Contributor

❗ By default, the pull request is configured to backport to all release branches.

  • To stop backporting this pr, remove the label: backport-requested ◀️ or add the label 'do not backport'
  • To stop backporting this pr to a certain release branch, remove the specific branch label: release-x.y

@dosubot dosubot Bot added bug 🐛 Something isn't working ok to merge 👌 This PR can be merged labels Oct 25, 2025
@mnencia mnencia added do not backport This PR must not be backported - it will be in the next minor release and removed backport-requested ◀️ This pull request should be backported to all supported releases release-1.28 release-1.29 release-1.30 labels Jul 15, 2026
@mnencia mnencia added this to the 1.31.0 milestone Jul 15, 2026
…list too long errors

This change sets enableServiceLinks to false in all bootstrap job pod specs
(initdb, join, recovery, etc.) to prevent the 'exec /manager: argument list
too long' error that occurs in namespaces with many services.

While PR cloudnative-pg#6323 fixed this issue for regular cluster instance pods by
introducing the cnpg.io/podPatch annotation, bootstrap jobs are created
separately and were not covered by that fix. Since these jobs use the same
bootstrap-controller init container that fails when receiving excessive
environment variables from Kubernetes service links, they experience the
same issue in high-density namespaces.

This issue was reported in a production environment with 465 PostgreSQL
databases in a single namespace, where the bootstrap controller in job pods
would fail to start during cluster initialization or when adding new replicas.

Bootstrap jobs do not require service discovery via environment variables
as they use DNS or explicit connection strings from secrets, making this
a safe default.

Fixes cloudnative-pg#6234

Signed-off-by: Jonathan Siegel <[email protected]>
@gbartolini

Copy link
Copy Markdown
Contributor

Hi @usiegj00, thank you so much for your contribution and apologies for the delay.

In today's office hours, we agreed to set enableServiceLinks: false by default on both jobs and pods just in 1.31.0, without backporting the feature. Could you please update the PR?

@dosubot dosubot Bot added size:XS This PR changes 0-9 lines, ignoring generated files. and removed size:S This PR changes 10-29 lines, ignoring generated files. labels Jul 15, 2026
Extend the enableServiceLinks=false default from bootstrap jobs to the
PostgreSQL instance pods.

Setting EnableServiceLinks to false on the instance PodSpec built by
createClusterPodSpec prevents the "exec /manager: argument list too long"
error in namespaces with many services, without relying on the
cnpg.io/podPatch annotation workaround from cloudnative-pg#6323. Also aligns the
service-links field formatting in the bootstrap job spec.

Signed-off-by: Jonathan Siegel <[email protected]>
@usiegj00

Copy link
Copy Markdown
Author

Thanks @gbartolini, and no problem on the delay. I've updated the PR to match the office-hours decision:

  • Extended the enableServiceLinks: false default to the instance pods as well (in createClusterPodSpec), so it now applies to both jobs and pods by default for 1.31.0 — no backport.
  • Added a NewInstance test asserting the instance PodSpec has EnableServiceLinks: false.
  • Also fixed the gofmt alignment on the Hostname/EnableServiceLinks fields in the bootstrap job spec (the previous commit's formatting would otherwise fail the format check).

@kfcss

kfcss commented Sep 23, 2026

Copy link
Copy Markdown

@gbartolini could this get merged? I really want to see this in the new release. It fixes bugs and actually also fixes performance issues for wal-restore and backups. We have 25k environment variables and https://github.com/cloudnative-pg/plugin-barman-cloud/blob/2db8cc37074254633293762a3a1b33b88334f5ca/internal/cnpgi/common/common.go#L76-L99 is an O(n^2) algorithm. There is a fix for the algorithm here: cloudnative-pg/plugin-barman-cloud#1114, but it would not have been a problem if the 25k environment variables was not there in the first place.

@gnarlex gnarlex left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for you contribution :) Change is quite small and shouldn't cause regressions, since nothing depends on the service links.

@gbartolini could you review as well?

danilmolkov added a commit to danilmolkov/opensearch-k8s-operator that referenced this pull request Nov 25, 2026
Create the bootstrap pod with enableServiceLinks: false, so Kubernetes no
longer injects environment variables for every Service in the namespace
into it. The bootstrap pod does not use these variables. In a namespace
with many Services they make the container fail to start with
"argument list too long".

Setting the value in the operator also removes the need for a mutating
policy (such as Kyverno) to set it. On releases without the fix for opensearch-project#1364,
such a policy makes the operator recreate the bootstrap pod in a loop.

CloudNativePG made the same change for its bootstrap job pods
(cloudnative-pg/cloudnative-pg#8943, which fixes
cloudnative-pg/cloudnative-pg#6234).

Signed-off-by: Daniil Molkov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐛 Something isn't working do not backport This PR must not be backported - it will be in the next minor release ok to merge 👌 This PR can be merged size:XS This PR changes 0-9 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: "exec /manager: argument list too long" in bootstrap-controller

6 participants