Skip to content

Commit 49cc989

Browse files
committed
feat(stackable-operator): Add EoS checker skeleton
1 parent 69bd3d6 commit 49cc989

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
use chrono::{DateTime, Utc};
2+
use stackable_shared::time::Duration;
3+
4+
/// Available options to configure a [`EndOfSupportChecker`].
5+
///
6+
/// Additionally, this struct can be used as operator CLI arguments. This functionality is only
7+
/// available if the feature `clap` is enabled.
8+
#[cfg_attr(feature = "clap", derive(clap::Args))]
9+
#[derive(Debug, PartialEq, Eq)]
10+
pub struct EndOfSupportOptions {
11+
/// The end-of-support check mode. Currently, only "offline" is supported.
12+
#[arg(
13+
long = "eos-check-mode",
14+
env = "EOS_CHECK_MODE",
15+
default_value_t = EndOfSupportCheckMode::default(),
16+
value_enum
17+
)]
18+
pub check_mode: EndOfSupportCheckMode,
19+
20+
/// The interval in which the end-of-support check should run.
21+
#[arg(
22+
long = "eos-interval",
23+
env = "EOS_INTERVAL",
24+
default_value_t = Self::default_interval()
25+
)]
26+
pub interval: Duration,
27+
28+
/// The support duration (how long the operator should be considered supported after
29+
/// it's built-date).
30+
///
31+
/// This field is currently not exposed as a CLI argument or environment variable. The default
32+
/// value is provided by [`Self::default_support_duration()`].
33+
#[arg(skip = Self::default_support_duration())]
34+
pub support_duration: Duration,
35+
}
36+
37+
impl EndOfSupportOptions {
38+
fn default_interval() -> Duration {
39+
Duration::from_days_unchecked(1)
40+
}
41+
42+
fn default_support_duration() -> Duration {
43+
Duration::from_days_unchecked(365)
44+
}
45+
}
46+
47+
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
48+
#[derive(Clone, Debug, Default, PartialEq, Eq)]
49+
pub enum EndOfSupportCheckMode {
50+
#[default]
51+
Offline,
52+
}
53+
54+
pub enum Error {}
55+
56+
pub struct EndOfSupportChecker {
57+
check_mode: EndOfSupportCheckMode,
58+
datetime: DateTime<Utc>,
59+
interval: Duration,
60+
}
61+
62+
impl EndOfSupportChecker {
63+
/// Creates and returns a new end-of-support checker.
64+
pub fn new(built_time: &str, options: EndOfSupportOptions) -> Result<Self, Error> {
65+
let EndOfSupportOptions {
66+
check_mode,
67+
interval,
68+
support_duration,
69+
} = options;
70+
71+
// Parse the built-time from the RFC2822-encoded string and add the support duration to it.
72+
// This is datetime marks the end-of-support date.
73+
let datetime =
74+
DateTime::parse_from_rfc2822(built_time).unwrap().to_utc() + *support_duration;
75+
76+
Ok(Self {
77+
check_mode,
78+
datetime,
79+
interval,
80+
})
81+
}
82+
83+
/// Run the end-of-support checker.
84+
pub async fn run(self) {
85+
// Construct an interval which can be polled.
86+
let mut interval = tokio::time::interval(self.interval.into());
87+
88+
loop {
89+
// TODO: Add way to stop from the outside
90+
// The first tick ticks immediately.
91+
interval.tick().await;
92+
93+
if !self.is_eos() {
94+
continue;
95+
}
96+
97+
match self.check_mode {
98+
EndOfSupportCheckMode::Offline => todo!(),
99+
}
100+
}
101+
}
102+
103+
/// Returns if the operator is considered as end-of-support based on the built-time and the
104+
/// support duration.
105+
fn is_eos(&self) -> bool {
106+
let now = Utc::now();
107+
now > self.datetime
108+
}
109+
}

crates/stackable-operator/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub mod config;
1515
pub mod constants;
1616
pub mod cpu;
1717
pub mod crd;
18+
pub mod eos;
1819
pub mod helm;
1920
pub mod iter;
2021
pub mod kvp;

0 commit comments

Comments
 (0)