I have a web application that has a server side UI component that needs to talk to another component which exposes a REST interface. The UI needs to know the address of the endpoint that the REST component exposes.
When the UI starts, I set an environment variable ( ADDRESS_SERVICE_URI ) that contains the REST endpoint host and port.
I now want to deploy this application into Kubernetes, but I want to do it in a way that does not enforce any dependency in the application code on Kubernetes. I was hoping to use the environment variables that Kubernetes exposes to aid service discovery, so I have the following in my UI's deployment description
env:
- name: ADDRESS_SERVICE_URI
value: http://${REST_SERVICE_HOST}:${REST_SERVICE_PORT}
I was hoping that the environment variables would be evaluated by Kuberbetes, but they appear to be being passed thru "as is" to my application code, as I get the following exception when the code executes.
java.lang.IllegalArgumentException: Illegal character in authority at index 7: http://${REST_SERVICE_HOST}:${REST_SERVICE_PORT}/addresses/postcode/WA11
java.net.URI.create(URI.java:852)
com.sun.jersey.api.client.Client.resource(Client.java:434)
uk.gov.dwp.digital.addresslookup.dao.impl.PostCodeDAOImpl.byPostCode(PostCodeDAOImpl.java:44)
uk.gov.dwp.digital.addresslookup.service.impl.PostCodeServiceImpl.byPostcode(PostCodeServiceImpl.java:17)
uk.gov.dwp.digital.addresslookup.controllers.PostCodeController.processSearchRequest(PostCodeController.java:83)
uk.gov.dwp.digital.addresslookup.controllers.PostCodeController.executeSearch(PostCodeController.java:59)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Is it possible to evaluate the Kubernetes environment variables, or do I need to alter my code to expect the variables to be presented to it as two separate variables, with names that Kubernetes dictates?
Since env
only appears to support key:value pairs, your best bet is to use an ENTRYPOINT
script to pre-populate your ENV before launching the app.
Dockerfile
FROM yourbaseimage
COPY entrypoint.sh /
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
#!/bin/bash
export ADDRESS_SERVICE_URI="http://${REST_SERVICE_HOST}:${REST_SERVICE_PORT}"
exec "$@"