@@ -21,6 +21,7 @@ use k8s_openapi::apimachinery::pkg::apis::meta::v1::{
21
21
} ;
22
22
use kube:: { Resource , ResourceExt } ;
23
23
use std:: collections:: BTreeMap ;
24
+ use std:: fmt;
24
25
use tracing:: warn;
25
26
26
27
/// A builder to build [`ConfigMap`] objects.
@@ -214,6 +215,28 @@ impl SecurityContextBuilder {
214
215
}
215
216
}
216
217
218
+ /// Downward API capabilities available via `fieldRef`
219
+ /// See: https://kubernetes.io/docs/tasks/inject-data-application/downward-api-volume-expose-pod-information/#capabilities-of-the-downward-api
220
+ #[ derive( Debug ) ]
221
+ pub enum FieldPathEnvVar {
222
+ Name ,
223
+ Namespace ,
224
+ UID ,
225
+ Labels ( String ) ,
226
+ Annotations ( String ) ,
227
+ }
228
+
229
+ impl fmt:: Display for FieldPathEnvVar {
230
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
231
+ match self {
232
+ FieldPathEnvVar :: Name => write ! ( f, "metadata.name" ) ,
233
+ FieldPathEnvVar :: Namespace => write ! ( f, "metadata.namespace" ) ,
234
+ FieldPathEnvVar :: UID => write ! ( f, "metadata.uid" ) ,
235
+ FieldPathEnvVar :: Labels ( name) => write ! ( f, "metadata.labels['{name}']" ) ,
236
+ FieldPathEnvVar :: Annotations ( name) => write ! ( f, "metadata.annotations['{name}']" ) ,
237
+ }
238
+ }
239
+ }
217
240
/// A builder to build [`Container`] objects.
218
241
///
219
242
/// This will automatically create the necessary volumes and mounts for each `ConfigMap` which is added.
@@ -261,16 +284,16 @@ impl ContainerBuilder {
261
284
}
262
285
263
286
/// Used for pushing down attributes like the Pod's namespace into the containers.
264
- pub fn add_env_var_from_field_ref (
287
+ pub fn add_env_var_from_field_path (
265
288
& mut self ,
266
289
name : impl Into < String > ,
267
- value : impl Into < String > ,
290
+ field_path : FieldPathEnvVar ,
268
291
) -> & mut Self {
269
292
self . env . get_or_insert_with ( Vec :: new) . push ( EnvVar {
270
293
name : name. into ( ) ,
271
294
value_from : Some ( EnvVarSource {
272
295
field_ref : Some ( ObjectFieldSelector {
273
- field_path : value . into ( ) ,
296
+ field_path : field_path . to_string ( ) ,
274
297
..ObjectFieldSelector :: default ( )
275
298
} ) ,
276
299
..EnvVarSource :: default ( )
@@ -1532,8 +1555,8 @@ impl VolumeMountBuilder {
1532
1555
mod tests {
1533
1556
use crate :: builder:: {
1534
1557
ConfigMapBuilder , ContainerBuilder , ContainerPortBuilder , EventBuilder , EventType ,
1535
- NodeBuilder , ObjectMetaBuilder , PodBuilder , PodSecurityContextBuilder , VolumeBuilder ,
1536
- VolumeMountBuilder ,
1558
+ FieldPathEnvVar , NodeBuilder , ObjectMetaBuilder , PodBuilder , PodSecurityContextBuilder ,
1559
+ VolumeBuilder , VolumeMountBuilder ,
1537
1560
} ;
1538
1561
use k8s_openapi:: api:: core:: v1:: {
1539
1562
EnvVar , Pod , PodAffinity , PodAffinityTerm , PodSecurityContext , ResourceRequirements ,
@@ -1885,4 +1908,12 @@ mod tests {
1885
1908
. unwrap ( ) ;
1886
1909
assert_eq ! ( pod. metadata. name. unwrap( ) , "foo" ) ;
1887
1910
}
1911
+
1912
+ #[ test]
1913
+ pub fn test_field_ref_env_var_serialization ( ) {
1914
+ assert_eq ! (
1915
+ "metadata.labels['some-label-name']" ,
1916
+ FieldPathEnvVar :: Labels ( "some-label-name" . to_string( ) ) . to_string( )
1917
+ ) ;
1918
+ }
1888
1919
}
0 commit comments