-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.rs
75 lines (65 loc) · 1.86 KB
/
util.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std::ops::SubAssign;
use k8s_openapi::api::core::v1 as corev1;
use kube::api::{
Resource,
ResourceExt,
};
use kube::runtime::reflector;
use kube::Client;
use kube_quantity::ParsedQuantity;
pub struct Context {
pub client: Client,
pub node_store: reflector::Store<corev1::Node>,
}
pub struct PodResources {
pub cpu: ParsedQuantity,
pub memory: ParsedQuantity,
}
impl PodResources {
pub fn new() -> PodResources {
return PodResources {
cpu: "0".try_into().unwrap(),
memory: "0".try_into().unwrap(),
};
}
}
impl SubAssign for PodResources {
fn sub_assign(&mut self, other: Self) {
self.cpu -= other.cpu;
self.memory -= other.memory;
}
}
pub fn is_pod_bound(pod: &corev1::Pod) -> bool {
if let Some(spec) = &pod.spec {
if spec.node_name.is_some() {
return true;
}
}
return false;
}
pub fn full_name(obj: &impl Resource) -> String {
return match obj.namespace() {
Some(ns) => format!("{}/{}", ns, obj.name_any()),
None => obj.name_any().clone(),
};
}
pub fn total_pod_resources(pod: &corev1::Pod) -> PodResources {
let mut pod_resources = PodResources::new();
if let Some(spec) = &pod.spec {
for c in &spec.containers {
if let corev1::Container {
resources: Some(corev1::ResourceRequirements { requests: Some(requests), .. }),
..
} = c
{
if let Some(cpu) = requests.get("cpu") {
pod_resources.cpu += cpu.try_into().expect("invalid pod spec: cpu request");
}
if let Some(memory) = requests.get("memory") {
pod_resources.memory += memory.try_into().expect("invalid pod spec: memory request");
}
}
}
}
return pod_resources;
}