Skip to main content

cdk8s-operator

Project description

cdk8s-operator

Create Kubernetes CRD Operators using CDK8s Constructs

This is a multi-language (jsii) library and a command-line tool that allows you to create Kubernetes operators for CRDs (Custom Resource Definitions) using CDK8s.

Getting Started

Let's create our first CRD served by a CDK8s construct using TypeScript.

Install CDK8s

Make sure your system has the required CDK8s prerequisites.

Install the CDK8s CLI globally through npm:

$ npm i -g cdk8s-cli
Installing...

# Verify installation
$ cdk8s --version
1.0.0-beta.3

Create a new CDK8s app

Now, let's create a new CDK8s typescript app:

mkdir hello-operator && cd hello-operator
git init
cdk8s init typescript-app

Install cdk8s-operator

Next, let's install this module as a dependency of our TypeScript project:

npm install cdk8s-operator

Construct

We will start by creating the construct that implements the abstraction. This is is just a normal CDK8s custom construct:

Let's create a construct called PodCollection which represents a collection of pods:

pod-collection.ts:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk8s_plus_17 import Pod
from constructs import Construct

class PodCollection(Construct):
    def __init__(self, scope, id, *, count, image):
        super().__init__(scope, id)for (let i = 0; i < props.count; ++i) {
              new Pod(this, `pod-${i}`, {
                containers: [ { image: props.image } ]
              });
            }

Operator App

Now, we will need to replace out main.ts file with an "operator app", which is a special kind of CDK8s app designed to be executed by the cdk8s-server CLI which is included in this module.

The Operator app construct can be used to create "CDK8s Operators" which are CDK8s apps that accept input from a file (or STDIN) with a Kubernetes manifest, instantiates a construct with the spec as its input and emits the resulting manifest to STDOUT.

Replace the contents of main.ts with the following. We initialize an Operator app and then register a provider which handles resources of API version samples.cdk8s.org/v1alpha1 and kind PodCollection.

main.ts:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
from cdk8s_operator import Operator
from ..pod_collection import PodCollection

app = Operator()

app.add_provider(
    api_version="samples.cdk8s.org/v1alpha1",
    kind="PodCollection",
    handler={
        "apply": (scope, id, props) => new PodCollection(scope, id, props)
    }
)

app.synth()

A single operator can handle any number of resource kinds. Simply call addProvider() for each apiVersion/kind.

Using Operators

To use this operator, create an input.json file, e.g:

input.json:

{
  "apiVersion": "samples.cdk8s.org/v1alpha1",
  "kind": "PodCollection",
  "metadata": {
    "name": "my-collection"
  },
  "spec": {
    "image": "paulbouwer/hello-kubernetes",
    "count": 5
  }
}

Compile your code:

# delete `main.test.ts` since it has some code that won't compile
$ rm -f main.test.*

# compile
$ npm run compile

And run:

$ node main.js input.json
STDOUT
apiVersion: "v1"
kind: "Pod"
metadata:
  name: "my-collection-pod-0-c8735c52"
spec:
  containers:
    - env: []
      image: "paulbouwer/hello-kubernetes"
      imagePullPolicy: "Always"
      name: "main"
      ports: []
      volumeMounts: []
  volumes: []
---
apiVersion: "v1"
kind: "Pod"
metadata:
  name: "my-collection-pod-1-c89f58d7"
spec:
  containers:
    - env: []
      image: "paulbouwer/hello-kubernetes"
      imagePullPolicy: "Always"
      name: "main"
      ports: []
      volumeMounts: []
  volumes: []
---
apiVersion: "v1"
kind: "Pod"
metadata:
  name: "my-collection-pod-2-c88d4268"
spec:
  containers:
    - env: []
      image: "paulbouwer/hello-kubernetes"
      imagePullPolicy: "Always"
      name: "main"
      ports: []
      volumeMounts: []
  volumes: []
---
apiVersion: "v1"
kind: "Pod"
metadata:
  name: "my-collection-pod-3-c86866b1"
spec:
  containers:
    - env: []
      image: "paulbouwer/hello-kubernetes"
      imagePullPolicy: "Always"
      name: "main"
      ports: []
      volumeMounts: []
  volumes: []
---
apiVersion: "v1"
kind: "Pod"
metadata:
  name: "my-collection-pod-4-c8b74b1d"
spec:
  containers:
    - env: []
      image: "paulbouwer/hello-kubernetes"
      imagePullPolicy: "Always"
      name: "main"
      ports: []
      volumeMounts: []
  volumes: []

cdk8s-server

This library is shipped with a program called cdk8s-server which can be used to host your operator inside an HTTP server. This server can be used as a sidecar container with a generic CRD operator (TBD).

$ PORT=8080 npx cdk8s-server
Listening on 8080
- App command: node main.js
- Request body should include a single k8s resource in JSON format
- Request will be piped through STDIN to "node main.js"
- Response is the STDOUT and expected to be a multi-resource yaml manifest

Now, you can send input.json over HTTP:

$ curl -d @input.json http://localhost:8080
MANIFEST...

License

Apache 2.0

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cdk8s-operator-0.0.63.tar.gz (130.7 kB view details)

Uploaded Source

Built Distribution

cdk8s_operator-0.0.63-py3-none-any.whl (128.8 kB view details)

Uploaded Python 3

File details

Details for the file cdk8s-operator-0.0.63.tar.gz.

File metadata

  • Download URL: cdk8s-operator-0.0.63.tar.gz
  • Upload date:
  • Size: 130.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for cdk8s-operator-0.0.63.tar.gz
Algorithm Hash digest
SHA256 4d505913bdca1f5f06964a29bae6dafc0f465ae5048148437c7dfd745ab65d8f
MD5 58a7e8625a24e0695bab91528d912e9d
BLAKE2b-256 df8531060929d1da4e57730bee6c9bfe0c8339832a5bb71b549f0f2c55a2175d

See more details on using hashes here.

File details

Details for the file cdk8s_operator-0.0.63-py3-none-any.whl.

File metadata

  • Download URL: cdk8s_operator-0.0.63-py3-none-any.whl
  • Upload date:
  • Size: 128.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.7.9

File hashes

Hashes for cdk8s_operator-0.0.63-py3-none-any.whl
Algorithm Hash digest
SHA256 0b5f2d600e81da140d902e37a83fcf5874d996ad3f96cd2771438566a4ce6c13
MD5 b6139d665edf01979fddb880318706c4
BLAKE2b-256 d7dd6d79973b92c5a10ce8d0afdf6471b2c001e71edaaf8aff891b21bc35eaf4

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page