MCPcopy Index your code
hub / github.com/kubernetes-sigs/controller-runtime / ExampleController

Function ExampleController

pkg/controller/example_test.go:57–85  ·  view source on GitHub ↗

This example starts a new Controller named "pod-controller" to Watch Pods and call a no-op Reconciler.

()

Source from the content-addressed store, hash-verified

55
56// This example starts a new Controller named "pod-controller" to Watch Pods and call a no-op Reconciler.
57func ExampleController() {
58 // mgr is a manager.Manager
59
60 // Create a new Controller that will call the provided Reconciler function in response
61 // to events.
62 c, err := controller.New("pod-controller", mgr, controller.Options{
63 Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
64 // Your business logic to implement the API by creating, updating, deleting objects goes here.
65 return reconcile.Result{}, nil
66 }),
67 })
68 if err != nil {
69 log.Error(err, "unable to create pod-controller")
70 os.Exit(1)
71 }
72
73 // Watch for Pod create / update / delete events and call Reconcile
74 err = c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, &handler.TypedEnqueueRequestForObject[*corev1.Pod]{}))
75 if err != nil {
76 log.Error(err, "unable to watch pods")
77 os.Exit(1)
78 }
79
80 // Start the Controller through the manager.
81 if err := mgr.Start(signals.SetupSignalHandler()); err != nil {
82 log.Error(err, "unable to continue running manager")
83 os.Exit(1)
84 }
85}
86
87// This example starts a new Controller named "pod-controller" to Watch Pods with the unstructured object and call a no-op Reconciler.
88func ExampleController_unstructured() {

Callers

nothing calls this directly

Calls 7

NewFunction · 0.92
KindFunction · 0.92
SetupSignalHandlerFunction · 0.92
WatchMethod · 0.65
GetCacheMethod · 0.65
StartMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…