Kubernetes

Overview

Very similar to how things are done with terraform configs, we create a context and add kubernetes objects to it:

from p10s import k8s, value

c = k8s.Context()

deployment = k8s.Deployment(yaml("""
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx-deployment
      labels:
        app: nginx
    spec:
"""))

deployment.body['metadata']['labels']['env'] = value('ENV')

c += deployment

c += k8s.Service(yaml("""
    kind: Service
    apiVersion: v1
    metadata:
      name: my-service
    spec:
        ...
"""))

...

While you can always pass in a dict object to the various KubernetesObject class constructors it’s usually easier, and closer to what you already know, to use the yaml helper to parse a bock of yaml code and start working with that. you can use the apiVersion, metadata, and spec properties no matter how the object was created.

The various KubernetesObject clsses exist only for readability and convenience, creating a base KubernetesObject and passing in kind is the fully equivalent. This is also the way to create ojects for kinds that are not pre-defined.

p10s takes advantage of the fact that you can have multiple kubernetes objects in the same yaml file (represented as multiple kubernetes documents). Multiple KubernetesObject objects can be added to the same k8s.Context, in the same p10s script, and kubernetes and helm will be able to properly parse it.

Objects

class p10s.kubernetes.Deployment(data=None, kind=None)[source]
class p10s.kubernetes.ConfigMap(data=None, kind=None)[source]
class p10s.kubernetes.Service(data=None, kind=None)[source]
class p10s.kubernetes.Job(data=None, kind=None)[source]
class p10s.kubernetes.StatefulSet(data=None, kind=None)[source]
class p10s.kubernetes.Ingress(data=None, kind=None)[source]
class p10s.kubernetes.Secret(data=None, kind=None)[source]

Helpers

p10s.kubernetes.from_yaml(yaml_input)[source]

Parse the given yaml_input and return the corresponding KubernetesObject

p10s.kubernetes.many_from_yaml(yaml_input)[source]

Parse the documents from yaml_input and returns a list of KubernetesObject.

Base Classes

class p10s.kubernetes.Context(*args, data=None, **kwargs)[source]

Context class for generating kubernetes and helm files.

Really is just a YAML context.

class p10s.kubernetes.KubernetesObject(data=None, kind=None)[source]
property apiVersion

Property mapping this object’s apiVersion value

property kind

Property mapping this object’s kind value

property metadata

Property mapping this object’s metadata value

property spec

Property mapping this object’s spec value