Skip to content

Declarative Resource Management: CNPA practice questions

Declarative resource management means describing the state you want rather than the steps to get there, and letting software work out and apply the difference. Kubernetes is built around it. Objects carry a spec that records the desired state and a status that reports the current one, and controllers keep working to bring the two together.

By Ibrahim Yesilay · Published · Updated

Independent community practice, not affiliated with or endorsed by CNCF or The Linux Foundation.

What Declarative Resource Management covers in CNPA

Declarative resource management means describing the state you want rather than the steps to get there, and letting software work out and apply the difference. Kubernetes is built around it. Objects carry a spec that records the desired state and a status that reports the current one, and controllers keep working to bring the two together. For a platform team this is what makes self-service, review and automation possible: a desired state written in a file can be stored in Git, reviewed in a pull request, reproduced in another environment and reapplied after a failure.

The Kubernetes documentation separates three management techniques. Imperative commands such as kubectl create deployment act directly on live objects; they are quick for experiments but leave no record, no review step and no template. Imperative object configuration (kubectl create -f, replace -f, delete -f) names the operation and a file; replace overwrites the whole spec and drops anything missing from the file, which breaks fields that the cluster updates on its own. Declarative object configuration (kubectl apply -f on files or directories) lets kubectl detect per object whether to create, patch or delete. It writes only the observed differences with a patch, so changes made by other writers survive. The documentation warns that an object should be managed with only one technique, because mixing them for the same object results in undefined behavior.

The workflow it recommends is to preview with kubectl diff and then run kubectl apply, recursively with -R where needed. Client-side apply stores what was last applied in the kubectl.kubernetes.io/last-applied-configuration annotation and uses it to work out what to remove. Server-side apply moves that logic into the API server: it tracks which field manager owns each field in managedFields, and an apply that tries to change a field owned by another manager is reported as a conflict instead of silently overwriting it. Kustomize, built into kubectl since 1.14 and used with kubectl apply -k, and Helm charts are common ways to generate declarative manifests for several environments.

GitOps builds directly on this. The first OpenGitOps principle is that a system managed by GitOps must have its desired state expressed declaratively; the others require that state to be versioned and immutable, pulled automatically by software agents and continuously reconciled.

Common mistakes are treating a YAML file as declarative while still deploying it with create or replace, assuming declarative means nothing can drift (it can, until something reconciles it), and expecting kubectl apply to delete objects whose files were removed. The documentation recommends kubectl delete -f as the explicit way to delete, and warns that the alternative, apply with --prune, needs care because the selected objects can be unexpectedly deleted or retained.

At associate level, expect to choose between imperative and declarative approaches for a scenario, to recognize the benefits of declarative configuration for audit, review and repeatability, and to connect desired state in files with the reconciliation that keeps a cluster on it.

Key points

  • Kubernetes supports imperative commands, imperative object configuration and declarative object configuration; use one per object.
  • kubectl diff previews and kubectl apply patches only the differences, keeping changes from other writers.
  • Server-side apply records field ownership in managedFields and reports conflicts between field managers.
  • Declarative desired state is the first of the four OpenGitOps principles.

Three sample questions

9 CNPA questions on Declarative Resource Management, 9 with explanations. How practice works

Question 1

A team manages the same Deployment with kubectl create/kubectl replace in one script and kubectl apply on a directory in another. What does the Kubernetes object management guide say about this setup?
  1. A.Mixing is recommended, because imperative commands give the audit trail and apply keeps the full history.
  2. B.Use only one technique per object; mixing them on the same object gives undefined behavior.
  3. C.It is safe as long as both scripts use the same YAML files.
  4. D.kubectl detects the conflict and converts the object to declarative management automatically.

Correct answer: B

The kubectl object management overview warns that a Kubernetes object should be managed using only one technique, and that mixing and matching techniques for the same object results in undefined behavior. Why the other options are wrong. "Mixing is recommended, because imperative commands give the audit ...": Mixing is not recommended, and imperative commands are documented as providing no audit trail at all. "It is safe as long as both scripts use the same YAML files.": Using the same files does not help: the documented rule is about the management technique, not about the file contents. "kubectl detects the conflict and converts the object to ...": kubectl does no such conversion; the guide describes the result of mixing as undefined behavior.

Sources: CNCF CNPA curriculum (CNPA_Curriculum.pdf), Kubernetes: Object management

Question 2

Another controller sets a field on a live object that is not present in your configuration file. You then run kubectl apply -f on that file. According to kubernetes.io, why is the other writer's field normally kept?
  1. A.It uses the patch operation to write only observed differences, instead of replacing the whole object.
  2. B.kubectl apply ignores the live object and only compares two versions of the local file.
  3. C.kubectl apply takes a lock on the object so that no other writer can change it afterwards.
  4. D.kubectl apply replaces the full object, and the API server then restores fields it remembers.

Correct answer: A

The object management guide notes that declarative object configuration retains changes made by other writers, even if they are not merged back to the file, because it uses the patch API operation to write only observed differences rather than the replace operation. Why the other options are wrong. "kubectl apply ignores the live object and only compares ...": Apply does consider the live object; it computes differences between the file, the last applied configuration and the live state. "kubectl apply takes a lock on the object so ...": There is no lock; other writers can keep changing the object, which is exactly why apply has to merge. "kubectl apply replaces the full object, and the API ...": Replacing the full object is what imperative kubectl replace does, and the guide warns that it drops changes missing from the file.

Sources: CNCF CNPA curriculum (CNPA_Curriculum.pdf), Kubernetes: Object management, Kubernetes: Declarative management with configuration files

Question 3

Which description of a declarative API matches how kubernetes.io explains the Kubernetes API model?
  1. A.The API accepts only YAML, while imperative APIs accept only JSON.
  2. B.You send the exact sequence of commands to run, and the server executes them once, in that order.
  3. C.Changes are applied once at submission time and the system never revisits them.
  4. D.You declare the desired state, and a controller keeps the current state in sync with it.

Correct answer: D

kubernetes.io describes the Kubernetes declarative API as a separation of responsibilities: you declare the desired state of your resource, and the controller keeps the current state of objects in sync with it. This contrasts with an imperative API, where you instruct a server what to do. Why the other options are wrong. "The API accepts only YAML, while imperative APIs accept only JSON.": The serialization format is not what makes an API declarative; Kubernetes accepts both formats. "You send the exact sequence of commands to run ...": That is the imperative model, which the same page contrasts with the declarative API. "Changes are applied once at submission time and the system never revisits them.": The point of the declarative model is continuous convergence: controllers keep working toward the declared state.

Sources: CNCF CNPA curriculum (CNPA_Curriculum.pdf), Kubernetes: Custom resources, Kubernetes: Controllers

Practice this topic

Focus mode keeps every question on Declarative Resource Management until you leave it. 10 free guest answers, then a free account; no credit card.

Or open CNPA practice across all topics.

Sources

The explanation above was checked against these official documents.