Skip to content

Commit 88a7bcc

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 88a7bcc

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

container/containerd/client.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
containersapi "github.com/containerd/containerd/api/services/containers/v1"
2626
tasksapi "github.com/containerd/containerd/api/services/tasks/v1"
2727
versionapi "github.com/containerd/containerd/api/services/version/v1"
28+
"github.com/containerd/containerd/api/types/task"
2829
tasktypes "github.com/containerd/containerd/api/types/task"
2930
"github.com/containerd/errdefs/pkg/errgrpc"
3031
"google.golang.org/grpc"
@@ -45,6 +46,7 @@ type client struct {
4546
type ContainerdClient interface {
4647
LoadContainer(ctx context.Context, id string) (*containers.Container, error)
4748
TaskPid(ctx context.Context, id string) (uint32, error)
49+
LoadTaskProcess(ctx context.Context, id string) (*task.Process, error)
4850
Version(ctx context.Context) (string, error)
4951
}
5052

@@ -132,6 +134,17 @@ func (c *client) TaskPid(ctx context.Context, id string) (uint32, error) {
132134
return response.Process.Pid, nil
133135
}
134136

137+
func (c *client) LoadTaskProcess(ctx context.Context, id string) (*task.Process, error) {
138+
response, err := c.taskService.Get(ctx, &tasksapi.GetRequest{
139+
ContainerID: id,
140+
})
141+
if err != nil {
142+
return nil, errgrpc.ToNative(err)
143+
}
144+
145+
return response.Process, nil
146+
}
147+
135148
func (c *client) Version(ctx context.Context) (string, error) {
136149
response, err := c.versionService.Version(ctx, &emptypb.Empty{})
137150
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)