cni_plugin/
inputs.rs

1use std::path::PathBuf;
2
3use crate::{config::NetworkConfig, Cni, Command};
4
5/// An alternate representation of plugin inputs.
6///
7/// This can be obtained from [`Cni`] with the [`Cni::into_inputs()`] method.
8#[derive(Clone, Debug)]
9pub struct Inputs {
10	/// The command given to the plugin.
11	pub command: Command,
12
13	/// The container ID, as provided by the runtime.
14	///
15	/// The spec says:
16	/// > A unique plaintext identifier for a container, allocated by the
17	/// > runtime. Not empty.
18	///
19	/// In practice, this may not be the ID of an actual container, but
20	/// rather the ID of the logical container grouping this network applies
21	/// to. E.g. a Pod, Alloc, etc.
22	pub container_id: String,
23
24	/// The name of the interface to create, delete, check, or manage inside the container.
25	pub ifname: String,
26
27	/// The container’s “isolation domain.”
28	///
29	/// If using network namespaces, then a path to the network namespace.
30	///
31	/// Optional for DEL.
32	pub netns: Option<PathBuf>,
33
34	/// List of paths to search for CNI plugin executables.
35	///
36	/// This is in the same format as the host system’s `PATH` variable: e.g.
37	/// separated by `:` on unix, and by `;` on Windows.
38	pub path: Vec<PathBuf>,
39
40	/// The input network configuration.
41	pub config: NetworkConfig,
42}
43
44impl Cni {
45	/// Converts this enum into an alternate representation which holds the Command separately from the inputs.
46	///
47	/// This is useful to deduplicate prep work between command implementations.
48	pub fn into_inputs(self) -> Option<Inputs> {
49		let command = match &self {
50			Cni::Add { .. } => Command::Add,
51			Cni::Del { .. } => Command::Del,
52			Cni::Check { .. } => Command::Check,
53			Cni::Version(_) => return None,
54		};
55
56		match self {
57			Cni::Add {
58				container_id,
59				ifname,
60				netns,
61				path,
62				config,
63			}
64			| Cni::Check {
65				container_id,
66				ifname,
67				netns,
68				path,
69				config,
70			} => Some(Inputs {
71				command,
72				container_id,
73				ifname,
74				netns: Some(netns),
75				path,
76				config,
77			}),
78			Cni::Del {
79				container_id,
80				ifname,
81				netns,
82				path,
83				config,
84			} => Some(Inputs {
85				command,
86				container_id,
87				ifname,
88				netns,
89				path,
90				config,
91			}),
92			Cni::Version(_) => unreachable!(),
93		}
94	}
95}