Skip to content

Commit 1f4c7e6

Browse files
committed
fix: add ipam config while setting up container's network
1 parent e408d9e commit 1f4c7e6

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

project/rkl/src/commands/compose/network.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use anyhow::Ok;
1414
use anyhow::Result;
1515
use anyhow::anyhow;
1616
use serde::{Deserialize, Serialize};
17+
use serde_json::Value;
18+
use serde_json::json;
1719

1820
pub const CNI_VERSION: &str = "1.0.0";
1921
pub const STD_CONF_PATH: &str = "/etc/cni/net.d";
@@ -49,10 +51,7 @@ pub struct CliNetworkConfig {
4951
pub mac_spoof_check: Option<bool>,
5052
/// IPAM type(like host-local, static, etc.)
5153
#[serde(default)]
52-
pub ipam_type: Option<String>,
53-
/// IPAM configuration's file path(Optional)
54-
#[serde(default)]
55-
pub ipam_config: Option<String>,
54+
pub ipam: Option<IpamConfig>,
5655
/// enable hairpin mod
5756
#[serde(default)]
5857
pub hairpin_mode: Option<bool>,
@@ -63,6 +62,26 @@ pub struct CliNetworkConfig {
6362
#[serde(default)]
6463
pub vlan_trunk: Option<Vec<u16>>,
6564
}
65+
#[derive(Clone, Debug, Deserialize, Serialize)]
66+
#[serde(rename_all = "camelCase")]
67+
pub struct IpamConfig {
68+
/// Name of the IPAM plugin binary on disk.
69+
///
70+
/// This is called `type` in the JSON.
71+
#[serde(rename = "type")]
72+
pub plugin: String,
73+
74+
/// All other IPAM fields.
75+
///
76+
/// This is a [`serde(flatten)`](https://serde.rs/field-attrs.html#flatten)
77+
/// field which aggregates any and all additional fields apart from the
78+
/// `plugin` field above.
79+
///
80+
/// The spec describes nothing in particular for this section, so it is
81+
/// entirely up to plugins to interpret it as required.
82+
#[serde(flatten)]
83+
pub specific: HashMap<String, Value>,
84+
}
6685

6786
impl CliNetworkConfig {
6887
pub fn from_name_bridge(network_name: &str, bridge: &str) -> Self {
@@ -76,6 +95,15 @@ impl CliNetworkConfig {
7695

7796
impl Default for CliNetworkConfig {
7897
fn default() -> Self {
98+
let specific: HashMap<String, serde_json::Value> = [
99+
("type", json!(BRIDGE_PLUGIN_NAME)),
100+
("subnet", json!("10.10.1.0/24")),
101+
("gateway", json!("10.10.1.0")),
102+
]
103+
.into_iter()
104+
.map(|(k, v)| (k.to_string(), v))
105+
.collect();
106+
79107
Self {
80108
cni_version: String::from(CNI_VERSION),
81109
plugin: String::from(BRIDGE_PLUGIN_NAME),
@@ -85,11 +113,13 @@ impl Default for CliNetworkConfig {
85113
is_gateway: Some(true),
86114
mtu: Some(1500),
87115
mac_spoof_check: Default::default(),
88-
ipam_type: Default::default(),
89-
ipam_config: Default::default(),
90116
hairpin_mode: Default::default(),
91117
vlan: Default::default(),
92118
vlan_trunk: Default::default(),
119+
ipam: Some(IpamConfig {
120+
plugin: BRIDGE_PLUGIN_NAME.to_string(),
121+
specific,
122+
}),
93123
}
94124
}
95125
}

project/rkl/src/commands/container/mod.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl ContainerRunner {
265265
.clone()
266266
.build();
267267

268-
debug!("After Building Config: {:?}", config);
268+
debug!("After building config: {:#?}", config);
269269
self.config = Some(config);
270270
Ok(())
271271
}
@@ -277,7 +277,7 @@ impl ContainerRunner {
277277
.as_ref()
278278
.ok_or_else(|| anyhow!("Container's Config is required"))?;
279279

280-
debug!("Get Config: {:#?}", config);
280+
debug!("Get container config while create oci_spec: {:#?}", config);
281281

282282
let mut spec = Spec::default();
283283

@@ -288,7 +288,6 @@ impl ContainerRunner {
288288
spec.set_root(Some(root));
289289

290290
// use the default namespace configuration
291-
292291
let namespaces = get_default_namespaces();
293292

294293
let mut linux: LinuxBuilder = LinuxBuilder::default().namespaces(namespaces);
@@ -326,13 +325,20 @@ impl ContainerRunner {
326325
// determine if it's in the single mode
327326

328327
// create oci spec
329-
let spec: Spec = self.create_oci_spec()?;
328+
let oci_spec: Spec = self.create_oci_spec()?;
329+
330+
debug!(
331+
"[container {}] created oci_spec {:?}",
332+
self.spec.name, oci_spec
333+
);
330334

331335
// create a config.path at the bundle path
332-
// TODO: Here use the local file path directly
333336
let bundle_path = self.spec.image.clone();
334337
if bundle_path.is_empty() {
335-
return Err(anyhow!("Bundle path (image) is empty"));
338+
return Err(anyhow!(
339+
"[container {}] Bundle path is empty",
340+
self.spec.name
341+
));
336342
}
337343
let bundle_dir = Path::new(&bundle_path);
338344
if !bundle_dir.exists() {
@@ -351,7 +357,7 @@ impl ContainerRunner {
351357
}
352358
let file = File::create(&config_path)?;
353359
let mut writer = BufWriter::new(file);
354-
serde_json::to_writer_pretty(&mut writer, &spec)?;
360+
serde_json::to_writer_pretty(&mut writer, &oci_spec)?;
355361
writer.flush()?;
356362

357363
let create_args = Create {

0 commit comments

Comments
 (0)