|
| 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 | +} |
0 commit comments