-
Notifications
You must be signed in to change notification settings - Fork 1k
/
image_fs_info.go
72 lines (62 loc) · 2.1 KB
/
image_fs_info.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package server
import (
"context"
"fmt"
"path"
"time"
"github.com/containers/storage"
crioStorage "github.com/cri-o/cri-o/utils"
types "k8s.io/cri-api/pkg/apis/runtime/v1"
)
func getStorageFsInfo(store storage.Store) (*types.ImageFsInfoResponse, error) {
rootPath := store.GraphRoot()
imagePath := store.ImageStore()
storageDriver := store.GraphDriverName()
var graphRootPath string
if imagePath == "" {
graphRootPath = path.Join(rootPath, storageDriver+"-images")
} else {
graphRootPath = path.Join(rootPath, storageDriver+"-containers")
}
graphUsage, err := getUsage(graphRootPath)
if err != nil {
return nil, fmt.Errorf("unable to get usage for %s: %w", graphRootPath, err)
}
if imagePath == "" {
return &types.ImageFsInfoResponse{
ImageFilesystems: []*types.FilesystemUsage{graphUsage},
ContainerFilesystems: []*types.FilesystemUsage{graphUsage},
}, nil
}
resp := &types.ImageFsInfoResponse{
ContainerFilesystems: []*types.FilesystemUsage{graphUsage},
}
imageRoot := path.Join(imagePath, storageDriver+"-images")
imageUsage, err := getUsage(imageRoot)
if err != nil {
return nil, fmt.Errorf("unable to get usage for %s: %w", imageRoot, err)
}
resp.ImageFilesystems = []*types.FilesystemUsage{imageUsage}
return resp, nil
}
// ImageFsInfo returns information of the filesystem that is used to store images.
func (s *Server) ImageFsInfo(context.Context, *types.ImageFsInfoRequest) (*types.ImageFsInfoResponse, error) {
store := s.StorageImageServer().GetStore()
fsUsage, err := getStorageFsInfo(store)
if err != nil {
return nil, fmt.Errorf("get image fs info %w", err)
}
return fsUsage, nil
}
func getUsage(containerPath string) (*types.FilesystemUsage, error) {
bytes, inodes, err := crioStorage.GetDiskUsageStats(containerPath)
if err != nil {
return nil, fmt.Errorf("get disk usage for path %s: %w", containerPath, err)
}
return &types.FilesystemUsage{
Timestamp: time.Now().UnixNano(),
FsId: &types.FilesystemIdentifier{Mountpoint: containerPath},
UsedBytes: &types.UInt64Value{Value: bytes},
InodesUsed: &types.UInt64Value{Value: inodes},
}, nil
}