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
use super::*;

/// Builders for `corev1::EnvVar` objects
pub trait EnvVarExt {
    /// Set an env var `name` with a given `value`
    fn value(name: impl ToString, value: impl ToString) -> Self;
    /// Set an env var `name` that points to a pod's name
    fn metadata_name(name: impl ToString) -> Self;
    fn metadata_namespace(name: impl ToString) -> Self;
    fn metadata_uid(name: impl ToString) -> Self;
    // metadata.labels\['\<KEY\>'\], metadata.annotations\['\<KEY\>'\],
    /// Set an env var `name` that points to the node which this pod was requested to scheduled on
    fn spec_nodename(name: impl ToString) -> Self;
    /// Set an env var `name` that points to the requested ServiceAccountName for this pod
    fn spec_service_account_name(name: impl ToString) -> Self;
    fn status_host_ip(name: impl ToString) -> Self;
    fn status_pod_ip(name: impl ToString) -> Self;
    fn status_pod_ips(name: impl ToString) -> Self;
    /// Set an env var `name` that points to the selected field in the pod
    fn field_ref(name: impl ToString, field_path: impl ToString) -> Self;
}

impl EnvVarExt for corev1::EnvVar {
    fn value(name: impl ToString, value: impl ToString) -> Self {
        let name = name.to_string();
        let value = Some(value.to_string());
        Self {
            name,
            value,
            value_from: None,
        }
    }

    fn metadata_name(name: impl ToString) -> Self {
        Self::field_ref(name, "metadata.name")
    }

    fn metadata_namespace(name: impl ToString) -> Self {
        Self::field_ref(name, "metadata.namespace")
    }

    fn metadata_uid(name: impl ToString) -> Self {
        Self::field_ref(name, "metadata.uid")
    }

    fn spec_nodename(name: impl ToString) -> Self {
        Self::field_ref(name, "spec.nodeName")
    }

    fn spec_service_account_name(name: impl ToString) -> Self {
        Self::field_ref(name, "spec.serviceAccountName")
    }

    fn status_host_ip(name: impl ToString) -> Self {
        Self::field_ref(name, "status.hostIP")
    }
    fn status_pod_ip(name: impl ToString) -> Self {
        Self::field_ref(name, "status.podIP")
    }
    fn status_pod_ips(name: impl ToString) -> Self {
        Self::field_ref(name, "status.podIPs")
    }

    fn field_ref(name: impl ToString, field_path: impl ToString) -> Self {
        let name = name.to_string();
        let field_ref = corev1::ObjectFieldSelector {
            api_version: None,
            field_path: field_path.to_string(),
        };
        let source = corev1::EnvVarSource {
            field_ref: Some(field_ref),
            ..corev1::EnvVarSource::default()
        };
        Self {
            name,
            value: None,
            value_from: Some(source),
        }
    }
}

pub trait ToEnvVar {
    fn to_envvar(&self) -> corev1::EnvVar;
}

impl ToEnvVar for corev1::EnvVar {
    fn to_envvar(&self) -> corev1::EnvVar {
        self.clone()
    }
}

impl<T, U> ToEnvVar for (T, U)
where
    T: std::fmt::Display,
    U: std::fmt::Display,
{
    fn to_envvar(&self) -> corev1::EnvVar {
        let (ref name, ref value) = *self;
        corev1::EnvVar::value(name, value)
    }
}

pub trait ToEnvFrom {
    fn to_envfrom(self) -> corev1::EnvFromSource;
}

impl ToEnvFrom for corev1::SecretEnvSource {
    fn to_envfrom(self) -> corev1::EnvFromSource {
        corev1::EnvFromSource {
            secret_ref: Some(self),
            ..corev1::EnvFromSource::default()
        }
    }
}

impl ToEnvFrom for corev1::ConfigMapEnvSource {
    fn to_envfrom(self) -> corev1::EnvFromSource {
        corev1::EnvFromSource {
            config_map_ref: Some(self),
            ..corev1::EnvFromSource::default()
        }
    }
}

// trait EnvVarExtPrivate {
//     fn config_map_key_ref(name: impl ToString, field_path: impl ToString) -> Self;
//     fn resource_field_ref(name: impl ToString, field_path: impl ToString) -> Self;
//     fn secret_key_ref(name: impl ToString, field_path: impl ToString) -> Self;
// }

// impl EnvVarExtPrivate for corev1::EnvVar {
//     fn config_map_key_ref(name: impl ToString, field_path: impl ToString) -> Self {
//         todo!()
//     }

//     fn resource_field_ref(name: impl ToString, field_path: impl ToString) -> Self {
//         todo!()
//     }

//     fn secret_key_ref(name: impl ToString, field_path: impl ToString) -> Self {
//         todo!()
//     }
// }