1use std::fs::read_to_string;
2use std::path::Path;
3
4use tracing::debug;
5use tracing::error;
6use tracing::trace;
7
8const BASE_DIR: &str = "/var/run/secrets/kubernetes.io/serviceaccount";
9const API_SERVER: &str = "https://kubernetes.default.svc";
10
11#[derive(Debug, Default, Clone)]
13pub struct PodConfig {
14 pub namespace: String,
15 pub token: String,
16}
17
18impl PodConfig {
19 pub fn load() -> Option<Self> {
20 let path = Path::new(BASE_DIR);
22 if !path.exists() {
23 debug!(
24 "pod config dir: {} is not found, skipping pod config",
25 BASE_DIR
26 );
27 return None;
28 }
29
30 let namespace = read_file("namespace")?;
31 let token = read_file("token")?;
32
33 Some(Self { namespace, token })
34 }
35
36 pub fn api_path(&self) -> &'static str {
37 API_SERVER
38 }
39
40 pub fn ca_path(&self) -> String {
42 format!("{}/{}", BASE_DIR, "ca.crt")
43 }
44}
45
46fn read_file(name: &str) -> Option<String> {
48 let full_path = format!("{}/{}", BASE_DIR, name);
49 match read_to_string(&full_path) {
50 Ok(value) => Some(value),
51 Err(err) => {
52 error!("no {} found as pod in {}", name, full_path);
53 trace!("unable to read pod: {} value: {}", name, err);
54 None
55 }
56 }
57}