yaml configs for daemon

This commit is contained in:
ghe0 2024-12-11 20:13:17 +02:00
parent b0cf13ddfb
commit ec962197ff
Signed by: ghe0
GPG Key ID: 451028EE56A0FBB4
10 changed files with 210 additions and 9 deletions

20
Cargo.lock generated

@ -157,6 +157,7 @@ dependencies = [
"rand", "rand",
"reqwest", "reqwest",
"serde", "serde",
"serde_yaml",
"sha2", "sha2",
] ]
@ -1039,6 +1040,19 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_yaml"
version = "0.9.34+deprecated"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]] [[package]]
name = "sha2" name = "sha2"
version = "0.10.8" version = "0.10.8"
@ -1265,6 +1279,12 @@ version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83" checksum = "adb9e6ca4f869e1180728b7950e35922a7fc6397f7b641499e8f3ef06e50dc83"
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861"
[[package]] [[package]]
name = "untrusted" name = "untrusted"
version = "0.9.0" version = "0.9.0"

@ -9,4 +9,5 @@ cidr = { version = "0.3.0", features = ["serde"] }
rand = "0.8.5" rand = "0.8.5"
reqwest = { version = "0.12.9", features = ["blocking"] } reqwest = { version = "0.12.9", features = ["blocking"] }
serde = { version = "1.0.215", features = ["derive"] } serde = { version = "1.0.215", features = ["derive"] }
serde_yaml = "0.9.34"
sha2 = "0.10.8" sha2 = "0.10.8"

@ -1,30 +1,31 @@
#![allow(dead_code)] #![allow(dead_code)]
use cidr::Ipv4Cidr; use cidr::{Ipv4Cidr, Ipv6Cidr};
use cidr::Ipv6Cidr; use serde::Deserialize;
use core::net::Ipv4Addr;
use core::net::Ipv6Addr;
use std::collections::HashSet; use std::collections::HashSet;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::ops::Range; use std::ops::Range;
#[derive(Deserialize, Debug)]
pub struct Volume { pub struct Volume {
path: String, path: String,
// maximum allowed storage in GB
max_reservation: u64, max_reservation: u64,
} }
#[derive(Deserialize, Debug)]
pub struct IPv4Range { pub struct IPv4Range {
pub subnet: Ipv4Cidr, pub subnet: Ipv4Cidr,
pub gateway: Ipv4Addr, pub gateway: Ipv4Addr,
pub reserved_addrs: HashSet<Ipv4Addr>, pub reserved_addrs: HashSet<Ipv4Addr>,
} }
#[derive(Deserialize, Debug)]
pub struct IPv6Range { pub struct IPv6Range {
pub subnet: Ipv6Cidr, pub subnet: Ipv6Cidr,
pub gateway: Ipv6Addr, pub gateway: Ipv6Addr,
pub reserved_addrs: HashSet<Ipv6Addr>, pub reserved_addrs: HashSet<Ipv6Addr>,
} }
#[derive(Deserialize, Debug)]
pub struct Interface { pub struct Interface {
pub driver: InterfaceType, pub driver: InterfaceType,
pub device: String, pub device: String,
@ -32,19 +33,60 @@ pub struct Interface {
pub ipv6: Vec<IPv6Range>, pub ipv6: Vec<IPv6Range>,
} }
// TODO: create mechanic to autodetect interface type #[derive(Deserialize, Debug)]
pub enum InterfaceType { pub enum InterfaceType {
MACVTAP, MACVTAP,
IPVTAP, IPVTAP,
Bridge, Bridge,
} }
#[derive(Deserialize, Debug)]
pub struct Config { pub struct Config {
pub max_cores_per_vm: usize, pub max_cores_per_vm: usize,
pub max_vcpu_reservation: usize, pub max_vcpu_reservation: usize,
pub max_mem_reservation: usize, pub max_mem_reservation: usize,
pub network_interfaces: Vec<Interface>, pub network_interfaces: Vec<Interface>,
pub volumes: Vec<Volume>, pub volumes: Vec<Volume>,
#[serde(with = "range_format")]
pub public_port_range: Range<u16>, pub public_port_range: Range<u16>,
pub max_ports_per_vm: u16, pub max_ports_per_vm: u16,
} }
mod range_format {
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::ops::Range;
pub fn serialize<S>(range: &Range<u16>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let range_repr = RangeRepr {
start: range.start,
end: range.end,
};
range_repr.serialize(serializer)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Range<u16>, D::Error>
where
D: Deserializer<'de>,
{
let range_repr = RangeRepr::deserialize(deserializer)?;
Ok(range_repr.start..range_repr.end)
}
#[derive(Serialize, Deserialize)]
struct RangeRepr {
start: u16,
end: u16,
}
}
impl Config {
pub fn from_file(path: &str) -> Result<Self, Box<dyn std::error::Error>> {
let content = std::fs::read_to_string(path)?;
let config: Config = serde_yaml::from_str(&content)?;
Ok(config)
}
}

@ -3,6 +3,18 @@ mod state;
mod constants; mod constants;
mod tcontract; mod tcontract;
fn main() { use crate::config::Config;
println!("Hello, world!");
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = Config::from_file("test_data/config1.yaml")?;
println!("{:#?}", config);
let config = Config::from_file("test_data/config2.yaml")?;
println!("{:#?}", config);
let config = Config::from_file("test_data/config3.yaml")?;
println!("{:#?}", config);
let config = Config::from_file("test_data/config4.yaml")?;
println!("{:#?}", config);
let config = Config::from_file("test_data/config5.yaml")?;
println!("{:#?}", config);
Ok(())
} }

@ -22,3 +22,4 @@ pub struct ResourceAllocation {
pub public_ipv4: Option<String>, pub public_ipv4: Option<String>,
pub public_ipv6: Option<String>, pub public_ipv6: Option<String>,
} }

25
test_data/config1.yaml Normal file

@ -0,0 +1,25 @@
max_cores_per_vm: 4
max_vcpu_reservation: 8
max_mem_reservation: 16384
network_interfaces:
- driver: "MACVTAP"
device: "eth0"
ipv4:
- subnet: "192.168.1.0/24"
gateway: "192.168.1.1"
reserved_addrs:
- "192.168.1.100"
- "192.168.1.101"
ipv6:
- subnet: "2001:db8::/32"
gateway: "2001:db8::1"
reserved_addrs:
- "2001:db8::1234"
- "2001:db8::5678"
volumes:
- path: "/mnt/storage"
max_reservation: 200
public_port_range:
start: 8000
end: 9000
max_ports_per_vm: 5

39
test_data/config2.yaml Normal file

@ -0,0 +1,39 @@
max_cores_per_vm: 16
max_vcpu_reservation: 32
max_mem_reservation: 65536
network_interfaces:
- driver: "Bridge"
device: "br0"
ipv4:
- subnet: "10.0.0.0/16"
gateway: "10.0.0.1"
reserved_addrs:
- "10.0.0.100"
- "10.0.0.101"
- "10.0.0.102"
ipv6:
- subnet: "fd00::/48"
gateway: "fd00::1"
reserved_addrs:
- "fd00::1000"
- driver: "IPVTAP"
device: "tap1"
ipv4:
- subnet: "172.16.0.0/20"
gateway: "172.16.0.1"
reserved_addrs:
- "172.16.0.10"
- "172.16.0.11"
ipv6: []
volumes:
- path: "/data/volume1"
max_reservation: 500
- path: "/data/volume2"
max_reservation: 1000
- path: "/backup"
max_reservation: 2000
public_port_range:
start: 10000
end: 11000
max_ports_per_vm: 20

20
test_data/config3.yaml Normal file

@ -0,0 +1,20 @@
max_cores_per_vm: 12
max_vcpu_reservation: 24
max_mem_reservation: 49152
network_interfaces:
- driver: "IPVTAP"
device: "tap0"
ipv4: []
ipv6:
- subnet: "2001:db8:abcd:1234::/64"
gateway: "2001:db8:abcd:1234::1"
reserved_addrs:
- "2001:db8:abcd:1234::dead"
- "2001:db8:abcd:1234::beef"
volumes:
- path: "/ipv6/volume"
max_reservation: 600
public_port_range:
start: 15000
end: 16000
max_ports_per_vm: 10

19
test_data/config4.yaml Normal file

@ -0,0 +1,19 @@
max_cores_per_vm: 2
max_vcpu_reservation: 4
max_mem_reservation: 8192
network_interfaces:
- driver: "MACVTAP"
device: "eth0"
ipv4:
- subnet: "192.168.0.0/24"
gateway: "192.168.0.1"
reserved_addrs: []
ipv6: []
volumes:
- path: "/minimal/volume"
max_reservation: 100
public_port_range:
start: 5000
end: 5100
max_ports_per_vm: 3

22
test_data/config5.yaml Normal file

@ -0,0 +1,22 @@
max_cores_per_vm: 8
max_vcpu_reservation: 16
max_mem_reservation: 32768
network_interfaces:
- driver: "Bridge"
device: "br1"
ipv4:
- subnet: "192.168.100.0/24"
gateway: "192.168.100.1"
reserved_addrs: []
ipv6:
- subnet: "2001:abcd::/48"
gateway: "2001:abcd::1"
reserved_addrs: []
volumes:
- path: "/network/volume"
max_reservation: 750
public_port_range:
start: 6000
end: 7000
max_ports_per_vm: 8