Merge pull request #443 from vdemeester/442-and-share-context

Fix panic if listContainers fails…
This commit is contained in:
Emile Vauge 2016-06-09 09:37:48 +02:00
commit d1ffbd8a03

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
}