所以我正在努力从kubernetes读取xml文件。我正在运行一个开放的自由服务器和一个Maven打包的war应用程序。我需要从Java代码中进行哪些更改,以便可以从kubernetes中读取文件。
这里的问题是代码从war文件中的资源文件夹中读取了pdp.xml,而不是从kubernetes卷中的外部读取。
我的configMap看起来像这样
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>
这是带有卷的yaml文件
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
我不太了解Java,但是这就是我读取文件的方式。
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);
}
这是容器内的文件夹结构
答案 0 :(得分:0)
非常简单。只需从安装文件的位置读取文件:/config/pdp.xml
。
因此,在您的代码中:
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);
}
...或将其安装到应用程序正在从中读取的目录中:
mountPath: /liberty/usr/servers/defaultServer/pdp.xml