@@ -22,11 +22,6 @@ const (
22
22
dockerTimeout = 5 * time .Second
23
23
)
24
24
25
- func toCtx () context.Context {
26
- c , _ := context .WithTimeout (context .Background (), dockerTimeout )
27
- return c
28
- }
29
-
30
25
// Core is a wrapper for docker client type things
31
26
type Core struct {
32
27
dc * client.Client
@@ -100,7 +95,9 @@ func (c *Core) getNrFromCache(s string) *types.NetworkResource {
100
95
101
96
// GetContainers gets a list of docker containers
102
97
func (c * Core ) GetContainers () ([]types.Container , error ) {
103
- return c .dc .ContainerList (toCtx (), types.ContainerListOptions {})
98
+ ctx , cancel := context .WithTimeout (context .Background (), dockerTimeout )
99
+ defer cancel ()
100
+ return c .dc .ContainerList (ctx , types.ContainerListOptions {})
104
101
}
105
102
106
103
// GetNetworkResourceByID gets a network resource by ID (checks cache first)
@@ -114,7 +111,9 @@ func (c *Core) GetNetworkResourceByID(id string) (*types.NetworkResource, error)
114
111
}
115
112
116
113
//netid wasn't in cache, fetch from docker inspect
117
- nnr , err := c .dc .NetworkInspect (toCtx (), id )
114
+ ctx , cancel := context .WithTimeout (context .Background (), dockerTimeout )
115
+ defer cancel ()
116
+ nnr , err := c .dc .NetworkInspect (ctx , id )
118
117
if err != nil {
119
118
log .WithError (err ).Error ("failed to inspect network" )
120
119
return nil , err
@@ -138,7 +137,9 @@ func (c *Core) GetNetworkResourceByPool(pool string) (*types.NetworkResource, er
138
137
139
138
flts := filters .NewArgs ()
140
139
flts .Add ("driver" , networkDriverName )
141
- nl , err := c .dc .NetworkList (toCtx (), types.NetworkListOptions {Filters : flts })
140
+ ctx , cancel := context .WithTimeout (context .Background (), dockerTimeout )
141
+ defer cancel ()
142
+ nl , err := c .dc .NetworkList (ctx , types.NetworkListOptions {Filters : flts })
142
143
if err != nil {
143
144
log .WithError (err ).Error ("failed to list networks" )
144
145
return nil , err
0 commit comments