Skip to content

controller-runtime

controller-runtime is a subproject of kubebuilder which provides a lot of useful tools that help develop Kubernetes Operator.

Version: v0.13.0

Overview

  1. Create a Manager.
    1. Cluster, which has Cache, Client, Scheme, etc, is created internally. Read cluster for more details.
  2. Create one or multiple Reconcilers. For more details, read reconciler.
  3. Build the Reconciler(s) with the Manager using Builder.
    1. Internally, builder.doWatch and builder.doController are called.
    2. bldr.doController calls newController to create a new Controller and add it to manager.runnables.Others by Manager.Add(Runnable). (controller)
      1. Inject dependencies to Reconciler and Controller. e.g. Cache.
    3. bldr.doWatch creates Kind (Source) and call controller.Watch for For, Owns, and Watches.
      1. controller.Watch calls source.Start(), which gets informer from the injected cache and add the event handler.
  4. Start the Manager, which trigger to start all the mgr.runnables (Caches, Webhooks, Others) in the Manager.
    1. Informer.Run is called in cm.runnables.Caches.Start(cm.internalCtx)

For more details, you can check the architecture in book.kubebuilder.io:

List of components:

  1. Manager: Package manager is required to create Controllers and provides shared dependencies such as clients, caches, schemes, etc.
  2. Controller: Package controller provides types and functions for building Controllers. The Controller MUST be started by calling Manager.Start.
    1. Event
    2. Builder
    3. Source
    4. Handler
    5. Predicate
  3. Client: Package client contains functionality for interacting with Kubernetes API servers.
    1. delegatingClient: The default client type whose Get and List get object from cache.CacheReader, which reduces the API requests to API server.
  4. Cache
    1. client.Reader: Cache acts as a client to objects stored in the cache.
    2. Informers: Cache loads informers and adds field indices.
  5. Scheme: Wraps apimachinery/Scheme.
  6. Webhook
  7. Envtest

Components

  1. manager
  2. reconciler
  3. log
  4. controller
  5. cluster
    1. client
    2. cache
  6. inject
  7. source
  8. builder
  9. handler

Examples

  1. example-controller
  2. envtest

Memo

  1. v0.11.0: Allow Specification of the Log Timestamp Format. -> Default EpochTimeEncoder
  2. v0.15.0

    1. ⚠️ Refactor source/handler/predicate packages to remove dep injection #2120

      -       kindWithCacheMysqlUser := source.NewKindWithCache(mysqluser, cache)
      -       kindWithCacheMysql := source.NewKindWithCache(mysql, cache)
      -       kindWithCachesecret := source.NewKindWithCache(secret, cache)
      +       kindWithCacheMysqlUser := source.Kind(cache, mysqluser)
      +       kindWithCacheMysql := source.Kind(cache, mysql)
      +       kindWithCachesecret := source.Kind(cache, secret)
      
    2. Example PR: https://github.com/nakamasato/secret-mirror-operator/pull/28

  3. v0.16.0

    1. ⚠ Introduce Metrics Options struct & secure metrics serving #2407

      import (
      + metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
      )
      - MetricsBindAddress: metricsAddr
      + Metrics: metricsserver.Options{BindAddress: metricsAddr},
      
    2. ⚠ Remove deprecated manager, webhook and cluster options #2422

    3. Example PR: https://github.com/nakamasato/secret-mirror-operator/pull/28