How @Value in Spring works internally so it could read Kubernetes ConfigMap?

Viewed 394

I'm curious on how the @Value works internally on Spring so that it can actually read value from ConfigMap of Kubernetes Cluster.

I know that:

  • @Value("${my.nested.variable}") were used to access variables declared on application.properties or in OS' environment variable (higher priority).
  • When creating new ConfigMap on kubernetes (for Spring project), you usually do kubectl create configmap my-config-name --from-file=application.properties, and it will magically connect those ConfigMap values with respective @Value() on Spring, of course we have to select my-config-name on deployment YAML file.

Notice above that we didnt expose/map those configmap to container's environment variable, already checked the inside container with printenv , can't find it. However, Spring were still able to retrieve those value from ConfigMap to be used in java program.

How is it possible? anyone know how Spring's @Value works or how the ConfigMap actually works internally so those two can magically connected?

Thank You.

1 Answers

This is a community wiki answer. Feel free to expand it.

As already mentioned by David Maze in the comments, Spring Cloud Kubernetes is reading ConfigMaps by using Kubernetes API. The mechanisms behind it are described in the linked docs.

Related