Skip to content

Commit 9e7a939

Browse files
committed
support disk usage metrics for containerd factory
query rootfs dir from contianerd status and get disk usage stat by rootfs dir `/run/containerd/io.containerd.runtime.v2.task/${contianerid}/rootfs` Signed-off-by: joey <[email protected]>
1 parent c15f44e commit 9e7a939

File tree

20 files changed

+651
-121
lines changed

20 files changed

+651
-121
lines changed

build/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fi
6161

6262
# Since github.com/google/cadvisor/cmd is a submodule, we must build from inside that directory
6363
pushd cmd > /dev/null
64-
go build ${GO_FLAGS} -ldflags "${ldflags}" -o "${output_file}" "${repo_path}/cmd"
64+
go mod tidy && go build ${GO_FLAGS} -ldflags "${ldflags}" -o "${output_file}" "${repo_path}/cmd"
6565
popd > /dev/null
6666

6767
exit 0

cmd/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ require (
9393
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
9494
github.com/karrick/godirwalk v1.17.0 // indirect
9595
github.com/klauspost/compress v1.15.11 // indirect
96-
github.com/kr/pretty v0.3.1 // indirect
96+
github.com/kr/text v0.2.0 // indirect
9797
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
9898
github.com/mistifyio/go-zfs v2.1.2-0.20190413222219-f784269be439+incompatible // indirect
9999
github.com/moby/docker-image-spec v1.3.1 // indirect
@@ -135,4 +135,5 @@ require (
135135
google.golang.org/grpc v1.64.1 // indirect
136136
google.golang.org/protobuf v1.34.1 // indirect
137137
gopkg.in/yaml.v3 v3.0.1 // indirect
138+
k8s.io/cri-api v0.29.3 // indirect
138139
)

cmd/go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ github.com/opencontainers/selinux v1.10.0 h1:rAiKF8hTcgLI3w0DHm6i0ylVVcOrlgR1kK9
351351
github.com/opencontainers/selinux v1.10.0/go.mod h1:2i0OySw99QjzBBQByd1Gr9gSjvuho1lHsJxIJ3gGbJI=
352352
github.com/pierrec/lz4/v4 v4.1.17 h1:kV4Ip+/hUBC+8T6+2EgburRtkE9ef4nbY3f4dFhGjMc=
353353
github.com/pierrec/lz4/v4 v4.1.17/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
354-
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
355354
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
356355
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
357356
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
@@ -389,7 +388,6 @@ github.com/prometheus/procfs v0.8.0/go.mod h1:z7EfXMXOkbkqb9IINtpCn86r/to3BnA0ua
389388
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
390389
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
391390
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
392-
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
393391
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
394392
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
395393
github.com/seccomp/libseccomp-golang v0.9.2-0.20220502022130-f33da4d89646 h1:RpforrEYXWkmGwJHIGnLZ3tTWStkjVVstwzNGqxX2Ds=
@@ -790,6 +788,8 @@ honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWh
790788
honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg=
791789
honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
792790
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
791+
k8s.io/cri-api v0.29.3 h1:ppKSui+hhTJW774Mou6x+/ealmzt2jmTM0vsEQVWrjI=
792+
k8s.io/cri-api v0.29.3/go.mod h1:3X7EnhsNaQnCweGhQCJwKNHlH7wHEYuKQ19bRvXMoJY=
793793
k8s.io/klog/v2 v2.100.1 h1:7WCHKK6K8fNhTqfBhISHQ97KrnJNFZMcQvKp7gP/tmg=
794794
k8s.io/klog/v2 v2.100.1/go.mod h1:y1WjHnz7Dj687irZUWR/WLkLc5N1YHtjLdmgWjndZn0=
795795
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 h1:qY1Ad8PODbnymg2pRbkyMT/ylpTrCM8P2RJ0yroCyIk=

container/containerd/client.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ package containerd
1616

1717
import (
1818
"context"
19+
"encoding/json"
1920
"errors"
2021
"fmt"
2122
"net"
23+
"path"
24+
"strings"
2225
"sync"
2326
"time"
2427

@@ -31,6 +34,9 @@ import (
3134
"google.golang.org/grpc/backoff"
3235
"google.golang.org/grpc/credentials/insecure"
3336
emptypb "google.golang.org/protobuf/types/known/emptypb"
37+
runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
38+
39+
"github.com/google/cadvisor/container/containerd/config"
3440

3541
"github.com/google/cadvisor/container/containerd/containers"
3642
"github.com/google/cadvisor/container/containerd/pkg/dialer"
@@ -40,18 +46,25 @@ type client struct {
4046
containerService containersapi.ContainersClient
4147
taskService tasksapi.TasksClient
4248
versionService versionapi.VersionClient
49+
runtimeService runtimeapi.RuntimeServiceClient
4350
}
4451

4552
type ContainerdClient interface {
4653
LoadContainer(ctx context.Context, id string) (*containers.Container, error)
4754
TaskPid(ctx context.Context, id string) (uint32, error)
4855
Version(ctx context.Context) (string, error)
56+
RootfsDir(ctx context.Context) (string, error)
4957
}
5058

5159
var (
5260
ErrTaskIsInUnknownState = errors.New("containerd task is in unknown state") // used when process reported in containerd task is in Unknown State
5361
)
5462

63+
var (
64+
statePlugin = "io.containerd.grpc.v1.cri"
65+
taskRuntimePlugin = "io.containerd.runtime.v2.task"
66+
)
67+
5568
var once sync.Once
5669
var ctrdClient ContainerdClient = nil
5770

@@ -104,6 +117,7 @@ func Client(address, namespace string) (ContainerdClient, error) {
104117
containerService: containersapi.NewContainersClient(conn),
105118
taskService: tasksapi.NewTasksClient(conn),
106119
versionService: versionapi.NewVersionClient(conn),
120+
runtimeService: runtimeapi.NewRuntimeServiceClient(conn),
107121
}
108122
})
109123
return ctrdClient, retErr
@@ -140,6 +154,19 @@ func (c *client) Version(ctx context.Context) (string, error) {
140154
return response.Version, nil
141155
}
142156

157+
func (c *client) RootfsDir(ctx context.Context) (string, error) {
158+
r, err := c.runtimeService.Status(ctx, &runtimeapi.StatusRequest{Verbose: true})
159+
if err != nil {
160+
return "", err
161+
}
162+
configStr := r.Info["config"]
163+
config := config.Config{}
164+
if err := json.Unmarshal([]byte(configStr), &config); err != nil {
165+
return "", err
166+
}
167+
return path.Join(strings.TrimSuffix(config.StateDir, statePlugin), taskRuntimePlugin), nil
168+
}
169+
143170
func containerFromProto(containerpb *containersapi.Container) *containers.Container {
144171
var runtime containers.RuntimeInfo
145172
// TODO: is nil check required for containerpb

container/containerd/client_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ func (c *containerdClientMock) TaskPid(ctx context.Context, id string) (uint32,
4545
return 2389, nil
4646
}
4747

48+
func (c *containerdClientMock) RootfsDir(ctx context.Context) (string, error) {
49+
return "/run/containerd/io.containerd.runtime.v2.task", nil
50+
}
51+
4852
func mockcontainerdClient(cntrs map[string]*containers.Container, returnErr error) ContainerdClient {
4953
return &containerdClientMock{
5054
cntrs: cntrs,

0 commit comments

Comments
 (0)