Working with CRDs

With k8s working with CRDs is very much like the way you would work with Deployment, Service or any of the other resources provided by Kubernetes. Creating a CRD in the cluster is also quite straight forward.

Defining your CRD in the cluster

In your application, you can create a CustomResourceDefinition. When you save this to the cluster, your CRD is created. Kubernetes uses some time to make things ready, so you will need to poll the API to see when it gets ready.

Example:

name = "%s.%s" % ("applications", "fiaas.schibsted.io")
metadata = ObjectMeta(name=name)
names = CustomResourceDefinitionNames(kind="Application", plural="applications", shortNames=("app", "fa"))
spec = CustomResourceDefinitionSpec(group="fiaas.schibsted.io", names=names, version="v1")
definition = CustomResourceDefinition.get_or_create(metadata=metadata, spec=spec)
definition.save()

Defining your CRD in code

When a CRD is defined in the API, it can be used like any other Model, but you need to define it yourself.

This is similar to how we describe the built in models in this library. Check out Adding support for new object types for details.

Example:

class ApplicationSpec(Model):
    application = RequiredField(str)
    image = RequiredField(str)
    config = RequiredField(dict)

class Application(Model):
    class Meta:
        list_url = "/apis/fiaas.schibsted.io/v1/applications"
        url_template = "/apis/fiaas.schibsted.io/v1/namespaces/{namespace}/applications/{name}"
        watch_list_url = "/apis/fiaas.schibsted.io/v1/watch/applications"
        watch_list_url_template = "/apis/fiaas.schibsted.io/v1/watch/namespaces/{namespace}/applications"

    # Workaround for https://github.com/kubernetes/kubernetes/issues/44182
    apiVersion = Field(str, "fiaas.schibsted.io/v1")  # NOQA
    kind = Field(str, "Application")

    metadata = Field(ObjectMeta)
    spec = Field(ApplicationSpec)