-
Notifications
You must be signed in to change notification settings - Fork 8
fix: Correctly handle variable overrides #1252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jimmidyson
wants to merge
4
commits into
main
Choose a base branch
from
jimmi/fix-overrides
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
86cfcef
fix: Correctly handle variable overrides
jimmidyson ba5ba73
fixup! refactor: Address review feedback
jimmidyson 6761b76
fixup! refactor: Move merge function to common module
jimmidyson c5a0cdc
fixup! refactor: Handle overrides in failure domain validation
jimmidyson File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2023 Nutanix. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package variables | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"maps" | ||
|
||
"dario.cat/mergo" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
// MergeVariableOverridesWithGlobal merges the provided variable overrides with the global variables. | ||
// It performs a deep merge, ensuring that if a variable exists in both maps, the value from the overrides is used. | ||
func MergeVariableOverridesWithGlobal( | ||
overrideVars, globalVars map[string]apiextensionsv1.JSON, | ||
) (map[string]apiextensionsv1.JSON, error) { | ||
mergedVars := maps.Clone(overrideVars) | ||
|
||
for k, v := range globalVars { | ||
// If the value of v is nil, skip it. | ||
if v.Raw == nil { | ||
continue | ||
} | ||
|
||
existingValue, exists := mergedVars[k] | ||
|
||
// If the variable does not exist in the mergedVars or the value is nil, add it and continue. | ||
if !exists || existingValue.Raw == nil { | ||
mergedVars[k] = v | ||
continue | ||
} | ||
|
||
// Wrap the value in a temporary key to ensure we can unmarshal to a map. | ||
// This is necessary because the values could be scalars. | ||
tempValJSON := fmt.Sprintf(`{"value": %s}`, string(existingValue.Raw)) | ||
tempGlobalValJSON := fmt.Sprintf(`{"value": %s}`, string(v.Raw)) | ||
|
||
// Unmarshal the existing value and the global value into maps. | ||
var val, globalVal map[string]interface{} | ||
if err := json.Unmarshal([]byte(tempValJSON), &val); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal existing value for key %q: %w", k, err) | ||
} | ||
|
||
if err := json.Unmarshal([]byte(tempGlobalValJSON), &globalVal); err != nil { | ||
return nil, fmt.Errorf("failed to unmarshal global value for key %q: %w", k, err) | ||
} | ||
|
||
// Now use mergo to perform a deep merge of the values, retaining the values in `val` if present. | ||
if err := mergo.Merge(&val, globalVal); err != nil { | ||
return nil, fmt.Errorf("failed to merge values for key %q: %w", k, err) | ||
} | ||
|
||
// Marshal the merged value back to JSON. | ||
mergedVal, err := json.Marshal(val["value"]) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal merged value for key %q: %w", k, err) | ||
} | ||
|
||
mergedVars[k] = apiextensionsv1.JSON{Raw: mergedVal} | ||
} | ||
|
||
return mergedVars, nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.