From f286cb9a34dc049e95ada0d90148e959e0eb58d4 Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Wed, 8 Jun 2016 19:39:38 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20panic=20if=20listContainers=20fails?= =?UTF-8?q?=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit … 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 --- provider/docker.go | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/provider/docker.go b/provider/docker.go index 30e651305..8f2dcfb87 100644 --- a/provider/docker.go +++ b/provider/docker.go @@ -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 }