KubraGen: programmatic Kubernetes YAML generator
Overview
KubraGen is a Kubernetes YAML generator library that makes it possible to generate configurations using the full power of the Python programming language.
Using plugins (called Builders), it is possible to include libraries that know how to configure specific services, like Prometheus, RabbitMQ, Traefik, etc.
The jsonpatchext library (an extension of jsonpatch) is used to make it possible to check and modify the output objects in any way without accessing the returned dicts directly, including merging dicts with the deepmerge library.
See source code for examples
- Website: https://github.com/RangelReale/kubragen
- Repository: https://github.com/RangelReale/kubragen.git
- Documentation: https://kubragen.readthedocs.org/
- PyPI: https://pypi.python.org/pypi/kubragen
Samples
- Real-world samples: kubragen_samples
Builders
- RabbitMQ: kg_rabbitmq
- Traefik 2: kg_traefik2
- Keycloak: kg_keycloak
- Ingress NGINX: kg_ingressnginx
- Prometheus: kg_prometheus
- Grafana: kg_grafana
- Node Exporter: kg_nodeexporter
- Kube State Metrics: kg_kubestatemetrics
- Loki: kg_loki
- Promtail: kg_promtail
- Kube Resource Report: kg_kuberesourcereport
- RabbitMQ (Online YAML download): kg_rabbitmqonline
- Prometheus Stack (Prometheus, Grafana, Node Exporter, Kube State Metrics): kg_prometheusstack
- EFK Stack (Elasticsearch, Fluentd, Kibana): kg_efk
- Loki Stack (Loki, Promtail, Grafana): kg_lokistack
Providers
- Core (Amazon EKS, Google GKE, DigitalOcean Kubernetes, K3D, KIND): kgpr_core
Plugins
- Config Renderer - TOML: kgp_configrendertoml
Example
This example is purposeful convoluted to illustrate the many features of the library.
from kubragen import KubraGen
from kubragen.consts import PROVIDER_GOOGLE, PROVIDERSVC_GOOGLE_GKE
from kubragen.data import ValueData
from kubragen.helper import QuotedStr
from kubragen.jsonpatch import FilterJSONPatches_Apply, FilterJSONPatch, ObjectFilter
from kubragen.kdata import KData_Secret
from kubragen.object import Object
from kubragen.option import OptionRoot
from kubragen.options import Options
from kubragen.output import OutputProject, OD_FileTemplate, OutputFile_ShellScript, OutputFile_Kubernetes, \
OutputDriver_Print
from kubragen.provider import Provider
from kg_rabbitmq import RabbitMQBuilder, RabbitMQOptions
kg = KubraGen(provider=Provider(PROVIDER_GOOGLE, PROVIDERSVC_GOOGLE_GKE), options=Options({
'namespaces': {
'default': 'app-default',
'monitoring': 'app-monitoring',
},
}))
out = OutputProject(kg)
shell_script = OutputFile_ShellScript('create_gke.sh')
out.append(shell_script)
shell_script.append('set -e')
#
# OUTPUTFILE: app-namespace.yaml
#
file = OutputFile_Kubernetes('app-namespace.yaml')
out.append(file)
file.append(FilterJSONPatches_Apply([
Object({
'apiVersion': 'v1',
'kind': 'Namespace',
'metadata': {
'name': 'app-default',
'annotations': {
'will-not-output': ValueData(value='anything', enabled=False),
}
},
}, name='ns-default', source='app'), Object({
'apiVersion': 'v1',
'kind': 'Namespace',
'metadata': {
'name': 'app-monitoring',
},
}, name='ns-monitoring', source='app'),
], jsonpatches=[
FilterJSONPatch(filters=ObjectFilter(names=['ns-monitoring']), patches=[
{'op': 'add', 'path': '/metadata/annotations', 'value': {
'kubragen.github.io/patches': QuotedStr('true'),
}},
])
]))
shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))
shell_script.append(f'kubectl config set-context --current --namespace=app-default')
#
# SETUP: rabbitmq
#
rabbitmq_config = RabbitMQBuilder(kubragen=kg, options=RabbitMQOptions({
'namespace': OptionRoot('namespaces.monitoring'),
'basename': 'myrabbit',
'config': {
'erlang_cookie': KData_Secret(secretName='app-global-secrets', secretData='erlang_cookie'),
'enable_prometheus': True,
'prometheus_annotation': True,
'authorization': {
'serviceaccount_create': True,
'roles_create': True,
'roles_bind': True,
},
},
'kubernetes': {
'volumes': {
'data': {
'persistentVolumeClaim': {
'claimName': 'rabbitmq-storage-claim'
}
}
},
'resources': {
'statefulset': {
'requests': {
'cpu': '150m',
'memory': '300Mi'
},
'limits': {
'cpu': '300m',
'memory': '450Mi'
},
},
},
}
})).jsonpatches([
FilterJSONPatch(filters={'names': [RabbitMQBuilder.BUILDITEM_SERVICE]}, patches=[
{'op': 'check', 'path': '/spec/ports/0/name', 'cmp': 'equals', 'value': 'http'},
{'op': 'replace', 'path': '/spec/type', 'value': 'LoadBalancer'},
]),
])
rabbitmq_config.ensure_build_names(rabbitmq_config.BUILD_ACCESSCONTROL, rabbitmq_config.BUILD_CONFIG,
rabbitmq_config.BUILD_SERVICE)
#
# OUTPUTFILE: rabbitmq-config.yaml
#
file = OutputFile_Kubernetes('rabbitmq-config.yaml')
out.append(file)
file.append(rabbitmq_config.build(rabbitmq_config.BUILD_ACCESSCONTROL, rabbitmq_config.BUILD_CONFIG))
shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))
#
# OUTPUTFILE: rabbitmq.yaml
#
file = OutputFile_Kubernetes('rabbitmq.yaml')
out.append(file)
file.append(rabbitmq_config.build(rabbitmq_config.BUILD_SERVICE))
shell_script.append(OD_FileTemplate(f'kubectl apply -f ${{FILE_{file.fileid}}}'))
#
# OUTPUT
#
out.output(OutputDriver_Print())
# out.output(OutputDriver_Directory('/tmp/app-gke'))
Output:
****** BEGIN FILE: 001-app-namespace.yaml ********
apiVersion: v1
kind: Namespace
metadata:
name: app-default
annotations: {}
---
apiVersion: v1
kind: Namespace
metadata:
name: app-monitoring
annotations:
kubragen.github.io/patches: 'true'
****** END FILE: 001-app-namespace.yaml ********
****** BEGIN FILE: 002-rabbitmq-config.yaml ********
apiVersion: v1
kind: ServiceAccount
metadata:
name: myrabbit
namespace: app-monitoring
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
<...more...>
****** END FILE: 002-rabbitmq-config.yaml ********
****** BEGIN FILE: 003-rabbitmq.yaml ********
apiVersion: v1
kind: Service
metadata:
name: myrabbit-headless
namespace: app-monitoring
spec:
clusterIP: None
<...more...>
****** END FILE: 003-rabbitmq.yaml ********
****** BEGIN FILE: create_gke.sh ********
#!/bin/bash
set -e
kubectl apply -f 001-app-namespace.yaml
kubectl config set-context --current --namespace=app-default
kubectl apply -f 002-rabbitmq-config.yaml
kubectl apply -f 003-rabbitmq.yaml
****** END FILE: create_gke.sh ********
Design Philosophy
-
As the generated Kubernetes files can have critical consequences, the library is designed to fail on the minimal possibility of error, and also gives tools to users of the library to check any generated value for critical options, using the jsonpatchext library to check the data output by the builders.
-
To minimize the use of dict concatenation, a special type kubragen.data.Data can be used anywhere in the object, and it has a is_enabled() method that removes the value (and its key if it is contained in a dict/list) if it returns False.
-
Only YAML is supported by the library, it is not possible to generate JSON directly at the moment.
Author
Rangel Reale (rangelreale@gmail.com)
Metadata
Release files for kubragen 0.8.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| kubragen-0.8.4.tar.gz | 32.4 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| kubragen-0.8.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 71.0 kB
Release files / kubragen-0.8.4.tar.gz
| Download URL | kubragen-0.8.4.tar.gz |
|---|---|
| Size | 32.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3fe4e2ff71abe321c8e7c6d24dbc93ed9a245ae25458673413801fb05ae85df5
|
|
BLAKE2b-256 checksum How to use checksums |
a59a261a9d25e76e8792354628a54e0e5d9a5fd23259f68892a838fc6ab45a46
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3
|
Release files / kubragen-0.8.4-py3-none-any.whl
| Download URL | kubragen-0.8.4-py3-none-any.whl |
|---|---|
| Size | 38.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5787e9c8df00e3dd2b871cfec1be0beabc8492f015a1250db70b344e10070545
|
|
BLAKE2b-256 checksum How to use checksums |
3b1338bac5bb430761d3faffe46e0280015509d0ee2c62623907bfbb417c4c5c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.8.3
|
Security for kubragen 0.8.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.