Case: I want to build images which I can run in a kubernetes cluster
later on. It seems to be very common to use placeholders in the application.properties
file to set these values in the deployment description later on.
My application.properties
looks like this:
...
spring.datasource.continueOnError=true
spring.datasource.initialization-mode=embedded
catalogservice.baseurl=http://${CATALOG_SERVICE_ADDRESS}:${CATALOG_SERVICE_PORT}
spring.application.name=cartservice
...
I try to access these values using
@Value("${catalogservice.baseurl}")
String baseUrl;
When I try to build the project I recieve this exception:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'catalogRequestComponent': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'CATALOG_SERVICE_ADDRESS' in value "http://${CATALOG_SERVICE_ADDRESS}:${CATALOG_SERVICE_PORT}" at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:380) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1378) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:575) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:498) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:846) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:863) ~[spring-context-5.1.2.RELEASE.jar:5.1.2.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546) ~[spring-context- ...
I googled lots but only found ConfigMaps
, but these are to inject the values later on. Nevertheless the project will not build when I create a kubernetes ConfigMap
, or am I wrong? I'd expect I need to create the images in the very first step, before creating the cluster and everything else.
How do I pass my values
Snippet of the kubernetes deployment script:
spec:
containers:
- name: myapp
image: myimage
ports:
- containerPort: 8080
env:
- name: CATALOG_SERVICE_ADDRESS <--
valueFrom:
configMapKeyRef:
name: catalogmap
key: catalog-address
...
These values are stored in a ConfigMap
:
apiVersion: v1
kind: ConfigMap
metadata:
name: catalogmap
data:
catalog-address: 192.168.178.10
All credits to @M. Deinum
Running mvn install -DskipTests
solves all my problems.