-
Notifications
You must be signed in to change notification settings - Fork 127
Introduce netavark create command #1394
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ashley-cui
wants to merge
3
commits into
containers:main
Choose a base branch
from
ashley-cui:create
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,052
−233
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| # Netavark Create Input | ||
|
|
||
| ## Overview | ||
|
|
||
| The `netavark create` command accepts a JSON configuration with three main sections: | ||
| - `network`: Network configuration details | ||
| - `used`: Information about already used resources | ||
| - `options`: Creation options and settings | ||
|
|
||
| ## Network Fields | ||
|
|
||
| The `network` object contains the network configuration. | ||
|
|
||
| | Field | Description | Required | | ||
| |-------|-------------|----------| | ||
| | `name` | Name of the network. Must match the pattern `[a-zA-Z0-9][a-zA-Z0-9_.-]*` and cannot be empty. | Yes | | ||
| | `id` | Network ID. Must be 64-bit hexadecimal | Yes | | ||
| | `driver` | Network driver type (e.g., "bridge", "macvlan", "ipvlan") | Yes | | ||
| | `dns_enabled` | Boolean indicating whether DNS should be enabled for this network | Yes | | ||
| | `internal` | Boolean indicating whether the network should be internal | Yes | | ||
| | `ipv6_enabled` | Boolean indicating if IPv6 is enabled | Yes | | ||
| | `network_interface` | Name of the network interface on the host| No | | ||
| | `options` | Key-value map of driver-specific network options (e.g., "mtu", "vlan", "isolate", "metric", "vrf", "mode"). | No | | ||
| | `ipam_options` | Key-value map of IPAM (IP Address Management) options. Supported drivers: "host-local", "dhcp", "none". | No | | ||
| | `subnets` | Array of subnet configurations. Each subnet can include a subnet CIDR, optional gateway, and optional lease range. | No | | ||
| | `routes` | Array of static routes for the network. Each route includes destination (CIDR), gateway, and optional metric. | No | | ||
| | `network_dns_servers` | Array of IP addresses for DNS servers used by aardvark-dns. | No | | ||
| | `labels` | Key-value map of labels associated with the network. | No | | ||
|
|
||
| ## Used Fields | ||
|
|
||
| The `used` object contains information about resources that are already in use and should be avoided. | ||
|
|
||
| | Field | Description | Required | | ||
| |-------|-------------|----------| | ||
| | `interfaces` | Array of network interface names that are already in use on the host. | Yes | | ||
| | `names` | Map of network names to their IDs for networks that already exist. Used to prevent duplicate network names. | Yes | | ||
| | `subnets` | Array of subnet CIDR ranges that are already in use on the host or by other network configurations. | Yes | | ||
|
|
||
| ## Options Fields | ||
|
|
||
| The `options` object contains creation options and settings. | ||
|
|
||
| | Field | Description | Required | | ||
| |-------|-------------|----------| | ||
| | `subnet_pools` | Array of subnet pools from which to allocate subnets. Each pool contains a `base` (CIDR) and `size` (subnet size in bits). | Yes | | ||
| | `default_interface_name` | Default prefix for auto-generated interface names (e.g., "podman" will generate "podman0", "podman1", etc.). | No | | ||
| | `check_used_subnets` | Boolean flag indicating whether to check if subnets conflict with already used subnets. | Yes | | ||
|
|
||
| ## Example | ||
|
|
||
| ```json | ||
| { | ||
| "network": { | ||
| "name": "example-network", | ||
| "id": "abc123def4567890123456789012345678901234567890123456789012345678", | ||
| "driver": "bridge", | ||
| "dns_enabled": false, | ||
| "internal": false, | ||
| "ipv6_enabled": false, | ||
| "subnets": [ | ||
| { | ||
| "subnet": "10.100.0.0/24" | ||
| } | ||
| ], | ||
| "options": { | ||
| "mtu": "1500" | ||
| }, | ||
| "labels": { | ||
| "key1": "value1", | ||
| "key2": "value2" | ||
| } | ||
| }, | ||
| "used": { | ||
| "interfaces": [], | ||
| "names": {}, | ||
| "subnets": [] | ||
| }, | ||
| "options": { | ||
| "subnet_pools": [ | ||
| { | ||
| "base": "10.89.0.0/16", | ||
| "size": 24 | ||
| } | ||
| ], | ||
| "default_interface_name": "podman", | ||
| "check_used_subnets": false | ||
| } | ||
| } | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| use crate::error::NetavarkResult; | ||
| use crate::network; | ||
| use clap::Parser; | ||
| use std::ffi::OsString; | ||
|
|
||
| #[derive(Parser, Debug)] | ||
| pub struct Create {} | ||
|
|
||
| impl Create { | ||
| pub fn exec( | ||
| &self, | ||
| input_file: Option<OsString>, | ||
| plugin_directories: Option<Vec<OsString>>, | ||
| ) -> NetavarkResult<()> { | ||
| let network_options = network::types::NetworkCreateConfig::load(input_file)?; | ||
|
|
||
| let network = network::create_config::new_network(network_options, &plugin_directories)?; | ||
| let response_json = serde_json::to_string(&network)?; | ||
| println!("{response_json}"); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| use crate::network::types; | ||
| use chrono::Utc; | ||
| use std::ffi::OsString; | ||
|
|
||
| use crate::error::{NetavarkError, NetavarkResult}; | ||
|
|
||
| use super::driver::*; | ||
| use super::validation::*; | ||
|
|
||
| pub fn new_network( | ||
| create: types::NetworkCreateConfig, | ||
| plugin_directories: &Option<Vec<OsString>>, | ||
| ) -> NetavarkResult<types::Network> { | ||
| use crate::network::constants; | ||
| let mut network = create.network; | ||
| validate_name_id(&network.name, &network.id, &create.used.names)?; | ||
| if let Some(ref if_name) = network.network_interface { | ||
| validate_interface_name(if_name)?; | ||
| } | ||
| validate_ipam_driver(&network.ipam_options, &network.subnets)?; | ||
|
|
||
| let mut check_used = false; | ||
|
|
||
| match network.driver.as_str() { | ||
| constants::DRIVER_BRIDGE => { | ||
| let (check_used_val, check_bridge_conflict) = setup_bridge_options(&mut network)?; | ||
| check_used = check_used_val && create.create_opts.check_used_subnets; | ||
| create_bridge( | ||
| &mut network, | ||
| &create.used, | ||
| check_used, | ||
| check_bridge_conflict, | ||
| create.create_opts, | ||
| )?; | ||
| } | ||
| constants::DRIVER_IPVLAN | constants::DRIVER_MACVLAN => { | ||
| create_ipvlan_macvlan(&mut network)?; | ||
| } | ||
| _ => exec_plugin_driver(&mut network, plugin_directories)?, | ||
| } | ||
|
|
||
| if network | ||
| .ipam_options | ||
| .as_ref() | ||
| .and_then(|ipam_opts| ipam_opts.get("driver")) | ||
| .map(|driver| driver == constants::IPAM_NONE) | ||
| .unwrap_or(false) | ||
| { | ||
| log::debug!( | ||
| "dns disabled for network {:?} because ipam driver is set to none", | ||
| network.name | ||
| ); | ||
| network.dns_enabled = false; | ||
| } | ||
| if network.network_dns_servers.is_some() && !network.dns_enabled { | ||
| return Err(NetavarkError::msg( | ||
| "cannot set NetworkDNSServers if DNS is not enabled for the network", | ||
| )); | ||
| } | ||
|
|
||
| let add_gateway = !network.internal || network.dns_enabled; | ||
| validate_subnets(&mut network, add_gateway, check_used, &create.used.subnets)?; | ||
|
|
||
| if network.routes.is_some() { | ||
| validate_routes(&mut network)?; | ||
| } | ||
|
|
||
| network.created = Some(Utc::now()); | ||
|
|
||
| Ok(network) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.