Skip to content

Commit 32362ac

Browse files
authored
Merge pull request #171 from mxpv/client
Improve containerd client ergonomics
2 parents 166b0f0 + a65ec7f commit 32362ac

File tree

2 files changed

+128
-6
lines changed

2 files changed

+128
-6
lines changed

crates/client/examples/version.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
limitations under the License.
1515
*/
1616

17-
use client::services::v1::version_client::VersionClient;
18-
use containerd_client as client;
17+
use containerd_client::Client;
1918

2019
/// Make sure you run containerd before running this example.
2120
#[tokio::main(flavor = "current_thread")]
2221
async fn main() {
23-
let channel = client::connect("/run/containerd/containerd.sock")
22+
let client = Client::from_path("/var/run/containerd/containerd.sock")
2423
.await
25-
.expect("Connect Failed");
24+
.expect("Connect failed");
2625

27-
let mut client = VersionClient::new(channel);
28-
let resp = client.version(()).await.expect("Failed to query version");
26+
let resp = client
27+
.version()
28+
.version(())
29+
.await
30+
.expect("Failed to query version");
2931

3032
println!("Response: {:?}", resp.get_ref());
3133
}

crates/client/src/lib.rs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,123 @@ macro_rules! with_namespace {
107107
req
108108
}};
109109
}
110+
111+
use services::v1::{
112+
containers_client::ContainersClient,
113+
content_client::ContentClient,
114+
diff_client::DiffClient,
115+
events_client::EventsClient,
116+
images_client::ImagesClient,
117+
introspection_client::IntrospectionClient,
118+
leases_client::LeasesClient,
119+
namespaces_client::NamespacesClient,
120+
sandbox::{controller_client::ControllerClient, store_client::StoreClient},
121+
snapshots::snapshots_client::SnapshotsClient,
122+
tasks_client::TasksClient,
123+
version_client::VersionClient,
124+
};
125+
use tonic::transport::{Channel, Error};
126+
127+
/// Client to containerd's APIs.
128+
pub struct Client {
129+
channel: Channel,
130+
}
131+
132+
impl From<Channel> for Client {
133+
fn from(value: Channel) -> Self {
134+
Self { channel: value }
135+
}
136+
}
137+
138+
impl Client {
139+
/// Create a new client from UDS socket.
140+
#[cfg(feature = "connect")]
141+
pub async fn from_path(path: impl AsRef<std::path::Path>) -> Result<Self, Error> {
142+
let channel = connect(path).await?;
143+
Ok(Self { channel })
144+
}
145+
146+
/// Access to the underlying Tonic channel.
147+
#[inline]
148+
pub fn channel(&self) -> Channel {
149+
self.channel.clone()
150+
}
151+
152+
/// Version service.
153+
#[inline]
154+
pub fn version(&self) -> VersionClient<Channel> {
155+
VersionClient::new(self.channel())
156+
}
157+
158+
/// Task service client.
159+
#[inline]
160+
pub fn tasks(&self) -> TasksClient<Channel> {
161+
TasksClient::new(self.channel())
162+
}
163+
164+
/// Sandbox store client.
165+
#[inline]
166+
pub fn sandbox_store(&self) -> StoreClient<Channel> {
167+
StoreClient::new(self.channel())
168+
}
169+
170+
/// Sandbox controller client.
171+
#[inline]
172+
pub fn sandbox_controller(&self) -> ControllerClient<Channel> {
173+
ControllerClient::new(self.channel())
174+
}
175+
176+
/// Snapshots service.
177+
#[inline]
178+
pub fn snapshots(&self) -> SnapshotsClient<Channel> {
179+
SnapshotsClient::new(self.channel())
180+
}
181+
182+
/// Namespaces service.
183+
#[inline]
184+
pub fn namespaces(&self) -> NamespacesClient<Channel> {
185+
NamespacesClient::new(self.channel())
186+
}
187+
188+
/// Leases service.
189+
#[inline]
190+
pub fn leases(&self) -> LeasesClient<Channel> {
191+
LeasesClient::new(self.channel())
192+
}
193+
194+
/// Intropection service.
195+
#[inline]
196+
pub fn introspection(&self) -> IntrospectionClient<Channel> {
197+
IntrospectionClient::new(self.channel())
198+
}
199+
200+
/// Image service.
201+
#[inline]
202+
pub fn images(&self) -> ImagesClient<Channel> {
203+
ImagesClient::new(self.channel())
204+
}
205+
206+
/// Event service.
207+
#[inline]
208+
pub fn events(&self) -> EventsClient<Channel> {
209+
EventsClient::new(self.channel())
210+
}
211+
212+
/// Diff service.
213+
#[inline]
214+
pub fn diff(&self) -> DiffClient<Channel> {
215+
DiffClient::new(self.channel())
216+
}
217+
218+
/// Content service.
219+
#[inline]
220+
pub fn content(&self) -> ContentClient<Channel> {
221+
ContentClient::new(self.channel())
222+
}
223+
224+
/// Container service.
225+
#[inline]
226+
pub fn containers(&self) -> ContainersClient<Channel> {
227+
ContainersClient::new(self.channel())
228+
}
229+
}

0 commit comments

Comments
 (0)