Skip to content

fix: reorder variable merging logic in GetAllVariable for clarity and…#3094

Merged
kubesphere-prow[bot] merged 1 commit into
kubesphere:mainfrom
redscholar:correct_get_variable
Apr 29, 2026
Merged

fix: reorder variable merging logic in GetAllVariable for clarity and…#3094
kubesphere-prow[bot] merged 1 commit into
kubesphere:mainfrom
redscholar:correct_get_variable

Conversation

@redscholar

Copy link
Copy Markdown
Contributor

What type of PR is this?

/kind bug

What this PR does / why we need it:

Fix variable priority: config > inventory(host > groups > vars) > runtime > remote

Which issue(s) this PR fixes:

Fixes #

Special notes for reviewers:

Does this PR introduced a user-facing change?

none

Additional documentation, usage docs, etc.:


… priority

Signed-off-by: redscholar <blacktiledhouse@gmail.com>
@kubesphere-prow kubesphere-prow Bot added release-note-none kind/bug Categorizes issue or PR as related to a bug. labels Apr 29, 2026
@kubesphere-prow

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: redscholar

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

The pull request process is described 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

@kubesphere-prow kubesphere-prow Bot added approved Indicates a PR has been approved by an approver from all required OWNERS files. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Apr 29, 2026
@sonarqubecloud

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request reorders the variable merging logic within the getHostsVariable function to establish a specific priority hierarchy, moving remote and runtime variables to the lowest priority. Feedback focuses on performance optimizations, specifically recommending that constant inventory and configuration variables be pre-calculated outside the host loop to avoid redundant processing. Additionally, the reviewer noted a potential non-determinism issue when merging group variables due to Go's map iteration behavior, suggesting that group names should be sorted first.

hostVars = CombineVariables(hostVars, v.value.Hosts[hostname].RemoteVars)
// 2. Merge runtime variables
hostVars = CombineVariables(hostVars, v.value.Hosts[hostname].RuntimeVars)
// 3. Merge inventory-level variables (vars)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The variable merging logic in the following line is inefficient because Extension2Variables (which performs JSON unmarshaling) is called for every host even though the inventory variables are constant. To improve performance, especially with large inventories, pre-calculate this variable map once outside the loop.

// 3. Merge inventory-level variables (vars)
hostVars = CombineVariables(hostVars, Extension2Variables(v.value.Inventory.Spec.Vars))
// Set group variables for hosts that belong to groups (override spec.vars)
// 4. Merge group variables (groups > vars)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The group merging logic that follows has two issues:

  1. Non-determinism: Iterating over the groups map is non-deterministic in Go. If a host belongs to multiple groups with conflicting variables, the result will be inconsistent across different executions. Consider sorting the group names before merging.
  2. Efficiency: Extension2Variables is called repeatedly for each host. Pre-calculating these maps outside the loop would significantly improve performance.

// 5. Merge host-specific variables from inventory (host > groups)
hostVars = CombineVariables(hostVars, Extension2Variables(v.value.Inventory.Spec.Hosts[hostname]))
// Merge configuration variables
// 6. Merge configuration variables (highest priority)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Similar to the inventory variables, the configuration variables in the following line should be pre-calculated outside the loop to avoid redundant JSON unmarshaling for every host.

@redscholar redscholar added the lgtm Indicates that a PR is ready to be merged. label Apr 29, 2026
@kubesphere-prow kubesphere-prow Bot merged commit b251e76 into kubesphere:main Apr 29, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. kind/bug Categorizes issue or PR as related to a bug. lgtm Indicates that a PR is ready to be merged. release-note-none size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant