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
use std::fs::read_to_string;
use std::path::Path;

use tracing::debug;
use tracing::error;
use tracing::trace;

const BASE_DIR: &str = "/var/run/secrets/kubernetes.io/serviceaccount";
const API_SERVER: &str = "https://kubernetes.default.svc";

///var/run/secrets/kubernetes.io/serviceaccount

/// Configuration as Pod
#[derive(Debug, Default, Clone)]
pub struct PodConfig {
    pub namespace: String,
    pub token: String,
}

impl PodConfig {
    pub fn load() -> Option<Self> {
        // first try to see if this base dir account exists, otherwise return non
        let path = Path::new(BASE_DIR);
        if !path.exists() {
            debug!(
                "pod config dir: {} is not founded, skipping pod config",
                BASE_DIR
            );
            return None;
        }

        let namespace = read_file("namespace")?;
        let token = read_file("token")?;

        Some(Self { namespace, token })
    }

    pub fn api_path(&self) -> &'static str {
        API_SERVER
    }

    /// path to CA certificate
    pub fn ca_path(&self) -> String {
        format!("{}/{}", BASE_DIR, "ca.crt")
    }
}

// read file
fn read_file(name: &str) -> Option<String> {
    let full_path = format!("{}/{}", BASE_DIR, name);
    match read_to_string(&full_path) {
        Ok(value) => Some(value),
        Err(err) => {
            error!("no {} founded as pod in {}", name, full_path);
            trace!("unable to read pod: {} value: {}", name, err);
            None
        }
    }
}