mirror of
https://github.com/moby/moby.git
synced 2022-11-09 12:21:53 -05:00
Merge pull request #37451 from thaJeztah/refactor_system_info
Refactor daemon.info to reduce cyclomatic complexity
This commit is contained in:
commit
a2a1d04bca
3 changed files with 108 additions and 97 deletions
197
daemon/info.go
197
daemon/info.go
|
@ -25,70 +25,9 @@ import (
|
|||
|
||||
// SystemInfo returns information about the host server the daemon is running on.
|
||||
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)
|
||||
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{
|
||||
ID: daemon.ID,
|
||||
Containers: cRunning + cPaused + cStopped,
|
||||
|
@ -96,27 +35,25 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
|
|||
ContainersPaused: cPaused,
|
||||
ContainersStopped: cStopped,
|
||||
Images: daemon.imageService.CountImages(),
|
||||
Driver: drivers,
|
||||
DriverStatus: ds,
|
||||
Plugins: daemon.showPluginsInfo(),
|
||||
IPv4Forwarding: !sysInfo.IPv4ForwardingDisabled,
|
||||
BridgeNfIptables: !sysInfo.BridgeNFCallIPTablesDisabled,
|
||||
BridgeNfIP6tables: !sysInfo.BridgeNFCallIP6TablesDisabled,
|
||||
Debug: debug.IsEnabled(),
|
||||
Name: hostName(),
|
||||
NFd: fileutils.GetTotalUsedFds(),
|
||||
NGoroutines: runtime.NumGoroutine(),
|
||||
SystemTime: time.Now().Format(time.RFC3339Nano),
|
||||
LoggingDriver: daemon.defaultLogConfig.Type,
|
||||
CgroupDriver: daemon.getCgroupDriver(),
|
||||
NEventsListener: daemon.EventsService.SubscribersCount(),
|
||||
KernelVersion: kernelVersion,
|
||||
OperatingSystem: operatingSystem,
|
||||
KernelVersion: kernelVersion(),
|
||||
OperatingSystem: operatingSystem(),
|
||||
IndexServerAddress: registry.IndexServer,
|
||||
OSType: platform.OSType,
|
||||
Architecture: platform.Architecture,
|
||||
RegistryConfig: daemon.RegistryService.ServiceConfig(),
|
||||
NCPU: sysinfo.NumCPU(),
|
||||
MemTotal: meminfo.MemTotal,
|
||||
MemTotal: memInfo().MemTotal,
|
||||
GenericResources: daemon.genericResources,
|
||||
DockerRootDir: daemon.configStore.Root,
|
||||
Labels: daemon.configStore.Labels,
|
||||
|
@ -128,32 +65,21 @@ func (daemon *Daemon) SystemInfo() (*types.Info, error) {
|
|||
HTTPSProxy: sockets.GetProxyEnv("https_proxy"),
|
||||
NoProxy: sockets.GetProxyEnv("no_proxy"),
|
||||
LiveRestoreEnabled: daemon.configStore.LiveRestoreEnabled,
|
||||
SecurityOptions: securityOptions,
|
||||
Isolation: daemon.defaultIsolation,
|
||||
}
|
||||
|
||||
// Retrieve platform specific info
|
||||
daemon.FillPlatformInfo(v, sysInfo)
|
||||
|
||||
hostname := ""
|
||||
if hn, err := os.Hostname(); err != nil {
|
||||
logrus.Warnf("Could not get hostname: %v", err)
|
||||
} else {
|
||||
hostname = hn
|
||||
}
|
||||
v.Name = hostname
|
||||
daemon.fillPlatformInfo(v, sysInfo)
|
||||
daemon.fillDriverInfo(v)
|
||||
daemon.fillPluginsInfo(v)
|
||||
daemon.fillSecurityOptions(v, sysInfo)
|
||||
|
||||
return v, nil
|
||||
}
|
||||
|
||||
// SystemVersion returns version information about the daemon.
|
||||
func (daemon *Daemon) SystemVersion() types.Version {
|
||||
kernelVersion := "<unknown>"
|
||||
if kv, err := kernel.GetKernelVersion(); err != nil {
|
||||
logrus.Warnf("Could not get kernel version: %v", err)
|
||||
} else {
|
||||
kernelVersion = kv.String()
|
||||
}
|
||||
kernelVersion := kernelVersion()
|
||||
|
||||
v := types.Version{
|
||||
Components: []types.ComponentVersion{
|
||||
|
@ -192,15 +118,100 @@ func (daemon *Daemon) SystemVersion() types.Version {
|
|||
return v
|
||||
}
|
||||
|
||||
func (daemon *Daemon) showPluginsInfo() types.PluginsInfo {
|
||||
var pluginsInfo types.PluginsInfo
|
||||
func (daemon *Daemon) fillDriverInfo(v *types.Info) {
|
||||
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)
|
||||
|
||||
pluginsInfo.Volume = daemon.volumes.GetDriverList()
|
||||
pluginsInfo.Network = daemon.GetNetworkDriverList()
|
||||
// The authorization plugins are returned in the order they are
|
||||
// used as they constitute a request/response modification chain.
|
||||
pluginsInfo.Authorization = daemon.configStore.AuthorizationPlugins
|
||||
pluginsInfo.Log = logger.ListDrivers()
|
||||
|
||||
return pluginsInfo
|
||||
v.Driver = drivers
|
||||
v.DriverStatus = ds
|
||||
}
|
||||
|
||||
func (daemon *Daemon) fillPluginsInfo(v *types.Info) {
|
||||
v.Plugins = types.PluginsInfo{
|
||||
Volume: daemon.volumes.GetDriverList(),
|
||||
Network: daemon.GetNetworkDriverList(),
|
||||
|
||||
// The authorization plugins are returned in the order they are
|
||||
// used as they constitute a request/response modification chain.
|
||||
Authorization: daemon.configStore.AuthorizationPlugins,
|
||||
Log: logger.ListDrivers(),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
|
|
@ -14,8 +14,8 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
// FillPlatformInfo fills the platform related info.
|
||||
func (daemon *Daemon) FillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo) {
|
||||
// fillPlatformInfo fills the platform related info.
|
||||
func (daemon *Daemon) fillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo) {
|
||||
v.MemoryLimit = sysInfo.MemoryLimit
|
||||
v.SwapLimit = sysInfo.SwapLimit
|
||||
v.KernelMemory = sysInfo.KernelMemory
|
||||
|
|
|
@ -5,6 +5,6 @@ import (
|
|||
"github.com/docker/docker/pkg/sysinfo"
|
||||
)
|
||||
|
||||
// FillPlatformInfo fills the platform related info.
|
||||
func (daemon *Daemon) FillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo) {
|
||||
// fillPlatformInfo fills the platform related info.
|
||||
func (daemon *Daemon) fillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo) {
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue