forked from cortexproject/cortex
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Runtime config endpoint now supports diff parameter (cortexproject#3700)
* Runtime config endpoint now supports diff parameter The runtime config endpoint now supports the diff parameter meaning that using /runtime_config?mode=diff will show only the non-default values for the configuration Signed-off-by: Mario de Frutos <[email protected]> * Fix lint errors Signed-off-by: Mario de Frutos <[email protected]> * Move runtime handler to the cortext package To avoid having a dependency of the validation package in the api one we have move the runtime configuration handle to the cortex package. This change also led to move some function of the handle to the util package Signed-off-by: Mario de Frutos <[email protected]> * Added kv.Multi diff too Now we're doing diff with the kv.Multi component for the runtime config Signed-off-by: Mario de Frutos <[email protected]> * Refactor and improvement of the runtime config diff Co-authored-by: Peter Štibraný <[email protected]> Signed-off-by: Mario de Frutos <[email protected]> Co-authored-by: Peter Štibraný <[email protected]>
- Loading branch information
Showing
9 changed files
with
152 additions
and
95 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
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,68 @@ | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
) | ||
|
||
// DiffConfig utility function that returns the diff between two config map objects | ||
func DiffConfig(defaultConfig, actualConfig map[interface{}]interface{}) (map[interface{}]interface{}, error) { | ||
output := make(map[interface{}]interface{}) | ||
|
||
for key, value := range actualConfig { | ||
|
||
defaultValue, ok := defaultConfig[key] | ||
if !ok { | ||
output[key] = value | ||
continue | ||
} | ||
|
||
switch v := value.(type) { | ||
case int: | ||
defaultV, ok := defaultValue.(int) | ||
if !ok || defaultV != v { | ||
output[key] = v | ||
} | ||
case string: | ||
defaultV, ok := defaultValue.(string) | ||
if !ok || defaultV != v { | ||
output[key] = v | ||
} | ||
case bool: | ||
defaultV, ok := defaultValue.(bool) | ||
if !ok || defaultV != v { | ||
output[key] = v | ||
} | ||
case []interface{}: | ||
defaultV, ok := defaultValue.([]interface{}) | ||
if !ok || !reflect.DeepEqual(defaultV, v) { | ||
output[key] = v | ||
} | ||
case float64: | ||
defaultV, ok := defaultValue.(float64) | ||
if !ok || !reflect.DeepEqual(defaultV, v) { | ||
output[key] = v | ||
} | ||
case nil: | ||
if defaultValue != nil { | ||
output[key] = v | ||
} | ||
case map[interface{}]interface{}: | ||
defaultV, ok := defaultValue.(map[interface{}]interface{}) | ||
if !ok { | ||
output[key] = value | ||
} | ||
diff, err := DiffConfig(defaultV, v) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(diff) > 0 { | ||
output[key] = diff | ||
} | ||
default: | ||
return nil, fmt.Errorf("unsupported type %T", v) | ||
} | ||
} | ||
|
||
return output, nil | ||
} |
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,19 @@ | ||
package util | ||
|
||
import "gopkg.in/yaml.v2" | ||
|
||
// YAMLMarshalUnmarshal utility function that converts a YAML interface in a map | ||
// doing marshal and unmarshal of the parameter | ||
func YAMLMarshalUnmarshal(in interface{}) (map[interface{}]interface{}, error) { | ||
yamlBytes, err := yaml.Marshal(in) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
object := make(map[interface{}]interface{}) | ||
if err := yaml.Unmarshal(yamlBytes, object); err != nil { | ||
return nil, err | ||
} | ||
|
||
return object, nil | ||
} |