MCPcopy
hub / github.com/kubernetes-sigs/controller-runtime / ExampleNewUnmanaged

Function ExampleNewUnmanaged

pkg/controller/example_test.go:127–165  ·  view source on GitHub ↗

This example creates a new controller named "pod-controller" to watch Pods and call a no-op reconciler. The controller is not added to the provided manager, and must thus be started and stopped by the caller.

()

Source from the content-addressed store, hash-verified

125// and call a no-op reconciler. The controller is not added to the provided
126// manager, and must thus be started and stopped by the caller.
127func ExampleNewUnmanaged() {
128 // mgr is a manager.Manager
129
130 // Configure creates a new controller but does not add it to the supplied
131 // manager.
132 c, err := controller.NewUnmanaged("pod-controller", controller.Options{
133 Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) {
134 return reconcile.Result{}, nil
135 }),
136 })
137 if err != nil {
138 log.Error(err, "unable to create pod-controller")
139 os.Exit(1)
140 }
141
142 if err := c.Watch(source.Kind(mgr.GetCache(), &corev1.Pod{}, &handler.TypedEnqueueRequestForObject[*corev1.Pod]{})); err != nil {
143 log.Error(err, "unable to watch pods")
144 os.Exit(1)
145 }
146
147 ctx, cancel := context.WithCancel(context.Background())
148
149 // Start our controller in a goroutine so that we do not block.
150 go func() {
151 // Block until our controller manager is elected leader. We presume our
152 // entire process will terminate if we lose leadership, so we don't need
153 // to handle that.
154 <-mgr.Elected()
155
156 // Start our controller. This will block until the context is
157 // closed, or the controller returns an error.
158 if err := c.Start(ctx); err != nil {
159 log.Error(err, "cannot run experiment controller")
160 }
161 }()
162
163 // Stop our controller.
164 cancel()
165}

Callers

nothing calls this directly

Calls 7

NewUnmanagedFunction · 0.92
KindFunction · 0.92
WatchMethod · 0.65
GetCacheMethod · 0.65
ElectedMethod · 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…