1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Refactor daemon.info to reduce cyclomatic complexity

Before this change;

    gocyclo daemon/info.go
    17 daemon (*Daemon).SystemInfo daemon/info.go:27:1
    2 daemon (*Daemon).SystemVersion daemon/info.go:150:1
    1 daemon (*Daemon).showPluginsInfo daemon/info.go:195:1

After this change;

    gocyclo daemon/info.go
    8 daemon (*Daemon).fillSecurityOptions daemon/info.go:150:1
    5 daemon operatingSystem daemon/info.go:201:1
    3 daemon (*Daemon).fillDriverInfo daemon/info.go:121:1
    2 daemon hostName daemon/info.go:172:1
    2 daemon memInfo daemon/info.go:192:1
    2 daemon kernelVersion daemon/info.go:182:1
    1 daemon (*Daemon).SystemVersion daemon/info.go:81:1
    1 daemon (*Daemon).SystemInfo daemon/info.go:27:1
    1 daemon (*Daemon).fillPluginsInfo daemon/info.go:138:1

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2018-07-13 12:55:59 +02:00
parent f9470d8189
commit 50eaed4d0c
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -25,70 +25,9 @@ import (
// SystemInfo returns information about the host server the daemon is running on. // SystemInfo returns information about the host server the daemon is running on.
func (daemon *Daemon) SystemInfo() (*types.Info, error) { func (daemon *Daemon) SystemInfo() (*types.Info, error) {
kernelVersion := "<unknown>"
if kv, err := kernel.GetKernelVersion(); err != nil {
logrus.Warnf("Could not get kernel version: %v", err)
} else {
kernelVersion = kv.String()
}
operatingSystem := "<unknown>"
if s, err := operatingsystem.GetOperatingSystem(); err != nil {
logrus.Warnf("Could not get operating system name: %v", err)
} else {
operatingSystem = s
}
// Don't do containerized check on Windows
if runtime.GOOS != "windows" {
if inContainer, err := operatingsystem.IsContainerized(); err != nil {
logrus.Errorf("Could not determine if daemon is containerized: %v", err)
operatingSystem += " (error determining if containerized)"
} else if inContainer {
operatingSystem += " (containerized)"
}
}
meminfo, err := system.ReadMemInfo()
if err != nil {
logrus.Errorf("Could not read system memory info: %v", err)
meminfo = &system.MemInfo{}
}
sysInfo := sysinfo.New(true) sysInfo := sysinfo.New(true)
cRunning, cPaused, cStopped := stateCtr.get() cRunning, cPaused, cStopped := stateCtr.get()
securityOptions := []string{}
if sysInfo.AppArmor {
securityOptions = append(securityOptions, "name=apparmor")
}
if sysInfo.Seccomp && supportsSeccomp {
profile := daemon.seccompProfilePath
if profile == "" {
profile = "default"
}
securityOptions = append(securityOptions, fmt.Sprintf("name=seccomp,profile=%s", profile))
}
if selinuxEnabled() {
securityOptions = append(securityOptions, "name=selinux")
}
rootIDs := daemon.idMappings.RootPair()
if rootIDs.UID != 0 || rootIDs.GID != 0 {
securityOptions = append(securityOptions, "name=userns")
}
var ds [][2]string
drivers := ""
statuses := daemon.imageService.LayerStoreStatus()
for os, gd := range daemon.graphDrivers {
ds = append(ds, statuses[os]...)
drivers += gd
if len(daemon.graphDrivers) > 1 {
drivers += fmt.Sprintf(" (%s) ", os)
}
}
drivers = strings.TrimSpace(drivers)
v := &types.Info{ v := &types.Info{
ID: daemon.ID, ID: daemon.ID,
Containers: cRunning + cPaused + cStopped, Containers: cRunning + cPaused + cStopped,
@ -96,27 +35,25 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
ContainersPaused: cPaused, ContainersPaused: cPaused,
ContainersStopped: cStopped, ContainersStopped: cStopped,
Images: daemon.imageService.CountImages(), Images: daemon.imageService.CountImages(),
Driver: drivers,
DriverStatus: ds,
Plugins: daemon.showPluginsInfo(),
IPv4Forwarding: !sysInfo.IPv4ForwardingDisabled, IPv4Forwarding: !sysInfo.IPv4ForwardingDisabled,
BridgeNfIptables: !sysInfo.BridgeNFCallIPTablesDisabled, BridgeNfIptables: !sysInfo.BridgeNFCallIPTablesDisabled,
BridgeNfIP6tables: !sysInfo.BridgeNFCallIP6TablesDisabled, BridgeNfIP6tables: !sysInfo.BridgeNFCallIP6TablesDisabled,
Debug: debug.IsEnabled(), Debug: debug.IsEnabled(),
Name: hostName(),
NFd: fileutils.GetTotalUsedFds(), NFd: fileutils.GetTotalUsedFds(),
NGoroutines: runtime.NumGoroutine(), NGoroutines: runtime.NumGoroutine(),
SystemTime: time.Now().Format(time.RFC3339Nano), SystemTime: time.Now().Format(time.RFC3339Nano),
LoggingDriver: daemon.defaultLogConfig.Type, LoggingDriver: daemon.defaultLogConfig.Type,
CgroupDriver: daemon.getCgroupDriver(), CgroupDriver: daemon.getCgroupDriver(),
NEventsListener: daemon.EventsService.SubscribersCount(), NEventsListener: daemon.EventsService.SubscribersCount(),
KernelVersion: kernelVersion, KernelVersion: kernelVersion(),
OperatingSystem: operatingSystem, OperatingSystem: operatingSystem(),
IndexServerAddress: registry.IndexServer, IndexServerAddress: registry.IndexServer,
OSType: platform.OSType, OSType: platform.OSType,
Architecture: platform.Architecture, Architecture: platform.Architecture,
RegistryConfig: daemon.RegistryService.ServiceConfig(), RegistryConfig: daemon.RegistryService.ServiceConfig(),
NCPU: sysinfo.NumCPU(), NCPU: sysinfo.NumCPU(),
MemTotal: meminfo.MemTotal, MemTotal: memInfo().MemTotal,
GenericResources: daemon.genericResources, GenericResources: daemon.genericResources,
DockerRootDir: daemon.configStore.Root, DockerRootDir: daemon.configStore.Root,
Labels: daemon.configStore.Labels, Labels: daemon.configStore.Labels,
@ -128,32 +65,21 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
HTTPSProxy: sockets.GetProxyEnv("https_proxy"), HTTPSProxy: sockets.GetProxyEnv("https_proxy"),
NoProxy: sockets.GetProxyEnv("no_proxy"), NoProxy: sockets.GetProxyEnv("no_proxy"),
LiveRestoreEnabled: daemon.configStore.LiveRestoreEnabled, LiveRestoreEnabled: daemon.configStore.LiveRestoreEnabled,
SecurityOptions: securityOptions,
Isolation: daemon.defaultIsolation, Isolation: daemon.defaultIsolation,
} }
// Retrieve platform specific info // Retrieve platform specific info
daemon.FillPlatformInfo(v, sysInfo) daemon.FillPlatformInfo(v, sysInfo)
daemon.fillDriverInfo(v)
hostname := "" daemon.fillPluginsInfo(v)
if hn, err := os.Hostname(); err != nil { daemon.fillSecurityOptions(v, sysInfo)
logrus.Warnf("Could not get hostname: %v", err)
} else {
hostname = hn
}
v.Name = hostname
return v, nil return v, nil
} }
// SystemVersion returns version information about the daemon. // SystemVersion returns version information about the daemon.
func (daemon *Daemon) SystemVersion() types.Version { func (daemon *Daemon) SystemVersion() types.Version {
kernelVersion := "<unknown>" kernelVersion := kernelVersion()
if kv, err := kernel.GetKernelVersion(); err != nil {
logrus.Warnf("Could not get kernel version: %v", err)
} else {
kernelVersion = kv.String()
}
v := types.Version{ v := types.Version{
Components: []types.ComponentVersion{ Components: []types.ComponentVersion{
@ -192,15 +118,100 @@ func (daemon *Daemon) SystemVersion() types.Version {
return v return v
} }
func (daemon *Daemon) showPluginsInfo() types.PluginsInfo { func (daemon *Daemon) fillDriverInfo(v *types.Info) {
var pluginsInfo types.PluginsInfo var ds [][2]string
drivers := ""
statuses := daemon.imageService.LayerStoreStatus()
for os, gd := range daemon.graphDrivers {
ds = append(ds, statuses[os]...)
drivers += gd
if len(daemon.graphDrivers) > 1 {
drivers += fmt.Sprintf(" (%s) ", os)
}
}
drivers = strings.TrimSpace(drivers)
v.Driver = drivers
v.DriverStatus = ds
}
func (daemon *Daemon) fillPluginsInfo(v *types.Info) {
v.Plugins = types.PluginsInfo{
Volume: daemon.volumes.GetDriverList(),
Network: daemon.GetNetworkDriverList(),
pluginsInfo.Volume = daemon.volumes.GetDriverList()
pluginsInfo.Network = daemon.GetNetworkDriverList()
// The authorization plugins are returned in the order they are // The authorization plugins are returned in the order they are
// used as they constitute a request/response modification chain. // used as they constitute a request/response modification chain.
pluginsInfo.Authorization = daemon.configStore.AuthorizationPlugins Authorization: daemon.configStore.AuthorizationPlugins,
pluginsInfo.Log = logger.ListDrivers() Log: logger.ListDrivers(),
}
return pluginsInfo }
func (daemon *Daemon) fillSecurityOptions(v *types.Info, sysInfo *sysinfo.SysInfo) {
var securityOptions []string
if sysInfo.AppArmor {
securityOptions = append(securityOptions, "name=apparmor")
}
if sysInfo.Seccomp && supportsSeccomp {
profile := daemon.seccompProfilePath
if profile == "" {
profile = "default"
}
securityOptions = append(securityOptions, fmt.Sprintf("name=seccomp,profile=%s", profile))
}
if selinuxEnabled() {
securityOptions = append(securityOptions, "name=selinux")
}
if rootIDs := daemon.idMappings.RootPair(); rootIDs.UID != 0 || rootIDs.GID != 0 {
securityOptions = append(securityOptions, "name=userns")
}
v.SecurityOptions = securityOptions
}
func hostName() string {
hostname := ""
if hn, err := os.Hostname(); err != nil {
logrus.Warnf("Could not get hostname: %v", err)
} else {
hostname = hn
}
return hostname
}
func kernelVersion() string {
kernelVersion := "<unknown>"
if kv, err := kernel.GetKernelVersion(); err != nil {
logrus.Warnf("Could not get kernel version: %v", err)
} else {
kernelVersion = kv.String()
}
return kernelVersion
}
func memInfo() *system.MemInfo {
memInfo, err := system.ReadMemInfo()
if err != nil {
logrus.Errorf("Could not read system memory info: %v", err)
memInfo = &system.MemInfo{}
}
return memInfo
}
func operatingSystem() string {
operatingSystem := "<unknown>"
if s, err := operatingsystem.GetOperatingSystem(); err != nil {
logrus.Warnf("Could not get operating system name: %v", err)
} else {
operatingSystem = s
}
// Don't do containerized check on Windows
if runtime.GOOS != "windows" {
if inContainer, err := operatingsystem.IsContainerized(); err != nil {
logrus.Errorf("Could not determine if daemon is containerized: %v", err)
operatingSystem += " (error determining if containerized)"
} else if inContainer {
operatingSystem += " (containerized)"
}
}
return operatingSystem
} }