Merge pull request #443 from vdemeester/442-and-share-context
Fix panic if listContainers fails…
This commit is contained in:
commit
d1ffbd8a03
1 changed files with 12 additions and 8 deletions
|
@ -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)
|
log.Errorf("Failed to create a client for docker, error: %s", err)
|
||||||
return 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)
|
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 {
|
if err != nil {
|
||||||
log.Errorf("Failed to list containers for docker, error %s", err)
|
log.Errorf("Failed to list containers for docker, error %s", err)
|
||||||
return err
|
return err
|
||||||
|
@ -104,7 +106,7 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po
|
||||||
Configuration: configuration,
|
Configuration: configuration,
|
||||||
}
|
}
|
||||||
if provider.Watch {
|
if provider.Watch {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(ctx)
|
||||||
f := filters.NewArgs()
|
f := filters.NewArgs()
|
||||||
f.Add("type", "container")
|
f.Add("type", "container")
|
||||||
options := dockertypes.EventsOptions{
|
options := dockertypes.EventsOptions{
|
||||||
|
@ -113,11 +115,12 @@ func (provider *Docker) Provide(configurationChan chan<- types.ConfigMessage, po
|
||||||
eventHandler := events.NewHandler(events.ByAction)
|
eventHandler := events.NewHandler(events.ByAction)
|
||||||
startStopHandle := func(m eventtypes.Message) {
|
startStopHandle := func(m eventtypes.Message) {
|
||||||
log.Debugf("Docker event received %+v", m)
|
log.Debugf("Docker event received %+v", m)
|
||||||
containers, err := listContainers(dockerClient)
|
containers, err := listContainers(ctx, dockerClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Failed to list containers for docker, error %s", err)
|
log.Errorf("Failed to list containers for docker, error %s", err)
|
||||||
// Call cancel to get out of the monitor
|
// Call cancel to get out of the monitor
|
||||||
cancel()
|
cancel()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
configuration := provider.loadDockerConfig(containers)
|
configuration := provider.loadDockerConfig(containers)
|
||||||
if configuration != nil {
|
if configuration != nil {
|
||||||
|
@ -340,8 +343,8 @@ func getLabels(container dockertypes.ContainerJSON, labels []string) (map[string
|
||||||
return foundLabels, globalErr
|
return foundLabels, globalErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func listContainers(dockerClient client.APIClient) ([]dockertypes.ContainerJSON, error) {
|
func listContainers(ctx context.Context, dockerClient client.APIClient) ([]dockertypes.ContainerJSON, error) {
|
||||||
containerList, err := dockerClient.ContainerList(context.Background(), dockertypes.ContainerListOptions{})
|
containerList, err := dockerClient.ContainerList(ctx, dockertypes.ContainerListOptions{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []dockertypes.ContainerJSON{}, err
|
return []dockertypes.ContainerJSON{}, err
|
||||||
}
|
}
|
||||||
|
@ -349,12 +352,13 @@ func listContainers(dockerClient client.APIClient) ([]dockertypes.ContainerJSON,
|
||||||
|
|
||||||
// get inspect containers
|
// get inspect containers
|
||||||
for _, container := range containerList {
|
for _, container := range containerList {
|
||||||
containerInspected, err := dockerClient.ContainerInspect(context.Background(), container.ID)
|
containerInspected, err := dockerClient.ContainerInspect(ctx, container.ID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warnf("Failed to inpsect container %s, error: %s", container.ID, err)
|
log.Warnf("Failed to inpsect container %s, error: %s", container.ID, err)
|
||||||
}
|
} else {
|
||||||
containersInspected = append(containersInspected, containerInspected)
|
containersInspected = append(containersInspected, containerInspected)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return containersInspected, nil
|
return containersInspected, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue