Skip to content

Commit

Permalink
Provide info about mountpoint/fs usage in cri stats
Browse files Browse the repository at this point in the history
  • Loading branch information
jellonek committed Aug 22, 2018
1 parent 24c2601 commit cb13ced
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
18 changes: 4 additions & 14 deletions pkg/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/golang/glog"
digest "github.com/opencontainers/go-digest"

"github.com/Mirantis/virtlet/pkg/metadata/types"
"github.com/Mirantis/virtlet/pkg/utils"
)

Expand Down Expand Up @@ -57,17 +58,6 @@ type Translator func(context.Context, string) Endpoint
// that are currently in use.
type RefGetter func() (map[string]bool, error)

// FilesystemStats contains info about filesystem mountpoint and
// space/inodes used by images on it
type FilesystemStats struct {
// Mountpoint denotes the filesystem mount point
Mountpoint string
// UsedBytes is the number of bytes used by images
UsedBytes uint64
// UsedInodes is the number of inodes used by images
UsedInodes uint64
}

// Store is an interface for the image store.
type Store interface {
// ListImage returns the list of images in the store.
Expand Down Expand Up @@ -101,7 +91,7 @@ type Store interface {
SetRefGetter(imageRefGetter RefGetter)

// FilesystemStats returns disk space and inode usage info for this store.
FilesystemStats() (*FilesystemStats, error)
FilesystemStats() (*types.FilesystemStats, error)
}

// VirtualSizeFunc specifies a function that returns the virtual
Expand Down Expand Up @@ -563,7 +553,7 @@ func GetHexDigest(imageSpec string) string {
// TODO: instead of returning data from filesystem we should retrieve from
// metadata store sizes of images and sum them, or even retrieve precalculated
// sum. That's because same filesystem could be used by other things than images.
func (s *FileStore) FilesystemStats() (*FilesystemStats, error) {
func (s *FileStore) FilesystemStats() (*types.FilesystemStats, error) {
occupiedBytes, occupiedInodes, err := utils.GetFsStatsForPath(s.dir)
if err != nil {
return nil, err
Expand All @@ -576,7 +566,7 @@ func (s *FileStore) FilesystemStats() (*FilesystemStats, error) {
if err != nil {
return nil, err
}
return &FilesystemStats{
return &types.FilesystemStats{
Mountpoint: mount.FSRoot,
UsedBytes: occupiedBytes,
UsedInodes: occupiedInodes,
Expand Down
31 changes: 27 additions & 4 deletions pkg/libvirttools/virtualization.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package libvirttools

import (
"fmt"
"os"
"path/filepath"
"strings"
"time"

"github.com/golang/glog"
Expand Down Expand Up @@ -837,9 +839,30 @@ func (v *VirtualizationTool) VMStats(containerID string) (*types.VMStats, error)
}
vs.CpuUsage = cpuTime

// TODO: find image created by rootfs provider, stat it and fill there
// used by it bytes/inodes. Additionally mountpoint of fs on which
// root volume is located should be found and filled there
domainxml, err := domain.XML()
if err != nil {
return nil, err
}

rootDiskLocation := ""
for _, disk := range domainxml.Devices.Disks {
fname := disk.Source.File.File
// TODO: split file name and use HasPrefix on last part
// instead of Contains
if strings.Contains(fname, "virtlet_root_") {
rootDiskLocation = fname
}
}
if rootDiskLocation == "" {
return nil, fmt.Errorf("cannot locate root disk in domain definition")
}

fstat, err := os.Stat(rootDiskLocation)
if err != nil {
return nil, err
}
vs.FsBytes = uint64(fstat.Size())

return &vs, nil
}

Expand Down Expand Up @@ -878,7 +901,7 @@ func (v *VirtualizationTool) ListVMStats(filter *types.VMStatsFilter) ([]types.V

// volumeOwner implementation follows

// StoragePool returns StoragePool for volumes
// StoragePool implements volumeOwner StoragePool method
func (v *VirtualizationTool) StoragePool() (virt.StoragePool, error) {
return ensureStoragePool(v.storageConn, v.config.VolumePoolName)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/libvirttools/volumes.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
// ImageManager describes a images info provider.
type ImageManager interface {
GetImagePathAndVirtualSize(ref string) (string, uint64, error)
FilesystemStats() (*types.FilesystemStats, error)
}

type volumeOwner interface {
Expand Down
18 changes: 13 additions & 5 deletions pkg/manager/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -463,8 +463,12 @@ func (v *VirtletRuntimeService) ContainerStats(ctx context.Context, in *kubeapi.
if err != nil {
return nil, err
}
fsstats, err := v.virtTool.ImageManager().FilesystemStats()
if err != nil {
return nil, err
}
return &kubeapi.ContainerStatsResponse{
Stats: VMStatsToCRIContainerStats(vs),
Stats: VMStatsToCRIContainerStats(*vs, fsstats.Mountpoint),
}, nil
}

Expand All @@ -476,9 +480,13 @@ func (v *VirtletRuntimeService) ListContainerStats(ctx context.Context, in *kube
if err != nil {
return nil, err
}
fsstats, err := v.virtTool.ImageManager().FilesystemStats()
if err != nil {
return nil, err
}
var stats []*kubeapi.ContainerStats
for _, vs := range vmstatsList {
stats = append(stats, VMStatsToCRIContainerStats(vs))
stats = append(stats, VMStatsToCRIContainerStats(vs, fsstats.Mountpoint))
}

return &kubeapi.ListContainerStatsResponse{
Expand All @@ -488,7 +496,7 @@ func (v *VirtletRuntimeService) ListContainerStats(ctx context.Context, in *kube

// VMStatsToCRIContainerStats converts internal representation of vm/container stats
// to corresponding kubeapi type object
func VMStatsToCRIContainerStats(vs types.VMStats) *kubeapi.ContainerStats {
func VMStatsToCRIContainerStats(vs types.VMStats, mountpoint string) *kubeapi.ContainerStats {
return &kubeapi.ContainerStats{
Attributes: &kubeapi.ContainerAttributes{
Id: vs.ContainerID,
Expand All @@ -504,10 +512,10 @@ func VMStatsToCRIContainerStats(vs types.VMStats) *kubeapi.ContainerStats {
WritableLayer: &kubeapi.FilesystemUsage{
Timestamp: vs.Timestamp,
FsId: &kubeapi.FilesystemIdentifier{
Mountpoint: vs.Mountpoint,
Mountpoint: mountpoint,
},
UsedBytes: &kubeapi.UInt64Value{Value: vs.FsBytes},
InodesUsed: &kubeapi.UInt64Value{Value: vs.FsInodes},
InodesUsed: &kubeapi.UInt64Value{Value: 1},
},
}
}
Expand Down
16 changes: 11 additions & 5 deletions pkg/metadata/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,8 @@ type VMStats struct {
// MemoryUsage is expected to contain the amount of working set memory
// in bytes what in our case will be returned using RSS value
MemoryUsage uint64
// Mountpoint on host for filesystem containing the directory in which
// rootfs of VM is stored
Mountpoint string
// FsBytes represents current size of rootfs in bytes
FsBytes uint64
// FsInodes contains number of inodes used by rootfs image
FsInodes uint64
}

// NamespaceOption provides options for Linux namespaces.
Expand Down Expand Up @@ -288,3 +283,14 @@ type VMStatsFilter struct {
// Match Expressions is not supported.
LabelSelector map[string]string
}

// FilesystemStats contains info about filesystem mountpoint and
// space/inodes used by images on it
type FilesystemStats struct {
// Mountpoint denotes the filesystem mount point
Mountpoint string
// UsedBytes is the number of bytes used by images
UsedBytes uint64
// UsedInodes is the number of inodes used by images
UsedInodes uint64
}

0 comments on commit cb13ced

Please sign in to comment.