container_device_interface/generate/
config.rs

1use std::collections::HashMap;
2
3use oci_spec::runtime::{Hooks, Linux, LinuxIntelRdt, LinuxResources, Mount, Process, Spec};
4
5pub struct Generator {
6    pub config: Option<Spec>,
7    pub host_specific: bool,
8    pub env_map: HashMap<String, usize>,
9}
10
11impl Generator {
12    pub fn spec_gen(spec: Option<Spec>) -> Self {
13        Generator {
14            config: spec,
15            host_specific: false,
16            env_map: HashMap::new(),
17        }
18    }
19
20    pub fn init_config(&mut self) {
21        if self.config.is_none() {
22            self.config = Some(Spec::default());
23        }
24    }
25
26    pub fn init_config_process(&mut self) {
27        self.init_config();
28
29        if let Some(spec) = self.config.as_mut() {
30            if spec.process().is_none() {
31                spec.set_process(Some(Process::default()));
32            }
33        }
34    }
35
36    pub fn init_config_linux(&mut self) {
37        self.init_config();
38
39        if let Some(spec) = self.config.as_mut() {
40            if spec.linux().is_none() {
41                spec.set_linux(Some(Linux::default()));
42            }
43        }
44    }
45
46    pub fn init_config_linux_resources(&mut self) {
47        self.init_config_linux();
48
49        if let Some(linux) = self.config.as_mut().unwrap().linux_mut() {
50            if linux.resources().is_none() {
51                linux.set_resources(Some(LinuxResources::default()));
52            }
53        }
54    }
55
56    pub fn init_config_linux_resources_devices(&mut self) {
57        self.init_config_linux_resources();
58
59        if let Some(linux) = self.config.as_mut().unwrap().linux_mut() {
60            if let Some(resource) = linux.resources_mut() {
61                if resource.devices().is_none() {
62                    resource.set_devices(Some(Vec::new()));
63                }
64            }
65        }
66    }
67
68    pub fn init_config_hooks(&mut self) {
69        self.init_config();
70
71        if let Some(spec) = self.config.as_mut() {
72            if spec.hooks().is_none() {
73                spec.set_hooks(Some(Hooks::default()));
74            }
75        }
76    }
77
78    pub fn init_config_mounts(&mut self) {
79        self.init_config();
80
81        if let Some(spec) = self.config.as_mut() {
82            if spec.mounts().is_none() {
83                spec.set_mounts(Some(vec![Mount::default()]));
84            }
85        }
86    }
87
88    pub fn init_config_linux_intel_rdt(&mut self) {
89        self.init_config_linux();
90
91        if let Some(linux) = self.config.as_mut().unwrap().linux_mut() {
92            if linux.intel_rdt().is_none() {
93                linux.set_intel_rdt(Some(LinuxIntelRdt::default()));
94            }
95        }
96    }
97}