Skip to content

fix(common): inject STATEFUL_REPLICA_COUNT for ordinal election - #4124

Open
shashankxrm wants to merge 1 commit into
tektoncd:mainfrom
shashankxrm:fix/stateful-replica-count-3384
Open

shashankxrm wants to merge 1 commit into
tektoncd:mainfrom
shashankxrm:fix/stateful-replica-count-3384

Conversation

@shashankxrm

@shashankxrm shashankxrm commented Sep 19, 2026 •

Copy link
Copy Markdown

Changes

When StatefulSet ordinal leader election is enabled, inject STATEFUL_REPLICA_COUNT from the final rendered StatefulSet.spec.replicas.

  • Add STATEFUL_REPLICA_COUNT in AddStatefulEnvVars from the current StatefulSet replica count (nil is treated as 1).
  • Synchronize that env after ExecuteAdditionalOptionsTransformer, so additional options that override replicas are reflected on the pod.
  • Apply the sync for Pipeline (controller and remote resolvers), Chains, and Results.
  • Update only StatefulSets that already have STATEFUL_CONTROLLER_ORDINAL.

AddStatefulEnvVars alone is not enough: it runs before additional options, which can change spec.replicas (for example performance replicas 4 overridden to 8). The pod must receive 8, not the earlier value.

This does not change 1:1 ordinal-to-bucket ownership or redistribute buckets. It supplies the replica-count information required by the knative/pkg StatefulSet ordinal validation from knative/pkg#3384.

Related to knative/pkg#3384.

Submitter Checklist

These are the criteria that every PR should meet, please check them off as you
review them:

Release Notes

Inject STATEFUL_REPLICA_COUNT from the rendered StatefulSet replica count when ordinal leader election is enabled.

@tekton-robot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
To complete the pull request process, please assign jkhelil after the PR has been reviewed.
You can assign the PR to them by writing /assign @jkhelil in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@tekton-robot tekton-robot added the do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. label Sep 19, 2026
@linux-foundation-easycla

linux-foundation-easycla Bot commented Sep 19, 2026 •

Copy link
Copy Markdown

CLA Signed
The committers listed above are authorized under a signed CLA.

  • ✅ login: shashankxrm / name: Shashank RM (40084c1)

@tekton-robot tekton-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Sep 19, 2026
@jkhelil

jkhelil commented Sep 28, 2026

Copy link
Copy Markdown
Member

@shashankxrm Thanks for the PR. please confirm human ownership of the change:

  1. In your own words: what breaks without STATEFUL_REPLICA_COUNT, and why syncing after additional options is required? (concrete CR example)
  2. Paste local test output for:
    • go test ./pkg/reconciler/common/ -run TestAddStatefulEnvVars -count=1
    • go test ./pkg/reconciler/kubernetes/tektonpipeline/ -run TestValidateStatefulSetOrdinalsAfterOptions -count=1
  3. Confirm whether this is needed now because knative/pkg already requires the env in our go.mod, or is preparatory for a future bump (link the validating code).
  4. Optional but preferred: show rendered StatefulSet env before/after an options replica override (4 → 8) with matching buckets.

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 27.72%. Comparing base (a951115) to head (40084c1).
⚠️ Report is 30 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4124      +/-   ##
==========================================
+ Coverage   27.66%   27.72%   +0.05%     
==========================================
  Files         477      477              
  Lines       25469    25518      +49     
==========================================
+ Hits         7046     7074      +28     
- Misses      17699    17714      +15     
- Partials      724      730       +6     
Flag Coverage Δ
unit-tests 27.72% <ø> (+0.05%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 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.

@jkhelil

jkhelil commented Sep 28, 2026

Copy link
Copy Markdown
Member

STATEFUL_REPLICA_COUNT is not used in knative/pkg.
knative/pkg#3393 is still open. Buckets==replicas is already enforced by the operator.

@jkhelil

jkhelil commented Sep 28, 2026

Copy link
Copy Markdown
Member

/hold

@tekton-robot tekton-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 28, 2026
@shashankxrm

Copy link
Copy Markdown
Author

@shashankxrm Thanks for the PR. please confirm human ownership of the change:

  1. In your own words: what breaks without STATEFUL_REPLICA_COUNT, and why syncing after additional options is required? (concrete CR example)

  2. Paste local test output for:

    • go test ./pkg/reconciler/common/ -run TestAddStatefulEnvVars -count=1
    • go test ./pkg/reconciler/kubernetes/tektonpipeline/ -run TestValidateStatefulSetOrdinalsAfterOptions -count=1
  3. Confirm whether this is needed now because knative/pkg already requires the env in our go.mod, or is preparatory for a future bump (link the validating code).

  4. Optional but preferred: show rendered StatefulSet env before/after an options replica override (4 → 8) with matching buckets.

You're right that the current knative.dev/pkg version doesn't use STATEFUL_REPLICA_COUNT yet. The operator already validates that buckets and replicas match, so this is mainly in preparation for #3393.

The reason I added the sync after additional options is because the env is set before those options are applied. For example, if the initial values are replicas: 4 and buckets: 4, the env gets set to 4. If an additional option changes the rendered StatefulSet replicas and buckets to 8, the env would still be 4. With #3393, that would fail the new replica-count check even though the final rendered StatefulSet and ConfigMap both have 8.

The two requested tests pass:

$ go test ./pkg/reconciler/common/ -run TestAddStatefulEnvVars -count=1
ok  	github.com/tektoncd/operator/pkg/reconciler/common	1.436s

$ go test ./pkg/reconciler/kubernetes/tektonpipeline/ -run TestValidateStatefulSetOrdinalsAfterOptions -count=1
ok  	github.com/tektoncd/operator/pkg/reconciler/kubernetes/tektonpipeline	1.039s

The current buckets == replicas validation is in pkg/apis/operator/v1alpha1/performance_validation.go, and the rendered Pipeline validation after options is in pkg/reconciler/kubernetes/tektonpipeline/transform.go.

I didn't add separate before/after output since the requested tests don't print the rendered StatefulSet, but the test does verify the final replica count and env value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants