Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update gopsutil to v3 to fix MacOS deprecation warnings #16321

Merged
merged 4 commits into from Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 41 additions & 16 deletions helper/hostutil/hostinfo.go
Expand Up @@ -96,26 +96,41 @@ func CollectHostMemory(ctx context.Context) (*VirtualMemoryStat, error) {
}

return &VirtualMemoryStat{
VirtualMemoryStat: m,
// Fields below are added to maintain backwards compatibility with gopsutil v2
Total: m.Total,
Available: m.Available,
Used: m.Used,
UsedPercent: m.UsedPercent,
Free: m.Free,
Active: m.Active,
Inactive: m.Inactive,
Wired: m.Wired,
Laundry: m.Laundry,
Buffers: m.Buffers,
Cached: m.Cached,
Writeback: m.WriteBack,
Dirty: m.Dirty,
WritebackTmp: m.WriteBackTmp,
Shared: m.Shared,
Slab: m.Slab,
SReclaimable: m.Sreclaimable,
SUnreclaim: m.Sunreclaim,
PageTables: m.PageTables,
SwapCached: m.SwapCached,
CommitLimit: m.CommitLimit,
CommittedAS: m.CommittedAS,
HighFree: m.HighFree,
HighTotal: m.HighTotal,
HugePagesFree: m.HugePagesFree,
HugePageSize: m.HugePageSize,
HugePagesTotal: m.HugePagesTotal,
LowFree: m.LowFree,
HighFree: m.HighFree,
LowTotal: m.LowTotal,
PageTables: m.PageTables,
SwapCached: m.SwapCached,
SwapFree: m.SwapFree,
LowFree: m.LowFree,
SwapTotal: m.SwapTotal,
VMallocChunk: m.VmallocChunk,
SwapFree: m.SwapFree,
Mapped: m.Mapped,
VMallocTotal: m.VmallocTotal,
VMallocUsed: m.VmallocUsed,
Writeback: m.WriteBack,
WritebackTmp: m.WriteBackTmp,
VMallocChunk: m.VmallocChunk,
HugePagesTotal: m.HugePagesTotal,
HugePagesFree: m.HugePagesFree,
HugePageSize: m.HugePageSize,
}, nil
}

Expand All @@ -126,8 +141,18 @@ func CollectHostInfoStat(ctx context.Context) (*HostInfoStat, error) {
}

return &HostInfoStat{
InfoStat: h,
// Fields below are added to maintain backwards compatibility with gopsutil v2
HostID: h.HostID,
Hostname: h.Hostname,
Uptime: h.Uptime,
BootTime: h.BootTime,
Procs: h.Procs,
OS: h.OS,
Platform: h.Platform,
PlatformFamily: h.PlatformFamily,
PlatformVersion: h.PlatformVersion,
KernelVersion: h.KernelVersion,
KernelArch: h.KernelArch,
VirtualizationSystem: h.VirtualizationSystem,
VirtualizationRole: h.VirtualizationRole,
HostID: h.HostID,
}, nil
}
112 changes: 79 additions & 33 deletions helper/hostutil/hostinfo_util.go
@@ -1,51 +1,97 @@
package hostutil

import (
"github.com/shirou/gopsutil/v3/host"
"github.com/shirou/gopsutil/v3/mem"
)

// VirutalMemoryStat holds commonly used memory measurements. We must have a
// VirtualMemoryStat holds commonly used memory measurements. We must have a
// local type here in order to avoid building the gopsutil library on certain
// arch types.
//
// This struct is copied from https://github.com/shirou/gopsutil/blob/b49f37e9f30f49530cf2ad6038a4dac1b746c8f7/mem/mem.go#L15
austingebauer marked this conversation as resolved.
Show resolved Hide resolved
// to maintain backwards compatibility in the Vault host-info API. This is done because
// gopsutil changed JSON struct tags between its v2 and v3 releases. For details see
// https://github.com/shirou/gopsutil/tree/master/_tools/v3migration.
type VirtualMemoryStat struct {
*mem.VirtualMemoryStat
// Total amount of RAM on this system
Total uint64 `json:"total"`

// RAM available for programs to allocate
//
// This value is computed from the kernel specific values.
Available uint64 `json:"available"`

// RAM used by programs
//
// This value is computed from the kernel specific values.
Used uint64 `json:"used"`

// A subset of JSON struct tags in mem.VirtualMemoryStat were changed in a backwards
// incompatible way in the v3 release of gopsutil. The fields below are copied from
// v2 of gopsutil to maintain backwards compatibility in the Vault host-info API.
// Percentage of RAM used by programs
//
// The following details the changed JSON struct tags between v2 and v3:
// https://github.com/shirou/gopsutil/blob/master/_tools/v3migration/v3migration.sh#L61
// This value is computed from the kernel specific values.
UsedPercent float64 `json:"usedPercent"`

// This is the kernel's notion of free memory; RAM chips whose bits nobody
// cares about the value of right now. For a human consumable number,
// Available is what you really want.
Free uint64 `json:"free"`

// OS X / BSD specific numbers:
// http://www.macyourself.com/2010/02/17/what-is-free-wired-active-and-inactive-system-memory-ram/
Active uint64 `json:"active"`
Inactive uint64 `json:"inactive"`
Wired uint64 `json:"wired"`

// FreeBSD specific numbers:
// https://reviews.freebsd.org/D8467
Laundry uint64 `json:"laundry"`

// Linux specific numbers
// https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
// https://www.kernel.org/doc/Documentation/vm/overcommit-accounting
Buffers uint64 `json:"buffers"`
Cached uint64 `json:"cached"`
Writeback uint64 `json:"writeback"`
Dirty uint64 `json:"dirty"`
WritebackTmp uint64 `json:"writebacktmp"`
Shared uint64 `json:"shared"`
Slab uint64 `json:"slab"`
SReclaimable uint64 `json:"sreclaimable"`
SUnreclaim uint64 `json:"sunreclaim"`
PageTables uint64 `json:"pagetables"`
SwapCached uint64 `json:"swapcached"`
CommitLimit uint64 `json:"commitlimit"`
CommittedAS uint64 `json:"committedas"`
HighFree uint64 `json:"highfree"`
HighTotal uint64 `json:"hightotal"`
HugePagesFree uint64 `json:"hugepagesfree"`
HugePageSize uint64 `json:"hugepagesize"`
HugePagesTotal uint64 `json:"hugepagestotal"`
LowFree uint64 `json:"lowfree"`
HighFree uint64 `json:"highfree"`
LowTotal uint64 `json:"lowtotal"`
PageTables uint64 `json:"pagetables"`
SwapCached uint64 `json:"swapcached"`
SwapFree uint64 `json:"swapfree"`
LowFree uint64 `json:"lowfree"`
SwapTotal uint64 `json:"swaptotal"`
VMallocChunk uint64 `json:"vmallocchunk"`
SwapFree uint64 `json:"swapfree"`
Mapped uint64 `json:"mapped"`
VMallocTotal uint64 `json:"vmalloctotal"`
VMallocUsed uint64 `json:"vmallocused"`
Writeback uint64 `json:"writeback"`
WritebackTmp uint64 `json:"writebacktmp"`
VMallocChunk uint64 `json:"vmallocchunk"`
HugePagesTotal uint64 `json:"hugepagestotal"`
HugePagesFree uint64 `json:"hugepagesfree"`
HugePageSize uint64 `json:"hugepagesize"`
}

// A HostInfoStat describes the host status.
// HostInfoStat describes the host status.
//
// This struct is copied from https://github.com/shirou/gopsutil/blob/b49f37e9f30f49530cf2ad6038a4dac1b746c8f7/host/host.go#L17
// to maintain backwards compatibility in the Vault host-info API. This is done because
// gopsutil changed JSON struct tags between its v2 and v3 releases. For details see
// https://github.com/shirou/gopsutil/tree/master/_tools/v3migration.
type HostInfoStat struct {
*host.InfoStat

// A subset of JSON struct tags in host.InfoStat were changed in a backwards
// incompatible way in the v3 release of gopsutil. The fields below are copied from
// v2 of gopsutil to maintain backwards compatibility in the Vault host-info API.
//
// The following details the changed JSON struct tags between v2 and v3:
// https://github.com/shirou/gopsutil/blob/master/_tools/v3migration/v3migration.sh#L72
HostID string `json:"hostid"`
Hostname string `json:"hostname"`
Uptime uint64 `json:"uptime"`
BootTime uint64 `json:"bootTime"`
Procs uint64 `json:"procs"`
OS string `json:"os"`
Platform string `json:"platform"`
PlatformFamily string `json:"platformFamily"`
PlatformVersion string `json:"platformVersion"`
KernelVersion string `json:"kernelVersion"`
KernelArch string `json:"kernelArch"`
VirtualizationSystem string `json:"virtualizationSystem"`
VirtualizationRole string `json:"virtualizationRole"`
HostID string `json:"hostid"`
}