Skip to content

Commit

Permalink
Merge pull request moby#49185 from thaJeztah/daemon_info_cleanups
Browse files Browse the repository at this point in the history
daemon: minor cleanups for getting system info
  • Loading branch information
thaJeztah authored Jan 1, 2025
2 parents 6f6c3b9 + aa7493f commit 700c91b
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions daemon/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"os"
"runtime"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -105,7 +106,7 @@ func (daemon *Daemon) SystemInfo(ctx context.Context) (*system.Info, error) {
func (daemon *Daemon) SystemVersion(ctx context.Context) (types.Version, error) {
defer metrics.StartTimer(hostInfoFunctions.WithValues("system_version"))()

kernelVersion := kernelVersion(ctx)
kernelVer := kernelVersion(ctx)
cfg := daemon.config()

v := types.Version{
Expand All @@ -121,8 +122,8 @@ func (daemon *Daemon) SystemVersion(ctx context.Context) (types.Version, error)
"Os": runtime.GOOS,
"Arch": runtime.GOARCH,
"BuildTime": dockerversion.BuildTime,
"KernelVersion": kernelVersion,
"Experimental": fmt.Sprintf("%t", cfg.Experimental),
"KernelVersion": kernelVer,
"Experimental": strconv.FormatBool(cfg.Experimental),
},
},
},
Expand All @@ -136,7 +137,7 @@ func (daemon *Daemon) SystemVersion(ctx context.Context) (types.Version, error)
Os: runtime.GOOS,
Arch: runtime.GOARCH,
BuildTime: dockerversion.BuildTime,
KernelVersion: kernelVersion,
KernelVersion: kernelVer,
Experimental: cfg.Experimental,
}

Expand Down Expand Up @@ -286,38 +287,37 @@ func (daemon *Daemon) fillDefaultAddressPools(ctx context.Context, v *system.Inf
func hostName(ctx context.Context) string {
ctx, span := tracing.StartSpan(ctx, "hostName")
defer span.End()
hostname := ""
if hn, err := os.Hostname(); err != nil {
log.G(ctx).Warnf("Could not get hostname: %v", err)
} else {
hostname = hn
hn, err := os.Hostname()
if err != nil {
log.G(ctx).WithError(err).Warn("Could not get hostname")
return ""
}
return hostname
return hn
}

func kernelVersion(ctx context.Context) string {
ctx, span := tracing.StartSpan(ctx, "kernelVersion")
defer span.End()

var kernelVersion string
var ver string
if kv, err := kernel.GetKernelVersion(); err != nil {
log.G(ctx).Warnf("Could not get kernel version: %v", err)
log.G(ctx).WithError(err).Warn("Could not get kernel version")
} else {
kernelVersion = kv.String()
ver = kv.String()
}
return kernelVersion
return ver
}

func memInfo(ctx context.Context) *meminfo.Memory {
ctx, span := tracing.StartSpan(ctx, "memInfo")
defer span.End()

memInfo, err := meminfo.Read()
mi, err := meminfo.Read()
if err != nil {
log.G(ctx).Errorf("Could not read system memory info: %v", err)
memInfo = &meminfo.Memory{}
log.G(ctx).WithError(err).Error("Could not read system memory info")
return &meminfo.Memory{}
}
return memInfo
return mi
}

func operatingSystem(ctx context.Context) (operatingSystem string) {
Expand All @@ -327,12 +327,12 @@ func operatingSystem(ctx context.Context) (operatingSystem string) {
defer metrics.StartTimer(hostInfoFunctions.WithValues("operating_system"))()

if s, err := operatingsystem.GetOperatingSystem(); err != nil {
log.G(ctx).Warnf("Could not get operating system name: %v", err)
log.G(ctx).WithError(err).Warn("Could not get operating system name")
} else {
operatingSystem = s
}
if inContainer, err := operatingsystem.IsContainerized(); err != nil {
log.G(ctx).Errorf("Could not determine if daemon is containerized: %v", err)
log.G(ctx).WithError(err).Error("Could not determine if daemon is containerized")
operatingSystem += " (error determining if containerized)"
} else if inContainer {
operatingSystem += " (containerized)"
Expand All @@ -349,7 +349,8 @@ func osVersion(ctx context.Context) (version string) {

version, err := operatingsystem.GetOperatingSystemVersion()
if err != nil {
log.G(ctx).Warnf("Could not get operating system version: %v", err)
log.G(ctx).WithError(err).Warn("Could not get operating system version")
return ""
}

return version
Expand Down

0 comments on commit 700c91b

Please sign in to comment.