Package: cdk8s-operator
cdk8s-operatorCreate Kubernetes CRD Operators using CDK8s ConstructsThis 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 StartedLet's create our first CRD served by a CDK8s construct using TypeScript.Install CDK8sMake sure your system has the required CDK8sprerequisites.Install the CDK8s CLI globally through npm:$npmi-gcdk8s-cli
Installing...# Verify installation$cdk8s--version1.0.0-beta.3Create a new CDK8s appNow, let's create a new CDK8s typescript app:mkdirhello-operator&&cdhello-operator
gitinit
cdk8sinittypescript-appInstall cdk8s-operatorNext, let's install this module as a dependency of our TypeScript project:npminstallcdk8s-operatorConstructWe will start by creating the construct that implements the abstraction. This is
is just a normal CDK8s custom construct:Let's create a construct calledPodCollectionwhich represents a collection of
pods:pod-collection.ts:import{Pod}from'cdk8s-plus-17';import{Construct}from'constructs';exportinterfacePodCollectionProps{/**Numberofpods*/readonlycount:number;/**Thedockerimagetodeploy*/readonlyimage:string;}exportclassPodCollectionextendsConstruct{constructor(scope:Construct,id:string,props:PodCollectionProps){super(scope,id);for(leti=0;i<props.count;++i){newPod(this,`pod-${i}`,{containers:[{image:props.image}]});}}}Operator AppNow, we will need to replace outmain.tsfile with an "operator app", which is
a special kind of CDK8s app designed to be executed by thecdk8s-serverCLI
which is included in this module.TheOperatorapp 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 thespecas its input and emits the resulting
manifest to STDOUT.Replace the contents ofmain.tswith the following. We initialize anOperatorapp and then register a provider which handles resources of API
versionsamples.cdk8s.org/v1alpha1and kindPodCollection.main.ts:import{Operator}from'cdk8s-operator';import{PodCollection}from'./pod-collection';constapp=newOperator();app.addProvider({apiVersion:'samples.cdk8s.org/v1alpha1',kind:'PodCollection',handler:{apply:(scope,id,props)=>newPodCollection(scope,id,props)}})app.synth();A single operator can handle any number of resource kinds. Simply calladdProvider()for each apiVersion/kind.Using OperatorsTo use this operator, create aninput.jsonfile, 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-fmain.test.*# compile$npmruncompileAnd run:$nodemain.jsinput.jsonSTDOUTapiVersion:"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-serverThis library is shipped with a program calledcdk8s-serverwhich 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=8080npxcdk8s-server
Listeningon8080-Appcommand:nodemain.js
-Requestbodyshouldincludeasinglek8sresourceinJSONformat
-RequestwillbepipedthroughSTDINto"node main.js"-ResponseistheSTDOUTandexpectedtobeamulti-resourceyamlmanifestNow, you can sendinput.jsonover HTTP:$curl-d@input.jsonhttp://localhost:8080
MANIFEST...LicenseApache 2.0