Skip to main content

Kubernetes templating tool.

Project description

kuku

Build Status

kuku renders kubernetes yaml manifests using python templates. It is similar to helm in usage (templates dir, value files, etc..).

Installation:

pip3 install kuku

or using the docker image:

docker pull xarg/kuku

# Example usage (see more below):
docker run -v $(pwd)/:/tmp/ --rm xarg/kuku render -f /tmp/your-values.yaml /tmp/your-templates/ | kubectl apply -

Usage

Suppose you want to create a k8s service using a template where you define the service name, internalPort and externalPort.

Given the following service.py template:

from kubernetes import client


def template(context):
    return client.V1Service(
        api_version="v1",
        kind="Service",
        metadata=client.V1ObjectMeta(name=context["name"]),
        spec=client.V1ServiceSpec(
            type="NodePort",
            ports=[
                {"port": context["externalPort"], "targetPort": context["internalPort"]}
            ],
            selector={"app": context["name"]},
        ),
    )

You can now generate a yaml output from the above template using kuku by running:

$ ls .
service.py 
$ kuku render -s name=kuku-web,internalPort=80,externalPort=80 .

the above produces:

# Source: service.py
apiVersion: v1
kind: Service
metadata:
  name: kuku-web
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: kuku-web
  type: NodePort

You can also combine the above with kubectl apply -f - to actually create your service on k8s:

kuku render -s name=kuku-web,internalPort=80,externalPort=80 . | kubectl apply -f -

Same as above, but let's make it shorter:

kuku apply -s name=kuku-web,internalPort=80,externalPort=80 .

Finally to delete it:

kuku delete -s name=kuku-web,internalPort=80,externalPort=80 .
# same as above
kuku render -s name=kuku-web,internalPort=80,externalPort=80 . | kubectl delete -f - 

Templates

Templates are python files that are defining a function called template that accepts a dict argument context and returns a k8s object or a list of k8s objects:

def template(context):
    return V1Namespace(name=context['namespace'])  # example k8s object 

You can create multiple template files each defining their own template function. kuku uses the k8s objects (aka models) from official kubernetes python client package. You can find them all here

CLI

Similar to helm kuku accepts defining it's context variables from the CLI:

kuku -s namespace=kuku .

-s namespace=kuku will be passed to the context argument in your template function. Run kuku -h to find out more.

Goals

Write python code to generate k8s manifests.

Python is a very popular language with a huge ecosystem of devops packages. Most importantly it's easier to debug than some templating languages used today to generate k8s manifests.

No k8s server side dependencies (i.e. tiller).

k8s already has a database for it's current state (using etcd). We can connect directly to it from the client to do our operations instead of relying on an extra server side dependency.

Local validation of manifests before running kubectl apply.

Where possible do the validation locally using the official k8s python client.

Use standard tools

Where possible use kubectl to apply changes to the k8s cluster instead of implementing a specific protocol. Again, this will make debugging easier for the end user.

Why not helm?

At Gorgias we use helm to manage our infrastructure, but there are a few things that we found problematic with it:

  • Poor templating language: requires constant referral to the docs, whitespace issues, yaml formatting is hard.
  • Server side dependency: if you upgrade the server -> every user needs to update their client - waste of valuable time.
  • Lack of local validation: helm lint does not really ensure the validity (i.e. required keys for a k8s object) of the manifest.

Chart names, releases and other helm specific features do not really fit with our current workflow.

Contributing

Contributions (code, issues, docs, etc..) are welcome!

Once you have your python environment setup:

pip install -e .[dev] # will install dev dependencies
pre-commit install # will install pre-commit hooks for code quality checking 

Publish a new version to pypi:

python setup.py upload

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

kuku-0.1.2.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kuku-0.1.2-py2.py3-none-any.whl (17.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file kuku-0.1.2.tar.gz.

File metadata

  • Download URL: kuku-0.1.2.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for kuku-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2d39b157ac5fcc3de8cc2d9376957c5b40de4ef8be63f48a7adf0ea7197126d4
MD5 de12c57d76af4e36d3d489a749157bb4
BLAKE2b-256 f3b0a4bb3b59bb78cdbf435d034ece0682d70550b6948611b3909f52c68a2b20

See more details on using hashes here.

File details

Details for the file kuku-0.1.2-py2.py3-none-any.whl.

File metadata

  • Download URL: kuku-0.1.2-py2.py3-none-any.whl
  • Upload date:
  • Size: 17.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for kuku-0.1.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 eba810df2164ef455d0d53d3f08b41ac7d7fdd5257b452ad80081e71d06b5ec1
MD5 a6c18dab43066a0f76baa1986b6a31da
BLAKE2b-256 cf145387f527b1bb3216f2e2d12f982a5fc2b3a7a7cfa01eb580c17c69da7aa1

See more details on using hashes here.

Supported by

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