Skip to content

Commit

Permalink
controller: replaced parse_duration package with custom parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mjsterckx committed Oct 13, 2022
1 parent bdcffc9 commit 8e82cc9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
1 change: 0 additions & 1 deletion controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ kube-runtime = "0.75"
lazy_static = "1"
log = "0.4"
model = { version = "0.0.2", path = "../model" }
parse_duration = "2.1"
schemars = "=0.8.10"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
Expand Down
19 changes: 19 additions & 0 deletions controller/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use kube_runtime::controller::Action;
use std::collections::VecDeque;
use std::num::ParseIntError;
use std::time::Duration;

/// Tell the controller to reconcile the object again after some duration.
Expand All @@ -15,3 +17,20 @@ pub(crate) fn requeue_slow() -> Action {
pub(crate) fn no_requeue() -> Action {
Action::await_change()
}

/// Parse a duration string into a Duration object.
pub(crate) fn parse_duration(input: &str) -> Result<Duration, ParseIntError> {
let mut secs: u64 = 0;
let mut duration_string = input;
let units = vec![('d', 86400), ('h', 3600), ('m', 60)];
for unit in units {
let mut vec: VecDeque<&str> = duration_string.split(unit.0).collect();
if vec.len() > 1 {
secs += vec.pop_front().unwrap_or("0").parse::<u64>()? * unit.1;
}
duration_string = vec.pop_front().unwrap_or("0");
}
let mut vec: VecDeque<&str> = duration_string.split('s').collect();
secs += vec.pop_front().unwrap_or("0").parse::<u64>().unwrap_or(0);
Ok(Duration::from_secs(secs))
}
6 changes: 3 additions & 3 deletions controller/src/resource_controller/action.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::constants::parse_duration;
use crate::error::Result;
use crate::job::{JobState, TEST_START_TIME_LIMIT};
use crate::resource_controller::context::ResourceInterface;
Expand All @@ -7,7 +8,6 @@ use log::{debug, trace};
use model::clients::{AllowNotFound, CrdClient, TestClient};
use model::constants::{FINALIZER_CREATION_JOB, FINALIZER_MAIN, FINALIZER_RESOURCE};
use model::{CrdExt, DestructionPolicy, ResourceAction, TaskState, TestUserState};
use parse_duration::parse;

/// The action that the controller needs to take in order to reconcile the [`Resource`].
#[derive(Debug, Clone, Eq, PartialEq)]
Expand Down Expand Up @@ -155,7 +155,7 @@ async fn creation_not_done_action(
.agent
.timeout
.as_ref()
.map(|timeout| parse(timeout).map(|timeout| std_duration > timeout))
.map(|timeout| parse_duration(timeout).map(|timeout| std_duration > timeout))
.unwrap_or(Ok(false))
.unwrap_or(false)
{
Expand Down Expand Up @@ -299,7 +299,7 @@ async fn destruction_not_done_action(
.agent
.timeout
.as_ref()
.map(|timeout| parse(timeout).map(|timeout| std_duration > timeout))
.map(|timeout| parse_duration(timeout).map(|timeout| std_duration > timeout))
.unwrap_or(Ok(false))
.unwrap_or(false)
{
Expand Down
4 changes: 2 additions & 2 deletions controller/src/test_controller/action.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::constants::parse_duration;
use crate::error::Result;
use crate::job::{JobState, TEST_START_TIME_LIMIT};
use crate::test_controller::context::TestInterface;
Expand All @@ -7,7 +8,6 @@ use log::trace;
use model::clients::{CrdClient, HttpStatusCode, StatusCode};
use model::constants::{FINALIZER_MAIN, FINALIZER_TEST_JOB, NAMESPACE};
use model::{CrdExt, Outcome, Resource, ResourceAction, TaskState};
use parse_duration::parse;
use std::fmt::{Display, Formatter};

/// The action that the controller needs to take in order to reconcile the `Test`.
Expand Down Expand Up @@ -219,7 +219,7 @@ async fn task_not_done_action(t: &TestInterface, is_task_state_running: bool) ->
.agent
.timeout
.as_ref()
.map(|timeout| parse(timeout).map(|timeout| std_duration > timeout))
.map(|timeout| parse_duration(timeout).map(|timeout| std_duration > timeout))
.unwrap_or(Ok(false))
.unwrap_or(false)
{
Expand Down

0 comments on commit 8e82cc9

Please sign in to comment.