@@ -87,8 +87,8 @@ func GenericExample() {
87
87
88
88
b := ctrl .NewControllerManagedBy (manager ) // Create the Controller
89
89
// ReplicaSet is the Application API
90
- b .Add (builder .For (b , & appsv1.ReplicaSet {})).
91
- Add (builder .Owns (b , & appsv1.ReplicaSet {}, & corev1.Pod {})). // ReplicaSet owns Pods created by it
90
+ b .Add (builder .For (manager , & appsv1.ReplicaSet {})).
91
+ Add (builder .Owns (manager , & appsv1.ReplicaSet {}, & corev1.Pod {})). // ReplicaSet owns Pods created by it
92
92
Complete (& ReplicaSetReconciler {Client : manager .GetClient ()})
93
93
if err != nil {
94
94
log .Error (err , "could not create controller" )
@@ -189,6 +189,58 @@ func Example_customHandler() {
189
189
}
190
190
}
191
191
192
+ // This example creates a simple application Controller that is configured for ExampleCRDWithConfigMapRef CRD.
193
+ // Any change in the configMap referenced in this Custom Resource will cause the re-reconcile of the parent ExampleCRDWithConfigMapRef
194
+ // due to the implementation of the .Watches method of "sigs.k8s.io/controller-runtime/pkg/builder".Builder.
195
+ func Example_generic_customHandler () {
196
+ log := ctrl .Log .WithName ("builder-examples" )
197
+
198
+ manager , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {})
199
+ if err != nil {
200
+ log .Error (err , "could not create manager" )
201
+ os .Exit (1 )
202
+ }
203
+
204
+ err = ctrl .
205
+ NewControllerManagedBy (manager ).
206
+ For (& ExampleCRDWithConfigMapRef {}).
207
+ Add (builder .Watches (manager , & corev1.ConfigMap {}, handler .EnqueueRequestsFromObjectMap (func (ctx context.Context , cm * corev1.ConfigMap ) []ctrl.Request {
208
+ // map a change from referenced configMap to ExampleCRDWithConfigMapRef, which causes its re-reconcile
209
+ crList := & ExampleCRDWithConfigMapRefList {}
210
+ if err := manager .GetClient ().List (ctx , crList ); err != nil {
211
+ manager .GetLogger ().Error (err , "while listing ExampleCRDWithConfigMapRefs" )
212
+ return nil
213
+ }
214
+
215
+ reqs := make ([]ctrl.Request , 0 , len (crList .Items ))
216
+ for _ , item := range crList .Items {
217
+ if item .ConfigMapRef .Name == cm .Name && cm .Data ["Namespace" ] == item .GetNamespace () {
218
+ reqs = append (reqs , ctrl.Request {
219
+ NamespacedName : types.NamespacedName {
220
+ Namespace : item .GetNamespace (),
221
+ Name : item .GetName (),
222
+ },
223
+ })
224
+ }
225
+ }
226
+
227
+ return reqs
228
+ }))).
229
+ Complete (reconcile .Func (func (ctx context.Context , r reconcile.Request ) (reconcile.Result , error ) {
230
+ // Your business logic to implement the API by creating, updating, deleting objects goes here.
231
+ return reconcile.Result {}, nil
232
+ }))
233
+ if err != nil {
234
+ log .Error (err , "could not create controller" )
235
+ os .Exit (1 )
236
+ }
237
+
238
+ if err := manager .Start (ctrl .SetupSignalHandler ()); err != nil {
239
+ log .Error (err , "could not start manager" )
240
+ os .Exit (1 )
241
+ }
242
+ }
243
+
192
244
// This example creates a simple application Controller that is configured for ReplicaSets and Pods.
193
245
// This application controller will be running leader election with the provided configuration in the manager options.
194
246
// If leader election configuration is not provided, controller runs leader election with default values.
0 commit comments