read a section of config (#255)

This commit is contained in:
Liang Mingqiang
2018-07-31 16:28:42 -04:00
committed by Robbie Zhang
parent 3e1beb3d92
commit f9c7af5ec9
2 changed files with 26 additions and 18 deletions

View File

@@ -67,6 +67,11 @@ func Execute() {
func init() { func init() {
cobra.OnInitialize(initConfig) cobra.OnInitialize(initConfig)
// read default node name from environment variable.
// it can be overwritten by cli flags if specified.
if os.Getenv("DEFAULT_NODE_NAME") != "" {
nodeName = os.Getenv("DEFAULT_NODE_NAME")
}
// Here you will define your flags and configuration settings. // Here you will define your flags and configuration settings.
// Cobra supports persistent flags, which, if defined here, // Cobra supports persistent flags, which, if defined here,
// will be global for your application. // will be global for your application.

View File

@@ -42,7 +42,7 @@ type MockConfig struct {
// NewMockProvider creates a new MockProvider // NewMockProvider creates a new MockProvider
func NewMockProvider(providerConfig, nodeName, operatingSystem string, internalIP string, daemonEndpointPort int32) (*MockProvider, error) { func NewMockProvider(providerConfig, nodeName, operatingSystem string, internalIP string, daemonEndpointPort int32) (*MockProvider, error) {
config, err := loadConfig(providerConfig) config, err := loadConfig(providerConfig, nodeName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -59,25 +59,28 @@ func NewMockProvider(providerConfig, nodeName, operatingSystem string, internalI
} }
// loadConfig loads the given json configuration files. // loadConfig loads the given json configuration files.
func loadConfig(providerConfig string) (config MockConfig, err error) {
if providerConfig != "" { func loadConfig(providerConfig, nodeName string) (config MockConfig, err error) {
data, err := ioutil.ReadFile(providerConfig) data, err := ioutil.ReadFile(providerConfig)
if err != nil { if err != nil {
return config, err return config, err
}
configMap := map[string]MockConfig{}
err = json.Unmarshal(data, &configMap)
if err != nil {
return config, err
}
if _, exist := configMap[nodeName]; exist {
config = configMap[nodeName]
if config.CPU == "" {
config.CPU = defaultCPUCapacity
} }
err = json.Unmarshal(data, &config) if config.Memory == "" {
if err != nil { config.Memory = defaultMemoryCapacity
return config, err }
if config.Pods == "" {
config.Pods = defaultPodCapacity
} }
}
if config.CPU == "" {
config.CPU = defaultCPUCapacity
}
if config.Memory == "" {
config.Memory = defaultMemoryCapacity
}
if config.Pods == "" {
config.Pods = defaultPodCapacity
} }
if _, err = resource.ParseQuantity(config.CPU); err != nil { if _, err = resource.ParseQuantity(config.CPU); err != nil {