0
0
Kubernetesdevops~10 mins

Operator pattern overview in Kubernetes - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a basic Custom Resource Definition (CRD) in Kubernetes.

Kubernetes
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: myresources.example.com
spec:
  group: example.com
  versions:
  - name: v1
    served: true
    storage: true
  scope: [1]
  names:
    plural: myresources
    singular: myresource
    kind: MyResource
    shortNames:
    - mr
Drag options to blanks, or click blank then click option'
AGlobal
BNamespaced
CCluster
DUniversal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Cluster' when the resource should be namespaced.
Confusing 'Global' or 'Universal' which are not valid scope values.
2fill in blank
medium

Complete the command to create an Operator using the Operator SDK CLI.

Kubernetes
operator-sdk init --domain example.com --repo github.com/example/[1]
Drag options to blanks, or click blank then click option'
Ak8s-operator
Boperator-sdk
Cmy-operator
Dexample-operator
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic names like 'operator-sdk' which is the tool name, not the repo.
Using names with spaces or invalid characters.
3fill in blank
hard

Fix the error in the reconciliation function signature in a Go Operator controller.

Kubernetes
func (r *MyResourceReconciler) Reconcile(ctx context.Context, req [1]) (ctrl.Result, error) {
Drag options to blanks, or click blank then click option'
Actrl.Request
Bclient.Request
Creconcile.Request
DRequest
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect package prefixes like client.Request or reconcile.Request.
Omitting the package prefix causing undefined type errors.
4fill in blank
hard

Fill both blanks to create a watch on a Kubernetes resource in the Operator controller setup.

Kubernetes
func (r *MyResourceReconciler) SetupWithManager(mgr ctrl.Manager) error {
  return ctrl.NewControllerManagedBy(mgr).
    For(&[1]{}).
    Owns(&[2]{}).
    Complete(r)
}
Drag options to blanks, or click blank then click option'
Aexamplev1.MyResource
Bcorev1.Pod
Cappsv1.Deployment
Dbatchv1.Job
Attempts:
3 left
💡 Hint
Common Mistakes
Using corev1.Pod as the primary resource instead of the custom resource.
Mixing up the order of For and Owns.
5fill in blank
hard

Fill all three blanks to define a simple reconciliation loop that fetches a resource, checks if it exists, and returns without error if not found.

Kubernetes
func (r *MyResourceReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
  var resource [1]
  err := r.Client.Get(ctx, req.NamespacedName, &[3])
  if [2] {
    if errors.IsNotFound(err) {
      return ctrl.Result{}, nil
    }
    return ctrl.Result{}, err
  }
  // Continue reconciliation logic here
  return ctrl.Result{}, nil
}
Drag options to blanks, or click blank then click option'
Aexamplev1.MyResource
Berr == nil
Cerr != nil
Dresource
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if err == nil instead of err != nil.
Using wrong variable types for the resource.
Not passing the address of the resource variable to Get.