kubetpl icon indicating copy to clipboard operation
kubetpl copied to clipboard

Render multiple configs to single template

Open dibi-codes opened this issue 5 years ago • 2 comments

Maybe i've missed something but I'm looking for a way to do a rendering for multiple input files.

Example: My template is deployment.yml. I have multiple config files in one directory config1.yml and config2.yml.

deployment.yml contains:

# kubetpl:syntax:go-template

apiVersion: apps/v1
kind: Deployment
metadata:
  name: {{ .NAME }}

config1.yml contains:

NAME: a

config2.yml contains:

NAME: b

What I expect to happen:

$ kubetpl r templates/deployment.yml -i configs/*.yml

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: a
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: b

what actually happens:

$ kubetpl r templates/deployment.yml -i configs/*.yml

Resource "kind" is missing 

Is there any way to accomplish this scenario?

dibi-codes avatar Apr 04 '19 12:04 dibi-codes

Hey Daniel,

The reason kubetpl r templates/deployment.yml -i configs/*.yml isn't working is shell expansion. kubetpl r templates/deployment.yml -i configs/*.yml is interpreted (by the shell) as kubetpl r templates/deployment.yml -i configs/config1.yml configs/config2.yml (not kubetpl r templates/deployment.yml -i configs/config1.yml -i configs/config2.yml) and so kubetpl thinks it's got 2 templates to render - templates/deployment.yml and configs/config2.yml.

On a side note, I do agree the error is confusing (I'll keep this ticket open until the error message is updated to include the path to a file that failed to render).

Getting back to the original question, kubetpl merges data from all -i <file> and so the only way to render the same template multiple times given different configs is (unfortunately) to

for cfg in configs/*.yml; do kubetpl r templates/deployment.yml -i $cfg; done

shyiko avatar Apr 04 '19 16:04 shyiko

OK, got you. Thanks for the explanation.

dibi-codes avatar Apr 04 '19 17:04 dibi-codes