@@ -14,6 +14,8 @@ use anyhow::Ok;
1414use anyhow:: Result ;
1515use anyhow:: anyhow;
1616use serde:: { Deserialize , Serialize } ;
17+ use serde_json:: Value ;
18+ use serde_json:: json;
1719
1820pub const CNI_VERSION : & str = "1.0.0" ;
1921pub 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
6786impl CliNetworkConfig {
6887 pub fn from_name_bridge ( network_name : & str , bridge : & str ) -> Self {
@@ -76,6 +95,15 @@ impl CliNetworkConfig {
7695
7796impl 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}
0 commit comments