3 releases

0.1.2 Mar 6, 2025
0.1.1 Mar 5, 2025
0.1.0 Jan 2, 2025

#1 in #cni

Download history 140/week @ 2025-01-01 5/week @ 2025-01-08 3/week @ 2025-02-05 2/week @ 2025-02-12 1/week @ 2025-02-19 3/week @ 2025-02-26 282/week @ 2025-03-05

288 downloads per month

Apache-2.0

68KB
1.5K SLoC

rust-cni

CI Crates.io

This is the CNI plugin impl by rust for container-runtime create CNI network.

requirements

  • Install cni plugin in /opt/cni/bin
  • Prepare cni config in /etc/cni/net.d

Run test

run it as root.

cargo test --test it_test --  --test-threads=1 --nocapture

example

use std::{fs::File, io};
use rust_cni::cni::Libcni;
use netns_rs::NetNs;
use nix::sched::setns;

fn create_ns() -> Result<NetNs, String> {
    let ns = NetNs::new("ns_name").unwrap();
    println!("{:?}", ns.path());
    Ok(ns)
}

fn main() {
    let ns = create_ns().unwrap();
    // Default cni config is in /etc/cni/net.d/
    // Default cni bin is in /opt/cni/bin/  
    let mut cni = Libcni::default();
    cni.load_default_conf();
    cni.add_lo_network().unwrap();

    let id = "test".to_string();
    let path = ns.path().to_string_lossy().to_string();
    cni.setup(id.clone(), path.clone()).unwrap();

    println!("try to remove --------------------");
    cni.remove(id.clone(), path.clone()).unwrap();
    ns.remove().unwrap();
}

License

This project is licensed under the Apache License 2.0. See the LICENSE file for details.

references

Contributing

Contributions are welcome! Please open an issue or submit a pull request if you have any improvements or bug fixes.

For more detailed information, please refer to the source code and documentation.

Dependencies

~1.9–2.9MB
~60K SLoC