Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -965,7 +965,10 @@ func (a *agent) run() (retErr error) {
if err != nil {
return xerrors.Errorf("failed to create resources fetcher: %w", err)
}
resourcesFetcher := resourcesmonitor.NewFetcher(statfetcher)
resourcesFetcher, err := resourcesmonitor.NewFetcher(statfetcher)
if err != nil {
return xerrors.Errorf("new resource fetcher: %w", err)
}

resourcesmonitor := resourcesmonitor.NewResourcesMonitor(logger, clk, config, resourcesFetcher, aAPI)
return resourcesmonitor.Start(ctx)
Expand Down
37 changes: 31 additions & 6 deletions agent/proto/resourcesmonitor/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,44 @@ type Fetcher interface {

type fetcher struct {
*clistat.Statter
isContainerized bool
}

//nolint:revive
func NewFetcher(f *clistat.Statter) *fetcher {
return &fetcher{
f,
func NewFetcher(f *clistat.Statter) (*fetcher, error) {
isContainerized, err := f.IsContainerized()
if err != nil {
return nil, xerrors.Errorf("check is containerized: %w", err)
}

return &fetcher{f, isContainerized}, nil
}

func (f *fetcher) FetchMemory() (total int64, used int64, err error) {
mem, err := f.HostMemory(clistat.PrefixDefault)
if err != nil {
return 0, 0, xerrors.Errorf("failed to fetch memory: %w", err)
var mem *clistat.Result

if f.isContainerized {
mem, err = f.ContainerMemory(clistat.PrefixDefault)
if err != nil {
return 0, 0, xerrors.Errorf("fetch container memory: %w", err)
}

// A container might not have a memory limit set. If this
// happens we want to fallback to querying the host's memory
// to know what the total memory is on the host.
if mem.Total == nil {
hostMem, err := f.HostMemory(clistat.PrefixDefault)
if err != nil {
return 0, 0, xerrors.Errorf("fetch host memory: %w", err)
}

mem.Total = hostMem.Total
}
} else {
mem, err = f.HostMemory(clistat.PrefixDefault)
if err != nil {
return 0, 0, xerrors.Errorf("fetch host memory: %w", err)
}
}

if mem.Total == nil {
Expand Down
4 changes: 4 additions & 0 deletions cli/clistat/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const (
kubernetesDefaultServiceAccountToken = "/var/run/secrets/kubernetes.io/serviceaccount/token" //nolint:gosec
)

func (s *Statter) IsContainerized() (ok bool, err error) {
return IsContainerized(s.fs)
}

// IsContainerized returns whether the host is containerized.
// This is adapted from https://github.com/elastic/go-sysinfo/tree/main/providers/linux/container.go#L31
// with modifications to support Sysbox containers.
Expand Down
Loading