Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specify API version when querying Docker API #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
### Environment

- Docker
- API 1.31.1
- API version 1.26
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thx 😭


- OS
- Linux (developped in Ubuntu(amd64))
Expand Down
7 changes: 5 additions & 2 deletions src/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ pub static DEFAULT_DOCKER_HOST: &'static str = "unix:///var/run/docker.sock";
#[cfg(windows)]
pub static DEFAULT_DOCKER_HOST: &'static str = "tcp://localhost:2375";

/// The default `DOCKER_API_VERSION` that we support.
pub const DEFAULT_DOCKER_API_VERSION: &str = "1.26";

/// The default directory in which to look for our Docker certificate
/// files.
pub fn default_cert_path() -> Result<PathBuf> {
Expand Down Expand Up @@ -191,10 +194,10 @@ impl Docker {
#[cfg(unix)]
pub fn connect_with_unix(addr: &str) -> Result<Docker> {
if addr.starts_with("unix://") {
let client = HyperClient::connect_with_unix(&addr[7..]);
let client = HyperClient::connect_with_unix(&addr[7..])?;
Ok(Docker::new(client, Protocol::Unix))
} else {
let client = HyperClient::connect_with_unix(addr);
let client = HyperClient::connect_with_unix(addr)?;
Ok(Docker::new(client, Protocol::Unix))
}
}
Expand Down
20 changes: 14 additions & 6 deletions src/hyper_client.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::env;
use std::fs::File;
use std::path::Path;
use std::result;
Expand All @@ -8,6 +9,7 @@ use hyper::rt::Stream;
use hyper::Uri;
pub use hyperx::header::{ContentType, Headers};

use docker::DEFAULT_DOCKER_API_VERSION;
use errors::*;
use http_client::HttpClient;

Expand Down Expand Up @@ -268,17 +270,23 @@ fn request_with_redirect<T: Into<hyper::Body> + Sync + Send + 'static + Clone>(
}

impl HyperClient {
fn new(client: Client, base: Uri) -> Self {
Self {
fn new(client: Client, base: Uri) -> result::Result<Self, Error> {
// Append Docker API version to base
let mut ver =
env::var("DOCKER_API_VERSION").unwrap_or(DEFAULT_DOCKER_API_VERSION.to_string());
ver.insert_str(0, "v");
let base = join_uri(&base, &ver)?;

Ok(Self {
client,
base,
tokio_runtime: std::sync::Mutex::new(tokio::runtime::Runtime::new().unwrap()),
}
})
}

/// path to unix socket
#[cfg(unix)]
pub fn connect_with_unix(path: &str) -> Self {
pub fn connect_with_unix(path: &str) -> result::Result<Self, Error> {
let url = hyperlocal::Uri::new(path, "").into();
let unix = hyperlocal::UnixConnector::new();
let client = hyper::Client::builder().build::<_, hyper::Body>(unix);
Expand Down Expand Up @@ -321,14 +329,14 @@ impl HyperClient {
http.enforce_http(false);
let https = hyper_tls::HttpsConnector::from((http, builder.build()?));
let client = hyper::Client::builder().build::<_, hyper::Body>(https);
Ok(Self::new(Client::HttpsClient(client), url))
Self::new(Client::HttpsClient(client), url)
}

pub fn connect_with_http(addr: &str) -> result::Result<Self, Error> {
// This ensures that using docker-machine-esque addresses work with Hyper.
let addr_https = addr.clone().replace("tcp://", "http://");
let url = Uri::from_str(&addr_https).context(ErrorKind::InvalidUri { var: addr_https })?;
Ok(Self::new(Client::HttpClient(hyper::Client::new()), url))
Self::new(Client::HttpClient(hyper::Client::new()), url)
}
}

Expand Down