@@ -4,6 +4,7 @@ pub mod security;
4
4
pub mod volume;
5
5
6
6
use std:: collections:: BTreeMap ;
7
+ use std:: num:: TryFromIntError ;
7
8
8
9
use crate :: builder:: meta:: ObjectMetaBuilder ;
9
10
use crate :: commons:: affinity:: StackableAffinity ;
@@ -12,7 +13,8 @@ use crate::commons::resources::{
12
13
ComputeResource , ResourceRequirementsExt , ResourceRequirementsType , LIMIT_REQUEST_RATIO_CPU ,
13
14
LIMIT_REQUEST_RATIO_MEMORY ,
14
15
} ;
15
- use crate :: error:: { Error , OperatorResult } ;
16
+ use crate :: duration:: Duration ;
17
+ use crate :: error:: { self , OperatorResult } ;
16
18
17
19
use super :: { ListenerOperatorVolumeSourceBuilder , ListenerReference , VolumeBuilder } ;
18
20
use k8s_openapi:: {
@@ -25,8 +27,18 @@ use k8s_openapi::{
25
27
} ;
26
28
use tracing:: warn;
27
29
30
+ #[ derive( Debug , thiserror:: Error ) ]
31
+ pub enum Error {
32
+ #[ error( "termination grace period is too long (got {duration}, maximum allowed is {max})" , max = Duration :: from_secs( i64 :: MAX as u64 ) ) ]
33
+ TerminationGracePeriodTooLong {
34
+ source : TryFromIntError ,
35
+ duration : Duration ,
36
+ } ,
37
+ }
38
+ pub type Result < T > = std:: result:: Result < T , Error > ;
39
+
28
40
/// A builder to build [`Pod`] or [`PodTemplateSpec`] objects.
29
- #[ derive( Clone , Default ) ]
41
+ #[ derive( Clone , Debug , Default , PartialEq ) ]
30
42
pub struct PodBuilder {
31
43
containers : Vec < Container > ,
32
44
host_network : Option < bool > ,
@@ -452,19 +464,27 @@ impl PodBuilder {
452
464
self
453
465
}
454
466
455
- pub fn termination_grace_period_seconds (
467
+ pub fn termination_grace_period (
456
468
& mut self ,
457
- termination_grace_period_seconds : i64 ,
458
- ) -> & mut Self {
469
+ termination_grace_period : & Duration ,
470
+ ) -> Result < & mut Self > {
471
+ let termination_grace_period_seconds = termination_grace_period
472
+ . as_secs ( )
473
+ . try_into ( )
474
+ . map_err ( |err| Error :: TerminationGracePeriodTooLong {
475
+ source : err,
476
+ duration : * termination_grace_period,
477
+ } ) ?;
478
+
459
479
self . termination_grace_period_seconds = Some ( termination_grace_period_seconds) ;
460
- self
480
+ Ok ( self )
461
481
}
462
482
463
483
/// Consumes the Builder and returns a constructed [`Pod`]
464
484
pub fn build ( & self ) -> OperatorResult < Pod > {
465
485
Ok ( Pod {
466
486
metadata : match self . metadata {
467
- None => return Err ( Error :: MissingObjectKey { key : "metadata" } ) ,
487
+ None => return Err ( error :: Error :: MissingObjectKey { key : "metadata" } ) ,
468
488
Some ( ref metadata) => metadata. clone ( ) ,
469
489
} ,
470
490
spec : Some ( self . build_spec ( ) ) ,
@@ -637,7 +657,8 @@ mod tests {
637
657
. with_config_map ( "configmap" )
638
658
. build ( ) ,
639
659
)
640
- . termination_grace_period_seconds ( 42 )
660
+ . termination_grace_period ( & Duration :: from_secs ( 42 ) )
661
+ . unwrap ( )
641
662
. build ( )
642
663
. unwrap ( ) ;
643
664
@@ -691,4 +712,19 @@ mod tests {
691
712
. unwrap ( ) ;
692
713
assert_eq ! ( pod. spec. unwrap( ) . restart_policy. unwrap( ) , "Always" ) ;
693
714
}
715
+
716
+ #[ test]
717
+ fn test_pod_builder_too_long_termination_grace_period ( ) {
718
+ let too_long_duration = Duration :: from_secs ( i64:: MAX as u64 + 1 ) ;
719
+ let mut pod_builder = PodBuilder :: new ( ) ;
720
+
721
+ let result = pod_builder. termination_grace_period ( & too_long_duration) ;
722
+ assert ! ( matches!(
723
+ result,
724
+ Err ( Error :: TerminationGracePeriodTooLong {
725
+ source: TryFromIntError { .. } ,
726
+ duration,
727
+ } ) if duration == too_long_duration
728
+ ) ) ;
729
+ }
694
730
}
0 commit comments