Skip to content

Commit

Permalink
vmm: Deprecate static CPU templates
Browse files Browse the repository at this point in the history
Deprecates the `cpu_template` field in `/machine-config` API.

Signed-off-by: Takahiro Itazuri <itazur@amazon.com>
  • Loading branch information
zulinx86 committed Sep 21, 2023
1 parent 0c3c9fc commit b8d6b66
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 13 deletions.
63 changes: 54 additions & 9 deletions src/api_server/src/request/machine_configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,25 @@ pub(crate) fn parse_put_machine_config(body: &Body) -> Result<ParsedRequest, Err
err
})?;

// Check for the presence of deprecated `cpu_template` field.
let mut deprecation_message = None;
if config.cpu_template.is_some() {
// `cpu_template` field in request is deprecated.
METRICS.deprecated_api.deprecated_http_api_calls.inc();
deprecation_message = Some("PUT /machine-config: cpu_template field is deprecated.");
}

// Convert `MachineConfig` to `MachineConfigUpdate`.
let config_update = MachineConfigUpdate::from(config);

Ok(ParsedRequest::new_sync(VmmAction::UpdateVmConfiguration(
config_update,
)))
// Construct the `ParsedRequest` object.
let mut parsed_req = ParsedRequest::new_sync(VmmAction::UpdateVmConfiguration(config_update));
// If `cpu_template` was present, set the deprecation message in `parsing_info`.
if let Some(msg) = deprecation_message {
parsed_req.parsing_info().append_deprecation_message(msg);
}

Ok(parsed_req)
}

pub(crate) fn parse_patch_machine_config(body: &Body) -> Result<ParsedRequest, Error> {
Expand All @@ -39,9 +53,22 @@ pub(crate) fn parse_patch_machine_config(body: &Body) -> Result<ParsedRequest, E
return method_to_error(Method::Patch);
}

Ok(ParsedRequest::new_sync(VmmAction::UpdateVmConfiguration(
config_update,
)))
// Check for the presence of deprecated `cpu_template` field.
let mut deprecation_message = None;
if config_update.cpu_template.is_some() {
// `cpu_template` field in request is deprecated.
METRICS.deprecated_api.deprecated_http_api_calls.inc();
deprecation_message = Some("PATCH /machine-config: cpu_template is deprecated.");
}

// Construct the `ParsedRequest` object.
let mut parsed_req = ParsedRequest::new_sync(VmmAction::UpdateVmConfiguration(config_update));
// If `cpu_template` was present, set the deprecation message in `parsing_info`.
if let Some(msg) = deprecation_message {
parsed_req.parsing_info().append_deprecation_message(msg);
}

Ok(parsed_req)
}

#[cfg(test)]
Expand Down Expand Up @@ -78,7 +105,25 @@ mod tests {
let body = r#"{
"vcpu_count": 8,
"mem_size_mib": 1024
}"#;
}"#;
let expected_config = MachineConfigUpdate {
vcpu_count: Some(8),
mem_size_mib: Some(1024),
smt: Some(false),
cpu_template: None,
track_dirty_pages: Some(false),
};

match vmm_action_from_request(parse_put_machine_config(&Body::new(body)).unwrap()) {
VmmAction::UpdateVmConfiguration(config) => assert_eq!(config, expected_config),
_ => panic!("Test failed."),
}

let body = r#"{
"vcpu_count": 8,
"mem_size_mib": 1024,
"cpu_template": "None"
}"#;
let expected_config = MachineConfigUpdate {
vcpu_count: Some(8),
mem_size_mib: Some(1024),
Expand All @@ -102,7 +147,7 @@ mod tests {
vcpu_count: Some(8),
mem_size_mib: Some(1024),
smt: Some(false),
cpu_template: Some(StaticCpuTemplate::None),
cpu_template: None,
track_dirty_pages: Some(true),
};

Expand Down Expand Up @@ -155,7 +200,7 @@ mod tests {
vcpu_count: Some(8),
mem_size_mib: Some(1024),
smt: Some(true),
cpu_template: Some(StaticCpuTemplate::None),
cpu_template: None,
track_dirty_pages: Some(true),
};

Expand Down
8 changes: 4 additions & 4 deletions src/vmm/src/vmm_config/machine_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub struct MachineConfig {
#[serde(default, deserialize_with = "deserialize_smt")]
pub smt: bool,
/// A CPU template that it is used to filter the CPU features exposed to the guest.
#[serde(default, skip_serializing_if = "StaticCpuTemplate::is_none")]
pub cpu_template: StaticCpuTemplate,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub cpu_template: Option<StaticCpuTemplate>,
/// Enables or disables dirty page tracking. Enabling allows incremental snapshots.
#[serde(default)]
pub track_dirty_pages: bool,
Expand Down Expand Up @@ -122,7 +122,7 @@ impl From<MachineConfig> for MachineConfigUpdate {
vcpu_count: Some(cfg.vcpu_count),
mem_size_mib: Some(cfg.mem_size_mib),
smt: Some(cfg.smt),
cpu_template: Some(cfg.cpu_template),
cpu_template: cfg.cpu_template,
track_dirty_pages: Some(cfg.track_dirty_pages),
}
}
Expand Down Expand Up @@ -212,7 +212,7 @@ impl From<&VmConfig> for MachineConfig {
vcpu_count: value.vcpu_count,
mem_size_mib: value.mem_size_mib,
smt: value.smt,
cpu_template: (&value.cpu_template).into(),
cpu_template: Some((&value.cpu_template).into()),
track_dirty_pages: value.track_dirty_pages,
}
}
Expand Down

0 comments on commit b8d6b66

Please sign in to comment.