inputs.docker: broken against daemons with API version below 1.40 since the moby client migration
Relevant telegraf.conf
[[inputs.docker]]
endpoint = "unix:///var/run/docker.sock"Logs from Telegraf
E! [telegraf] Error running agent: starting input inputs.docker: failed to get Docker info: Error response from daemon: client version 1.55 is too new. Maximum supported API version is 1.39The version in the message is the client library's MaxAPIVersion, so on 1.40.x it reads 1.56 instead.
System info
Telegraf 1.39.1 through 1.40.1 (also reproduces on master @ 7bd3f120c); any Docker Engine whose maximum API version is below 1.40, e.g. 18.09 (API 1.39)
Steps to reproduce
- Run a Docker daemon that advertises an API version below
client.MinAPIVersion(1.40), e.g. Docker Engine 18.09. - Configure
[[inputs.docker]]pointing at its socket. - Start Telegraf.
No old daemon is needed to reproduce — the existing mock server is enough. Setting server.APIVersion = "1.39" in any of the plugins/inputs/docker tests reproduces it, once the mock is made to send the Api-Version header the client actually reads (it currently only marshals PingResult into the ping body, which the client ignores):
server.go: unhandled url: "/v1.55/info"
failed to get Docker info: request returned 404 Not Found for API route and version .../v1.55/info, check if the server supports the requested API versionExpected behavior
The client downgrades to the API version the daemon advertises and metrics are collected, as it did up to 1.39.0. Back then the plugin passed client.WithAPIVersionNegotiation() and docker/docker's negotiation had no lower bound:
// if server version is lower than the client version, downgrade
if versions.LessThan(pingResponse.APIVersion, cli.version) {
cli.version = pingResponse.APIVersion
}Actual behavior
Every request goes out at client.MaxAPIVersion and the daemon rejects it. Start returns a non-retryable StartupError (the 400 is not IsErrConnectionFailed), so with the default startup_error_behavior the whole agent aborts — every other input stops too, not just inputs.docker.
Additional info
Introduced by #18879, which moved the plugin from github.com/docker/docker/client to github.com/moby/moby/client. Three things combine:
plugins/inputs/docker/docker.gobuilds the client with no version option and pings withclient.PingOptions{}, i.e. negotiation off, soStartnever sees the mismatch:
c, err := client.New(options...)
...
if _, err := d.client.Ping(ctxPing, client.PingOptions{}); err != nil {- The lazy negotiation on the first real request refuses to go below the library's floor (
client/client.go, v0.6.0):
const MaxAPIVersion = "1.56"
const MinAPIVersion = "1.40"
if versions.LessThan(pingVersion, MinAPIVersion) {
return cerrdefs.ErrInvalidArgument.WithMessage(...)
}getAPIPathdiscards that error, so the client silently keepsMaxAPIVersion:
_ = cli.checkVersion(ctx)A fix is possible without waiting on the library. /_ping is unversioned, so it reaches any daemon and returns the real version in the Api-Version header — the plugin already pings and throws that value away. And client.WithAPIVersion deliberately bypasses the floor ("WithAPIVersion does not validate if the client supports the given version"). So: read ping.APIVersion, and if it is below client.MinAPIVersion while the client sits above it, rebuild the client with client.WithAPIVersion(ping.APIVersion). Guarding on both conditions leaves an explicit DOCKER_API_VERSION untouched, since envAPIVersion wins over manualAPIVersion in client.New.
I have this working locally with tests, and can open a PR if the approach looks right.
The call sites are compatible with 1.39 once pinned: ContainerStats is called with IncludePreviousSample: true, which emits only stream=false and skips the one-shot parameter (API 1.41+), and ContainerList{All: true} sends all=1.
Two smaller things fall out of this:
plugins/common/docker/mock/server.gomarshals aPingResultinto the ping response body, but the client readsApi-Version/Ostypefrom the response headers. The mock therefore never exercises version negotiation, which is why this went unnoticed.- The
storage_objectsguard inStartcompares against a client version of 1.55/1.56 rather than the daemon's, so on an old daemon it passes and the request fails later instead of producing the intended "upgrade to docker engine 23.0+" error.
inputs.docker_log builds its client the same way in Init and never pings, so it has the same problem, surfacing at ContainerList during Gather.
Source: influxdata/telegraf