Skip to content

feat: add prefix and suffix options to replacements for targeting con...#6090

Open
Br1an67 wants to merge 1 commit intokubernetes-sigs:masterfrom
Br1an67:fix/issue-4080-replacements-prefix-suffix
Open

feat: add prefix and suffix options to replacements for targeting con...#6090
Br1an67 wants to merge 1 commit intokubernetes-sigs:masterfrom
Br1an67:fix/issue-4080-replacements-prefix-suffix

Conversation

@Br1an67
Copy link
Copy Markdown

@Br1an67 Br1an67 commented Mar 8, 2026

Fixes #4080

Summary

Add prefix and suffix options to the FieldOptions type for replacements. This enables targeting specific elements in string arrays (like container args) that match a prefix and/or suffix pattern. The matched prefix and suffix are preserved in the resulting value.

This addresses the issue where replacements couldn't reliably target --key=value style container arguments without relying on array index ordering.

Changes

  • Added Prefix and Suffix string fields to FieldOptions in api/types/replacement.go
  • Updated setFieldValue function in api/filters/replacement/replacement.go to handle prefix/suffix matching for both scalar and sequence (array) nodes
  • Added setFieldValueInSequence helper function to iterate over array elements and replace values that match the prefix/suffix pattern
  • Added matchesPrefixAndSuffix helper function for prefix/suffix validation
  • Added comprehensive test cases covering:
    • Prefix option on scalar fields
    • Prefix option on sequence fields (container args)
    • Prefix option with multiple matches in a sequence
    • Suffix option on scalar fields
    • Combined prefix and suffix options
    • No-match scenarios (prefix doesn't match)

Usage Example

replacements:
- source:
    kind: Namespace
    name: my-namespace
    fieldPath: metadata.name
  targets:
  - select:
      kind: Deployment
    fieldPaths:
    - spec.template.spec.containers.[name=cert-manager].args
    options:
      prefix: "--leader-election-namespace="

This replaces any arg starting with --leader-election-namespace= (e.g., --leader-election-namespace=kube-system) with --leader-election-namespace=my-namespace.

Testing

All existing tests pass, plus new test cases for the prefix/suffix functionality.

 api/filters/replacement/replacement.go      |  64 +++++++
 api/filters/replacement/replacement_test.go | 283 ++++++++++++++++++++++++++++
 api/types/replacement.go                    |  14 +-
 4 files changed, 360 insertions(+), 2 deletions(-)

…tainer args

Add prefix and suffix options to FieldOptions for replacements. This allows
targeting specific elements in string arrays (like container args) that match
a prefix and/or suffix pattern. The matched prefix/suffix is preserved in the
resulting value.

This enables replacements like:
- spec.template.spec.containers.[name=cert-manager].args
  with prefix: "--leader-election-namespace="
  to replace args like "--leader-election-namespace=kube-system"
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Mar 8, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: Br1an67 / name: Br1an (9624549)

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Br1an67
Once this PR has been reviewed and has the lgtm label, please assign varshaprasad96 for approval. For more information see the Code Review Process.

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

@k8s-ci-robot
Copy link
Copy Markdown
Contributor

Hi @Br1an67. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Mar 8, 2026
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Mar 8, 2026
@koba1t
Copy link
Copy Markdown
Member

koba1t commented Mar 9, 2026

Please pass the CLA.

@Br1an67 Br1an67 force-pushed the fix/issue-4080-replacements-prefix-suffix branch from b35db85 to 9624549 Compare March 18, 2026 00:26
@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. and removed cncf-cla: no Indicates the PR's author has not signed the CNCF CLA. labels Mar 18, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

PR needs rebase.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

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

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. 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.

Replacements: better support for targeting --key=value container args

3 participants