Skip to main content

Kubernetes templating engine based on Jinja2

Project description

k8t

Pronounced katie [ˈkeɪti]

Build Status PyPi version PyPi downloads CLARK Open Source

Simple cluster and environment specific aware templating for kubernetes manifests.

Table of Contents generated with DocToc

Installation

run this

$ pip install --user --upgrade k8t

note: k8t is not Python 2 compatible

Completion

Run the following and store the file in your distribution/OS specific spot

bash:

$ _K8T_COMPLETE=source k8t > k8t-completion.sh

zsh:

$ _K8T_COMPLETE=source_zsh k8t > k8t-completion.sh

Concepts

By combining those concepts you can quickly add completely new environments to your deployment pipeline just by modifying specializing values and sharing the rest.

Check out our examples here.

Clusters and Environments

k8t comes with a builtin framework for clusters and environments (e.g. production, staging). This came from the need to be able to deploy the same application over multiple clusters and in different environments with completely different setups and values. This idea is helped by the fact that k8t deep-merges values and configs, allowing easy variation through different stages of your application deployment.

Both clusters and environments are intentionally working the same way and can be used to add another degree of freedom when combined. Environments however are also available globally, meaning clusters can share environment specific configuration while specifying differences in those environments.

Templating

Templating is supported via Jinja. k8t also comes with some additional helper functions and a validation function with verbose output to quickly verify the written templates.

Template helper functions

  • random_password(N: int) - generate a random string of length N
  • envvar(key: str, [default]) - get a value from any environment variable with optional default
  • b64encode(value: str) - encodes a value in base64 (usually required for secrets)
  • b64decode(value: str) - decodes a value from base64
  • hash(value: str, [method: str]) - hashes a given value (default using sha256)
  • get_secret(key: str) - provides a secret value from a given provider (see here)
  • bool(value: Any) - casts value to boolean ("true", "on", "yes", "1", 1 are considered as True)

Usage

Scaffolding

Create a new project folder with a cluster directory and an empty defaults file

$ k8t new project .

Create a new cluster

$ k8t new cluster MyCluster

Create a new environment

$ k8t new environment staging

Generate a new deployment template for cluster MyCluster (for a list of available templates see the k8t new template --help)

$ k8t new template deployment -c MyCluster -e staging

Config management

To ease file access a little bit k8t can open config and value files in your $EDITOR or fallback to a sensible default.

$ k8t edit values --environment staging
$ k8t edit config --cluster MyCluster

Validate templates

While validation is done before generating, templates can be validated for environment files easily.

$ k8t validate

To validate for clusters/environments the usual options can be used

$ k8t validate -c MyCluster -e production

Generate manifests

The --cluster flag will load variables from a directory. By default the file default.yaml in that directory will be loaded, however an environment can be specified with --environment.

$ k8t gen -c MyCluster -e staging

Additionally k8t will attempt to load a file defaults.yaml in the root directory. This way a set of default variables can be specified and selectively overriden via cluster and environment.

Additional values can be given via flag --value-file in the form of a file or --value KEY VALUE, both can be supplied multiple times.

Variables will be merged via deep merging. Default merge strategy is left-to-right.

Overriding templates

Templates can be overriden on a cluster/environment level.

If a file application.yaml exists in the root templates folder, simply add a file with the same name to the cluster/environment template folder.

Managing secrets

Secrets can be interpolated with the helper function get_secret. It requires a key as first argument and providers are configurable by environment/cluster.

foobar: "{{ get_secret('/my-key') }}"

Providers

SSM

Setup secrets on SSM

secrets:
  provider: ssm
  region: "eu-central-1"
  prefix: "/foobar"

Keep in mind that SSM parameter names can be formed as a path and they can only consist of sub-paths divided by slash symbol; each sub-path can be formed as a mix of letters, numbers and the following 3 symbols: .-_

Be careful to follow this format when setting up the provider prefix and get_secret(key).

Random

Random secrets can be generated easily by using the random provider. This provider uses a global dictionary to store results for the time of the run in python so keys should always produce the same result.

secrets:
  provider: random

TODO

  • testing needs to be expanded
  • the ability to add additional template directories via the CLI
  • validation functions for template values (e.g. memory/cpu values)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

k8t-0.4.2.tar.gz (29.8 kB view details)

Uploaded Source

Built Distribution

k8t-0.4.2-py3-none-any.whl (26.2 kB view details)

Uploaded Python 3

File details

Details for the file k8t-0.4.2.tar.gz.

File metadata

  • Download URL: k8t-0.4.2.tar.gz
  • Upload date:
  • Size: 29.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for k8t-0.4.2.tar.gz
Algorithm Hash digest
SHA256 311f8a88944cb568d3fd34b0db9ba021716b8021f3c2d82decdeba65e09a19c5
MD5 b66bedde540dddefea910541a2b665d0
BLAKE2b-256 92297ffdd215dd79cae6415ba3953c7e32c8ca7e1af2be66ab394f67c3a185fb

See more details on using hashes here.

File details

Details for the file k8t-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: k8t-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 26.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.3

File hashes

Hashes for k8t-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 cf1f9ae270bc24d2a99885db99a52dfdcc05b982803a2cf770558d690eb90354
MD5 6b71fc43ef95fa14422883f45bbf8c0d
BLAKE2b-256 acf73c0be4b288a8b108a13a2c38e6fb12e3ce6a0f5fb485a8c2bee2ea3a2923

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page