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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//! A module for use in managing volumes in providers. Use of this module is not mandatory to create
//! a Provider, but it does provide common implementation logic for supported volume providers.
use std::collections::HashMap;
use std::ops::Deref;
use std::path::PathBuf;

use k8s_openapi::api::core::v1::Volume as KubeVolume;
use k8s_openapi::api::core::v1::{ConfigMap, KeyToPath, Secret};
use k8s_openapi::ByteString;
use kube::api::Api;
use log::{debug, error};

use crate::pod::Pod;

#[derive(Debug)]
enum Type {
    ConfigMap,
    Secret,
    HostPath,
}

/// A smart wrapper around the location of a volume on the host system. If this is a ConfigMap or
/// Secret volume, dropping this reference will clean up the temporary volume. [AsRef] and
/// [std::ops::Deref] are implemented for this type so you can still use it like a normal PathBuf
#[derive(Debug)]
pub struct Ref {
    host_path: PathBuf,
    volume_type: Type,
}

impl Ref {
    /// Resolves the volumes for a pod, including preparing temporary directories containing the
    /// contents of secrets and configmaps. Returns a HashMap of volume names to a PathBuf for the
    /// directory where the volume is mounted
    pub async fn volumes_from_pod(
        volume_dir: &PathBuf,
        pod: &Pod,
        client: &kube::Client,
    ) -> anyhow::Result<HashMap<String, Self>> {
        let base_path = volume_dir.join(pod_dir_name(pod));
        tokio::fs::create_dir_all(&base_path).await?;
        if let Some(vols) = pod.volumes() {
            let volumes = vols.iter().map(|v| {
                let mut host_path = base_path.clone();
                host_path.push(&v.name);
                async move {
                    let volume_type = configure(v, pod.namespace(), client, &host_path).await?;
                    Ok((
                        v.name.to_owned(),
                        // Every other volume type should mount to the given host_path except for a
                        // hostpath volume type. So we need to handle that special case here
                        match &v.host_path {
                            Some(hostpath) => Ref {
                                host_path: PathBuf::from(&hostpath.path),
                                volume_type,
                            },
                            None => Ref {
                                host_path,
                                volume_type,
                            },
                        },
                    ))
                }
            });
            futures::future::join_all(volumes)
                .await
                .into_iter()
                .collect()
        } else {
            Ok(HashMap::default())
        }
    }
}

impl AsRef<PathBuf> for Ref {
    fn as_ref(&self) -> &PathBuf {
        &self.host_path
    }
}

impl Deref for Ref {
    type Target = PathBuf;

    fn deref(&self) -> &Self::Target {
        &self.host_path
    }
}

impl Drop for Ref {
    fn drop(&mut self) {
        if matches!(self.volume_type, Type::ConfigMap | Type::Secret) {
            // TODO: Currently there is no way to do this async (though there is an async destructors proposal)
            debug!(
                "deleting {:?} directory {:?}",
                self.volume_type, self.host_path
            );
            std::fs::remove_dir_all(&self.host_path).unwrap_or_else(|e| {
                error!(
                    "unable to delete directory {:?} on volume cleanup: {:?}",
                    self.host_path, e
                )
            });
        }
    }
}

/// This is a gnarly function to check all of the supported data members of the Volume struct.
/// Because it isn't a HashMap, we need to check all fields individually
async fn configure(
    vol: &KubeVolume,
    namespace: &str,
    client: &kube::Client,
    path: &PathBuf,
) -> anyhow::Result<Type> {
    if let Some(cm) = &vol.config_map {
        populate_from_config_map(
            &cm.name
                .as_ref()
                .ok_or_else(|| anyhow::anyhow!("no configmap name was given"))?,
            namespace,
            client,
            path,
            &cm.items,
        )
        .await
    } else if let Some(s) = &vol.secret {
        populate_from_secret(
            &s.secret_name
                .as_ref()
                .ok_or_else(|| anyhow::anyhow!("no secret name was given"))?,
            namespace,
            client,
            path,
            &s.items,
        )
        .await
    } else if let Some(hostpath) = &vol.host_path {
        // Check the the directory exists on the host
        tokio::fs::metadata(&hostpath.path).await?;
        Ok(Type::HostPath)
    } else {
        Err(anyhow::anyhow!(
            "Unsupported volume type. Currently supported types: ConfigMap, Secret, and HostPath"
        ))
    }
}

async fn populate_from_secret(
    name: &str,
    namespace: &str,
    client: &kube::Client,
    path: &PathBuf,
    items: &Option<Vec<KeyToPath>>,
) -> anyhow::Result<Type> {
    tokio::fs::create_dir_all(path).await?;
    let secret_client: Api<Secret> = Api::namespaced(client.clone(), namespace);
    let secret = secret_client.get(name).await?;
    let data = secret.data.unwrap_or_default();
    let data = data.iter().map(|(key, ByteString(data))| async move {
        match mount_setting_for(key, items) {
            ItemMount::MountAt(mount_path) => {
                let file_path = path.join(mount_path);
                tokio::fs::write(file_path, &data).await
            }
            ItemMount::DoNotMount => Ok(()),
        }
    });
    futures::future::join_all(data)
        .await
        .into_iter()
        .collect::<tokio::io::Result<_>>()?;

    Ok(Type::Secret)
}

async fn populate_from_config_map(
    name: &str,
    namespace: &str,
    client: &kube::Client,
    path: &PathBuf,
    items: &Option<Vec<KeyToPath>>,
) -> anyhow::Result<Type> {
    tokio::fs::create_dir_all(path).await?;
    let cm_client: Api<ConfigMap> = Api::namespaced(client.clone(), namespace);
    let config_map = cm_client.get(name).await?;
    let binary_data = config_map.binary_data.unwrap_or_default();
    let binary_data = binary_data.iter().map(|(key, data)| async move {
        match mount_setting_for(key, items) {
            ItemMount::MountAt(mount_path) => {
                let file_path = path.join(mount_path);
                tokio::fs::write(file_path, &data.0).await
            }
            ItemMount::DoNotMount => Ok(()),
        }
    });
    let binary_data = futures::future::join_all(binary_data);
    let data = config_map.data.unwrap_or_default();
    let data = data.iter().map(|(key, data)| async move {
        match mount_setting_for(key, items) {
            ItemMount::MountAt(mount_path) => {
                let file_path = path.join(mount_path);
                tokio::fs::write(file_path, data).await
            }
            ItemMount::DoNotMount => Ok(()),
        }
    });
    let data = futures::future::join_all(data);
    let (binary_data, data) = futures::future::join(binary_data, data).await;
    binary_data
        .into_iter()
        .chain(data)
        .collect::<tokio::io::Result<_>>()?;

    Ok(Type::ConfigMap)
}

fn pod_dir_name(pod: &Pod) -> String {
    format!("{}-{}", pod.name(), pod.namespace())
}

fn mount_setting_for(key: &str, items_to_mount: &Option<Vec<KeyToPath>>) -> ItemMount {
    match items_to_mount {
        None => ItemMount::MountAt(key.to_string()),
        Some(items) => ItemMount::from(
            items
                .iter()
                .find(|kp| kp.key == key)
                .map(|kp| kp.path.to_string()),
        ),
    }
}

enum ItemMount {
    MountAt(String),
    DoNotMount,
}

impl From<Option<String>> for ItemMount {
    fn from(option: Option<String>) -> Self {
        match option {
            None => ItemMount::DoNotMount,
            Some(path) => ItemMount::MountAt(path),
        }
    }
}