@@ -22,11 +22,6 @@ const (
2222 dockerTimeout = 5 * time .Second
2323)
2424
25- func toCtx () context.Context {
26- c , _ := context .WithTimeout (context .Background (), dockerTimeout )
27- return c
28- }
29-
3025// Core is a wrapper for docker client type things
3126type Core struct {
3227 dc * client.Client
@@ -100,7 +95,9 @@ func (c *Core) getNrFromCache(s string) *types.NetworkResource {
10095
10196// GetContainers gets a list of docker containers
10297func (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 {})
104101}
105102
106103// GetNetworkResourceByID gets a network resource by ID (checks cache first)
@@ -114,7 +111,9 @@ func (c *Core) GetNetworkResourceByID(id string) (*types.NetworkResource, error)
114111 }
115112
116113 //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 )
118117 if err != nil {
119118 log .WithError (err ).Error ("failed to inspect network" )
120119 return nil , err
@@ -138,7 +137,9 @@ func (c *Core) GetNetworkResourceByPool(pool string) (*types.NetworkResource, er
138137
139138 flts := filters .NewArgs ()
140139 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 })
142143 if err != nil {
143144 log .WithError (err ).Error ("failed to list networks" )
144145 return nil , err
0 commit comments