We can define custom K8s resources (objects) using CRDs.

Let’s consider an example of creating a FlightTicket object using the definition file below. Creating a resource using the definition file below will throw an error as FlightTicket object is not yet defined in K8s. We first need to create a CRD for it.

apiVersion: flights.com/v1
kind: FlightTicket
metadata:
	name: my-flight-ticket
spec:
	from: Mumbai
	to: London
	count: 2

The CRD to create FlightTicket object in K8s:

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
	name: flighttickets.flights.com
spec:
	scope: Namespaced
	group: flights.com
	names:
		kind: FlightTicket
		singular: flightticket
		plural: flighttickets
		shortNames:
			- ft
	versions:
		- name: v1
			served: true
			storage: true
			schema:
				openAPIV3Schema:
					type: object
					properties:
						spec:
							type: object
							properties:
								from:
									type: string
								to:
									type: string
								count:
									type: integer
									minimum: 1
									maximum: 10

Custom Controllers

Creating a customer resource in K8s such as FlightTicket doesn’t do much. It’s just a configuration saved in the etcd store. To actually book a flight ticket when a FlightTicket object is created, we need to write a custom controller which will continuously monitor the FlightTicket objects in the etcd store and make API calls to a flight booking service.