@@ -7,9 +7,11 @@ use crate::time::Duration;
7
7
8
8
#[ derive( Debug ) ]
9
9
pub struct ProbeBuilder < Action , Period > {
10
+ // Mandatory field
10
11
action : Action ,
11
12
period : Period ,
12
13
14
+ // Fields with defaults
13
15
success_threshold : i32 ,
14
16
failure_threshold : i32 ,
15
17
timeout : Duration ,
@@ -22,7 +24,7 @@ impl Default for ProbeBuilder<(), ()> {
22
24
Self {
23
25
action : ( ) ,
24
26
period : ( ) ,
25
- // The following values match the Kubernetes default
27
+ // The following values match the Kubernetes defaults
26
28
success_threshold : 1 ,
27
29
failure_threshold : 1 ,
28
30
timeout : Duration :: from_secs ( 1 ) ,
@@ -64,10 +66,10 @@ impl<Period> ProbeBuilder<(), Period> {
64
66
pub fn with_http_get_action_helper (
65
67
self ,
66
68
port : u16 ,
67
- path : Option < impl Into < String > > ,
69
+ path : Option < String > ,
68
70
) -> ProbeBuilder < ProbeAction , Period > {
69
71
self . with_http_get_action ( HTTPGetAction {
70
- path : path . map ( Into :: into ) ,
72
+ path,
71
73
port : IntOrString :: Int ( port. into ( ) ) ,
72
74
..Default :: default ( )
73
75
} )
@@ -138,12 +140,6 @@ impl ProbeBuilder<ProbeAction, ()> {
138
140
}
139
141
}
140
142
141
- // success_threshold: i32,
142
- // failure_threshold: i32,
143
- // timeout: Duration,
144
- // initial_delay: Duration,
145
- // termination_grace_period: Duration,
146
-
147
143
impl ProbeBuilder < ProbeAction , Duration > {
148
144
/// How often the probe must succeed before being considered successful.
149
145
pub fn with_success_threshold ( mut self , success_threshold : i32 ) -> Self {
@@ -169,6 +165,21 @@ impl ProbeBuilder<ProbeAction, Duration> {
169
165
self
170
166
}
171
167
168
+ pub fn with_timeout ( mut self , timeout : Duration ) -> Self {
169
+ self . timeout = timeout;
170
+ self
171
+ }
172
+
173
+ pub fn with_initial_delay ( mut self , initial_delay : Duration ) -> Self {
174
+ self . initial_delay = initial_delay;
175
+ self
176
+ }
177
+
178
+ pub fn with_termination_grace_period ( mut self , termination_grace_period : Duration ) -> Self {
179
+ self . termination_grace_period = termination_grace_period;
180
+ self
181
+ }
182
+
172
183
/// The duration the probe needs to fail before being considered failed.
173
184
///
174
185
/// This internally calculates the needed failure threshold based on the period and passes that
@@ -231,11 +242,32 @@ mod tests {
231
242
use super :: * ;
232
243
233
244
#[ test]
234
- fn test_probe_builder ( ) {
245
+ fn test_probe_builder_minimal ( ) {
246
+ let probe = ProbeBuilder :: default ( )
247
+ . with_http_get_action_helper ( 8080 , None )
248
+ . with_period ( Duration :: from_secs ( 10 ) )
249
+ . build ( ) ;
250
+
251
+ assert_eq ! (
252
+ probe. http_get,
253
+ Some ( HTTPGetAction {
254
+ port: IntOrString :: Int ( 8080 ) ,
255
+ ..Default :: default ( )
256
+ } )
257
+ ) ;
258
+ assert_eq ! ( probe. period_seconds, Some ( 10 ) ) ;
259
+ }
260
+
261
+ #[ test]
262
+ fn test_probe_builder_complex ( ) {
235
263
let probe = ProbeBuilder :: default ( )
236
264
. with_exec_action_helper ( [ "sleep" , "1" ] )
237
265
. with_period ( Duration :: from_secs ( 5 ) )
266
+ . with_success_threshold ( 2 )
238
267
. with_failure_threshold_duration ( Duration :: from_secs ( 33 ) )
268
+ . with_timeout ( Duration :: from_secs ( 3 ) )
269
+ . with_initial_delay ( Duration :: from_secs ( 7 ) )
270
+ . with_termination_grace_period ( Duration :: from_secs ( 4 ) )
239
271
. build ( ) ;
240
272
241
273
assert_eq ! (
@@ -247,12 +279,12 @@ mod tests {
247
279
failure_threshold: Some ( 7 ) ,
248
280
grpc: None ,
249
281
http_get: None ,
250
- initial_delay_seconds: Some ( 0 ) ,
282
+ initial_delay_seconds: Some ( 7 ) ,
251
283
period_seconds: Some ( 5 ) ,
252
- success_threshold: Some ( 1 ) ,
284
+ success_threshold: Some ( 2 ) ,
253
285
tcp_socket: None ,
254
- termination_grace_period_seconds: Some ( 0 ) ,
255
- timeout_seconds: Some ( 1 ) ,
286
+ termination_grace_period_seconds: Some ( 4 ) ,
287
+ timeout_seconds: Some ( 3 ) ,
256
288
}
257
289
) ;
258
290
}
0 commit comments