Refactor assets/addons.go to de-dupe map parsing.

pull/11432/head
Andriy Dzikh 2021-05-13 13:52:54 -07:00
parent 4f345f2b8f
commit 62b2c94c0f
1 changed files with 33 additions and 26 deletions

View File

@ -660,6 +660,23 @@ var Addons = map[string]*Addon{
}), }),
} }
// parseMapString creates a map based on `str` which is encoded as <key1>=<value1>,<key2>=<value2>,...
func parseMapString(str string) map[string]string {
mapResult := make(map[string]string)
if str == "" {
return mapResult
}
for _, pairText := range strings.Split(str, ",") {
vals := strings.Split(pairText, "=")
if len(vals) != 2 {
out.WarningT("Ignoring invalid pair entry {{.pair}}", out.V{"pair": pairText})
continue
}
mapResult[vals[0]] = vals[1]
}
return mapResult
}
// GenerateTemplateData generates template data for template assets // GenerateTemplateData generates template data for template assets
func GenerateTemplateData(addon *Addon, cfg config.KubernetesConfig, netInfo NetworkInfo) interface{} { func GenerateTemplateData(addon *Addon, cfg config.KubernetesConfig, netInfo NetworkInfo) interface{} {
@ -705,19 +722,16 @@ func GenerateTemplateData(addon *Addon, cfg config.KubernetesConfig, netInfo Net
opts.Images = make(map[string]string) // Avoid nil access when rendering opts.Images = make(map[string]string) // Avoid nil access when rendering
} }
images := viper.GetString(config.AddonImages) images := parseMapString(viper.GetString(config.AddonImages))
if images != "" { for name, image := range images {
for _, image := range strings.Split(images, ",") { if image == "" {
vals := strings.Split(image, "=") out.WarningT("Ignoring empty custom image {{.name}}", out.V{"name": name})
if len(vals) != 2 || vals[1] == "" {
out.WarningT("Ignoring invalid custom image {{.conf}}", out.V{"conf": image})
continue continue
} }
if _, ok := opts.Images[vals[0]]; ok { if _, ok := opts.Images[name]; ok {
opts.Images[vals[0]] = vals[1] opts.Images[name] = image
} else { } else {
out.WarningT("Ignoring unknown custom image {{.name}}", out.V{"name": vals[0]}) out.WarningT("Ignoring unknown custom image {{.name}}", out.V{"name": name})
}
} }
} }
@ -725,19 +739,12 @@ func GenerateTemplateData(addon *Addon, cfg config.KubernetesConfig, netInfo Net
opts.Registries = make(map[string]string) opts.Registries = make(map[string]string)
} }
registries := viper.GetString(config.AddonRegistries) registries := parseMapString(viper.GetString(config.AddonRegistries))
if registries != "" { for name, registry := range registries {
for _, registry := range strings.Split(registries, ",") { if _, ok := opts.Images[name]; ok { // check images map because registry map may omitted default registry
vals := strings.Split(registry, "=") opts.CustomRegistries[name] = registry
if len(vals) != 2 {
out.WarningT("Ignoring invalid custom registry {{.conf}}", out.V{"conf": registry})
continue
}
if _, ok := opts.Images[vals[0]]; ok { // check images map because registry map may omitted default registry
opts.CustomRegistries[vals[0]] = vals[1]
} else { } else {
out.WarningT("Ignoring unknown custom registry {{.name}}", out.V{"name": vals[0]}) out.WarningT("Ignoring unknown custom registry {{.name}}", out.V{"name": registry})
}
} }
} }