Skip to content

APIs for Self-Service Platforms (CRDs): CNPA practice questions

A self-service platform needs an API that users can call without opening a ticket.

By Ibrahim Yesilay · Published · Updated

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

What APIs for Self-Service Platforms (CRDs) covers in CNPA

A self-service platform needs an API that users can call without opening a ticket. Kubernetes offers a convenient way to build one: extend its own API with custom resources, so a request for a database, a preview environment or a message queue becomes an object that users create with kubectl, a pull request or a portal, and that a controller fulfills.

The Kubernetes documentation defines a resource as an endpoint in the API that stores a collection of objects of one kind, and a custom resource as an extension of that API which is not necessarily available in a default installation. A CustomResourceDefinition registers a new resource type with a name and schema that you specify; the API server then serves and stores those objects, so no separate API server has to be written. The alternative, API aggregation, places a custom API server behind the main one, which requires programming but gives more control over behavior such as storage and conversion between API versions. The documentation sums it up: CRDs are easier to use, aggregated APIs are more flexible.

On their own, custom resources only let you store and retrieve structured data. Combined with a custom controller they provide a true declarative API, in which users declare the desired state and the controller keeps the current state in sync; that pairing is the operator pattern. Because custom resources follow Kubernetes conventions, they inherit a lot: kubectl support, the usual RBAC rules, watches that automation can react to, and the metadata, spec and status structure. The documentation lists OpenAPI v3 validation and defaulting in the CRD schema, and a status subresource that allows fine-grained access control, where users write the spec and the controller writes the status.

Good platform APIs are abstractions, not mirrors of the underlying system. A small resource with an engine and a size field can hide dozens of provider-specific settings, which is how the platform reduces cognitive load and enforces its defaults. The Platforms White Paper lists Kubernetes, Crossplane, the Operator Framework, Helm and KubeVela as example projects for the provisioning API capability, and the maturity model's self-service level describes an API that abstracts the creation and maintenance of databases and gives users what they need, such as a connection string, a location for secret data and a dashboard.

The documentation also says when not to use a custom resource. A declarative API suits a relatively small number of small objects that describe configuration, change infrequently and are read and written by humans. It is a poor fit for high request rates, large amounts of data, or end-user data such as images or PII, and designs that store application data in the Kubernetes API are described as too closely coupled. When a program in a Pod just needs a configuration file, a ConfigMap is the better choice.

At associate level, expect questions on what a CRD adds, why a controller is needed, when to prefer a ConfigMap, and how custom resources give users a self-service interface with familiar tooling.

Key points

  • A CRD adds a new resource type that the API server stores and serves without a custom server.
  • A custom resource becomes a declarative API only when a controller reconciles it.
  • CRDs support OpenAPI v3 validation, defaulting and a status subresource.
  • Use a ConfigMap for a plain configuration file; avoid custom resources for application or end-user data.

Three sample questions

8 CNPA questions on APIs for Self-Service Platforms (CRDs), 8 with explanations. How practice works

Question 1

A platform team defines a CustomResourceDefinition for Database objects but deploys no controller for it. What do users get when they create Database objects?
  1. A.A provisioned database, because Kubernetes knows how to reconcile any custom resource.
  2. B.Stored structured data only; a controller is needed for it to act as a declarative API.
  3. C.An error, because custom resources cannot be created while no controller is running.
  4. D.An aggregated API server that is started automatically to run and serve the database.

Correct answer: B

kubernetes.io explains that on their own, custom resources let you store and retrieve structured data, and that when you combine a custom resource with a custom controller, custom resources provide a true declarative API in which the controller keeps current state in sync with the declared state. Why the other options are wrong. "A provisioned database, because Kubernetes knows how to reconcile ...": Kubernetes has no built-in logic for a new type; a controller must provide it. "An error, because custom resources cannot be created while ...": The objects can be created and read with kubectl; they just are not acted on. "An aggregated API server that is started automatically to ...": API aggregation is a different extension mechanism and does not appear from a CRD.

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

Question 2

A platform team needs a new resource type for its self-service API and does not want to write or run an extra API server. Which extension mechanism fits, according to kubernetes.io?
  1. A.A ConfigMap per resource with a naming convention
  2. B.A CustomResourceDefinition (CRD)
  3. C.A mutating admission webhook
  4. D.API aggregation with a subordinate API server

Correct answer: B

kubernetes.io describes two ways to add custom resources: CRDs, which are simple, can be created without programming and do not require another API server, and API aggregation, where subordinate API servers sit behind the primary API server acting as a proxy. Why the other options are wrong. "A ConfigMap per resource with a naming convention": A ConfigMap is a built-in type for configuration data; it does not add a new API type with its own schema. "A mutating admission webhook": Webhooks intercept requests for existing types; they do not define new resource types. "API aggregation with a subordinate API server": Aggregated APIs are served by additional API servers behind the main one, which is what the team wants to avoid.

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

Question 3

When does kubernetes.io suggest a stand-alone API, rather than adding a custom resource to the cluster, is the better choice?
  1. A.When the API is not declarative or must keep specific REST paths of an existing API
  2. B.When users should be able to read and write the new types with kubectl
  3. C.When the resources are naturally scoped to namespaces, or to the whole cluster, like built-in types
  4. D.When the objects are small configuration records that change infrequently

Correct answer: A

The custom resources page lists when to prefer a stand-alone API: the API does not fit the declarative model, kubectl and Kubernetes UI support are not required, a program already serves the API well, or specific REST paths are needed for compatibility. Why the other options are wrong. "When users should be able to read and write the new types with kubectl": Wanting kubectl access is a reason to add the API to the cluster, not to keep it separate. "When the resources are naturally scoped to namespaces, or ...": Namespace or cluster scope is listed as a reason to use Kubernetes APIs. "When the objects are small configuration records that change infrequently": Those are the typical traits of a declarative API that suits a custom resource.

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

Practice this topic

Focus mode keeps every question on APIs for Self-Service Platforms (CRDs) 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.