Chinglin

Take tremendous effort to be lazy !

22 Sep 2020

Lessons Learned for Reading K8s Source Code

Package

  • sigs.k8s.io/controller-runtime/pkg/manager

Lessons

Use stop channel to stop things

  • stop
  • engageStopProcedure
  • stopComplete
  • internalStop
  • internalStopper(write side of the internal stop channel)
  • onStoppedLeading
  • gracefulShutdownTimeout
  • shutdownCtx
  • stopErr may happen
  • errChan to signal error
  • support graceful shutdown(leader lost stop immediately)
  • use context to cancel things

how to view this flow visually, a slow test scenery to visualize it?

Use mutex to protect receiver changes

  • protect write or read for receiver(about 80% methods)

Use interface to inject dependencies

  • Runnable type

Use New with config and options

  • option provide default
  • config provide rest client mostly(init by user)
  • internal struct hidden from user, use extra method to expose field access.
  • provide set default options, make default useful.

Separate package to small scope, maximize reusability

Authors