{"id":13656476,"url":"https://github.com/shyiko/kubetpl","last_synced_at":"2025-05-17T09:39:44.262Z","repository":{"id":50223768,"uuid":"104681784","full_name":"shyiko/kubetpl","owner":"shyiko","description":"Kubernetes templates made easy #keep-it-simple #no-server-component","archived":false,"fork":false,"pushed_at":"2020-12-09T21:11:46.000Z","size":94,"stargazers_count":210,"open_issues_count":18,"forks_count":30,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-11-10T09:37:49.480Z","etag":null,"topics":["kubernetes","templating"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shyiko.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-09-24T22:51:28.000Z","updated_at":"2024-09-13T13:35:58.000Z","dependencies_parsed_at":"2022-09-24T08:50:38.004Z","dependency_job_id":null,"html_url":"https://github.com/shyiko/kubetpl","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shyiko%2Fkubetpl","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shyiko%2Fkubetpl/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shyiko%2Fkubetpl/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shyiko%2Fkubetpl/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shyiko","download_url":"https://codeload.github.com/shyiko/kubetpl/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225444525,"owners_count":17475355,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["kubernetes","templating"],"created_at":"2024-08-02T05:00:21.228Z","updated_at":"2024-11-20T00:04:54.687Z","avatar_url":"https://github.com/shyiko.png","language":"Go","funding_links":[],"categories":["Go"],"sub_categories":[],"readme":"# kubetpl ![Latest Version](https://img.shields.io/badge/latest-0.9.0-blue.svg) [![Build Status](https://travis-ci.org/shyiko/kubetpl.svg?branch=master)](https://travis-ci.org/shyiko/kubetpl)\n\nKubernetes templates made easy.  \n\\#keep-it-simple \\#no-server-component\n\nFeatures:\n- **Template flavor of your choice**\n  - [$](#-shell) (`$VAR` / `${VAR}`);\n  - [go-template](#go-go-template) (go-template enriched with [sprig](http://masterminds.github.io/sprig/)); \n  - [template-kind](#template-kind) (`kind: Template`).\n- Support for **\\*.env** (`\u003cVAR\u003e=\u003cVAL\u003e`) and **YAML** / **JSON** config files.\n- **Fail-fast defaults** (all variables must be given a value (unless explicitly marked optional)).\n- [ConfigMap/Secret freezing](#configmapsecret-freezing) for easier and less error-prone ConfigMap/Secret rollouts  \n(something to consider ~~if~~ when you hit [kubernetes/kubernetes#22368](https://github.com/kubernetes/kubernetes/issues/22368)).  \n- [ConfigMap/Secret \"data-from-file\" injection](#configmapsecret-data-from-file-injection) when `kubectl create configmap ... --from-file=... --from-file=... --from-file=... ...` feels like too much typing.\n- `image:tag` -\u003e `image@digest` pinning with the help of [dockry](https://github.com/shyiko/dockry)   \n(e.g. `kubetpl render -s IMAGE=$(dockry digest --fq user/image:master) ...` to force redeployment of the new build published under the same tag).\n\n## Installation\n\n#### macOS / Linux\n\n```sh\ncurl -sSL https://github.com/shyiko/kubetpl/releases/download/0.9.0/kubetpl-0.9.0-$(\n    bash -c '[[ $OSTYPE == darwin* ]] \u0026\u0026 echo darwin || echo linux'\n  )-amd64 -o kubetpl \u0026\u0026 chmod a+x kubetpl \u0026\u0026 sudo mv kubetpl /usr/local/bin/\n```\n    \nVerify PGP signature (optional but recommended):    \n```sh\ncurl -sSL https://github.com/shyiko/kubetpl/releases/download/0.9.0/kubetpl-0.9.0-$(\n    bash -c '[[ $OSTYPE == darwin* ]] \u0026\u0026 echo darwin || echo linux'\n  )-amd64.asc -o kubetpl.asc\ncurl -sS https://keybase.io/shyiko/pgp_keys.asc | gpg --import\ngpg --verify kubetpl.asc /usr/local/bin/kubetpl\n```  \n\n\u003e macOS: `gpg` can be installed with `brew install gnupg`\n\n#### Windows\n\nDownload executable from the [Releases](https://github.com/shyiko/kubetpl/releases) page.\n\n## Usage\n\n```sh\n# create template\necho $'\n# kubetpl:syntax:$\n\napiVersion: v1\nkind: Pod\nmetadata:\n  name: $NAME-pod\nspec:\n  containers:\n  - name: $NAME-container\n    image: $IMAGE\n    env:\n    - name: ENV_KEY\n      value: $ENV_KEY\n' \u003e template.yml \n\n# create config file (.env, .yml/.yaml or .json) (optional)\n# (you'll probably have a different config file for each cluster/namespace/etc)\necho $'\nNAME=sample-app\nENV_KEY=value\n' \u003e staging.env\n# you might not need a config file if there are only a handful of variables (like in this case)\n# -s/--set key=value might be enough\n\n# render template\nkubetpl render template.yml -i staging.env -s IMAGE=nginx \n\n# to apply, pipe \"render\"ed output through kubectl    \nkubetpl render template.yml -i staging.env -s IMAGE=nginx | \n  kubectl apply -f -\n  \n# you can also apply remote template(s) \nkubetpl render https://rawgit.com/shyiko/kubetpl/master/example/nginx.sh.yml \\\n  -s NAME=kubetpl-example-nginx -s MESSAGE=\"hello $(whoami)\" | \n  kubectl apply -f -\n```\n\n\u003e (for more examples see [Template flavors](#template-flavors))\n\n#### \u003ckbd\u003eTab\u003c/kbd\u003e completion\n\n```sh\n# bash\nsource \u003c(kubetpl completion bash)\n\n# zsh\nsource \u003c(kubetpl completion zsh)\n```\n\n## ConfigMap/Secret freezing\n\nWhen `kubetpl render --freeze ...` is used, kubetpl rewrites `ConfigMap`/`Secret`'s name to include hash of the content \nand then updates all the references (in `Pod`s / `DaemonSet`s / `Deployment`s / `Job`s / `ReplicaSet`s / `ReplicationController`s / `StatefulSet`s / `CronJob`s) with a new value.\n\nFor example, executing [`kubetpl render --freeze example/nginx-with-data-from-file.yml -s NAME=app -s MESSAGE=msg`](example/nginx-with-data-from-file.yml) \nshould produce [example/nginx-with-data-from-file.rendered+frozen.yml](example/nginx-with-data-from-file.rendered+frozen.yml#L15).\n \nNOTE: this feature can be used regardless of the [Template flavor](#template-flavors) choice (or lack thereof (i.e. on its own)).\n\n## ConfigMap/Secret \"data-from-file\" injection\n\n\u003e kubetpl@0.5.0+ also supports [`kubetpl/data-from-env-file`](https://github.com/shyiko/kubetpl/issues/3). \n\nOptionally, ConfigMap/Secret|s can be extended with `kubetpl/data-from-file` to load \"data\" from a list of files (relative to a template unless a different `-c/--chroot` is specified), e.g.  \n\n```yaml\nkind: ConfigMap\nkubetpl/data-from-file: \n  - file \n  - path/to/another-file\n  - custom-key=yet-another-file\ndata:\n  key: value\n...\n``` \n\nUpon `kubetpl render` the content of `file`, `another-file` and `yet-another-file` (using `custom-key` as a key)\nwill be added to the object's \"data\" (`kubetpl/data-from-file` is automatically striped away).\n\nFor example, executing [`kubetpl render --allow-fs-access example/nginx-with-data-from-file.yml -s NAME=app`](example/nginx-with-data-from-file.yml) \nshould produce [example/nginx-with-data-from-file.rendered.yml](example/nginx-with-data-from-file.rendered.yml).\n\nNOTE #1: for security reasons, `kubetpl/data-form-file` is not allowed to read files unless `--allow-fs-access` or `-c/--chroot=\u003croot dir\u003e` is specified (see `kubetpl render --help` for more). \n\nNOTE #2: this feature can be used regardless of the [Template flavor](#template-flavors) choice (or lack thereof (i.e. on its own)).\n\n## Template flavors\n\nTemplate syntax is determined by first checking template for `# kubetpl:syntax:\u003c$|go-template|template-kind\u003e` comment \nand then, if not found, `--syntax=\u003c$|go-template|template-kind\u003e` command line option. In the absence of both, \nkubetpl assumes that template is a regular resource definition file.\n\n### $ (shell)\n\nA type of template where all instances of $VAR / ${VAR} are replaced with corresponding values. If, for some variable, no value\nis given - an error will be raised. \n\n\u003e Use `$$` when you need a literal dollar sign (`$$v` is interpreted as `$v` string and not `'$' + \u003cvalue_of_v\u003e`). \n\n##### Example\n\nLet's say we have the following (click to expand):\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u0026lt;project_dir\u0026gt;/k8s/staging.env\u003c/summary\u003e\n\n```ini  \nNAME=sample-app\nREPLICAS=1\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003e\u0026lt;project_dir\u0026gt;/k8s/template.yml\u003c/summary\u003e\n\n```yaml  \n# kubetpl:syntax:$\n\napiVersion: v1\nkind: Service\nmetadata:\n  name: $NAME-service\nspec:\n  selector:\n    app: $NAME\n  ports:\n  - protocol: TCP\n    port: 80\n---\napiVersion: apps/v1beta1\nkind: Deployment\nmetadata:\n  name: $NAME-deployment\nspec:\n  replicas: $REPLICAS\n  template: \n    metadata:\n      labels:\n        app: $NAME\n    spec:\n      containers:\n      - name: nginx\n        image: nginx:1.7.9\n        ports:\n        - containerPort: 80\n```\n\u003c/details\u003e\n\u003cp\u003e\u003cp\u003e\n\n`kubetpl render k8s/template.yml -i k8s/staging.env -s REPLICAS=3` should then yield\n\n\u003cdetails\u003e\n  \u003csummary\u003e(click to expand)\u003c/summary\u003e\n\n```yaml\napiVersion: v1\nkind: Service\nmetadata:\n  name: sample-app-service\nspec:\n  selector:\n    app: sample-app\n  ports:\n  - protocol: TCP\n    port: 80\n---\napiVersion: apps/v1beta1\nkind: Deployment\nmetadata:\n  name: sample-app-deployment\nspec:\n  replicas: 3\n  template: \n    metadata:\n      labels:\n        app: sample-app\n    spec:\n      containers:\n      - name: nginx\n        image: nginx:1.7.9\n        ports:\n        - containerPort: 80\n```\n\u003c/details\u003e\n\u003cp\u003e\u003cp\u003e\n\n[kubetpl@0.8.0+](https://github.com/shyiko/kubetpl/blob/master/CHANGELOG.md#080---2018-09-28) default values can be specified via `# kubetpl:set:KEY=VALUE` directive(s), e.g.\n\n```yaml\n# kubetpl:syntax:$\n# kubetpl:set:NAME=nginx\n# kubetpl:set:REPLICAS=1\n\napiVersion: apps/v1beta1\nkind: Deployment\nmetadata:\n  name: $NAME\n  annotations:\n    version: $VERSION\nspec:\n  replicas: $REPLICAS\n...  \n``` \n\n### go-template\n\n\u003e All functions provided by [sprig](http://masterminds.github.io/sprig/) are available  \n(with the exception of `env` and `expandenv`).\n\nA good overview of go-template|s can be found [here](https://gohugo.io/templates/introduction/#variables). You might also want to check [official documentation](https://golang.org/pkg/text/template/).\n\nSome of the most commonly used expressions:\n* `{{ .VAR }}` - get the value of `VAR`;\n* `{{ if isset \"VAR\" }} ... {{ end }}` - render content between `}}` and `{{` only if .VAR is set;   \n* `{{ get \"VAR\" \"default\" }}` - get the value of `VAR`, return `\"default\"` if not set (e.g. `{{ get \"REPLICAS\" 1 }}`);   \n* `{{ .VAR | quote }}` - quote the value of VAR;   \n* `{{ .VAR | indent 4 }}` - indent value of VAR with 4 spaces;   \n* `{{ .VAR | b64enc }}` - base64-encode value of VAR.   \n\n##### Example\n\nLet's say we have the following (click to expand):\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u0026lt;project_dir\u0026gt;/k8s/staging.env\u003c/summary\u003e\n\n```yaml\nNAME=sample-app\nREPLICAS=1\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003e\u0026lt;project_dir\u0026gt;/k8s/template.yml\u003c/summary\u003e\n\n```yaml\n# kubetpl:syntax:go-template\n\napiVersion: v1\nkind: Service\nmetadata:\n  name: {{ .NAME }}-service\nspec:\n  selector:\n    app: {{ .NAME }}\n  ports:\n  - protocol: TCP\n    port: 80\n---\napiVersion: apps/v1beta1\nkind: Deployment\nmetadata:\n  name: {{ .NAME }}-deployment\nspec:\n  replicas: {{ .REPLICAS }}\n  template: \n    metadata:\n      labels:\n        app: {{ .NAME }}\n    spec:\n      containers:\n      - name: nginx\n        image: nginx:1.7.9\n        ports:\n        - containerPort: 80\n```\n\u003c/details\u003e\n\u003cp\u003e\u003cp\u003e\n\n`kubetpl render k8s/template.yml -i k8s/staging.env -s REPLICAS=3` should then yield\n\n\u003cdetails\u003e\n  \u003csummary\u003e(click to expand)\u003c/summary\u003e\n\n```yaml\napiVersion: v1\nkind: Service\nmetadata:\n  name: sample-app-service\nspec:\n  selector:\n    app: sample-app\n  ports:\n  - protocol: TCP\n    port: 80\n---\napiVersion: apps/v1beta1\nkind: Deployment\nmetadata:\n  name: sample-app-deployment\nspec:\n  replicas: 3\n  template: \n    metadata:\n      labels:\n        app: sample-app\n    spec:\n      containers:\n      - name: nginx\n        image: nginx:1.7.9\n        ports:\n        - containerPort: 80\n```\n\u003c/details\u003e\n\u003cp\u003e\u003cp\u003e\n\n### template-kind\n\n\u003e aka `kind: Template`. \n\nAs described in [Templates + Parameterization proposal](https://github.com/kubernetes/community/blob/master/contributors/design-proposals/apps/OBSOLETE_templates.md).\n\n##### Example\n\nLet's say we have the following (click to expand):\n\n\u003cdetails\u003e\n  \u003csummary\u003e\u0026lt;project_dir\u0026gt;/k8s/staging.env\u003c/summary\u003e\n\n```yaml\nNAME=sample-app\n```\n\u003c/details\u003e\n\u003cdetails\u003e\n  \u003csummary\u003e\u0026lt;project_dir\u0026gt;/k8s/template.yml\u003c/summary\u003e\n\n```yaml\n# kubetpl:syntax:template-kind\n\nkind: Template\napiVersion: v1\nmetadata:\n  name: nginx-template\n  annotations:\n    description: nginx template\nobjects:\n- apiVersion: v1\n  kind: Service\n  metadata:\n    name: $(NAME)-service\n  spec:\n    selector:\n      app: $(NAME)\n    ports:\n    - protocol: TCP\n      port: 80\n- apiVersion: apps/v1beta1\n  kind: Deployment\n  metadata:\n    name: $(NAME)-deployment\n  spec:\n    replicas: $((REPLICAS))\n    template: \n      metadata:\n        labels:\n          app: $(NAME)\n      spec:\n        containers:\n        - name: nginx\n          image: nginx:1.7.9\n          ports:\n          - containerPort: 80\nparameters:\n- name: NAME\n  description: Application name\n  required: true\n  parameterType: string\n- name: REPLICAS\n  description: Number of replicas\n  value: 1\n  required: true\n  parameterType: int\n```\n\u003c/details\u003e\n\u003cp\u003e\u003cp\u003e\n\n`kubetpl render k8s/template.yml -i k8s/staging.env -s REPLICAS=3` should then yield\n\n\u003cdetails\u003e\n  \u003csummary\u003e(click to expand)\u003c/summary\u003e\n\n```yaml\napiVersion: v1\nkind: Service\nmetadata:\n  name: sample-app-service\nspec:\n  selector:\n    app: sample-app\n  ports:\n  - protocol: TCP\n    port: 80\n---\napiVersion: apps/v1beta1\nkind: Deployment\nmetadata:\n  name: sample-app-deployment\nspec:\n  replicas: 3\n  template: \n    metadata:\n      labels:\n        app: sample-app\n    spec:\n      containers:\n      - name: nginx\n        image: nginx:1.7.9\n        ports:\n        - containerPort: 80\n```\n\u003c/details\u003e\n\n## Development\n\n\u003e PREREQUISITE: [go1.9+](https://golang.org/dl/).\n\n```sh\ngit clone https://github.com/shyiko/kubetpl $GOPATH/src/github.com/shyiko/kubetpl \ncd $GOPATH/src/github.com/shyiko/kubetpl\nmake fetch\n\ngo run kubetpl.go\n```\n\n## Legal\n\nAll code, unless specified otherwise, is licensed under the [MIT](https://opensource.org/licenses/MIT) license.  \nCopyright (c) 2018 Stanley Shyiko.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshyiko%2Fkubetpl","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshyiko%2Fkubetpl","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshyiko%2Fkubetpl/lists"}