Skip to content

fix(karmadactl): print the same policy name as promote creates for cluster-scoped resources - #8097

Open
BahaaMattar wants to merge 1 commit into
karmada-io:masterfrom
BahaaMattar:fix/promote-cluster-scoped-policy-name
Open

BahaaMattar wants to merge 1 commit into
karmada-io:masterfrom
BahaaMattar:fix/promote-cluster-scoped-policy-name

Conversation

@BahaaMattar

Copy link
Copy Markdown

What type of PR is this?

/kind bug

What this PR does / why we need it:

karmadactl promote -o yaml|json prints the promoted resource and the PropagationPolicy (or ClusterPropagationPolicy) that promote would create. For a cluster-scoped resource the printed policy was named from the namespace of the kubeconfig context (names.GeneratePolicyName(o.Namespace, ...)), but promoting the resource for real creates the ClusterPropagationPolicy with names.GeneratePolicyName("", ...). So the printed policy had a different name from the one that is created (and its name depended on the context's namespace).

This PR adds one policyName(namespace) helper (the --policy-name value, or the generated name) and uses it in printObjectAndPolicy, createPropagationPolicy and createClusterPropagationPolicy. A ClusterPropagationPolicy is named without a namespace in all three places; namespaced resources keep their current names.

TestPrintedPolicyNameMatchesCreatedPolicy prints the policy and creates it for a cluster-scoped and a namespaced resource and compares the names.

Which issue(s) this PR fixes:
Fixes #8096

Special notes for your reviewer:

go test ./pkg/karmadactl/promote/... and golangci-lint run ./pkg/karmadactl/promote/... pass. The new cluster-scoped case fails without the change: the printed policy is named "demo-clusterrole-5dc8bc456d", but promoting the resource creates the policy "demo-clusterrole-54499c54c". The namespaced case passes before and after.

Behavior change: the generated name of a ClusterPropagationPolicy printed by -o yaml|json changes (it now matches what promote creates). The name of the policy that promote creates does not change.

Does this PR introduce a user-facing change?:

`karmadactl promote`: `-o yaml|json` now prints a ClusterPropagationPolicy with the same name that promote creates for cluster-scoped resources.

Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:04
@karmada-bot karmada-bot added kind/bug Categorizes issue or PR as related to a bug. do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. labels Nov 19, 2026
@karmada-bot

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign chaosi-zju 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

@karmada-bot karmada-bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Nov 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified in the change aligning printed and created policy names.

No open issues 🎉

What changed in this PR

Fixes #8096 by making karmadactl promote -o yaml|json print the same ClusterPropagationPolicy name that promotion creates, regardless of the kubeconfig context’s namespace.

Changes:

  • Centralizes policy naming in a shared helper while preserving explicit --policy-name overrides.
  • Uses an empty namespace when generating cluster-scoped policy names; namespaced policy names remain unchanged.
  • Adds regression coverage comparing printed and created policy names for both resource scopes.
File Description
pkg/​karmadactl/​promote/​promote.go Shares naming logic across policy printing and creation, fixing cluster-scoped output.
pkg/​karmadactl/​promote/​promote_test.go Tests that printed and created policy names match for cluster-scoped and namespaced resources.

Review effort: Balanced

@codecov-commenter

codecov-commenter commented Nov 19, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 42.49%. Comparing base (f4c6997) to head (ebf8825).
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #8097      +/-   ##
==========================================
+ Coverage   42.46%   42.49%   +0.02%     
==========================================
  Files         880      880              
  Lines       55483    55474       -9     
==========================================
+ Hits        23563    23571       +8     
+ Misses      30157    30140      -17     
  Partials     1763     1763              
Flag Coverage Δ
unittests 42.49% <100.00%> (+0.02%) ⬆️

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.

…uster-scoped resources

For a cluster-scoped resource, `promote -o yaml|json` generated the name of
the ClusterPropagationPolicy from the namespace of the kubeconfig context,
while promoting the resource for real generates it without a namespace. So
the printed policy had a different name from the one that is created.

Generate the policy name in one place, which doesn't take the namespace into
account for a ClusterPropagationPolicy.

Signed-off-by: Bahaa Mattar <bahaa.kmattar@gmail.com>
@BahaaMattar
BahaaMattar force-pushed the fix/promote-cluster-scoped-policy-name branch from 312a842 to ebf8825 Compare November 20, 2026 05:43
@karmada-bot karmada-bot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/bug Categorizes issue or PR as related to a bug. 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.

karmadactl promote -o yaml prints a ClusterPropagationPolicy with a different name than the one promote creates

4 participants