Skip to content

Commit 9d9cc05

Browse files
estespmxpv
authored andcommitted
Add example using the transfer service
Pulls an image to the containerd content store Signed-off-by: Phil Estes <[email protected]>
1 parent 822a065 commit 9d9cc05

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
use std::env::consts;
18+
19+
use client::{
20+
services::v1::{transfer_client::TransferClient, TransferOptions, TransferRequest},
21+
to_any,
22+
types::{
23+
transfer::{ImageStore, OciRegistry, UnpackConfiguration},
24+
Platform,
25+
},
26+
with_namespace,
27+
};
28+
use containerd_client as client;
29+
use tonic::Request;
30+
31+
const IMAGE: &str = "docker.io/library/alpine:latest";
32+
const NAMESPACE: &str = "default";
33+
34+
/// Make sure you run containerd before running this example.
35+
/// NOTE: to run this example, you must prepare a rootfs.
36+
#[tokio::main(flavor = "current_thread")]
37+
async fn main() {
38+
let arch = match consts::ARCH {
39+
"x86_64" => "amd64",
40+
"aarch64" => "arm64",
41+
_ => consts::ARCH,
42+
};
43+
44+
let channel = client::connect("/run/containerd/containerd.sock")
45+
.await
46+
.expect("Connect Failed");
47+
let mut client = TransferClient::new(channel.clone());
48+
49+
// Create the source (OCIRegistry)
50+
let source = OciRegistry {
51+
reference: IMAGE.to_string(),
52+
resolver: Default::default(),
53+
};
54+
55+
let platform = Platform {
56+
os: "linux".to_string(),
57+
architecture: arch.to_string(),
58+
variant: "".to_string(),
59+
os_version: "".to_string(),
60+
};
61+
62+
// Create the destination (ImageStore)
63+
let destination = ImageStore {
64+
name: IMAGE.to_string(),
65+
platforms: vec![platform.clone()],
66+
unpacks: vec![UnpackConfiguration {
67+
platform: Some(platform),
68+
..Default::default()
69+
}],
70+
..Default::default()
71+
};
72+
73+
let anys = to_any(&source);
74+
let anyd = to_any(&destination);
75+
76+
println!("Pulling image for linux/{} from source: {:?}", arch, source);
77+
78+
// Create the transfer request
79+
let request = TransferRequest {
80+
source: Some(anys),
81+
destination: Some(anyd),
82+
options: Some(TransferOptions {
83+
..Default::default()
84+
}),
85+
};
86+
// Execute the transfer (pull)
87+
client
88+
.transfer(with_namespace!(request, NAMESPACE))
89+
.await
90+
.expect("unable to transfer image");
91+
}

0 commit comments

Comments
 (0)