Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suffix usage slot to annotation key name #560

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions agent/src/util/crictl_containers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use akri_shared::akri::AKRI_SLOT_ANNOTATION_NAME;
use akri_shared::akri::AKRI_SLOT_ANNOTATION_NAME_PREFIX;
use std::collections::{HashMap, HashSet};

/// Output from crictl query
Expand All @@ -15,18 +15,24 @@ struct CriCtlContainer {
annotations: HashMap<String, String>,
}

/// This gets the usage slots for an instance by getting the annotations that were stored at id `AKRI_SLOT_ANNOTATION_NAME` during allocate.
/// This gets the usage slots for an instance by getting the annotations that were stored at id `AKRI_SLOT_ANNOTATION_NAME_PREFIX` during allocate.
pub fn get_container_slot_usage(crictl_output: &str) -> HashSet<String> {
match serde_json::from_str::<CriCtlOutput>(crictl_output) {
Ok(crictl_output_parsed) => crictl_output_parsed
.containers
.iter()
.filter_map(|container| {
container
.annotations
.get(&AKRI_SLOT_ANNOTATION_NAME.to_string())
.flat_map(|container| &container.annotations)
.filter_map(|(key, value)| {
if key.starts_with(AKRI_SLOT_ANNOTATION_NAME_PREFIX)
&& value.eq(key
.strip_prefix(AKRI_SLOT_ANNOTATION_NAME_PREFIX)
.unwrap_or_default())
{
Some(value.clone())
} else {
None
}
})
.map(|string_ref| string_ref.to_string())
.collect(),
Err(e) => {
trace!(
Expand Down Expand Up @@ -108,15 +114,15 @@ mod tests {
expected,
get_container_slot_usage(&format!(
"{{ \"ddd\": \"\", \"containers\": [ {} ] }}",
&get_container_str("\"akri.agent.slot\": \"foo\",")
&get_container_str("\"akri.agent.slot-foo\": \"foo\",")
))
);
// Expected output with slot
assert_eq!(
expected,
get_container_slot_usage(&format!(
"{{ \"containers\": [ {} ] }}",
&get_container_str("\"akri.agent.slot\": \"foo\",")
&get_container_str("\"akri.agent.slot-foo\": \"foo\",")
))
);
// Expected output with multiple containers
Expand All @@ -127,8 +133,8 @@ mod tests {
expected_2,
get_container_slot_usage(&format!(
"{{ \"containers\": [ {}, {} ] }}",
&get_container_str("\"akri.agent.slot\": \"foo1\","),
&get_container_str("\"akri.agent.slot\": \"foo2\","),
&get_container_str("\"akri.agent.slot-foo1\": \"foo1\","),
&get_container_str("\"akri.agent.slot-foo2\": \"foo2\","),
))
);
}
Expand Down
6 changes: 3 additions & 3 deletions agent/src/util/device_plugin_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use akri_shared::{
configuration::ConfigurationSpec,
instance::InstanceSpec,
retry::{random_delay, MAX_INSTANCE_UPDATE_TRIES},
AKRI_SLOT_ANNOTATION_NAME,
AKRI_SLOT_ANNOTATION_NAME_PREFIX,
},
k8s,
k8s::KubeInterface,
Expand Down Expand Up @@ -287,7 +287,7 @@ impl DevicePluginService {
&self.instance_name,
request,
);
let mut akri_annotations = std::collections::HashMap::new();
let mut akri_annotations = HashMap::new();
for device_usage_id in request.devices_i_ds {
trace!(
"internal_allocate - for Instance {} processing request for device usage slot id {}",
Expand All @@ -296,7 +296,7 @@ impl DevicePluginService {
);

akri_annotations.insert(
AKRI_SLOT_ANNOTATION_NAME.to_string(),
format!("{}{}", AKRI_SLOT_ANNOTATION_NAME_PREFIX, &device_usage_id),
device_usage_id.clone(),
);

Expand Down
4 changes: 2 additions & 2 deletions shared/src/akri/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub const API_CONFIGURATIONS: &str = "configurations";
pub const API_INSTANCES: &str = "instances";
/// Akri prefix
pub const AKRI_PREFIX: &str = "akri.sh";
/// Container Annotation name used to store slot name
pub const AKRI_SLOT_ANNOTATION_NAME: &str = "akri.agent.slot";
/// Container Annotation name prefix used to store slot name
pub const AKRI_SLOT_ANNOTATION_NAME_PREFIX: &str = "akri.agent.slot-";

pub mod configuration;
pub mod instance;
Expand Down