API Extension#

Three ways to extend the Kubernetes API. All three are registered dynamically as API objects, without touching kube-apiserver flags.

MethodRegistration objectWhat it does
Admission webhookMutatingWebhookConfiguration, ValidatingWebhookConfigurationIntercepts requests to mutate or reject them
CRDCustomResourceDefinitionStores a new resource type in etcd
Aggregated APIAPIServiceProxies requests for an API group to a separate server

Admission Webhook#

There are two kinds of admission control.

KindHow to registerExamples
Compiled-in admission plugins--enable-admission-plugins flag (edit the static pod)NodeRestriction, ImagePolicyWebhook
Dynamic admission webhookskubectl apply a webhook configuration objectKueue, Istio sidecar injection, Gatekeeper

Dynamic webhooks are themselves called by two compiled-in plugins, MutatingAdmissionWebhook and ValidatingAdmissionWebhook. Both are enabled by default, so no flag change is needed. The API server watches webhook configuration objects and, when a request matches, makes an HTTPS call to the Service named there.

apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
  name: kueue-mutating-webhook-configuration
webhooks:
- name: mjob.kb.io
  clientConfig:
    service:                      # What the API server calls
      name: kueue-webhook-service
      namespace: kueue-system
      path: /mutate-batch-v1-job
  failurePolicy: Fail             # If the webhook does not answer, the request fails
  rules:
  - apiGroups: ["batch"]
    apiVersions: ["v1"]
    operations: ["CREATE"]
    resources: ["jobs"]
  • The configuration object only declares when and where to call. What to mutate or reject lives in the code of the server being called.
  • With failurePolicy: Fail, a dead backing Pod blocks all creation of the matched resources.
  • On private clusters, the control plane must be able to reach the webhook port.

Aggregated API (APIService)#

An APIService is a routing registration: “send requests for this API group and version to this Service”. The feature is called the API Aggregation Layer.

apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
  name: v1beta2.visibility.kueue.x-k8s.io
spec:
  group: visibility.kueue.x-k8s.io
  version: v1beta2
  service:
    name: kueue-visibility-server
    namespace: kueue-system
  • The APIService object is not a Pod. The extension API server that receives the requests runs as one.
  • List registrations with kubectl get apiservices. Built-in API groups appear there too.
  • Authentication, RBAC, and kubectl usage are the same as for ordinary resources.
  • The best-known example is metrics-server’s v1beta1.metrics.k8s.io, which backs kubectl top.

CRD vs Aggregated API#

CRDAggregated API
StorageetcdThe extension API server itself
Request handlingkube-apiserverProxied to a separate server
BehaviorCRUD and watch on stored objectsAny response you implement
Cost to buildLowHigh (you implement and operate an API server)

The choice depends on the nature of the data.

  • State that users declare and a controller reconciles: CRD + controller. Most operators use only this.
  • Volatile data computed on each read: Aggregated API. Storing it in etcd costs a write on every change, and the value is already stale by the time it is read.

Pod CPU usage (metrics-server) and queue positions (Kueue) are examples of the latter.