|
| 1 | +//! KubeVirt common-instancetypes support |
| 2 | +//! |
| 3 | +//! This module vendors the KubeVirt common-instancetypes definitions, |
| 4 | +//! specifically the U series (Universal/General Purpose) instance types. |
| 5 | +//! These provide standardized VM sizing with predefined vCPU and memory |
| 6 | +//! configurations. |
| 7 | +//! |
| 8 | +//! Instance types follow the format: u1.{size} |
| 9 | +//! Examples: u1.nano, u1.micro, u1.small, u1.medium, u1.large, etc. |
| 10 | +//! |
| 11 | +//! Source: https://github.com/kubevirt/common-instancetypes |
| 12 | +
|
| 13 | +/// Instance type variants with associated vCPU and memory specifications |
| 14 | +/// |
| 15 | +/// Source: https://github.com/kubevirt/common-instancetypes/blob/main/instancetypes/u/1/sizes.yaml |
| 16 | +#[derive( |
| 17 | + Debug, |
| 18 | + Clone, |
| 19 | + Copy, |
| 20 | + PartialEq, |
| 21 | + Eq, |
| 22 | + serde::Serialize, |
| 23 | + serde::Deserialize, |
| 24 | + strum::Display, |
| 25 | + strum::EnumString, |
| 26 | + strum::EnumIter, |
| 27 | +)] |
| 28 | +#[non_exhaustive] |
| 29 | +pub enum InstanceType { |
| 30 | + /// u1.nano - 1 vCPU, 512 MiB memory |
| 31 | + #[strum(serialize = "u1.nano")] |
| 32 | + U1Nano, |
| 33 | + /// u1.micro - 1 vCPU, 1 GiB memory |
| 34 | + #[strum(serialize = "u1.micro")] |
| 35 | + U1Micro, |
| 36 | + /// u1.small - 1 vCPU, 2 GiB memory |
| 37 | + #[strum(serialize = "u1.small")] |
| 38 | + U1Small, |
| 39 | + /// u1.medium - 1 vCPU, 4 GiB memory |
| 40 | + #[strum(serialize = "u1.medium")] |
| 41 | + U1Medium, |
| 42 | + /// u1.2xmedium - 2 vCPU, 4 GiB memory |
| 43 | + #[strum(serialize = "u1.2xmedium")] |
| 44 | + U1TwoXMedium, |
| 45 | + /// u1.large - 2 vCPU, 8 GiB memory |
| 46 | + #[strum(serialize = "u1.large")] |
| 47 | + U1Large, |
| 48 | + /// u1.xlarge - 4 vCPU, 16 GiB memory |
| 49 | + #[strum(serialize = "u1.xlarge")] |
| 50 | + U1XLarge, |
| 51 | + /// u1.2xlarge - 8 vCPU, 32 GiB memory |
| 52 | + #[strum(serialize = "u1.2xlarge")] |
| 53 | + U1TwoXLarge, |
| 54 | + /// u1.4xlarge - 16 vCPU, 64 GiB memory |
| 55 | + #[strum(serialize = "u1.4xlarge")] |
| 56 | + U1FourXLarge, |
| 57 | + /// u1.8xlarge - 32 vCPU, 128 GiB memory |
| 58 | + #[strum(serialize = "u1.8xlarge")] |
| 59 | + U1EightXLarge, |
| 60 | +} |
| 61 | + |
| 62 | +impl InstanceType { |
| 63 | + /// Get the number of vCPUs for this instance type |
| 64 | + pub const fn vcpus(self) -> u32 { |
| 65 | + match self { |
| 66 | + Self::U1Nano => 1, |
| 67 | + Self::U1Micro => 1, |
| 68 | + Self::U1Small => 1, |
| 69 | + Self::U1Medium => 1, |
| 70 | + Self::U1TwoXMedium => 2, |
| 71 | + Self::U1Large => 2, |
| 72 | + Self::U1XLarge => 4, |
| 73 | + Self::U1TwoXLarge => 8, |
| 74 | + Self::U1FourXLarge => 16, |
| 75 | + Self::U1EightXLarge => 32, |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /// Get the memory in megabytes for this instance type |
| 80 | + pub const fn memory_mb(self) -> u32 { |
| 81 | + match self { |
| 82 | + Self::U1Nano => 512, |
| 83 | + Self::U1Micro => 1024, |
| 84 | + Self::U1Small => 2048, |
| 85 | + Self::U1Medium => 4096, |
| 86 | + Self::U1TwoXMedium => 4096, |
| 87 | + Self::U1Large => 8192, |
| 88 | + Self::U1XLarge => 16384, |
| 89 | + Self::U1TwoXLarge => 32768, |
| 90 | + Self::U1FourXLarge => 65536, |
| 91 | + Self::U1EightXLarge => 131072, |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[cfg(test)] |
| 97 | +mod tests { |
| 98 | + use super::*; |
| 99 | + use std::str::FromStr; |
| 100 | + use strum::IntoEnumIterator; |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_properties() { |
| 104 | + for variant in InstanceType::iter() { |
| 105 | + let (expected_vcpus, expected_memory_mb) = match variant { |
| 106 | + InstanceType::U1Nano => (1, 512), |
| 107 | + InstanceType::U1Micro => (1, 1024), |
| 108 | + InstanceType::U1Small => (1, 2048), |
| 109 | + InstanceType::U1Medium => (1, 4096), |
| 110 | + InstanceType::U1TwoXMedium => (2, 4096), |
| 111 | + InstanceType::U1Large => (2, 8192), |
| 112 | + InstanceType::U1XLarge => (4, 16384), |
| 113 | + InstanceType::U1TwoXLarge => (8, 32768), |
| 114 | + InstanceType::U1FourXLarge => (16, 65536), |
| 115 | + InstanceType::U1EightXLarge => (32, 131072), |
| 116 | + }; |
| 117 | + assert_eq!( |
| 118 | + variant.vcpus(), |
| 119 | + expected_vcpus, |
| 120 | + "Mismatch in vcpus for {:?}", |
| 121 | + variant |
| 122 | + ); |
| 123 | + assert_eq!( |
| 124 | + variant.memory_mb(), |
| 125 | + expected_memory_mb, |
| 126 | + "Mismatch in memory_mb for {:?}", |
| 127 | + variant |
| 128 | + ); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_parse_invalid_instancetype() { |
| 134 | + let result = InstanceType::from_str("invalid"); |
| 135 | + assert!(result.is_err()); |
| 136 | + } |
| 137 | + |
| 138 | + #[test] |
| 139 | + fn test_roundtrip() { |
| 140 | + for variant in InstanceType::iter() { |
| 141 | + let s = variant.to_string(); |
| 142 | + let parsed = InstanceType::from_str(&s).unwrap(); |
| 143 | + assert_eq!(parsed, variant); |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments