Skip to main content

pykorm - Python Kubernetes Object-relational mapping (ORM)

pykorm is a simple library that links your models to their kubernetes counterpart.

Each model and instance on your code is thus directly linked to your kubernetes cluster and modifications are thus reflected both ways.

Examples

Namespaced Custom Resource

Setup

First of all, you need to have Custom Resource Definitions on your cluster.
This README will use the following Namespaced resource. You can apply it on your cluster with kubectl.

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: peaches.pykorm.infomaniak.com
spec:
  group: pykorm.infomaniak.com
  names:
    kind: Peach
    listKind: PeachList
    plural: peaches
    singular: peach
  scope: Namespaced
  versions:
  - name: v1
    served: true
    storage: true
    schema:
      openAPIV3Schema:
        type: object
        properties:
          spec:
            type: object
            properties:
              variety:
                type: string
            required:
              - variety


    additionalPrinterColumns:
    - name: Variety
      type: string
      description: The variety of the peach
      jsonPath: .spec.variety

Class definition

In order to link a python class to a kubernetes CustomResourceDefinition, you need to inherit the class from pykorm's NamespacedModel or ClusterModel and annotate it with the kubernetes CRD information like so:

import pykorm

@pykorm.k8s_custom_object('pykorm.infomaniak.com', 'v1', 'peaches')
class Peach(pykorm.NamespacedModel):
    variety: str = pykorm.fields.Spec('variety')

Notice that a class inheriting from pykorm.NamespacedModel already has the name and namespace fields setup.

Create a CR

In order to create a kubernetes custom resource from python, you just have to instantiate the class and save it with Pykorm.save():

import pykorm
pk = pykorm.Pykorm()

cake_peach = Peach(namespace='default', name='cake-peach', variety='Frost')
pk.save(cake_peach)  # We save the resource

as you can see, the model is instantly ensured in kubernetes:

$ kubectl get peach -n default
NAME         VARIETY
cake-peach   Frost

List resources

Pykorm can also list resources from kubernetes

>>> all_peaches = Peach.query.all()
>>> for peach in all_peaches:
>>>  print(peach)
<Peach namespace=default, name=cake-peach, variety=Frost>

# Filter by namespace
>>> Peach.query.filter_by(namespace='default').filter_by(variety='Frost').all()

You can even filter resources by some criterion:

>>> Peach.query.filter_by(name='cake-peach').all()
[<Peach namespace=default, name=cake-peach, variety=Frost>]
>>> Peach.query.filter_by(namespace='kube-system').all()
[]

Delete resources

You can delete a resource with pykorm too:

pk.delete(peach)
$ kubectl get peach
No resources found in default namespace.

More examples

For more examples, don't hesitate to look into the examples/ directory

Is pykorm stable ?

pykorm is still very young and very naive. It's also missing quite a lot of features (relationships, etc.). It was originally created because a lot of boilerplate code was written each time a kubernetes custom object had to be interfaced with python code.

Work on pykorm is actually on the way. Don't hesitate to contribute to the project if you have the energy for it !

Equivalences

Python Kubernetes
Class CustomResourceDefinition
Instance CustomResource

Metadata

Release files for pykorm 0.60.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pykorm 0.60.0
File Size Uploaded
pykorm-0.60.0.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pykorm 0.60.0
File Interpreter ABI Platform
pykorm-0.60.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.6 kB

Release files / pykorm-0.60.0.tar.gz

Download URL pykorm-0.60.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
63e46095d0c6c764731ab6cb48efde41e5afc82adc4e79d50b66da6e309f6e1f
BLAKE2b-256 checksum
How to use checksums
9d91d778b0af3f49bedbaaae6a73acb6b1618f22cba2c39e3b72a13a1c7ee065
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release files / pykorm-0.60.0-py3-none-any.whl

Download URL pykorm-0.60.0-py3-none-any.whl
Size 11.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a9eedcd9da1bff38133f941df5e643dbaa83e98e9af492fe67526a6a1b516487
BLAKE2b-256 checksum
How to use checksums
ac3dd1a72e7d8cf4b35c322fcac6856e509dc002c9881a87d8c2b3248c244f6a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.9.16

Release history Release notifications | RSS feed

This release

0.60.0 This release

2 release files

0.56.0

2 release files

0.55.1

2 release files

0.55.0

2 release files

0.54.0

2 release files

0.53.0

2 release files

0.52.0

2 release files

0.51.1

2 release files

0.51.0

2 release files

0.48.0

2 release files

0.45.2

2 release files

0.45.1

2 release files

0.45

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page