Skip to content

Commit c97f832

Browse files
committed
feat: add context for client interface
1 parent 6069e76 commit c97f832

File tree

6 files changed

+26
-19
lines changed

6 files changed

+26
-19
lines changed

runtime/client.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package runtime
22

3-
import "github.com/chaitin/veinmind-common-go/pkg/auth"
3+
import (
4+
"context"
5+
"github.com/chaitin/veinmind-common-go/pkg/auth"
6+
)
47

58
type Client interface {
6-
Pull(repo string) (string, error)
7-
Remove(id string) error
8-
Auth(config auth.AuthConfig) error
9+
Pull(ctx context.Context, repo string) (string, error)
10+
Remove(ctx context.Context, id string) error
11+
Auth(ctx context.Context, config auth.AuthConfig) error
912
}

runtime/containerd.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const (
1616
)
1717

1818
type ContainerdClient struct {
19+
ctx context.Context
1920
client *containerd.Client
2021
}
2122

@@ -31,16 +32,16 @@ func NewContainerdClient() (Client, error) {
3132
return c, nil
3233
}
3334

34-
func (c *ContainerdClient) Auth(config auth.AuthConfig) error {
35+
func (c *ContainerdClient) Auth(ctx context.Context, config auth.AuthConfig) error {
3536
return nil
3637
}
3738

38-
func (c *ContainerdClient) Pull(repo string) (string, error) {
39+
func (c *ContainerdClient) Pull(ctx context.Context, repo string) (string, error) {
3940
if named, err := reference.ParseDockerRef(repo); err == nil {
4041
repo = named.String()
4142
}
4243

43-
image, err := c.client.Pull(context.Background(), repo, containerd.WithPullUnpack)
44+
image, err := c.client.Pull(ctx, repo, containerd.WithPullUnpack)
4445
if err != nil {
4546
return "", err
4647
}
@@ -49,15 +50,15 @@ func (c *ContainerdClient) Pull(repo string) (string, error) {
4950
return imageID, nil
5051
}
5152

52-
func (c *ContainerdClient) Remove(repo string) error {
53+
func (c *ContainerdClient) Remove(ctx context.Context, repo string) error {
5354
if named, err := reference.ParseDockerRef(repo); err == nil {
5455
repo = named.String()
5556
}
5657

5758
var (
58-
ctx = namespaces.WithNamespace(context.Background(), ns)
5959
imageStore = c.client.ImageService()
6060
)
61+
ctx = namespaces.WithNamespace(ctx, ns)
6162

6263
var opts []images.DeleteOpt
6364
opts = append(opts, images.SynchronousDelete())

runtime/containerd_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package runtime
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
67
)
@@ -11,7 +12,7 @@ func TestContainerdClient_Pull(t *testing.T) {
1112
t.Error(err)
1213
}
1314

14-
repo, err := c.Pull("ubuntu:latest")
15+
repo, err := c.Pull(context.Background(), "ubuntu:latest")
1516
if err != nil {
1617
t.Error(err)
1718
}

runtime/docker.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,15 @@ func NewDockerClient(opts ...Option) (Client, error) {
136136
return c, nil
137137
}
138138

139-
func (client *DockerClient) Auth(config commonAuth.AuthConfig) error {
139+
func (client *DockerClient) Auth(ctx context.Context, config commonAuth.AuthConfig) error {
140140
for _, auth := range config.Auths {
141141
client.auth[auth.Registry] = auth
142142
}
143143

144144
return nil
145145
}
146146

147-
func (client *DockerClient) Pull(repo string) (string, error) {
147+
func (client *DockerClient) Pull(ctx context.Context, repo string) (string, error) {
148148
c, err := dockercli.NewClientWithOpts(dockercli.FromEnv, dockercli.WithAPIVersionNegotiation(), dockercli.WithHost(client.host))
149149
if err != nil {
150150
return "", err
@@ -168,12 +168,12 @@ func (client *DockerClient) Pull(repo string) (string, error) {
168168

169169
var closer io.ReadCloser
170170
if token == "" {
171-
closer, err = c.ImagePull(client.ctx, repo, dockertypes.ImagePullOptions{})
171+
closer, err = c.ImagePull(ctx, repo, dockertypes.ImagePullOptions{})
172172
if err != nil {
173173
return "", err
174174
}
175175
} else {
176-
closer, err = c.ImagePull(client.ctx, repo, dockertypes.ImagePullOptions{
176+
closer, err = c.ImagePull(ctx, repo, dockertypes.ImagePullOptions{
177177
RegistryAuth: token,
178178
})
179179
if err != nil {
@@ -189,13 +189,13 @@ func (client *DockerClient) Pull(repo string) (string, error) {
189189
return named.String(), nil
190190
}
191191

192-
func (client *DockerClient) Remove(id string) error {
192+
func (client *DockerClient) Remove(ctx context.Context, id string) error {
193193
c, err := dockercli.NewClientWithOpts(dockercli.FromEnv, dockercli.WithAPIVersionNegotiation(), dockercli.WithHost(client.host))
194194
if err != nil {
195195
return err
196196
}
197197

198-
_, err = c.ImageRemove(client.ctx, id, dockertypes.ImageRemoveOptions{
198+
_, err = c.ImageRemove(ctx, id, dockertypes.ImageRemoveOptions{
199199
Force: true,
200200
PruneChildren: true,
201201
})

runtime/docker_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package runtime
22

33
import (
4+
"context"
45
"fmt"
56
"testing"
67
)
@@ -11,7 +12,7 @@ func TestDockerClient_Pull(t *testing.T) {
1112
t.Error(err)
1213
}
1314

14-
repo, err := d.Pull("ubuntu:latest")
15+
repo, err := d.Pull(context.Background(), "ubuntu:latest")
1516
if err != nil {
1617
t.Error(err)
1718
}

runtime/option.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package runtime
22

33
import (
4+
"context"
45
"github.com/chaitin/veinmind-common-go/pkg/auth"
56
"github.com/pkg/errors"
67
)
@@ -19,7 +20,7 @@ func WithAuthFromPath(path string) Option {
1920
return nil, err
2021
}
2122

22-
err = c.Auth(*authConfig)
23+
err = c.Auth(context.Background(), *authConfig)
2324
if err != nil {
2425
return nil, err
2526
}
@@ -31,7 +32,7 @@ func WithAuthFromPath(path string) Option {
3132
// WithAuth init client authInfo with an entity directly
3233
func WithAuth(authConfig auth.AuthConfig) Option {
3334
return func(c Client) (Client, error) {
34-
err := c.Auth(authConfig)
35+
err := c.Auth(context.Background(), authConfig)
3536
if err != nil {
3637
return nil, err
3738
}

0 commit comments

Comments
 (0)