Active Nerds
Module #48 · advanced Article

What is the Kubernetes API Extension Model?: Architecture & Implementation

Kubernetes extends its API via Custom Resource Definitions (CRDs) and API Aggregation, allowing you to add new resource types (CRDs) or delegate entire API g...

8 min read+20 XPPublished 2026-07-05

Quick Answer:

Kubernetes extends its API via Custom Resource Definitions (CRDs) and API Aggregation, allowing you to add new resource types (CRDs) or delegate entire API groups to external servers (Aggregation Layer).


Detailed Answer:

The Kubernetes API is designed to be extensible at two layers. First, CRDs let you register new resource types (e.g., kind: DatabaseCluster) into the existing API server without writing server code — the API server stores and serves them automatically. Second, API Aggregation lets you run an entirely separate API server (e.g., metrics-server) registered at a sub-path like /apis/custom.metrics.k8s.io, proxied through the main API server. Operators combine CRDs with custom controllers (using controller-runtime or client-go) to implement reconciliation loops that act on your custom resources.


Deep Dive:

The extension model has three main patterns:

Pattern 1: Custom Resource Definitions (CRDs)

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: databaseclusters.mycompany.io
spec:
  group: mycompany.io
  versions:
    - name: v1alpha1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              required: ["replicas", "image"]
              properties:
                replicas:
                  type: integer
                  minimum: 1
                  maximum: 10
                image:
                  type: string
                storageSize:
                  type: string
                  default: "10Gi"
  scope: Namespaced
  names:
    plural: databaseclusters
    singular: databasecluster
    kind: DatabaseCluster
    shortNames: ["dbc"]

Pattern 2: API Aggregation (AA)

# Register an external API server
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
  name: v1beta1.custom.metrics.k8s.io
spec:
  service:
    name: custom-metrics-apiserver
    namespace: monitoring
    port: 443
  group: custom.metrics.k8s.io
  version: v1beta1
  insecureSkipTLSVerify: false
  caBundle: <base64-encoded-CA>
  groupPriorityMinimum: 100
  versionPriority: 100

Pattern 3: Operator with controller-runtime

// Reconciler loop for DatabaseCluster
func (r *DatabaseClusterReconciler) Reconcile(
    ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
    
    var db myv1.DatabaseCluster
    if err := r.Get(ctx, req.NamespacedName, &db); err != nil {
        return ctrl.Result{}, client.IgnoreNotFound(err)
    }
    
    // Desired state: ensure StatefulSet matches spec
    desired := r.buildStatefulSet(&db)
    existing := &appsv1.StatefulSet{}
    
    if err := r.Get(ctx, req.NamespacedName, existing); err != nil {
        if apierrors.IsNotFound(err) {
            return ctrl.Result{}, r.Create(ctx, desired)
        }
        return ctrl.Result{}, err
    }
    
    // Update if spec changed (use semantic equality)
    if !reflect.DeepEqual(existing.Spec, desired.Spec) {
        existing.Spec = desired.Spec
        return ctrl.Result{}, r.Update(ctx, existing)
    }
    
    // Requeue after 30s for drift detection
    return ctrl.Result{RequeueAfter: 30 * time.Second}, nil
}

CRD vs. AA Decision Matrix:

| Factor | CRD | API Aggregation | |---|---|---| | Custom storage backend | ❌ | ✅ | | Custom validation logic | Limited | Full | | Operational complexity | Low | High | | Sub-resources (status, scale) | ✅ | ✅ | | Non-CRUD operations | ❌ | ✅ | | Best for | Operators, config | Metrics, proxies |

Why it matters: Almost every enterprise Kubernetes platform uses operators built on CRDs. Understanding the extension model is the foundation for building platform engineering tooling.

Common mistake: Using API Aggregation when a CRD is sufficient. AA requires running a full TLS-secured API server, which is significant operational overhead. Default to CRDs.

Self-assessment: Can you explain the difference between a CRD and an AA server to a junior engineer? Can you write a CRD schema with validation? Can you describe what a reconciliation loop does without looking at notes?

Follow-up questions: Q2 (Admission Webhooks), Q3 (Build a webhook)


Finished reading?

Mark as complete to claim your +20 XP and track progress.