From 25b5ea5b82348f8d098c004bddc267a66ae72ced Mon Sep 17 00:00:00 2001 From: Arnaldo Garcia Rincon Date: Thu, 5 Sep 2024 22:19:02 +0000 Subject: [PATCH] feat: drop nvidia-device-plugin feature Use a setings extension instead of a cargo feature for the kubelet device plugins API Signed-off-by: Arnaldo Garcia Rincon --- Cargo.toml | 2 + .../modeled-types/src/kubernetes.rs | 25 ------- .../kubelet-device-plugins/Cargo.toml | 18 +++++ .../kubelet-device-plugins.toml | 13 ++++ .../kubelet-device-plugins/src/lib.rs | 75 +++++++++++++++++++ .../kubelet-device-plugins/src/main.rs | 18 +++++ .../settings-extensions/kubernetes/Cargo.toml | 5 +- .../settings-extensions/kubernetes/src/lib.rs | 6 -- .../settings-models/Cargo.toml | 4 +- .../settings-models/src/lib.rs | 1 + 10 files changed, 129 insertions(+), 38 deletions(-) create mode 100644 bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/Cargo.toml create mode 100644 bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/kubelet-device-plugins.toml create mode 100644 bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/src/lib.rs create mode 100644 bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/src/main.rs diff --git a/Cargo.toml b/Cargo.toml index ee63e9f1..19507f24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -31,6 +31,7 @@ members = [ "bottlerocket-settings-models/settings-extensions/host-containers", "bottlerocket-settings-models/settings-extensions/kernel", "bottlerocket-settings-models/settings-extensions/kubernetes", + "bottlerocket-settings-models/settings-extensions/kubelet-device-plugins", "bottlerocket-settings-models/settings-extensions/metrics", "bottlerocket-settings-models/settings-extensions/motd", "bottlerocket-settings-models/settings-extensions/network", @@ -74,6 +75,7 @@ settings-extension-ecs = { path = "./bottlerocket-settings-models/settings-exten settings-extension-host-containers = { path = "./bottlerocket-settings-models/settings-extensions/host-containers", version = "0.1" } settings-extension-kernel = { path = "./bottlerocket-settings-models/settings-extensions/kernel", version = "0.1" } settings-extension-kubernetes = { path = "./bottlerocket-settings-models/settings-extensions/kubernetes", version = "0.1" } +settings-extension-kubelet-device-plugins = { path = "./bottlerocket-settings-models/settings-extensions/kubelet-device-plugins", version = "0.1" } settings-extension-metrics = { path = "./bottlerocket-settings-models/settings-extensions/metrics", version = "0.1" } settings-extension-motd = { path = "./bottlerocket-settings-models/settings-extensions/motd", version = "0.1" } settings-extension-network = { path = "./bottlerocket-settings-models/settings-extensions/network", version = "0.1" } diff --git a/bottlerocket-settings-models/modeled-types/src/kubernetes.rs b/bottlerocket-settings-models/modeled-types/src/kubernetes.rs index 551f6115..3f4ee6ab 100644 --- a/bottlerocket-settings-models/modeled-types/src/kubernetes.rs +++ b/bottlerocket-settings-models/modeled-types/src/kubernetes.rs @@ -1445,11 +1445,6 @@ mod test_hostname_override_source { } // =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= =^..^= -// Define the DevicePlugin struct -#[model(impl_default = true)] -pub struct K8sDevicePluginsSettings { - nvidia: NvidiaDevicePluginSettings, -} /// NvidiaRuntimeSettings contains the container runtime settings for Nvidia gpu. #[model(impl_default = true)] @@ -1477,26 +1472,6 @@ pub enum NvidiaDeviceListStrategy { mod tests { use super::*; - #[test] - fn test_serde_k8s_device_plugins() { - let test_json = r#"{"nvidia":{"pass-device-specs":true,"device-id-strategy":"index","device-list-strategy":"volume-mounts"}}"#; - - let device_plugins: K8sDevicePluginsSettings = serde_json::from_str(test_json).unwrap(); - assert_eq!( - device_plugins, - K8sDevicePluginsSettings { - nvidia: Some(NvidiaDevicePluginSettings { - pass_device_specs: Some(true), - device_id_strategy: Some(NvidiaDeviceIdStrategy::Index), - device_list_strategy: Some(NvidiaDeviceListStrategy::VolumeMounts), - }), - } - ); - - let results = serde_json::to_string(&device_plugins).unwrap(); - assert_eq!(results, test_json); - } - #[test] fn test_serde_nvidia_device_plugins() { let test_json = r#"{"pass-device-specs":false,"device-id-strategy":"uuid","device-list-strategy":"envvar"}"#; diff --git a/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/Cargo.toml b/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/Cargo.toml new file mode 100644 index 00000000..c29ae4a0 --- /dev/null +++ b/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "settings-extension-kubelet-device-plugins" +version = "0.1.0" +authors = ["Arnaldo Garcia Rincon "] +license = "Apache-2.0 OR MIT" +edition = "2021" +publish = false + +[dependencies] +bottlerocket-modeled-types.workspace = true +bottlerocket-model-derive.workspace = true +bottlerocket-settings-sdk.workspace = true +env_logger.workspace = true +serde = { workspace = true, features = ["derive"] } +serde_json.workspace = true + +[lints] +workspace = true diff --git a/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/kubelet-device-plugins.toml b/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/kubelet-device-plugins.toml new file mode 100644 index 00000000..727dfb27 --- /dev/null +++ b/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/kubelet-device-plugins.toml @@ -0,0 +1,13 @@ +[extension] +supported-versions = [ + "v1" +] +default-version = "v1" + +[v1] +[v1.validation.cross-validates] + +[v1.templating] +helpers = [] + +[v1.generation.requires] diff --git a/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/src/lib.rs b/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/src/lib.rs new file mode 100644 index 00000000..db45ea33 --- /dev/null +++ b/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/src/lib.rs @@ -0,0 +1,75 @@ +//! Settings related to Kubelet Device Plugins +use bottlerocket_model_derive::model; +use bottlerocket_modeled_types::NvidiaDevicePluginSettings; +use bottlerocket_settings_sdk::{GenerateResult, SettingsModel}; +use std::convert::Infallible; + +#[model(impl_default = true)] +pub struct KubeletDevicePluginsV1 { + nvidia: NvidiaDevicePluginSettings, +} + +type Result = std::result::Result; + +impl SettingsModel for KubeletDevicePluginsV1 { + type PartialKind = Self; + type ErrorKind = Infallible; + + fn get_version() -> &'static str { + "v1" + } + + fn set(_current_value: Option, _target: Self) -> Result<()> { + // Set anything that can be parsed as ECSSettingsV1. + Ok(()) + } + + fn generate( + existing_partial: Option, + _dependent_settings: Option, + ) -> Result> { + Ok(GenerateResult::Complete( + existing_partial.unwrap_or_default(), + )) + } + + fn validate(_value: Self, _validated_settings: Option) -> Result<()> { + // KubeletDevicePluginsV1 is validated during deserialization. + Ok(()) + } +} + +#[cfg(test)] +mod test { + use super::*; + use bottlerocket_modeled_types::{NvidiaDeviceIdStrategy, NvidiaDeviceListStrategy}; + + #[test] + fn test_generate_kubelet_device_plugins() { + let generated = KubeletDevicePluginsV1::generate(None, None).unwrap(); + assert_eq!( + generated, + GenerateResult::Complete(KubeletDevicePluginsV1 { nvidia: None }) + ); + } + + #[test] + fn test_serde_kubelet_device_plugins() { + let test_json = r#"{"nvidia":{"pass-device-specs":true,"device-id-strategy":"index","device-list-strategy":"volume-mounts"}}"#; + + let device_plugins: KubeletDevicePluginsV1 = serde_json::from_str(test_json).unwrap(); + assert_eq!( + device_plugins, + KubeletDevicePluginsV1 { + nvidia: Some(NvidiaDevicePluginSettings { + pass_device_specs: Some(true), + device_id_strategy: Some(NvidiaDeviceIdStrategy::Index), + device_list_strategy: Some(NvidiaDeviceListStrategy::VolumeMounts), + }), + } + ); + + let results = serde_json::to_string(&device_plugins).unwrap(); + assert_eq!(results, test_json); + } +} diff --git a/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/src/main.rs b/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/src/main.rs new file mode 100644 index 00000000..40789f44 --- /dev/null +++ b/bottlerocket-settings-models/settings-extensions/kubelet-device-plugins/src/main.rs @@ -0,0 +1,18 @@ +use bottlerocket_settings_sdk::{BottlerocketSetting, NullMigratorExtensionBuilder}; +use settings_extension_kubelet_device_plugins::KubeletDevicePluginsV1; +use std::process::ExitCode; + +fn main() -> ExitCode { + env_logger::init(); + + match NullMigratorExtensionBuilder::with_name("kubelet-device-plugin") + .with_models(vec![BottlerocketSetting::::model()]) + .build() + { + Ok(extension) => extension.run(), + Err(e) => { + println!("{}", e); + ExitCode::FAILURE + } + } +} diff --git a/bottlerocket-settings-models/settings-extensions/kubernetes/Cargo.toml b/bottlerocket-settings-models/settings-extensions/kubernetes/Cargo.toml index 54ca6f51..4dfa67eb 100644 --- a/bottlerocket-settings-models/settings-extensions/kubernetes/Cargo.toml +++ b/bottlerocket-settings-models/settings-extensions/kubernetes/Cargo.toml @@ -7,9 +7,6 @@ edition = "2021" publish = false [features] -# With the 'nvidia-device-plugin' feature enabled, the compilation -# includes NVIDIA GPU-related code, for instance, NvidiaDevicePluginSettings. -nvidia-device-plugin = [] [dependencies] bottlerocket-modeled-types.workspace = true @@ -24,4 +21,4 @@ toml.workspace = true workspace = true [dev-dependencies] -settings-extension-kubernetes = { workspace = true, features = ["nvidia-device-plugin"] } +settings-extension-kubernetes = { workspace = true } diff --git a/bottlerocket-settings-models/settings-extensions/kubernetes/src/lib.rs b/bottlerocket-settings-models/settings-extensions/kubernetes/src/lib.rs index 53681a34..ce820db4 100644 --- a/bottlerocket-settings-models/settings-extensions/kubernetes/src/lib.rs +++ b/bottlerocket-settings-models/settings-extensions/kubernetes/src/lib.rs @@ -1,7 +1,5 @@ //! Modeled types for creating Kubernetes settings. use bottlerocket_model_derive::model; -#[cfg(feature = "nvidia-device-plugin")] -use bottlerocket_modeled_types::K8sDevicePluginsSettings; use bottlerocket_modeled_types::{ CpuManagerPolicy, CredentialProvider, DNSDomain, Identifier, IntegerPercent, KernelCpuSetValue, KubernetesAuthenticationMode, KubernetesBootstrapToken, KubernetesCPUManagerPolicyOption, @@ -96,8 +94,6 @@ pub struct KubernetesSettingsV1 { hostname_override_source: KubernetesHostnameOverrideSource, // Generated in `k8s-1.25+` variants only seccomp_default: bool, - #[cfg(feature = "nvidia-device-plugin")] - device_plugins: K8sDevicePluginsSettings, } type Result = std::result::Result; @@ -195,8 +191,6 @@ mod test { hostname_override: None, hostname_override_source: None, seccomp_default: None, - #[cfg(feature = "nvidia-device-plugin")] - device_plugins: None, }) ); } diff --git a/bottlerocket-settings-models/settings-models/Cargo.toml b/bottlerocket-settings-models/settings-models/Cargo.toml index cf334329..310fdbc9 100644 --- a/bottlerocket-settings-models/settings-models/Cargo.toml +++ b/bottlerocket-settings-models/settings-models/Cargo.toml @@ -9,9 +9,6 @@ publish = false exclude = ["README.md"] [features] -# With the 'nvidia-device-plugin' feature enabled, the compilation -# includes NVIDIA GPU-related code, for instance, NvidiaDevicePluginSettings. -nvidia-device-plugin = ["settings-extension-kubernetes/nvidia-device-plugin"] [dependencies] libc.workspace = true @@ -37,6 +34,7 @@ settings-extension-ecs.workspace = true settings-extension-host-containers.workspace = true settings-extension-kernel.workspace = true settings-extension-kubernetes.workspace = true +settings-extension-kubelet-device-plugins.workspace = true settings-extension-metrics.workspace = true settings-extension-motd.workspace = true settings-extension-network.workspace = true diff --git a/bottlerocket-settings-models/settings-models/src/lib.rs b/bottlerocket-settings-models/settings-models/src/lib.rs index 8b8448bd..556f2afe 100644 --- a/bottlerocket-settings-models/settings-models/src/lib.rs +++ b/bottlerocket-settings-models/settings-models/src/lib.rs @@ -36,6 +36,7 @@ pub use settings_extension_dns::{self, DnsSettingsV1}; pub use settings_extension_ecs::{self, ECSSettingsV1}; pub use settings_extension_host_containers::{self, HostContainersSettingsV1}; pub use settings_extension_kernel::{self, KernelSettingsV1}; +pub use settings_extension_kubelet_device_plugins::{self, KubeletDevicePluginsV1}; pub use settings_extension_kubernetes::{self, KubernetesSettingsV1}; pub use settings_extension_metrics::{self, MetricsSettingsV1}; pub use settings_extension_motd::{self, MotdV1};