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
use super::Result;
use std::collections::BTreeMap;

// These structs contain a straight translation of kubernetes volumes
// TODO: cross reference better with
// https://kubernetes.io/docs/concepts/storage/volumes/

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct VolumeSecretItem {
    #[serde(default = "volume_key")]
    pub key: String,
    pub path: String,
    #[serde(default = "volume_default_mode")]
    pub mode: u32,
}
fn volume_key() -> String {
    "value".into()
}
fn volume_default_mode() -> u32 {
    420
} // 0o644

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct VolumeSecretDetail {
    pub secretName: String,
    pub items: Vec<VolumeSecretItem>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ProjectedVolumeSecretSourceDetail {
    pub name: String,
    pub items: Vec<VolumeSecretItem>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ProjectedVolumeSecretSource {
    pub secret: ProjectedVolumeSecretSourceDetail,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct ProjectedVolumeSecret {
    pub sources: Vec<ProjectedVolumeSecretSource>,
    // pub default_mode: u32,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct DownwardApiWrapper {
    pub items: Vec<DownwardApiItem>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DownwardApiItem {
    /// Kube path to string
    pub path: String,
    /// Specific kube paths to values
    pub resourceFieldRef: DownWardApiResource,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct DownWardApiResource {
    /// Name of container TODO: default to service name
    pub containerName: String,
    /// Raw accesssor, e.g. limits.cpu, status.podIP, etc TODO: validate
    pub resource: String,
    /// Format resource is returned in (defaults to 1 if missing), can set to 1m
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub divisor: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Volume {
    pub name: String,
    /// A projection combines multiple volume items
    #[serde(skip_serializing_if = "Option::is_none")]
    pub projected: Option<ProjectedVolumeSecret>,
    /// The secret is fetched from kube secrets and mounted as a volume
    #[serde(skip_serializing_if = "Option::is_none")]
    pub secret: Option<VolumeSecretDetail>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub emptyDir: Option<BTreeMap<String, String>>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub persistentVolumeClaim: BTreeMap<String, String>,
    /// Items from the Downward API
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub downwardAPI: Option<DownwardApiWrapper>,
}

impl Volume {
    pub fn verify(&self) -> Result<()> {
        // TODO: verify stuff here
        Ok(())
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct VolumeMount {
    pub name: String,
    pub mountPath: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub subPath: Option<String>,
    #[serde(default)]
    pub readOnly: bool,
}