Simulate a OpenShift cluster using CRD objects

OpenShift Kubernetes CRD

3 min read | by Jordi Prats

Openshift has some objects that doesn't exists on Kubernetes such as Project or Route. If we try to push one of these OpenShift objects into a vanilla Kubernetes we'll get an error:

$ kubectl apply -f route.yaml error: resource mapping not found for name: "demo-route" namespace: "" from "route.yaml": no matches for kind "Route" in version "route.openshift.io/v1" ensure CRDs are installed first 

For CI/CD we might not need a fully features OpenShift cluster, just being able to push the objects might be enough, with the CRD generator we can create the needed CRDs to be able to create these OpenShift objects

We can take the object we need to create and paste it:

apiVersion: route.openshift.io/v1 kind: Route metadata:  name: demo-route spec:  host: demo.local  to:  kind: Service  name: not-important  weight: 100  wildcardPolicy: None  tls:  caCertificate: "-----BEGIN CERTIFICATE-----\r\nMIIAAA\r\n-----END CERTIFICATE-----\r\n"  certificate: "-----BEGIN CERTIFICATE-----\r\nMIIAAA\r\n-----END CERTIFICATE-----\r\n"  insecureEdgeTerminationPolicy: Redirect  key: "-----BEGIN RSA PRIVATE KEY-----\r\nMIIAAA\r\n-----END RSA PRIVATE KEY-----\r\n"  termination: edge 

It is going to generate a CustomResourceDefinition as follows to match this object:

apiVersion: apiextensions.k8s.io/v1 kind: CustomResourceDefinition metadata:  name: routes.route.openshift.io spec:  group: route.openshift.io  names:  kind: Route  plural: routes  scope: Namespaced  versions:  - name: v1  served: true  storage: true  schema:  openAPIV3Schema:  type: object  properties:  spec:  type: object  properties:  host:  type: string  to:  type: object  properties:  kind:  type: string  name:  type: string  weight:  type: integer  wildcardPolicy:  type: string  tls:  type: object  properties:  caCertificate:  type: string  certificate:  type: string  insecureEdgeTerminationPolicy:  type: string  key:  type: string  termination:  type: string 

We can apply it as usual:

$ kubectl apply -f crd_route.yaml customresourcedefinition.apiextensions.k8s.io/routes.route.openshift.io created 

Once the CRD available we'll be able to create Routes as if it was an OpenShift cluster:

$ kubectl apply -f demo.yaml route.route.openshift.io/demo-route created 

Although, of course, it won't be doing anything it all since there is no controller using the CRD: We are just going to be able to add the objects but for some contexts might be more than enough.

You can find the code that translates objects into CustomResourceDefinition in GitHub: pet2cattle/python-k8s2crd, the online tool CRD generator fetches the code from this repo, using pyodine (WebAssembly) to run python code from the browser


Posted on 17/10/2022

Categories