Skip to content

Commit de2f313

Browse files
committed
Use Quantity instead of String for resource definitions. (#402)
As agreed with @soenkeliebau and @teozkr
1 parent d868708 commit de2f313

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ All notable changes to this project will be documented in this file.
1010

1111
[#397]: https://github.com/stackabletech/operator-rs/pull/397
1212

13+
### Changed
14+
15+
- BREAKING: Memory and CPU resource definitions use quantity instead of String ([#402])
16+
17+
[#402]: https://github.com/stackabletech/operator-rs/pull/402
18+
1319
## [0.19.0] - 2022-05-05
1420

1521
### Changed

src/commons/resources.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ where
8989

9090
// Defines memory limits to be set on the pods
9191
// Is generic to enable adding custom configuration for specific runtimes or products
92-
#[derive(Clone, Debug, Deserialize, Default, Merge, JsonSchema, PartialEq, Serialize, Eq)]
92+
#[derive(Clone, Debug, Deserialize, Default, Merge, JsonSchema, PartialEq, Serialize)]
9393
#[merge(path_overrides(merge = "crate::config::merge"))]
9494
#[serde(rename_all = "camelCase")]
9595
pub struct MemoryLimits<T>
@@ -98,7 +98,7 @@ where
9898
{
9999
// The maximum amount of memory that should be available
100100
// Should in most cases be mapped to resources.limits.memory
101-
pub limit: Option<String>,
101+
pub limit: Option<Quantity>,
102102
// Additional options that may be required
103103
#[serde(default)]
104104
pub runtime_limits: T,
@@ -117,9 +117,9 @@ pub struct NoRuntimeLimits {}
117117
#[merge(path_overrides(merge = "crate::config::merge"))]
118118
#[serde(rename_all = "camelCase")]
119119
pub struct JvmHeapLimits {
120-
pub max: Option<String>,
120+
pub max: Option<Quantity>,
121121
#[serde(default, skip_serializing_if = "Option::is_none")]
122-
pub min: Option<String>,
122+
pub min: Option<Quantity>,
123123
}
124124

125125
// Cpu limits
@@ -128,16 +128,16 @@ pub struct JvmHeapLimits {
128128
#[merge(path_overrides(merge = "crate::config::merge"))]
129129
#[serde(rename_all = "camelCase")]
130130
pub struct CpuLimits {
131-
pub min: Option<String>,
132-
pub max: Option<String>,
131+
pub min: Option<Quantity>,
132+
pub max: Option<Quantity>,
133133
}
134134

135135
// Struct that exposes the values for a PVC which the user should be able to influence
136136
#[derive(Clone, Debug, Default, Deserialize, Merge, JsonSchema, PartialEq, Serialize)]
137137
#[merge(path_overrides(merge = "crate::config::merge"))]
138138
#[serde(rename_all = "camelCase")]
139139
pub struct PvcConfig {
140-
pub capacity: Option<String>,
140+
pub capacity: Option<Quantity>,
141141
#[serde(default, skip_serializing_if = "Option::is_none")]
142142
pub storage_class: Option<String>,
143143
#[serde(default, skip_serializing_if = "Option::is_none")]
@@ -160,7 +160,7 @@ impl PvcConfig {
160160
requests: Some({
161161
let mut map = BTreeMap::new();
162162
if let Some(capacity) = &self.capacity {
163-
map.insert("storage".to_string(), Quantity(capacity.to_string()));
163+
map.insert("storage".to_string(), capacity.clone());
164164
}
165165
map
166166
}),
@@ -184,15 +184,15 @@ where
184184
let mut limits = BTreeMap::new();
185185
let mut requests = BTreeMap::new();
186186
if let Some(memory_limit) = self.memory.limit {
187-
limits.insert("memory".to_string(), Quantity(memory_limit.to_string()));
188-
requests.insert("memory".to_string(), Quantity(memory_limit));
187+
limits.insert("memory".to_string(), memory_limit.clone());
188+
requests.insert("memory".to_string(), memory_limit);
189189
}
190190

191191
if let Some(cpu_max) = self.cpu.max {
192-
limits.insert("cpu".to_string(), Quantity(cpu_max));
192+
limits.insert("cpu".to_string(), cpu_max);
193193
}
194194
if let Some(cpu_min) = self.cpu.min {
195-
requests.insert("cpu".to_string(), Quantity(cpu_min));
195+
requests.insert("cpu".to_string(), cpu_min);
196196
}
197197

198198
ResourceRequirements {

src/config/merge.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use k8s_openapi::apimachinery::pkg::apis::meta::v1::LabelSelector;
1+
use k8s_openapi::apimachinery::pkg::{api::resource::Quantity, apis::meta::v1::LabelSelector};
22
use std::{
33
collections::{btree_map, hash_map, BTreeMap, HashMap},
44
hash::Hash,
@@ -131,6 +131,7 @@ impl Atomic for i128 {}
131131
impl Atomic for isize {}
132132
impl Atomic for bool {}
133133
impl Atomic for String {}
134+
impl Atomic for Quantity {}
134135
impl<'a> Atomic for &'a str {}
135136
impl Atomic for LabelSelector {}
136137

0 commit comments

Comments
 (0)