Skip to content
Hands-on IT IT Drills

Appendix B: Final Exam

Goal: Four tasks without instructions that tie together everything from the book. · Duration: approx. 90 min · Prerequisite: Days 0–15

Rules of Play

  • Work in ~/kro-lab/pruefung with a copy of your NotesApp RGD.
  • Each task has check commands. Only when they show what is written there is the task solved.
  • The solutions are in ~/kro-lab/code/loesung/pruefung/. Their starting point is the RGD after the competition (wettkampf/loesung/notesapp-rgd.yaml).
mkdir -p ~/kro-lab/pruefung
cd ~/kro-lab/pruefung
cp ~/kro-lab/tag09/notesapp-rgd.yaml .

Exercise 1: Pause

A team wants to pause its NotesApp overnight without deleting it. Build a field paused (default false). If it is true, no notes-api pod runs anymore. The database and the Service stay in place, and the instance still reports Ready.

Check:

kubectl -n app patch notesapp notes --type merge -p '{"spec":{"paused":true}}'
kubectl -n app wait notesapp/notes --for=condition=Ready --timeout=60s
kubectl -n app get deploy notes

Expected:

notesapp.lab.example.com/notes patched
notesapp.lab.example.com/notes condition met
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
notes   0/0     0            0           5h34m

Solution

In the schema:

      paused: boolean | default=false

In the Deployment:

          replicas: '${schema.spec.paused ? 0 : schema.spec.replicas}'

The single quotes are mandatory; otherwise you trip over the colon from Day 3. The readyWhen conditions stay unchanged: with zero desired and zero available pods, both are true. Turn it back on:

kubectl -n app patch notesapp notes --type merge -p '{"spec":{"paused":false}}'
kubectl -n app wait notesapp/notes --for=condition=Ready --timeout=90s
curl -s localhost:30080/version

Expected:

notesapp.lab.example.com/notes patched
notesapp.lab.example.com/notes condition met
0.5.1

Save your progress? Sign up for free

Exercise 2: Memory Limit

Every notes-api container should get a memory limit. The team may set it, default 64Mi, and only in Mi. The API server should reject a value like 1Gi or viel.

Check:

kubectl -n app get deploy notes \
  -o jsonpath='{.spec.template.spec.containers[0].resources}{"\n"}'
kubectl -n app patch notesapp notes --type merge -p '{"spec":{"memory":"viel"}}'

Expected:

{"limits":{"memory":"64Mi"}}
The NotesApp "notes" is invalid: spec.memory: Invalid value: "viel": spec.memory in
body should match '^[0-9]+Mi$'

Solution

      memory: string | default="64Mi" pattern="^[0-9]+Mi$"

In the container api, before the readinessProbe:

                  resources:
                    limits:
                      memory: ${schema.spec.memory}

The type matches: in Kubernetes, memory is a quantity that is also accepted as text.

Save your progress? Sign up for free

Exercise 3: Is the Database There?

The instance should show a field dbReady (true or false) in its status that says whether Postgres is ready.

Check:

kubectl -n app get notesapp notes -o jsonpath='dbReady={.status.dbReady}{"\n"}'

Expected:

dbReady=true

Solution

      dbReady: ${database.status.?readyReplicas.orValue(0) == 1}

kro derives the type boolean from it. If the instance jumps to ERROR with schema not found for field dbReady, you know the remedy: kubectl -n kro-system rollout restart deploy/kro.

Save your progress? Sign up for free

Exercise 4: The Overview

The platform team wants to know which NotesApps exist in the cluster. Build a new, cluster-scoped API NotesOverview. An instance counts all NotesApps in all namespaces, shows them in the status and writes the list into a ConfigMap notes-overview in the namespace platform. When a NotesApp is added, the overview updates itself.

Check:

kubectl get notesoverview alle -o jsonpath='{.status.total} | {.status.apps}{"\n"}'
kubectl -n platform get configmap notes-overview -o jsonpath='{.data.apps}{"\n"}'

Expected:

2 | app/notes, blau-dev/notes
app/notes, blau-dev/notes

Note: An external collection without namespace searches all namespaces. An empty selector {} matches everything. Remember the permissions (Day 12).

Solution

cat > overview-rgd.yaml <<'EOF'
apiVersion: kro.run/v1alpha1
kind: ResourceGraphDefinition
metadata:
  name: notesoverview
spec:
  schema:
    apiVersion: v1alpha1
    kind: NotesOverview
    group: lab.example.com
    scope: Cluster
    spec:
      target: string | default="platform"
    status:
      total: ${size(apps)}
      apps: ${apps.map(a, a.metadata.namespace + "/" + a.metadata.name).join(", ")}
  resources:
    - id: apps
      externalRef:
        apiVersion: lab.example.com/v1alpha1
        kind: NotesApp
        metadata:
          selector: {}
    - id: summary
      template:
        apiVersion: v1
        kind: ConfigMap
        metadata:
          name: notes-overview
          namespace: ${schema.spec.target}
        data:
          apps: ${apps.map(a, a.metadata.namespace + "/" + a.metadata.name).join(", ")}
EOF
cat > overview-rbac.yaml <<'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: kro:controller:overview
  labels:
    rbac.kro.run/aggregate-to-controller: "true"
rules:
  - apiGroups: ["lab.example.com"]
    resources: ["notesoverviews", "notesoverviews/status"]
    verbs: ["*"]
EOF
cat > overview.yaml <<'EOF'
apiVersion: lab.example.com/v1alpha1
kind: NotesOverview
metadata:
  name: alle
spec: {}
EOF
kubectl apply -f overview-rbac.yaml
kubectl apply -f overview-rgd.yaml
kubectl wait --for=condition=Ready rgd/notesoverview --timeout=90s
kubectl apply -f overview.yaml
kubectl wait notesoverview/alle --for=condition=Ready --timeout=60s

Expected:

clusterrole.rbac.authorization.k8s.io/kro:controller:overview created
resourcegraphdefinition.kro.run/notesoverview created
resourcegraphdefinition.kro.run/notesoverview condition met
notesoverview.lab.example.com/alle created
notesoverview.lab.example.com/alle condition met
  • scope: Cluster, which is why the ConfigMap needs an explicit namespace.
  • kro has been allowed to read notesapps since Day 12 (kro:controller:notes). Only the permissions for notesoverviews are new.
  • An RGD may consist of a single external reference plus one object. It doesn’t have to look like an application.

As a cross-check, create another environment (extend blau with test). The number in the overview rises to three without you touching the overview.

Save your progress? Sign up for free

Done

When all four exercises pass their check commands, you have completed your drills. You can:

  • build your own APIs for Kubernetes with schema, defaults and validation,
  • wire objects together via CEL and read kro’s validation messages,
  • use readiness, conditions, collections and external references,
  • compose APIs from building blocks,
  • run, upgrade and monitor kro with restricted permissions,
  • decide when kro fits and when Helm, Kustomize or an operator does.

Clean up when you no longer need the lab:

exit
multipass delete --purge kro