Issue #348: Refactor get<Thing>Files into getFiles (#359)

* issue #348: replace everywhere get<Thing>Files with getFiles
pull/357/head^2
zilard 5 years ago committed by Liz Rice
parent c87c5cfb51
commit b86dd92c91

@ -82,10 +82,10 @@ func runChecks(nodetype check.NodeType) {
exitWithError(err) exitWithError(err)
} }
confmap := getConfigFiles(typeConf) confmap := getFiles(typeConf, "config")
svcmap := getServiceFiles(typeConf) svcmap := getFiles(typeConf, "service")
kubeconfmap := getKubeConfigFiles(typeConf) kubeconfmap := getFiles(typeConf, "kubeconfig")
cafilemap := getCaFile(typeConf) cafilemap := getFiles(typeConf, "ca")
// Variable substitutions. Replace all occurrences of variables in controls files. // Variable substitutions. Replace all occurrences of variables in controls files.
s := string(in) s := string(in)

@ -27,6 +27,12 @@ var (
var psFunc func(string) string var psFunc func(string) string
var statFunc func(string) (os.FileInfo, error) var statFunc func(string) (os.FileInfo, error)
var TypeMap = map[string][]string{
"ca": []string{"cafile", "defaultcafile"},
"kubeconfig": []string{"kubeconfig", "defaultkubeconfig"},
"service": []string{"svc", "defaultsvc"},
"config": []string{"confs", "defaultconf"},
}
func init() { func init() {
psFunc = ps psFunc = ps
@ -165,40 +171,11 @@ func decrementVersion(version string) string {
return strings.Join(split, ".") return strings.Join(split, ".")
} }
// getConfigFiles finds which of the set of candidate config files exist // getFiles finds which of the set of candidate files exist
func getConfigFiles(v *viper.Viper) map[string]string { func getFiles(v *viper.Viper, fileType string) map[string]string {
confmap := make(map[string]string) filemap := make(map[string]string)
mainOpt := TypeMap[fileType][0]
for _, component := range v.GetStringSlice("components") { defaultOpt := TypeMap[fileType][1]
s := v.Sub(component)
if s == nil {
continue
}
// See if any of the candidate config files exist
conf := findConfigFile(s.GetStringSlice("confs"))
if conf == "" {
if s.IsSet("defaultconf") {
conf = s.GetString("defaultconf")
glog.V(2).Info(fmt.Sprintf("Using default config file name '%s' for component %s", conf, component))
} else {
// Default the config file name that we'll substitute to the name of the component
glog.V(2).Info(fmt.Sprintf("Missing config file for %s", component))
conf = component
}
} else {
glog.V(2).Info(fmt.Sprintf("Component %s uses config file '%s'", component, conf))
}
confmap[component] = conf
}
return confmap
}
// getServiceFiles finds which of the set of candidate service files exist
func getServiceFiles(v *viper.Viper) map[string]string {
svcmap := make(map[string]string)
for _, component := range v.GetStringSlice("components") { for _, component := range v.GetStringSlice("components") {
s := v.Sub(component) s := v.Sub(component)
@ -206,85 +183,25 @@ func getServiceFiles(v *viper.Viper) map[string]string {
continue continue
} }
// See if any of the candidate config files exist // See if any of the candidate files exist
svc := findConfigFile(s.GetStringSlice("svc")) file := findConfigFile(s.GetStringSlice(mainOpt))
if svc == "" { if file == "" {
if s.IsSet("defaultsvc") { if s.IsSet(defaultOpt) {
svc = s.GetString("defaultsvc") file = s.GetString(defaultOpt)
glog.V(2).Info(fmt.Sprintf("Using default service file name '%s' for component %s", svc, component)) glog.V(2).Info(fmt.Sprintf("Using default %s file name '%s' for component %s", fileType, file, component))
} else { } else {
// Default the service file name that we'll substitute to the name of the component // Default the file name that we'll substitute to the name of the component
glog.V(2).Info(fmt.Sprintf("Missing service file for %s", component)) glog.V(2).Info(fmt.Sprintf("Missing %s file for %s", fileType, component))
svc = component file = component
} }
} else { } else {
glog.V(2).Info(fmt.Sprintf("Component %s uses service file '%s'", component, svc)) glog.V(2).Info(fmt.Sprintf("Component %s uses %s file '%s'", component, fileType, file))
} }
svcmap[component] = svc filemap[component] = file
} }
return svcmap return filemap
}
// getKubeConfigFiles finds which of the set of candidate kubeconfig files exist
func getKubeConfigFiles(v *viper.Viper) map[string]string {
kubeconfigmap := make(map[string]string)
for _, component := range v.GetStringSlice("components") {
s := v.Sub(component)
if s == nil {
continue
}
// See if any of the candidate config files exist
kubeconfig := findConfigFile(s.GetStringSlice("kubeconfig"))
if kubeconfig == "" {
if s.IsSet("defaultkubeconfig") {
kubeconfig = s.GetString("defaultkubeconfig")
glog.V(2).Info(fmt.Sprintf("Using default kubeconfig file name '%s' for component %s", kubeconfig, component))
} else {
// Default the service file name that we'll substitute to the name of the component
glog.V(2).Info(fmt.Sprintf("Missing kubeconfig file for %s", component))
kubeconfig = component
}
} else {
glog.V(2).Info(fmt.Sprintf("Component %s uses kubeconfig file '%s'", component, kubeconfig))
}
kubeconfigmap[component] = kubeconfig
}
return kubeconfigmap
}
// getCaFile finds which of the set of client certificate authorities files exist
func getCaFile(v *viper.Viper) map[string]string {
cafilemap := make(map[string]string)
for _, component := range v.GetStringSlice("components") {
s := v.Sub(component)
if s == nil {
continue
}
cafile := findConfigFile(s.GetStringSlice("cafile"))
if cafile == "" {
if s.IsSet("defaultcafile") {
cafile = s.GetString("defaultcafile")
glog.V(2).Info(fmt.Sprintf("Using default client CA file name '%s' for component %s", cafile, component))
} else {
glog.V(2).Info(fmt.Sprintf("Missing client CA file for %s", component))
cafile = component
}
} else {
glog.V(2).Info(fmt.Sprintf("Component %s uses client CA file '%s'", component, cafile))
}
cafilemap[component] = cafile
}
return cafilemap
} }
// verifyBin checks that the binary specified is running // verifyBin checks that the binary specified is running

@ -298,7 +298,7 @@ func TestGetConfigFiles(t *testing.T) {
e = c.statResults e = c.statResults
eIndex = 0 eIndex = 0
m := getConfigFiles(v) m := getFiles(v, "config")
if !reflect.DeepEqual(m, c.exp) { if !reflect.DeepEqual(m, c.exp) {
t.Fatalf("Got %v\nExpected %v", m, c.exp) t.Fatalf("Got %v\nExpected %v", m, c.exp)
} }
@ -373,7 +373,7 @@ func TestGetServiceFiles(t *testing.T) {
e = c.statResults e = c.statResults
eIndex = 0 eIndex = 0
m := getServiceFiles(v) m := getFiles(v, "service")
if !reflect.DeepEqual(m, c.exp) { if !reflect.DeepEqual(m, c.exp) {
t.Fatalf("Got %v\nExpected %v", m, c.exp) t.Fatalf("Got %v\nExpected %v", m, c.exp)
} }

Loading…
Cancel
Save