Fix panic if listContainers fails…

… and also share context accross API call, as this is how it's meant to
be used (and it allows to skip some calls if `cancel` is called).

Signed-off-by: Vincent Demeester <vincent@sbr.pm>
This commit is contained in:
Vincent Demeester 2016-06-08 19:39:38 +02:00
parent 5c63855cc0
commit f286cb9a34
No known key found for this signature in database
GPG key ID: 083CC6FD6EB699A3

View file

@ -91,9 +91,11 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po
log.Errorf("Failed to create a client for docker, error: %s", err)
return err
}
version, err := dockerClient.ServerVersion(context.Background())
ctx := context.Background()
version, err := dockerClient.ServerVersion(ctx)
log.Debugf("Docker connection established with docker %s (API %s)", version.Version, version.APIVersion)
containers, err := listContainers(dockerClient)
containers, err := listContainers(ctx, dockerClient)
if err != nil {
log.Errorf("Failed to list containers for docker, error %s", err)
return err
@ -104,7 +106,7 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po
Configuration: configuration,
}
if provider.Watch {
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx)
f := filters.NewArgs()
f.Add("type", "container")
options := dockertypes.EventsOptions{
@ -113,11 +115,12 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po
eventHandler := events.NewHandler(events.ByAction)
startStopHandle := func(m eventtypes.Message) {
log.Debugf("Docker event received %+v", m)
containers, err := listContainers(dockerClient)
containers, err := listContainers(ctx, dockerClient)
if err != nil {
log.Errorf("Failed to list containers for docker, error %s", err)
// Call cancel to get out of the monitor
cancel()
return
}
configuration := provider.loadDockerConfig(containers)
if configuration != nil {
@ -340,8 +343,8 @@ func getLabels(container dockertypes.ContainerJSON, labels []string) (map[string
return foundLabels, globalErr
}
func listContainers(dockerClient client.APIClient) ([]dockertypes.ContainerJSON, error) {
containerList, err := dockerClient.ContainerList(context.Background(), dockertypes.ContainerListOptions{})
func listContainers(ctx context.Context, dockerClient client.APIClient) ([]dockertypes.ContainerJSON, error) {
containerList, err := dockerClient.ContainerList(ctx, dockertypes.ContainerListOptions{})
if err != nil {
return []dockertypes.ContainerJSON{}, err
}
@ -349,11 +352,12 @@ func listContainers(dockerClient client.APIClient) ([]dockertypes.ContainerJSON,
// get inspect containers
for _, container := range containerList {
containerInspected, err := dockerClient.ContainerInspect(context.Background(), container.ID)
containerInspected, err := dockerClient.ContainerInspect(ctx, container.ID)
if err != nil {
log.Warnf("Failed to inpsect container %s, error: %s", container.ID, err)
} else {
containersInspected = append(containersInspected, containerInspected)
}
containersInspected = append(containersInspected, containerInspected)
}
return containersInspected, nil
}