Maoz Zadok
Maoz Zadok

Reputation: 5900

kubernetes configmap set from-file in yaml configuration

how can I describe this command in yaml format?

kubectl create configmap somename --from-file=./conf/nginx.conf

I'd expect to do something like the following yaml, but it doesn't work

apiVersion: v1
kind: ConfigMap
metadata:
  name: somename
  namespace: default
fromfile: ./conf/nginx.conf

any idea?

Upvotes: 43

Views: 34299

Answers (3)

jaxxstorm
jaxxstorm

Reputation: 13241

That won't work, because kubernetes isn't aware of the local file's path. You can simulate it by doing something like this:

kubectl create configmap --dry-run=client somename --from-file=./conf/nginx.conf --output yaml

The --dry-run flag will simply show your changes on stdout, and not make the changes on the server. This will output a valid configmap, so if you pipe it to a file, you can use that:

kubectl create configmap --dry-run=client somename --from-file=./conf/nginx.conf --output yaml | tee somename.yaml

Upvotes: 29

bGuruprasad.com
bGuruprasad.com

Reputation: 362

Almost 3 years old question with an accepted answer, but just for those new people who are visiting.

This can be achieved with helm chart as well. If you are using the helm chart, you can put these files under files/ directory in the chart and refer these files from YAML as

{{ .Files.Get "files/filename.ext" }}

This inclusion can also be encoded based on available function in go, such as

{{ .Files.Get "files/filename.ext" | b64enc }}

Upvotes: 7

Zstack
Zstack

Reputation: 4733

You could use kustomize, and it manages not only configmaps but other resources easily. I think you wanted to create configmap from a file in yaml, so you could do something like the following in a kustomization.yaml file:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
configMapGenerator:
- files:
  - ./conf/nginx.conf
  name: nginx-config

Additionally, kustomize is very handy to manage all the deployments (particularly very handy for declarative management), and you can have everything in a single kustomize file as shown below:

apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization
secretGenerator:
- envs:
  - .env
  name: my-secrets
configMapGenerator:
- files:
  - ./conf/nginx.conf
  name: nginx-config
resources:
- ./nginx-deployment.yaml

The to deploy everything you could run it like this:

$ kustomize build | kubectl apply -f -

For more information please refer here

Upvotes: 14

Related Questions