Skip to content

Commit 79fafba

Browse files
feat: add LoadTaskProcess api in containerd client
This commit aims to add an api to get the task process of the containerd client. We have a use case where we are trying to consume the exited containers and get their exit code however the current implementation of the cliet dosn't expose this api. The broader issue and usecase is captured here #3722.
1 parent 5adb1c3 commit 79fafba

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

container/containerd/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type client struct {
4545
type ContainerdClient interface {
4646
LoadContainer(ctx context.Context, id string) (*containers.Container, error)
4747
TaskPid(ctx context.Context, id string) (uint32, error)
48+
LoadTaskProcess(ctx context.Context, id string) (*tasktypes.Process, error)
4849
Version(ctx context.Context) (string, error)
4950
}
5051

@@ -132,6 +133,17 @@ func (c *client) TaskPid(ctx context.Context, id string) (uint32, error) {
132133
return response.Process.Pid, nil
133134
}
134135

136+
func (c *client) LoadTaskProcess(ctx context.Context, id string) (*tasktypes.Process, error) {
137+
response, err := c.taskService.Get(ctx, &tasksapi.GetRequest{
138+
ContainerID: id,
139+
})
140+
if err != nil {
141+
return nil, errgrpc.ToNative(err)
142+
}
143+
144+
return response.Process, nil
145+
}
146+
135147
func (c *client) Version(ctx context.Context) (string, error) {
136148
response, err := c.versionService.Version(ctx, &emptypb.Empty{})
137149
if err != nil {

container/containerd/client_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import (
1818
"context"
1919
"fmt"
2020

21+
"github.com/containerd/containerd/api/types/task"
2122
"github.com/google/cadvisor/container/containerd/containers"
2223
)
2324

2425
type containerdClientMock struct {
2526
cntrs map[string]*containers.Container
2627
returnErr error
28+
tasks map[string]*task.Process
2729
}
2830

2931
func (c *containerdClientMock) LoadContainer(ctx context.Context, id string) (*containers.Container, error) {
@@ -45,9 +47,27 @@ func (c *containerdClientMock) TaskPid(ctx context.Context, id string) (uint32,
4547
return 2389, nil
4648
}
4749

50+
func (c *containerdClientMock) LoadTaskProcess(ctx context.Context, id string) (*task.Process, error) {
51+
if c.returnErr != nil {
52+
return nil, c.returnErr
53+
}
54+
task, ok := c.tasks[id]
55+
if !ok {
56+
return nil, fmt.Errorf("unable to find task for container %q", id)
57+
}
58+
return task, nil
59+
}
60+
4861
func mockcontainerdClient(cntrs map[string]*containers.Container, returnErr error) ContainerdClient {
62+
tasks := make(map[string]*task.Process)
63+
64+
for _, cntr := range cntrs {
65+
tasks[cntr.ID] = &task.Process{}
66+
}
67+
4968
return &containerdClientMock{
5069
cntrs: cntrs,
5170
returnErr: returnErr,
71+
tasks: tasks,
5272
}
5373
}

0 commit comments

Comments
 (0)