fix cyclomatic check failure

Moved part of the code from NewTopologyHints to getTopologyHint to
decrease cyclomatic complexity.

This should fix this failure:
  $ make checks
  Complexity is over 15 in
  20 topology NewTopologyHints pkg/topology/topology.go:83:1
  Makefile:23: recipe for target 'cyclomatic-check' failed
  make: *** [cyclomatic-check] Error 1
This commit is contained in:
Ed Bartosh 2020-02-11 13:48:16 +02:00
parent 366d07a35e
commit 1bbcbc65da

View File

@ -79,20 +79,13 @@ func getDevicesFromVirtual(realDevPath string) (devs []string, err error) {
}
}
// NewTopologyHints return array of hints for the device and its slaves (e.g. RAID).
func NewTopologyHints(devPath string) (hints Hints, err error) {
hints = make(Hints)
realDevPath, err := filepath.EvalSymlinks(devPath)
if err != nil {
return nil, errors.Wrapf(err, "failed get realpath for %s", devPath)
}
for p := realDevPath; strings.HasPrefix(p, mockRoot+"/sys/devices/"); p = filepath.Dir(p) {
hint := Hint{Provider: p}
func getTopologyHint(sysFSPath string) (*Hint, error) {
hint := Hint{Provider: sysFSPath}
fileMap := map[string]*string{
"local_cpulist": &hint.CPUs,
"numa_node": &hint.NUMAs,
}
if err = readFilesInDirectory(fileMap, p); err != nil {
if err := readFilesInDirectory(fileMap, sysFSPath); err != nil {
return nil, err
}
// Workarounds for broken information provided by kernel
@ -103,7 +96,7 @@ func NewTopologyHints(devPath string) (hints Hints, err error) {
if hint.NUMAs != "" && hint.CPUs == "" {
// broken topology hint. BIOS reports socket id as NUMA node
// First, try to get hints from parent device or bus.
parentHints, er := NewTopologyHints(filepath.Dir(p))
parentHints, er := NewTopologyHints(filepath.Dir(sysFSPath))
if er == nil {
cpulist := map[string]bool{}
numalist := map[string]bool{}
@ -128,8 +121,23 @@ func NewTopologyHints(devPath string) (hints Hints, err error) {
hint.NUMAs = ""
}
}
return &hint, nil
}
// NewTopologyHints return array of hints for the device and its slaves (e.g. RAID).
func NewTopologyHints(devPath string) (hints Hints, err error) {
hints = make(Hints)
realDevPath, err := filepath.EvalSymlinks(devPath)
if err != nil {
return nil, errors.Wrapf(err, "failed get realpath for %s", devPath)
}
for p := realDevPath; strings.HasPrefix(p, mockRoot+"/sys/devices/"); p = filepath.Dir(p) {
hint, err := getTopologyHint(p)
if err != nil {
return nil, err
}
if hint.CPUs != "" || hint.NUMAs != "" || hint.Sockets != "" {
hints[hint.Provider] = hint
hints[hint.Provider] = *hint
break
}
}