forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Negotiate Docker API version (elastic#7165)
New Docker versions have deprecated our hardcoded client API version (1.22), this change uses an API version negotiation mechanism to ensure compatibility. We should still upgrade to a newer client in the future, let's keep an eye on moby/moby#33989 and do it once we have something we can vendor.
- Loading branch information
Showing
4 changed files
with
49 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package docker | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/elastic/beats/libbeat/logp" | ||
|
||
"github.com/docker/docker/api" | ||
"github.com/docker/docker/api/types/versions" | ||
"github.com/docker/docker/client" | ||
"golang.org/x/net/context" | ||
) | ||
|
||
// Select Docker API version | ||
const dockerAPIVersion = api.DefaultVersion | ||
|
||
// NewClient builds and returns a new Docker client | ||
// It uses version 1.26 by default, and negotiates it with the server so it is downgraded if 1.26 is too high | ||
func NewClient(host string, httpClient *http.Client, httpHeaders map[string]string) (*client.Client, error) { | ||
c, err := client.NewClient(host, dockerAPIVersion, httpClient, nil) | ||
if err != nil { | ||
return c, err | ||
} | ||
|
||
logp.Debug("docker", "Negotiating client version") | ||
ping, err := c.Ping(context.Background()) | ||
if err != nil { | ||
logp.Debug("docker", "Failed to perform ping: %s", err) | ||
} | ||
|
||
// try the latest version before versioning headers existed | ||
if ping.APIVersion == "" { | ||
ping.APIVersion = "1.24" | ||
} | ||
|
||
// if server version is lower than the client version, downgrade | ||
if versions.LessThan(ping.APIVersion, dockerAPIVersion) { | ||
c.UpdateClientVersion(ping.APIVersion) | ||
} | ||
|
||
logp.Debug("docker", "Client version set to %s", c.ClientVersion()) | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters