Skip to content

Commit 603cc2d

Browse files
committed
feat: add nvidia shared GPU settings to k8s model
Signed-off-by: Monirul Islam <mamun.sust@gmail.com>
1 parent 291f07d commit 603cc2d

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

bottlerocket-settings-models/modeled-types/src/kubernetes.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::convert::TryFrom;
1414
use std::fmt::{self, Display, Formatter};
1515
use std::net::IpAddr;
1616

17+
use crate::PositiveInteger;
1718
use crate::SingleLineString;
1819

1920
// Declare constant values usable by any type
@@ -1455,6 +1456,8 @@ pub struct NvidiaDevicePluginSettings {
14551456
pass_device_specs: bool,
14561457
device_id_strategy: NvidiaDeviceIdStrategy,
14571458
device_list_strategy: NvidiaDeviceListStrategy,
1459+
max_sharing_per_gpu: PositiveInteger,
1460+
rename_shared_gpu: bool,
14581461
}
14591462

14601463
#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
@@ -1477,7 +1480,7 @@ mod tests {
14771480

14781481
#[test]
14791482
fn test_serde_k8s_device_plugins() {
1480-
let test_json = r#"{"nvidia":{"pass-device-specs":true,"device-id-strategy":"index","device-list-strategy":"volume-mounts"}}"#;
1483+
let test_json = r#"{"nvidia":{"pass-device-specs":true,"device-id-strategy":"index","device-list-strategy":"volume-mounts","max-sharing-per-gpu":10,"rename-shared-gpu":true}}"#;
14811484

14821485
let device_plugins: K8sDevicePluginsSettings = serde_json::from_str(test_json).unwrap();
14831486
assert_eq!(
@@ -1487,6 +1490,8 @@ mod tests {
14871490
pass_device_specs: Some(true),
14881491
device_id_strategy: Some(NvidiaDeviceIdStrategy::Index),
14891492
device_list_strategy: Some(NvidiaDeviceListStrategy::VolumeMounts),
1493+
max_sharing_per_gpu: Some(PositiveInteger { inner: 10 }),
1494+
rename_shared_gpu: Some(true),
14901495
}),
14911496
}
14921497
);
@@ -1497,7 +1502,7 @@ mod tests {
14971502

14981503
#[test]
14991504
fn test_serde_nvidia_device_plugins() {
1500-
let test_json = r#"{"pass-device-specs":false,"device-id-strategy":"uuid","device-list-strategy":"envvar"}"#;
1505+
let test_json = r#"{"pass-device-specs":false,"device-id-strategy":"uuid","device-list-strategy":"envvar","max-sharing-per-gpu":10,"rename-shared-gpu":false}"#;
15011506
let nvidia_device_plugins: NvidiaDevicePluginSettings =
15021507
serde_json::from_str(test_json).unwrap();
15031508
assert_eq!(
@@ -1506,10 +1511,18 @@ mod tests {
15061511
pass_device_specs: Some(false),
15071512
device_id_strategy: Some(NvidiaDeviceIdStrategy::Uuid),
15081513
device_list_strategy: Some(NvidiaDeviceListStrategy::Envvar),
1514+
max_sharing_per_gpu: Some(PositiveInteger { inner: 10 }),
1515+
rename_shared_gpu: Some(false),
15091516
}
15101517
);
15111518

15121519
let results = serde_json::to_string(&nvidia_device_plugins).unwrap();
15131520
assert_eq!(results, test_json);
15141521
}
1522+
#[test]
1523+
fn test_invalid_max_sharing_per_gpu() {
1524+
let test_json = r#"{"pass-device-specs":false,"device-id-strategy":"uuid","device-list-strategy":"envvar","max-sharing-per-gpu":0,"rename-shared-gpu":false}"#;
1525+
let result: Result<NvidiaDevicePluginSettings, _> = serde_json::from_str(test_json);
1526+
assert!(result.is_err(), "The JSON should not be parsed successfully as it contains an invalid value for 'max-sharing-per-gpu'.");
1527+
}
15151528
}

bottlerocket-settings-models/modeled-types/src/shared.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,6 +1181,47 @@ mod test_positive_integer {
11811181

11821182
// =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^=
11831183

1184+
/// Input value that needs to be a positive integer, but should not be greater
1185+
/// than an i32::MAX.
1186+
#[derive(Clone, Debug, PartialEq, Scalar)]
1187+
pub struct PositiveInteger {
1188+
pub inner: i32,
1189+
}
1190+
1191+
impl Validate for PositiveInteger {
1192+
fn validate<I: Into<i32>>(input: I) -> Result<PositiveInteger, ValidationError> {
1193+
let inner: i32 = input.into();
1194+
if inner < 1 {
1195+
Err(ValidationError::new(
1196+
"number must be great than or equal to 1",
1197+
))
1198+
} else {
1199+
Ok(Self { inner })
1200+
}
1201+
}
1202+
}
1203+
1204+
#[cfg(test)]
1205+
mod test_nonzero_integer {
1206+
use super::PositiveInteger;
1207+
use std::convert::TryFrom;
1208+
1209+
#[test]
1210+
fn valid_positive_integer() {
1211+
assert!(PositiveInteger::try_from(1).is_ok());
1212+
assert!(PositiveInteger::try_from(i32::MAX).is_ok());
1213+
assert!(PositiveInteger::try_from(42).is_ok());
1214+
}
1215+
1216+
#[test]
1217+
fn invalid_positive_integer() {
1218+
assert!(PositiveInteger::try_from(i32::MIN).is_err());
1219+
assert!(PositiveInteger::try_from(-1).is_err());
1220+
assert!(PositiveInteger::try_from(0).is_err());
1221+
}
1222+
}
1223+
// =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^=
1224+
11841225
/// KernelCpuSetValue represents a string that contains a valid Kernel CpuSet Value from
11851226
/// here: https://man7.org/linux/man-pages/man7/cpuset.7.html#FORMATS. This matches the
11861227
/// logic from https://github.com/kubernetes/utils/blob/d93618cff8a22d3aea7bf78d9d528fd859720c2d/cpuset/cpuset.go#L203

bottlerocket-settings-models/settings-models/src/kubernetes/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ use bottlerocket_modeled_types::{
2424
TopologyManagerScope, Url, ValidBase64, ValidLinuxHostname,
2525
};
2626

27+
// Feature flags
28+
#[cfg(feature = "nvidia-device-plugin")]
29+
pub const NVIDIA_DEVICE_PLUGIN_FEATURE_ENABLED: bool = true;
30+
#[cfg(not(feature = "nvidia-device-plugin"))]
31+
pub const NVIDIA_DEVICE_PLUGIN_FEATURE_ENABLED: bool = false;
32+
2733
// Kubernetes static pod manifest settings
2834
#[model]
2935
pub struct StaticPod {

bottlerocket-settings-models/settings-models/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ settings structures that helpfully validate inputs on deserialize.
1515
*/
1616

1717
mod boot;
18-
mod kubernetes;
18+
pub mod kubernetes;
1919

2020
// Expose types for creating new settings structs
2121
pub use bottlerocket_model_derive as model_derive;

0 commit comments

Comments
 (0)