-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add --merge-config flag to support merging with default configuration (…
…#1075) * add config merge support * fix indentation * Update cmd/polaris/root.go Co-authored-by: Andy Suderman <andy@fairwinds.com> --------- Co-authored-by: Andy Suderman <andy@fairwinds.com>
- Loading branch information
1 parent
9b5438d
commit 0738475
Showing
5 changed files
with
137 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,45 @@ | ||
package config | ||
|
||
import ( | ||
"gopkg.in/yaml.v3" // do not change the yaml import | ||
) | ||
|
||
func mergeYaml(defaultConfig, overridesConfig []byte) ([]byte, error) { | ||
var defaultData, overrideConfig map[string]any | ||
|
||
err := yaml.Unmarshal([]byte(defaultConfig), &defaultData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
err = yaml.Unmarshal([]byte(overridesConfig), &overrideConfig) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
mergedData := mergeYAMLMaps(defaultData, overrideConfig) | ||
|
||
mergedConfig, err := yaml.Marshal(mergedData) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return mergedConfig, nil | ||
} | ||
|
||
func mergeYAMLMaps(defaults, overrides map[string]any) map[string]any { | ||
for k, v := range overrides { | ||
if vMap, ok := v.(map[string]any); ok { | ||
// if the key exists in defaults and is a map, recursively merge | ||
if mv1, ok := defaults[k].(map[string]any); ok { | ||
defaults[k] = mergeYAMLMaps(mv1, vMap) | ||
} else { | ||
defaults[k] = vMap | ||
} | ||
} else { | ||
// add or overwrite the value in defaults | ||
defaults[k] = v | ||
} | ||
} | ||
return defaults | ||
} |
This file contains 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,50 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var defaults = ` | ||
checks: | ||
deploymentMissingReplicas: warning | ||
priorityClassNotSet: warning | ||
tagNotSpecified: danger | ||
existing: | ||
sub: | ||
key: value | ||
` | ||
|
||
var overrides = ` | ||
checks: | ||
pullPolicyNotAlways: ignore | ||
tagNotSpecified: overrides | ||
existing: | ||
sub: | ||
key1: value1 | ||
new: value | ||
new: | ||
key: value | ||
` | ||
|
||
func TestMergeYaml(t *testing.T) { | ||
mergedContent, err := mergeYaml([]byte(defaults), []byte(overrides)) | ||
assert.NoError(t, err) | ||
|
||
expectedYAML := `checks: | ||
deploymentMissingReplicas: warning | ||
priorityClassNotSet: warning | ||
pullPolicyNotAlways: ignore | ||
tagNotSpecified: overrides | ||
existing: | ||
new: value | ||
sub: | ||
key: value | ||
key1: value1 | ||
new: | ||
key: value | ||
` | ||
|
||
assert.Equal(t, expectedYAML, string(mergedContent)) | ||
} |