kubetpl
Templating helper for Kubernetes manifest files.
Usage
By CLI
npx @devdata/kubetpl \
--sources 'kubernetes/**/*.{yml,yaml}'
--config kubetpl.json \
--runtime development \
--vars.replicas 4 \
> compiled-manifest.yml
kubetpl.json:
{
"vars": {
"replicas": {
"default": "1",
"production": "3"
},
"namespace": {
"$default": "main"
}
}
}
By Code
import { Compiler } from 'kubetpl'
const config = {
vars: {
replicas: {
default: '1',
production: '3'
},
namespace: {
$default: 'main'
}
}
}
const compiler = new Compiler({
config,
sources: 'kubernetes/**/*.{yml,yaml}',
runtime: 'development',
// Inject/override existing vars (optional)
vars: {
namespace: 'development'
}
})
const manifest = compiler.compile()
// ... or
compiler.compileToFile('compiled-manifest.yml')
How it works
- Load up the sources from glob pattern/paths.
- Combine them into one using yaml Document starter (
---
) - Replace all placeholders with values from the config and optionally
extra vars. The
runtime
option determines which set of variables to use. - Output the result.
The compiler will abort if theres a placeholder without any corresponding variable defined.
Placeholders
Put placeholders like this in your source files: {{vars.example}}
.
Built in placeholders:
-
{{meta.runtime}}
- Contains the runtime parameter passed into the compiler.
Ideas
- Turn into GitHub Action for ease of use.