Kubernetes: Generate a CRD definition from an object

Kubernetes CRD generator

2 min read | by Jordi Prats

Let's face it, manually define a openAPIV3Schema definition is no easy task, so why not automating it? We can use this online CRD generator to be able to create it's definition from one sample object

Using the following sample object:

apiVersion: demo.pet2cattle.com/v1 kind: CronTab metadata:  name: my-new-cron-object spec:  cronSpec: "* * * * */5" 

We'll get de CustomResourceDefinition that would allow us to create it:

apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata:  name: crontabs.demo.pet2cattle.com spec: group: demo.pet2cattle.com names:  kind: CronTab  plural: crontabs scope: Namespaced versions: - name: v1  served: true  storage: true  schema:  openAPIV3Schema:  type: object  properties:  spec:  type: object  properties:  cronSpec:  type: string 

We can use it's offline cli tool, it takes the object as using the stdin:

$ cat samples/cron.yaml | python3 main.py 2>/dev/null apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition (...) 

We can use it to be able to create any object we need, even emulate an OpenShift cluster by creating all the CRD definitions to be able to persist certain objects (without it's functionality)


Posted on 21/11/2022

Categories