Bird
Raised Fist0
Kubernetesdevops~10 mins

Operator pattern overview in Kubernetes - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of the Kubernetes Operator pattern?
easy
A. To replace Kubernetes core components
B. To automate application management tasks on Kubernetes
C. To manually configure pods and services
D. To monitor network traffic between nodes

Solution

  1. Step 1: Understand the Operator pattern role

    The Operator pattern automates tasks like deployment, scaling, and updates for applications on Kubernetes.
  2. Step 2: Compare options with the pattern's purpose

    Only To automate application management tasks on Kubernetes describes automation of app management, which matches the Operator's goal.
  3. Final Answer:

    To automate application management tasks on Kubernetes -> Option B
  4. Quick Check:

    Operator automates app management = A [OK]
Hint: Operators automate apps, not replace Kubernetes core [OK]
Common Mistakes:
  • Thinking Operators replace Kubernetes components
  • Confusing manual config with automation
  • Assuming Operators handle network monitoring
2. Which Kubernetes resource is essential for an Operator to manage custom application logic?
easy
A. Pod
B. Service
C. Custom Resource Definition (CRD)
D. ConfigMap

Solution

  1. Step 1: Identify resource for extending Kubernetes

    Operators use Custom Resource Definitions (CRDs) to add new resource types representing app-specific data.
  2. Step 2: Match resource with Operator management

    CRDs enable Operators to watch and act on custom resources, unlike Pods, Services, or ConfigMaps.
  3. Final Answer:

    Custom Resource Definition (CRD) -> Option C
  4. Quick Check:

    CRD extends Kubernetes for Operators = B [OK]
Hint: CRDs define custom resources Operators manage [OK]
Common Mistakes:
  • Choosing Pod or Service which are standard resources
  • Confusing ConfigMap with custom resource definitions
  • Not knowing CRD extends Kubernetes API
3. Given this Operator controller snippet watching a custom resource, what will happen when a new resource instance is created?
func (r *MyOperatorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    var app MyApp
    if err := r.Get(ctx, req.NamespacedName, &app); err != nil {
        return ctrl.Result{}, client.IgnoreNotFound(err)
    }
    // Logic to create or update deployment based on app spec
    return ctrl.Result{}, nil
}
medium
A. The Operator will crash due to missing deployment code
B. The Operator will delete the custom resource immediately
C. The Operator will ignore the new resource and do nothing
D. The Operator will create or update a deployment matching the custom resource spec

Solution

  1. Step 1: Analyze Reconcile function behavior

    The function fetches the custom resource and applies logic to create or update a deployment accordingly.
  2. Step 2: Understand Operator reaction to resource creation

    When a new resource instance is created, the Operator reconciles state by creating/updating deployments to match spec.
  3. Final Answer:

    The Operator will create or update a deployment matching the custom resource spec -> Option D
  4. Quick Check:

    Reconcile creates/updates deployment = A [OK]
Hint: Reconcile syncs resources to desired state [OK]
Common Mistakes:
  • Thinking Operator deletes resource on creation
  • Assuming Operator ignores new resources
  • Believing missing code causes crash here
4. You wrote an Operator but it never reacts to changes in your custom resource. What is the most likely cause?
medium
A. The Operator's controller is not watching the Custom Resource Definition
B. The Kubernetes cluster is down
C. The custom resource YAML is invalid and rejected
D. The Operator is missing RBAC permissions for Pods

Solution

  1. Step 1: Identify why Operator ignores resource changes

    If the controller does not watch the custom resource, it won't get events to trigger reconciliation.
  2. Step 2: Compare other options

    Cluster down or invalid YAML would cause errors, not silent ignoring. Missing Pod RBAC affects pod actions, not event watching.
  3. Final Answer:

    The Operator's controller is not watching the Custom Resource Definition -> Option A
  4. Quick Check:

    Controller watch missing = no reactions = D [OK]
Hint: Ensure controller watches CRD to react to changes [OK]
Common Mistakes:
  • Assuming cluster down without checking logs
  • Blaming YAML without validation errors
  • Confusing RBAC for Pods with watching permissions
5. You want to build an Operator that manages a database cluster with automatic backups and scaling. Which two Kubernetes concepts must you combine to implement this Operator effectively?
hard
A. Custom Resource Definitions and Controllers
B. ConfigMaps and Secrets
C. Ingress and Network Policies
D. DaemonSets and StatefulSets

Solution

  1. Step 1: Identify core Operator components

    Operators use Custom Resource Definitions (CRDs) to define new resource types and Controllers to manage their lifecycle.
  2. Step 2: Evaluate other Kubernetes concepts

    ConfigMaps and Secrets store config data, Ingress and Network Policies manage traffic, DaemonSets and StatefulSets manage pods but don't implement custom logic.
  3. Final Answer:

    Custom Resource Definitions and Controllers -> Option A
  4. Quick Check:

    CRDs + Controllers build Operators = C [OK]
Hint: Operators = CRDs + Controllers for custom logic [OK]
Common Mistakes:
  • Confusing config storage with Operator logic
  • Mixing networking resources with Operator pattern
  • Thinking pod controllers alone build Operators