-
Notifications
You must be signed in to change notification settings - Fork 949
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
refactor: refactor config file resolve #1132
Merged
allencloud
merged 1 commit into
AliyunContainerService:master
from
Ace-Tang:decode_config_nest_json
Apr 18, 2018
Merged
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -1,7 +1,11 @@ | ||
package config | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
|
@@ -10,6 +14,8 @@ import ( | |
"github.com/alibaba/pouch/network" | ||
"github.com/alibaba/pouch/pkg/utils" | ||
"github.com/alibaba/pouch/storage/volume" | ||
|
||
"github.com/spf13/pflag" | ||
) | ||
|
||
// Config refers to daemon's whole configurations. | ||
|
@@ -110,3 +116,101 @@ func (cfg *Config) Validate() error { | |
|
||
return nil | ||
} | ||
|
||
//MergeConfigurations merges flagSet flags and config file flags into Config. | ||
func (cfg *Config) MergeConfigurations(config *Config, flagSet *pflag.FlagSet) error { | ||
contents, err := ioutil.ReadFile(cfg.ConfigFile) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return nil | ||
} | ||
return fmt.Errorf("failed to read contents from config file %s: %s", cfg.ConfigFile, err) | ||
} | ||
|
||
var origin map[string]interface{} | ||
if err = json.NewDecoder(bytes.NewReader(contents)).Decode(&origin); err != nil { | ||
return fmt.Errorf("failed to decode json: %s", err) | ||
} | ||
|
||
if len(origin) == 0 { | ||
return nil | ||
} | ||
|
||
fileFlags := make(map[string]interface{}, 0) | ||
iterateConfig(origin, fileFlags) | ||
|
||
// check if invalid or unknown flag exist in config file | ||
if err = getUnknownFlags(flagSet, fileFlags); err != nil { | ||
return err | ||
} | ||
|
||
// check conflict in command line flags and config file | ||
if err = getConflictConfigurations(flagSet, fileFlags); err != nil { | ||
return err | ||
} | ||
|
||
fileConfig := &Config{} | ||
if err = json.NewDecoder(bytes.NewReader(contents)).Decode(fileConfig); err != nil { | ||
return fmt.Errorf("failed to decode json: %s", err) | ||
} | ||
|
||
// merge configurations from command line flags and config file | ||
err = mergeConfigurations(fileConfig, cfg) | ||
return err | ||
|
||
} | ||
|
||
// iterateConfig resolves key-value from config file iteratly. | ||
func iterateConfig(origin map[string]interface{}, config map[string]interface{}) { | ||
for k, v := range origin { | ||
if c, ok := v.(map[string]interface{}); ok { | ||
iterateConfig(c, config) | ||
} else { | ||
config[k] = v | ||
} | ||
} | ||
} | ||
|
||
// find unknown flag in config file | ||
func getUnknownFlags(flagSet *pflag.FlagSet, fileFlags map[string]interface{}) error { | ||
var unknownFlags []string | ||
|
||
for k := range fileFlags { | ||
f := flagSet.Lookup(k) | ||
if f == nil { | ||
unknownFlags = append(unknownFlags, k) | ||
} | ||
} | ||
|
||
if len(unknownFlags) > 0 { | ||
return fmt.Errorf("unknown flags: %s", strings.Join(unknownFlags, ", ")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// find conflict in command line flags and config file, note that if flag value | ||
// is slice type, we will skip it and merge it from flags and config file later. | ||
func getConflictConfigurations(flagSet *pflag.FlagSet, fileFlags map[string]interface{}) error { | ||
var conflictFlags []string | ||
flagSet.Visit(func(f *pflag.Flag) { | ||
flagType := f.Value.Type() | ||
if strings.Contains(flagType, "Slice") { | ||
return | ||
} | ||
if v, exist := fileFlags[f.Name]; exist { | ||
conflictFlags = append(conflictFlags, fmt.Sprintf("from flag: %s and from config file: %s", f.Value.String(), v)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to remove |
||
} | ||
}) | ||
|
||
if len(conflictFlags) > 0 { | ||
return fmt.Errorf("found conflict flags in command line and config file: %v", strings.Join(conflictFlags, ", ")) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// merge flagSet and config file into cfg | ||
func mergeConfigurations(src *Config, dest *Config) error { | ||
return utils.Merge(src, dest) | ||
} |
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 ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIterateConfig(t *testing.T) { | ||
assert := assert.New(t) | ||
origin := map[string]interface{}{ | ||
"a": "a", | ||
"b": "b", | ||
"c": "c", | ||
"iter1": map[string]interface{}{ | ||
"i1": "i1", | ||
"i2": "i2", | ||
}, | ||
"iter11": map[string]interface{}{ | ||
"ii1": map[string]interface{}{ | ||
"iii1": "iii1", | ||
"iii2": "iii2", | ||
}, | ||
}, | ||
} | ||
|
||
expect := map[string]interface{}{ | ||
"a": "a", | ||
"b": "b", | ||
"c": "c", | ||
"i1": "i1", | ||
"i2": "i2", | ||
"iii1": "iii1", | ||
"iii2": "iii2", | ||
} | ||
|
||
config := make(map[string]interface{}) | ||
iterateConfig(origin, config) | ||
assert.Equal(config, expect) | ||
|
||
// test nil map will not cause panic | ||
config = make(map[string]interface{}) | ||
iterateConfig(nil, config) | ||
assert.Equal(config, map[string]interface{}{}) | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need to make this function some kind of robust. If the config is nil, I am afraid this function would panic. How about doing this nil-checking to improve robustness?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I refer to make this function before
iterateConfig
, so that origin can not be nil, and since it not a common function, will not be used by other function ,add nil check is redundancyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still insist on make functions independent from each other. As if you encapsulate this function out of the another place which is calling this. It has a trend to be general. If that, we cannot prevent others to use it in the future, as a result, there will be potential possibility to panic.
In addition, if the code which is calling this function changes or removes the nil checking in the upper layer, there will be potential panic as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for range a nil map will never cause panic, since nil value will convert to a nil map(map[] ), that add a nil check is useless.