Skip to main content

Pykorm: a dead simple Kubernetes ORM

Project description

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) # pk.apply(cake_peach) # you can apply 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

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

pykorm-0.51.0.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

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

pykorm-0.51.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file pykorm-0.51.0.tar.gz.

File metadata

  • Download URL: pykorm-0.51.0.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.9

File hashes

Hashes for pykorm-0.51.0.tar.gz
Algorithm Hash digest
SHA256 d7dc12decf5b87826689cdf45800d26bd8551300abaed46857eaf0796f46f20e
MD5 9a88d0d6ce95ab40bfc38ec185f051ff
BLAKE2b-256 b0627f8a18e55fabeea12b9b62b7379729c9fd4b361f784c2fe63cebcebac8e7

See more details on using hashes here.

File details

Details for the file pykorm-0.51.0-py3-none-any.whl.

File metadata

  • Download URL: pykorm-0.51.0-py3-none-any.whl
  • Upload date:
  • Size: 10.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.0 requests/2.24.0 setuptools/50.3.2 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.7.9

File hashes

Hashes for pykorm-0.51.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a099c4e21221c4135a989cbbc13934940cec4f23ba9c6b40ee1fe7fa13d9802
MD5 a9a906c3bfeab47d6b0e23085c9e57c0
BLAKE2b-256 83065fcdece108cff1bd7b6abdf34caac278310ff10c6fb72934f92f28c70f76

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