42

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?

Maoz Zadok
  • 4,871
  • 3
  • 33
  • 43
  • If you are using helm, there are special functions for doing this https://helm.sh/docs/chart_template_guide/accessing_files/#configmap-and-secrets-utility-functions – Jarno Aug 04 '20 at 12:43

3 Answers3

29

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
jws
  • 2,171
  • 19
  • 30
jaxxstorm
  • 12,422
  • 5
  • 57
  • 67
  • Elegant solution – Will Feb 23 '19 at 14:15
  • 2
    so there is no way we can write the above command way in yaml way ? – Tushar Seth Oct 07 '19 at 08:20
  • 4
    This doesn't actually answer OP's question. The dry run outputs the entire contents of the file into the yaml, which defeats the purpose of having it in a separate file in the first place. We want a *reference* to the file (its path) in the yaml. This is for local-dev configmaps. – Stevey Aug 31 '20 at 20:18
  • 1
    I believe this does answer the question by saying "what you're asking for is not supported". The ConfigMap schema does not support a `fromfile` property. It then explains a possible alternative that is possible. – Michael R Mar 09 '23 at 17:50
13

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

Zstack
  • 4,046
  • 1
  • 19
  • 22
7

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 }}
bGuruprasad.com
  • 362
  • 3
  • 10