From 723d8f0104ce565108f72debe88a51f9237b5bf0 Mon Sep 17 00:00:00 2001 From: Greg <2653109+glinton@users.noreply.github.com> Date: Thu, 3 Jan 2019 16:57:39 -0700 Subject: [PATCH] Fix panic in docker input with bad endpoint (#5226) --- plugins/inputs/docker/docker.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/plugins/inputs/docker/docker.go b/plugins/inputs/docker/docker.go index f5633f099..6d9d56372 100644 --- a/plugins/inputs/docker/docker.go +++ b/plugins/inputs/docker/docker.go @@ -129,18 +129,7 @@ func (d *Docker) SampleConfig() string { return sampleConfig } func (d *Docker) Gather(acc telegraf.Accumulator) error { if d.client == nil { - var c Client - var err error - if d.Endpoint == "ENV" { - c, err = d.newEnvClient() - } else { - tlsConfig, err := d.ClientConfig.TLSConfig() - if err != nil { - return err - } - - c, err = d.newClient(d.Endpoint, tlsConfig) - } + c, err := d.getNewClient() if err != nil { return err } @@ -219,7 +208,6 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error { } func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { - ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration) defer cancel() services, err := d.client.ServiceList(ctx, types.ServiceListOptions{}) @@ -228,7 +216,6 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { } if len(services) > 0 { - tasks, err := d.client.TaskList(ctx, types.TaskListOptions{}) if err != nil { return err @@ -834,6 +821,19 @@ func (d *Docker) createContainerStateFilters() error { return nil } +func (d *Docker) getNewClient() (Client, error) { + if d.Endpoint == "ENV" { + return d.newEnvClient() + } + + tlsConfig, err := d.ClientConfig.TLSConfig() + if err != nil { + return nil, err + } + + return d.newClient(d.Endpoint, tlsConfig) +} + func init() { inputs.Add("docker", func() telegraf.Input { return &Docker{