1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
use super::Result;

#[derive(Serialize, Deserialize, Clone, Default, Debug)]
#[cfg_attr(feature = "filesystem", serde(deny_unknown_fields))]
pub struct HttpGet {
    /// Uri path to GET (i.e. / or /health)
    pub path: String,
    /// Port name (i.e. http or http-health)
    #[serde(default = "http_get_default_port")]
    pub port: String,
    /// Headers to set
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub httpHeaders: Vec<HttpHeader>,
}
fn http_get_default_port() -> String {
    "http".into()
}

#[derive(Serialize, Deserialize, Clone, Default, Debug)]
#[cfg_attr(feature = "filesystem", serde(deny_unknown_fields))]
pub struct HttpHeader {
    pub name: String,
    pub value: String,
}

#[derive(Serialize, Deserialize, Clone, Default, Debug)]
#[cfg_attr(feature = "filesystem", serde(deny_unknown_fields))]
pub struct Exec {
    /// Command to execute in the container
    pub command: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, Default, Debug)]
#[cfg_attr(feature = "filesystem", serde(deny_unknown_fields))]
pub struct TcpSocket {
    pub port: String,
}

/// Liveness or readiness Probe
#[derive(Serialize, Deserialize, Clone, Default, Debug)]
#[cfg_attr(feature = "filesystem", serde(deny_unknown_fields))]
pub struct Probe {
    /// Http Get probe
    #[serde(default, skip_serializing_if = "Option::is_none")]
    httpGet: Option<HttpGet>,

    /// Shell exec probe
    #[serde(default, skip_serializing_if = "Option::is_none")]
    exec: Option<Exec>,

    /// Tcp Socket probe
    #[serde(default, skip_serializing_if = "Option::is_none")]
    tcpSocket: Option<TcpSocket>,

    /// How long to wait before kube performs first probe
    #[serde(default = "initial_delay_seconds_default")]
    pub initialDelaySeconds: u32,

    /// How long between each probe
    #[serde(default = "period_seconds_default")]
    pub periodSeconds: u32,

    /// Min consecutive successes before considering a failed probe successful
    #[serde(default = "success_threshold_default")]
    pub successThreshold: u32,

    /// Min consecutive failures before considering a probe failed
    #[serde(default = "failure_threshold_default")]
    pub failureThreshold: u32,

    /// Number of seconds after which the probe times out
    #[serde(default = "timeout_seconds_default")]
    pub timeoutSeconds: u32,
}

// 5 is kube standard delay default, we set it a little higher
fn initial_delay_seconds_default() -> u32 {
    30
}
// how frequently to poll
fn period_seconds_default() -> u32 {
    5
}
// Default values from Kubernetes
fn success_threshold_default() -> u32 {
    1
}
fn failure_threshold_default() -> u32 {
    3
}
fn timeout_seconds_default() -> u32 {
    1
}

impl Probe {
    pub fn verify(&self) -> Result<()> {
        if self.httpGet.is_some() && (self.exec.is_some() || self.tcpSocket.is_some()) {
            bail!("Probe needs to have at most one of 'httpGet' or 'exec'");
        }
        if self.httpGet.is_none() && self.exec.is_none() && self.tcpSocket.is_none() {
            bail!("Probe needs to define one of 'httpGet', 'exec', 'tcpSocket");
        }
        Ok(())
    }
}