The Pizza Order Controller is a custom Kubernetes controller that forms the core of the Kubernetes Pizza Observability project. It's responsible for watching PizzaOrder custom resources, processing these orders by interacting with the Dominos Pizza API, and updating their status within the Kubernetes cluster. This enables the automated ordering of pizza when specific cluster conditions (like high CPU usage) are met.
The controller is built using Go and the controller-runtime library, adhering to the Kubernetes operator pattern. Its main architectural components are:
PizzaOrder Custom Resource Definition (CRD): Defines the PizzaOrder custom resource, which represents a pizza order within the Kubernetes cluster. This CRD includes fields for customer details, delivery address, pizza specifications, and payment information.pizza-controller.go): Contains the primary reconciliation logic. The Reconcile function is triggered when PizzaOrder resources are created, updated, or deleted. It handles the entire lifecycle of a pizza order.pizza-controller-deployment.yaml): Defines the necessary ServiceAccount, ClusterRole, and ClusterRoleBinding to grant the controller appropriate permissions to interact with Kubernetes API resources (e.g., PizzaOrder CRDs, Secrets).The controller's workflow is as follows:
PizzaOrder custom resources in the cluster.PizzaOrder resource is created with the spec.placeOrder field set to true, or an existing one is updated to this state, the reconciler is invoked.PizzaOrder spec.Secret referenced in spec.paymentSecret.name.status subresource of the PizzaOrder CRD with:
orderId: The ID returned by Dominos API upon successful order placement.status: The current status of the order (e.g., "Pending", "Placed", "Failed", "Delivered").price: The total price of the order.lastUpdateTime: Timestamp of the last status update.PizzaOrder status accordingly.kubectl configured to communicate with your cluster.PizzaOrder CRD must be installed in the cluster.Secret containing Dominos payment information must be created.If you've made changes to the controller code, you'll need to rebuild the Docker image:
# Navigate to the controller directory
cd kubernetes/controller
# Build the Docker image
docker build -t your-registry/pizza-controller:latest .
# Push to your container registry (e.g., Docker Hub, ACR, GCR)
# docker login your-registry
docker push your-registry/pizza-controller:latest
Make sure to update the image path in kubernetes/controller/pizza-controller-deployment.yaml or your Helm values.yaml if you use a custom registry or tag.
This is the easiest way to deploy the controller along with all its dependencies (CRD, RBAC, etc.).
# From the project root directory
helm install k8s-pizza helm/k8s-pizza-observability-chart --namespace k8s-pizza
Ensure your values.yaml for the Helm chart correctly points to the controller image and configures any necessary parameters.
kubectlIf you prefer manual deployment or are not using Helm:
# 1. Apply the PizzaOrder CRD
kubectl apply -f kubernetes/crds/pizzaorders.yaml
# 2. Create the Dominos Payment Secret (see Configuration section below)
kubectl apply -f kubernetes/dominos-payment-secret.example.yaml # Modify with real data first!
# 3. Deploy the Controller
kubectl apply -f kubernetes/controller/pizza-controller-deployment.yaml
dominos-payment-secret)The controller requires payment information to place orders. This is securely stored in a Kubernetes Secret. Create a Secret named dominos-payment-secret in the same namespace as the controller.
Example dominos-payment-secret.yaml:
apiVersion: v1
kind: Secret
metadata:
name: dominos-payment-secret
namespace: default # or your chosen namespace
type: Opaque
stringData:
cardType: "Credit" # or "Debit"
cardNumber: "4100123456789010" # Example card number (use a real one)
cardExpiration: "01/25" # MM/YY format
cardSecurityCode: "123" # CVV
cardPostalCode: "90210" # Billing ZIP/postal code
To create a pizza order, you need to create a PizzaOrder custom resource. Here's an example:
apiVersion: k8spizza.com/v1alpha1
kind: PizzaOrder
metadata:
name: pepperoni-special
spec:
customer:
firstName: "Pizza"
lastName: "Lover"
email: "pizza@example.com"
phone: "1234567890"
delivery:
street: "123 Main St"
city: "Anytown"
state: "NY"
zipCode: "10001"
pizza:
size: "large"
type: "pepperoni"
paymentSecret:
name: "dominos-payment-secret"
placeOrder: true # Set to true to actually place the order
To view the controller logs:
kubectl logs deployment/pizza-controller
To check the status of pizza orders:
# List all pizza orders
kubectl get pizzaorder
# Get detailed information about a specific order
kubectl describe pizzaorder pepperoni-special
PizzaOrder CRD is installed in the cluster.PizzaOrder resource.--v flag in the deployment.To develop and test the controller locally:
# Clone the repository
git clone https://github.com/yourusername/k8s-pizza-observability.git
cd k8s-pizza-observability/kubernetes/controller
# Build the controller
go build -o pizza-controller
# Run the controller locally (pointing to your kubeconfig)
./pizza-controller --kubeconfig=$HOME/.kube/config
To extend the controller with new features:
PizzaOrder CRD to include new fields if necessary.pizza-controller.go.Potential improvements for the controller include: