Reputation: 8148
I am playing around with kubernetes config map and wanted to understand how the volume mounts work
I have a json config file called client_config.json
that is
{
"name": "place",
"animal": "thing",
"age": 10
}
I created a config map using the command
kubectl create configmap client-config --from-file=client_config.json
I then mount it to a volume onto my deployment as
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-web-app
spec:
replicas: 2
selector:
matchLabels:
name: go-web-app
template:
metadata:
labels:
name: go-web-app
spec:
containers:
- name: application
image: my/repo
ports:
- containerPort: 3000
volumeMounts:
- name: config
mountPath: /client-config
readOnly: true
volumes:
- name: config
configMap:
name: client-config
In my go application I am able to read the config using
func config(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadFile(filepath.Clean("./client_config.json"))
if err != nil {
fmt.Fprintf(w, fmt.Sprintf("error reading config: %s", err))
} else {
fmt.Fprintf(w, fmt.Sprintf("config value is : %s", string(b)))
}
}
My question is that configmap was mounted to a mount path as
mountPath: /client-config
But I am reading it from the code as
b, err := ioutil.ReadFile(filepath.Clean("./client_config.json"))
What is the use of the mount path /client-config
when I do not need to even reference it when I am reading the configmap as a file ?
Upvotes: 0
Views: 1325
Reputation: 8148
Thanks to David's comment I was able to resolve the issue.
Issue was I was including the client_config.json
file along with the image, because of which my code was able to reference it via the path ./client_config.json
After I rebuilt the image without client_config.json
in it I got the error
error reading config: open client_config.json: no such file or directory
I then corrected my code to use the mount path and I am now able to read the configmap as a file from the volume
b, err := ioutil.ReadFile(filepath.Clean("/client-config/client_config.json"))
if err != nil {
fmt.Fprintf(w, fmt.Sprintf("error reading config: %s", err))
} else {
fmt.Fprintf(w, fmt.Sprintf("config value is : %s", string(b)))
}
Upvotes: 1
Reputation: 1079
./client_config.json
is a relative file path. Most probably, your app's current working directory is /client-config
. Your actual file is located at /client-config/client_config.json
.
Upvotes: 0