So i am struggling to read a xml file from kubernetes. I am running an open liberty server and a maven packaged war application. what do i need to change from my Java code so i can read file from kubernetes.
The issue here is that the code reads pdp.xml from the resources folder inside the war file, not externally in a kubernetes volume.
My configMap looks like this
kind: ConfigMap
apiVersion: v1
metadata:
name: testing-config
data:
pdp.xml: |
<pdp xmlns="http://authzforce.github.io/core/xmlns/pdp/7"
xmlns:att="za.co.some.data"
xmlns:xacml="urn:oasis:names:tc:xacml:3.0:core:schema:wd-17"
version="7.1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
</pdp>
Here's the yaml file with volumes
apiVersion: apps/v1
kind: Deployment
metadata:
name: testing-app
labels:
app: testing-app
spec:
replicas: 1
selector:
matchLabels:
app: testing-app
template:
metadata:
labels:
app: testing-app
spec:
containers:
- name: testing-app
image: testing-app:1.0.24-SNAPSHOT
imagePullPolicy: Always
volumeMounts:
- name: testing-app-volume
mountPath: /config/pdp.xml
subPath: pdp.xml
readOnly: true
volumes:
- name: testing-app-volume
configMap:
name: testing-app-config
restartPolicy: Always
I don't really know Java that well but here's how i'm reading the file.
Resource resource = new ClassPathResource("pdp.xml");
InputStream cfgFile = null;
byte[] buffer = null;
try {
cfgFile = resource.getInputStream();
buffer = new byte[cfgFile.available()];
} catch (IOException e) {
log.error(ERROR_MESSAGE);
throw new IllegalStateException(ERROR_MESSAGE);
}
this is the folder structure inside the container
Very simple. Just read the file from the same location you are mounting it to: /config/pdp.xml
.
So, in your code:
Resource resource = new ClassPathResource("/config/pdp.xml");
InputStream cfgFile = null;
byte[] buffer = null;
try {
cfgFile = resource.getInputStream();
buffer = new byte[cfgFile.available()];
} catch (IOException e) {
log.error(ERROR_MESSAGE);
throw new IllegalStateException(ERROR_MESSAGE);
}
...or mount it to the directory the app is reading from: mountPath: /liberty/usr/servers/defaultServer/pdp.xml