-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathcloudconfig.rs
More file actions
268 lines (238 loc) · 9.73 KB
/
cloudconfig.rs
File metadata and controls
268 lines (238 loc) · 9.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//! KubeVirt cloud config parsing.
//!
//! This provider supports platforms based on KubeVirt.
//! It provides a config-drive as the only metadata source, whose layout
//! follows the `cloud-init ConfigDrive v2` [datasource][configdrive], with
//! the following details:
//! - disk filesystem label is `config-2` (lowercase)
//! - filesystem is `iso9660`
//! - drive contains a single directory at `/openstack/latest/`
//! - content is exposed as JSON or YAML files called `meta_data.json`.
//!
//! configdrive: https://cloudinit.readthedocs.io/en/latest/topics/datasources/configdrive.html
use super::provider::NetworkConfigurationFormat;
use crate::{
network::{DhcpSetting, Interface, VirtualNetDev},
providers::{kubevirt::configdrive::NetworkData, MetadataProvider},
};
use anyhow::{bail, Context, Result};
use ipnetwork::IpNetwork;
use openssh_keys::PublicKey;
use serde::Deserialize;
use slog_scope::warn;
use std::{collections::HashMap, fs::File, io::BufReader, path::Path};
/// Partial object for `meta_data.json` (ConfigDrive) or `meta-data` (NoCloud)
#[derive(Debug, Deserialize)]
pub struct MetaData {
/// Local hostname (ConfigDrive format)
#[serde(default)]
pub hostname: Option<String>,
/// Local hostname (NoCloud format)
#[serde(rename = "local-hostname", default)]
pub local_hostname: Option<String>,
/// Instance ID (ConfigDrive format - UUID)
#[serde(rename = "uuid", default)]
pub uuid: Option<String>,
/// Instance ID (NoCloud format)
#[serde(rename = "instance-id", default)]
pub instance_id: Option<String>,
/// Instance type.
pub instance_type: Option<String>,
/// SSH public keys.
pub public_keys: Option<HashMap<String, String>>,
}
#[derive(Debug)]
pub struct KubeVirtCloudConfig {
pub meta_data: MetaData,
pub configdrive_network_data: Option<super::configdrive::NetworkData>,
pub nocloud_network_config: Option<super::nocloud::NetworkConfig>,
}
impl KubeVirtCloudConfig {
pub fn try_new(path: &Path, format: NetworkConfigurationFormat) -> Result<Self> {
let meta_data = match format {
NetworkConfigurationFormat::ConfigDrive => {
match super::configdrive::read_config_file(path, "meta_data.json")? {
Some(reader) => Self::parse_metadata(reader)?,
None => bail!("meta_data.json file not found"),
}
}
NetworkConfigurationFormat::NoCloud => {
match super::nocloud::read_config_file(path, "meta-data")? {
Some(reader) => Self::parse_metadata(reader)?,
None => bail!("meta-data file not found"),
}
}
};
let (configdrive_network_data, nocloud_network_config) = match format {
NetworkConfigurationFormat::ConfigDrive => {
let config_drive_network_data = super::cloudconfig::NetworkData::from_file(path)?;
(config_drive_network_data, None)
}
NetworkConfigurationFormat::NoCloud => {
let nocloud_network_config = super::nocloud::NetworkConfig::from_file(path)?;
(None, nocloud_network_config)
}
};
Ok(Self {
meta_data,
configdrive_network_data,
nocloud_network_config,
})
}
/// Parse metadata attributes.
///
/// Metadata file contains a JSON or YAML object, corresponding to `MetaDataJSON`.
pub fn parse_metadata(input: BufReader<File>) -> Result<MetaData> {
serde_yaml::from_reader(input).context("failed to parse metadata")
}
}
impl MetadataProvider for KubeVirtCloudConfig {
/// Extract supported cloud config values and convert to Afterburn attributes.
///
/// The `AFTERBURN_` prefix is added later on, so it is not part of the
/// key-labels here.
fn attributes(&self) -> Result<HashMap<String, String>> {
// Get instance ID from either format
let instance_id = self
.meta_data
.instance_id
.as_ref()
.or(self.meta_data.uuid.as_ref())
.ok_or_else(|| anyhow::anyhow!("missing instance ID"))?;
// Get hostname from either format (prioritize ConfigDrive format for backwards compatibility)
let hostname_value = self
.meta_data
.hostname
.as_ref()
.or(self.meta_data.local_hostname.as_ref())
.ok_or_else(|| anyhow::anyhow!("missing hostname"))?;
let mut attrs = maplit::hashmap! {
"KUBEVIRT_INSTANCE_ID".to_string() => instance_id.clone(),
"KUBEVIRT_HOSTNAME".to_string() => hostname_value.clone(),
};
if let Some(instance_type) = &self.meta_data.instance_type {
attrs.insert("KUBEVIRT_INSTANCE_TYPE".to_string(), instance_type.clone());
}
if let Some(interface_with_ips) = self
.networks()?
.iter()
.find(|iface| !iface.ip_addresses.is_empty())
{
interface_with_ips
.ip_addresses
.iter()
.for_each(|ip| match ip {
IpNetwork::V4(network) => {
attrs
.entry("KUBEVIRT_IPV4".to_owned())
.or_insert_with(|| network.ip().to_string());
}
IpNetwork::V6(network) => {
attrs
.entry("KUBEVIRT_IPV6".to_owned())
.or_insert_with(|| network.ip().to_string());
}
});
}
Ok(attrs)
}
fn hostname(&self) -> Result<Option<String>> {
// Prefer ConfigDrive format hostname, fall back to NoCloud format
Ok(self
.meta_data
.hostname
.clone()
.or_else(|| self.meta_data.local_hostname.clone()))
}
/// The public key is stored as key:value pair in openstack/latest/meta_data.json file
fn ssh_keys(&self) -> Result<Vec<PublicKey>> {
self.meta_data
.public_keys
.iter()
.flat_map(|keys| keys.values())
.map(|key| PublicKey::parse(key).map_err(anyhow::Error::from))
.collect()
}
fn networks(&self) -> Result<Vec<Interface>> {
if let Some(configdrive_network_data) = &self.configdrive_network_data {
return configdrive_network_data.to_interfaces();
}
if let Some(nocloud_config) = &self.nocloud_network_config {
return nocloud_config.to_interfaces();
}
Ok(Vec::<Interface>::new())
}
fn rd_network_kargs(&self) -> Result<Option<String>> {
let mut kargs = Vec::new();
let mut all_nameservers = Vec::new();
let networks = self.networks()?;
for iface in networks {
// Use interface name as identifier if there is one
// else use mac address or continue
let id = if let Some(iface_name) = iface.name {
iface_name
} else if let Some(iface_mac) = iface.mac_address {
format!("{}", iface_mac)
} else {
continue;
};
// Add IP configuration if static
for addr in iface.ip_addresses {
let (ip, netmask_or_prefix) = match addr {
IpNetwork::V4(n) => (n.ip().to_string(), n.mask().to_string()),
IpNetwork::V6(n) => (n.ip().to_string(), n.prefix().to_string()),
};
let gateway = iface.routes.iter().find(|r| {
r.destination.prefix() == 0 && r.destination.is_ipv4() == addr.is_ipv4()
});
if let Some(gateway) = gateway {
kargs.push(format!(
"ip={}::{}:{}::{}:static",
ip, gateway.gateway, netmask_or_prefix, id,
));
} else {
kargs.push(format!("ip={}:::{}::{}:static", ip, netmask_or_prefix, id));
}
}
// Add DHCP configuration
if let Some(dhcp) = iface.dhcp {
match dhcp {
DhcpSetting::V4 => kargs.push(format!("ip={}:dhcp", id)),
DhcpSetting::V6 => kargs.push(format!("ip={}:dhcp6", id)),
DhcpSetting::Both => kargs.push(format!("ip={}:dhcp,dhcp6", id)),
}
}
// Add static routes for the interface (including DHCP interfaces)
// This allows DHCP interfaces to have static gateway configuration
for route in &iface.routes {
// Only add routes with prefix 0 (default routes)
if route.destination.prefix() == 0 {
kargs.push(format!("rd.route={}:{}", route.destination, route.gateway));
}
}
// Collect nameservers from all interfaces
for nameserver in &iface.nameservers {
if !all_nameservers.contains(nameserver) {
all_nameservers.push(*nameserver);
}
}
}
// Add nameservers as separate arguments
for nameserver in &all_nameservers {
kargs.push(format!("nameserver={}", nameserver));
}
if kargs.is_empty() {
Ok(None)
} else {
Ok(Some(kargs.join(" ")))
}
}
fn virtual_network_devices(&self) -> Result<Vec<VirtualNetDev>> {
warn!("virtual network devices metadata requested, but not supported on this platform");
Ok(vec![])
}
fn boot_checkin(&self) -> Result<()> {
warn!("boot check-in requested, but not supported on this platform");
Ok(())
}
}