k8s_openapi_ext/ext/
toleration.rs

1use super::*;
2
3pub trait TolerationExt {
4    fn no_schedule(key: impl ToString) -> TolerationBuilder;
5    fn prefer_no_schedule(key: impl ToString) -> TolerationBuilder;
6    fn no_execute(key: impl ToString) -> TolerationBuilder;
7    fn toleration_seconds(self, toleration_seconds: i64) -> Self;
8}
9
10impl TolerationExt for corev1::Toleration {
11    fn no_schedule(key: impl ToString) -> TolerationBuilder {
12        let effect = Some(Effect::NoSchedule);
13        let key = Some(key.to_string());
14        TolerationBuilder { effect, key }
15    }
16
17    fn prefer_no_schedule(key: impl ToString) -> TolerationBuilder {
18        let effect = Some(Effect::PreferNoSchedule);
19        let key = Some(key.to_string());
20        TolerationBuilder { effect, key }
21    }
22
23    fn no_execute(key: impl ToString) -> TolerationBuilder {
24        let effect = Some(Effect::NoExecute);
25        let key = Some(key.to_string());
26        TolerationBuilder { effect, key }
27    }
28
29    fn toleration_seconds(self, toleration_seconds: i64) -> Self {
30        Self {
31            toleration_seconds: Some(toleration_seconds),
32            ..self
33        }
34    }
35}
36
37#[derive(Debug)]
38#[must_use]
39pub struct TolerationBuilder {
40    effect: Option<Effect>,
41    key: Option<String>,
42}
43
44impl TolerationBuilder {
45    pub fn equal(self, value: impl ToString) -> corev1::Toleration {
46        corev1::Toleration {
47            effect: self.effect.map(|effect| effect.to_string()),
48            key: self.key,
49            operator: Some(Operator::Equal.to_string()),
50            toleration_seconds: None,
51            value: Some(value.to_string()),
52        }
53    }
54
55    pub fn exists(self) -> corev1::Toleration {
56        corev1::Toleration {
57            effect: self.effect.map(|effect| effect.as_str().into()),
58            key: self.key,
59            operator: Some(Operator::Exists.to_string()),
60            toleration_seconds: None,
61            value: None,
62        }
63    }
64}
65
66#[derive(Debug)]
67enum Operator {
68    Equal,
69    Exists,
70}
71
72impl Operator {
73    fn as_str(&self) -> &'static str {
74        match self {
75            Self::Equal => "Equal",
76            Self::Exists => "Exists",
77        }
78    }
79}
80
81impl fmt::Display for Operator {
82    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83        self.as_str().fmt(f)
84    }
85}