@@ -16,9 +16,12 @@ package containerd
16
16
17
17
import (
18
18
"context"
19
+ "encoding/json"
19
20
"errors"
20
21
"fmt"
21
22
"net"
23
+ "path"
24
+ "strings"
22
25
"sync"
23
26
"time"
24
27
@@ -27,10 +30,12 @@ import (
27
30
versionapi "github.com/containerd/containerd/api/services/version/v1"
28
31
tasktypes "github.com/containerd/containerd/api/types/task"
29
32
"github.com/containerd/errdefs"
33
+ "github.com/google/cadvisor/container/containerd/config"
30
34
"google.golang.org/grpc"
31
35
"google.golang.org/grpc/backoff"
32
36
"google.golang.org/grpc/credentials/insecure"
33
37
emptypb "google.golang.org/protobuf/types/known/emptypb"
38
+ runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1"
34
39
35
40
"github.com/google/cadvisor/container/containerd/containers"
36
41
"github.com/google/cadvisor/container/containerd/pkg/dialer"
@@ -40,18 +45,25 @@ type client struct {
40
45
containerService containersapi.ContainersClient
41
46
taskService tasksapi.TasksClient
42
47
versionService versionapi.VersionClient
48
+ runtimeService runtimeapi.RuntimeServiceClient
43
49
}
44
50
45
51
type ContainerdClient interface {
46
52
LoadContainer (ctx context.Context , id string ) (* containers.Container , error )
47
53
TaskPid (ctx context.Context , id string ) (uint32 , error )
48
54
Version (ctx context.Context ) (string , error )
55
+ RootfsDir (ctx context.Context ) (string , error )
49
56
}
50
57
51
58
var (
52
59
ErrTaskIsInUnknownState = errors .New ("containerd task is in unknown state" ) // used when process reported in containerd task is in Unknown State
53
60
)
54
61
62
+ var (
63
+ statePlugin = "io.containerd.grpc.v1.cri"
64
+ taskRuntimePlugin = "io.containerd.runtime.v2.task"
65
+ )
66
+
55
67
var once sync.Once
56
68
var ctrdClient ContainerdClient = nil
57
69
@@ -104,6 +116,7 @@ func Client(address, namespace string) (ContainerdClient, error) {
104
116
containerService : containersapi .NewContainersClient (conn ),
105
117
taskService : tasksapi .NewTasksClient (conn ),
106
118
versionService : versionapi .NewVersionClient (conn ),
119
+ runtimeService : runtimeapi .NewRuntimeServiceClient (conn ),
107
120
}
108
121
})
109
122
return ctrdClient , retErr
@@ -140,6 +153,19 @@ func (c *client) Version(ctx context.Context) (string, error) {
140
153
return response .Version , nil
141
154
}
142
155
156
+ func (c * client ) RootfsDir (ctx context.Context ) (string , error ) {
157
+ r , err := c .runtimeService .Status (ctx , & runtimeapi.StatusRequest {Verbose : true })
158
+ if err != nil {
159
+ return "" , err
160
+ }
161
+ configStr := r .Info ["config" ]
162
+ config := config.Config {}
163
+ if err := json .Unmarshal ([]byte (configStr ), & config ); err != nil {
164
+ return "" , err
165
+ }
166
+ return path .Join (strings .TrimSuffix (config .StateDir , statePlugin ), taskRuntimePlugin ), nil
167
+ }
168
+
143
169
func containerFromProto (containerpb * containersapi.Container ) * containers.Container {
144
170
var runtime containers.RuntimeInfo
145
171
// TODO: is nil check required for containerpb
0 commit comments