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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use super::Result;
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(untagged)]
pub enum AvailabilityPolicy {
Percentage(String),
Unsigned(u32),
}
impl AvailabilityPolicy {
fn verify(&self, name: &str, maxNumber: u32) -> Result<()> {
match self {
AvailabilityPolicy::Unsigned(n) => {
if *n > maxNumber {
bail!("Cannot have {} set higher than replicaCount {}", name, maxNumber);
}
}
AvailabilityPolicy::Percentage(s) => {
if !s.ends_with('%') {
bail!("{} must end with a '%' sign", name);
}
let digits = s.chars().take_while(|ch| *ch != '%').collect::<String>();
let res: u32 = digits.parse()?;
if res > 100 {
bail!("Percentage value for {} cannot exceed 100", name);
}
}
}
Ok(())
}
fn to_replicas_ceil(&self, replicas: u32) -> u32 {
match self {
AvailabilityPolicy::Percentage(percstr) => {
let digits = percstr.chars().take_while(|ch| *ch != '%').collect::<String>();
let surgeperc: u32 = digits.parse().unwrap();
((f64::from(replicas) * f64::from(surgeperc)) / 100.0).ceil() as u32
}
AvailabilityPolicy::Unsigned(u) => *u,
}
}
fn to_replicas_floor(&self, replicas: u32) -> u32 {
match self {
AvailabilityPolicy::Percentage(percstr) => {
let digits = percstr.chars().take_while(|ch| *ch != '%').collect::<String>();
let surgeperc: u32 = digits.parse().unwrap();
((f64::from(replicas) * f64::from(surgeperc)) / 100.0).floor() as u32
}
AvailabilityPolicy::Unsigned(u) => *u,
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct RollingUpdate {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub maxUnavailable: Option<AvailabilityPolicy>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub maxSurge: Option<AvailabilityPolicy>,
}
impl Default for RollingUpdate {
fn default() -> Self {
RollingUpdate {
maxUnavailable: Some(AvailabilityPolicy::Percentage(25.to_string())),
maxSurge: Some(AvailabilityPolicy::Percentage(25.to_string())),
}
}
}
impl RollingUpdate {
pub fn verify(&self, replicas: u32) -> Result<()> {
if self.maxUnavailable.is_none() && self.maxSurge.is_none() {
bail!("Need to set one of maxUnavailable or maxSurge in rollingUpdate");
}
if let Some(ref ma) = &self.maxUnavailable {
ma.verify("maxUnavailable", replicas)?;
}
if let Some(ref mu) = &self.maxSurge {
mu.verify("maxSurge", replicas)?;
}
Ok(())
}
}
impl RollingUpdate {
pub fn rollout_iterations(&self, replicas: u32) -> u32 {
let surge = if let Some(surge) = self.maxSurge.clone() {
surge.to_replicas_ceil(replicas)
} else {
(f64::from(replicas * 25) / 100.0).ceil() as u32
};
let unavail = if let Some(unav) = self.maxUnavailable.clone() {
unav.to_replicas_floor(replicas)
} else {
(f64::from(replicas * 25) / 100.0).floor() as u32
};
let mut newrs = 0;
let mut oldrs = replicas;
let mut iters = 0;
trace!(
"rollout iterations for {} replicas, surge={},unav={}",
replicas,
surge,
unavail
);
while newrs < replicas {
oldrs -= oldrs + newrs - replicas;
let total = newrs + oldrs;
let unavail_safe = if total <= unavail { 0 } else { unavail };
trace!(
"oldrs{}, total is {}, unavail_safe: {}",
oldrs,
total,
unavail_safe
);
oldrs -= std::cmp::min(oldrs, unavail_safe);
newrs += unavail_safe;
newrs += surge;
iters += 1;
trace!("rollout iter {}: old={}, new={}", iters, oldrs, newrs);
}
trace!("rollout iters={}", iters);
iters
}
pub fn rollout_iterations_default(replicas: u32) -> u32 {
((f64::from(replicas) * 25.0) / 100.0).ceil() as u32
}
}
#[cfg(test)]
mod tests {
use super::{AvailabilityPolicy, RollingUpdate};
#[test]
fn rollout_iteration_no_overflow() {
for i in 0..100 {
assert!(RollingUpdate::default().rollout_iterations(i) < 5);
}
}
#[test]
fn rollout_iteration_check() {
let ru = RollingUpdate::default();
assert_eq!(ru.rollout_iterations(1), 1);
assert_eq!(ru.rollout_iterations(2), 2);
assert_eq!(ru.rollout_iterations(4), 2);
assert_eq!(ru.rollout_iterations(8), 2);
let rusurge = RollingUpdate {
maxUnavailable: Some(AvailabilityPolicy::Percentage("25%".to_string())),
maxSurge: Some(AvailabilityPolicy::Percentage("50%".to_string())),
};
assert_eq!(rusurge.rollout_iterations(8), 2);
assert_eq!(rusurge.rollout_iterations(16), 2);
let rusurge = RollingUpdate {
maxUnavailable: Some(AvailabilityPolicy::Percentage("75%".to_string())),
maxSurge: Some(AvailabilityPolicy::Percentage("25%".to_string())),
};
assert_eq!(rusurge.rollout_iterations(8), 1);
let rusurge = RollingUpdate {
maxUnavailable: Some(AvailabilityPolicy::Percentage("25%".to_string())),
maxSurge: Some(AvailabilityPolicy::Percentage("0%".to_string())),
};
assert_eq!(rusurge.rollout_iterations(8), 4);
}
}