Kuber
kuber is Python library for the management of Kubernetes resources. It's
ideal for collectively managing groups of resources throughout their
lifecycle. Resource definitions can be created and managed entirely in Python
code (the pure-Python approach), but kuber is most effective when used in a
hybrid fashion that combines configuration files and Python code.
kuber also integrates and maintains compatibility with the lower-level official
Kubernetes Python client,
while abstracting basic CRUD operations into higher level constructs
more inline with the behaviors of tools like kubectl and helm.
Key Functionality
Here are some key things that kuber does well:
- A flexible workflow for managing Kubernetes resource configuration in Python
code.
- The ability to load resources directly from YAML or JSON configuration files,
modify them in code and then use them or save them back to YAML/JSON files.
- Resource bundling for managing groups of resource configurations collectively.
- CRUD operations exposed directly on the resource objects to reduce the
overhead in managing low-level clients.
- Convenience functions that simplify common operations, e.g. managing
containers within pods from the root resource.
- Very thorough type-hinting and object structure to support creating accurate
resource configurations and catch errors before runtime.
- All resources and sub-resources support used in
with
blocks as context
managers to simplify making multiple changes to a sub-resource. - Simultaneous support for multiple Kubernetes API versions. Manage multiple
Kubernetes API versions (e.g. while promoting new versions from development
to production) without having to pin and switch Python environments.
Installation
kuber available for installation with pip:
$ pip install kuber
Quickstart
kuber can be used to manage individual resources or a group of resources
collectively. kuber is also very flexible about how resources are created -
either directly from Python or by loading and parsing YAML/JSON configuration
files. The first example shows the multi-resource management path:
import kuber
from kuber.latest import apps_v1
resource_bundle = (
kuber.create_bundle()
.add_directory("app_configs")
.add_file("secrets/app-secret.yaml")
)
for resource in resource_bundle.resources:
resource.metadata.labels.update(environment="production")
d: apps_v1.Deployment = resource_bundle.get(
name="my-app",
kind="Deployment"
)
d.spec.replicas = 20
kuber.load_access_config()
resource_bundle.cli()
Or managing resources individually:
from kuber.latest import batch_v1
job = batch_v1.Job()
with job.metadata as md:
md.name = "my-job"
md.namespace = "jobs"
md.labels.update(
component="backend-tasks",
environment="production"
)
job.spec.append_container(
name="main",
image="my-registry.com/projects/my-job:1.0.1",
image_pull_policy="Always",
env=[batch_v1.EnvVar("ENVIRONMENT", "production")]
)
print(job.to_yaml())
Check out the kuber documentation
for more details and examples.