Fix panic in docker input with bad endpoint (#5226)

This commit is contained in:
Greg 2019-01-03 16:57:39 -07:00 committed by Daniel Nelson
parent 334f9267b6
commit 723d8f0104
1 changed files with 14 additions and 14 deletions

View File

@ -129,18 +129,7 @@ func (d *Docker) SampleConfig() string { return sampleConfig }
func (d *Docker) Gather(acc telegraf.Accumulator) error { func (d *Docker) Gather(acc telegraf.Accumulator) error {
if d.client == nil { if d.client == nil {
var c Client c, err := d.getNewClient()
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)
}
if err != nil { if err != nil {
return err return err
} }
@ -219,7 +208,6 @@ func (d *Docker) Gather(acc telegraf.Accumulator) error {
} }
func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error { func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error {
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration) ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel() defer cancel()
services, err := d.client.ServiceList(ctx, types.ServiceListOptions{}) services, err := d.client.ServiceList(ctx, types.ServiceListOptions{})
@ -228,7 +216,6 @@ func (d *Docker) gatherSwarmInfo(acc telegraf.Accumulator) error {
} }
if len(services) > 0 { if len(services) > 0 {
tasks, err := d.client.TaskList(ctx, types.TaskListOptions{}) tasks, err := d.client.TaskList(ctx, types.TaskListOptions{})
if err != nil { if err != nil {
return err return err
@ -834,6 +821,19 @@ func (d *Docker) createContainerStateFilters() error {
return nil 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() { func init() {
inputs.Add("docker", func() telegraf.Input { inputs.Add("docker", func() telegraf.Input {
return &Docker{ return &Docker{