Skip to content

Commit 6a1ddee

Browse files
committed
ex: dockergc: fix use of contexts
1 parent 45c72bd commit 6a1ddee

File tree

2 files changed

+93
-19
lines changed

2 files changed

+93
-19
lines changed
+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package dockergc
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
dockerapi "github.com/docker/engine-api/client"
8+
dockertypes "github.com/docker/engine-api/types"
9+
)
10+
11+
type dockerClient struct {
12+
// timeout is the timeout of short running docker operations.
13+
timeout time.Duration
14+
// docker API client
15+
client *dockerapi.Client
16+
}
17+
18+
func newDockerClient(timeout time.Duration) (*dockerClient, error) {
19+
client, err := dockerapi.NewEnvClient()
20+
if err != nil {
21+
return nil, err
22+
}
23+
return &dockerClient{
24+
client: client,
25+
timeout: timeout,
26+
}, nil
27+
}
28+
29+
func clientErr(ctx context.Context, err error) error {
30+
if ctx.Err() != nil {
31+
return ctx.Err()
32+
}
33+
return err
34+
}
35+
36+
func (c *dockerClient) getTimeoutContext() (context.Context, context.CancelFunc) {
37+
return context.WithTimeout(context.Background(), c.timeout)
38+
}
39+
40+
func (c *dockerClient) Info() (*dockertypes.Info, error) {
41+
ctx, cancel := c.getTimeoutContext()
42+
defer cancel()
43+
info, err := c.client.Info(ctx)
44+
if err := clientErr(ctx, err); err != nil {
45+
return nil, err
46+
}
47+
return &info, nil
48+
}
49+
50+
func (c *dockerClient) ContainerList(options dockertypes.ContainerListOptions) ([]dockertypes.Container, error) {
51+
ctx, cancel := c.getTimeoutContext()
52+
defer cancel()
53+
containers, err := c.client.ContainerList(ctx, options)
54+
if err := clientErr(ctx, err); err != nil {
55+
return nil, err
56+
}
57+
return containers, nil
58+
}
59+
60+
func (c *dockerClient) ContainerRemove(id string, opts dockertypes.ContainerRemoveOptions) error {
61+
ctx, cancel := c.getTimeoutContext()
62+
defer cancel()
63+
err := c.client.ContainerRemove(ctx, id, opts)
64+
return clientErr(ctx, err)
65+
}
66+
67+
func (c *dockerClient) ImageList(opts dockertypes.ImageListOptions) ([]dockertypes.Image, error) {
68+
ctx, cancel := c.getTimeoutContext()
69+
defer cancel()
70+
images, err := c.client.ImageList(ctx, opts)
71+
if err := clientErr(ctx, err); err != nil {
72+
return nil, err
73+
}
74+
return images, nil
75+
}
76+
77+
func (c *dockerClient) ImageRemove(image string, opts dockertypes.ImageRemoveOptions) error {
78+
ctx, cancel := c.getTimeoutContext()
79+
defer cancel()
80+
_, err := c.client.ImageRemove(ctx, image, opts)
81+
return clientErr(ctx, err)
82+
}

pkg/oc/experimental/dockergc/dockergc.go

+11-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dockergc
22

33
import (
4-
"context"
54
"fmt"
65
"io"
76
"os"
@@ -17,7 +16,6 @@ import (
1716
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1817
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1918

20-
dockerapi "github.com/docker/engine-api/client"
2119
dockertypes "github.com/docker/engine-api/types"
2220
dockerfilters "github.com/docker/engine-api/types/filters"
2321
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
@@ -31,6 +29,8 @@ const (
3129

3230
var (
3331
DefaultMinimumGCAge = metav1.Duration{Duration: time.Hour}
32+
33+
dockerTimeout = time.Duration(2 * time.Minute)
3434
)
3535

3636
// DockerGCConfigCmdOptions are options supported by the dockergc admin command.
@@ -146,7 +146,7 @@ func parseDockerTimestamp(s string) (time.Time, error) {
146146
return time.Parse(time.RFC3339Nano, s)
147147
}
148148

149-
func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options *dockerGCConfigCmdOptions, rootDir string) error {
149+
func doGarbageCollection(client *dockerClient, options *dockerGCConfigCmdOptions, rootDir string) error {
150150
glog.Infof("gathering disk usage data")
151151
capacityBytes, usageBytes, err := getRootDirInfo(rootDir)
152152
if err != nil {
@@ -167,10 +167,7 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
167167
// conatiners
168168
exitedFilter := dockerfilters.NewArgs()
169169
exitedFilter.Add("status", "exited")
170-
containers, err := client.ContainerList(ctx, dockertypes.ContainerListOptions{All: true, Filter: exitedFilter})
171-
if ctx.Err() == context.DeadlineExceeded {
172-
return ctx.Err()
173-
}
170+
containers, err := client.ContainerList(dockertypes.ContainerListOptions{All: true, Filter: exitedFilter})
174171
if err != nil {
175172
return err
176173
}
@@ -189,7 +186,7 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
189186
glog.Infof("removing container %v (size: %v, age: %v)", c.ID, c.SizeRw, age)
190187
var err error
191188
if !options.DryRun {
192-
err = client.ContainerRemove(ctx, c.ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
189+
err = client.ContainerRemove(c.ID, dockertypes.ContainerRemoveOptions{RemoveVolumes: true})
193190
}
194191
if err != nil {
195192
glog.Infof("unable to remove container: %v", err)
@@ -199,10 +196,7 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
199196
}
200197

201198
// images
202-
images, err := client.ImageList(ctx, dockertypes.ImageListOptions{})
203-
if ctx.Err() == context.DeadlineExceeded {
204-
return ctx.Err()
205-
}
199+
images, err := client.ImageList(dockertypes.ImageListOptions{})
206200
if err != nil {
207201
return err
208202
}
@@ -230,7 +224,7 @@ func doGarbageCollection(ctx context.Context, client *dockerapi.Client, options
230224
glog.Infof("removing image %v (size: %v, age: %v)", i.ID, i.Size, age)
231225
var err error
232226
if !options.DryRun {
233-
_, err = client.ImageRemove(ctx, i.ID, dockertypes.ImageRemoveOptions{PruneChildren: true})
227+
err = client.ImageRemove(i.ID, dockertypes.ImageRemoveOptions{PruneChildren: true})
234228
}
235229
if err != nil {
236230
glog.Infof("unable to remove image: %v", err)
@@ -250,14 +244,12 @@ func Run(f *clientcmd.Factory, options *dockerGCConfigCmdOptions, cmd *cobra.Com
250244
glog.Infof("Running in dry-run mode")
251245
}
252246
glog.Infof("MinimumGCAge: %v, ImageGCHighThresholdPercent: %v, ImageGCLowThresholdPercent: %v", options.MinimumGCAge, options.ImageGCHighThresholdPercent, options.ImageGCLowThresholdPercent)
253-
client, err := dockerapi.NewEnvClient()
247+
client, err := newDockerClient(dockerTimeout)
254248
if err != nil {
255249
return err
256250
}
257-
timeout := time.Duration(2 * time.Minute)
258-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
259-
defer cancel()
260-
info, err := client.Info(ctx)
251+
252+
info, err := client.Info()
261253
if err != nil {
262254
return err
263255
}
@@ -270,7 +262,7 @@ func Run(f *clientcmd.Factory, options *dockerGCConfigCmdOptions, cmd *cobra.Com
270262
}
271263

272264
for {
273-
err := doGarbageCollection(ctx, client, options, rootDir)
265+
err := doGarbageCollection(client, options, rootDir)
274266
if err != nil {
275267
return err
276268
}

0 commit comments

Comments
 (0)