...

Source file src/sigs.k8s.io/controller-runtime/pkg/handler/example_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/handler

     1  /*
     2  Copyright 2018 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package handler_test
    18  
    19  import (
    20  	"context"
    21  
    22  	appsv1 "k8s.io/api/apps/v1"
    23  	corev1 "k8s.io/api/core/v1"
    24  	"k8s.io/apimachinery/pkg/types"
    25  	"k8s.io/client-go/util/workqueue"
    26  	"sigs.k8s.io/controller-runtime/pkg/controller"
    27  	"sigs.k8s.io/controller-runtime/pkg/event"
    28  	"sigs.k8s.io/controller-runtime/pkg/handler"
    29  	"sigs.k8s.io/controller-runtime/pkg/manager"
    30  	"sigs.k8s.io/controller-runtime/pkg/reconcile"
    31  	"sigs.k8s.io/controller-runtime/pkg/source"
    32  )
    33  
    34  var (
    35  	mgr manager.Manager
    36  	c   controller.Controller
    37  )
    38  
    39  // This example watches Pods and enqueues Requests with the Name and Namespace of the Pod from
    40  // the Event (i.e. change caused by a Create, Update, Delete).
    41  func ExampleEnqueueRequestForObject() {
    42  	// controller is a controller.controller
    43  	err := c.Watch(
    44  		source.Kind(mgr.GetCache(), &corev1.Pod{}, &handler.TypedEnqueueRequestForObject[*corev1.Pod]{}),
    45  	)
    46  	if err != nil {
    47  		// handle it
    48  	}
    49  }
    50  
    51  // This example watches ReplicaSets and enqueues a Request containing the Name and Namespace of the
    52  // owning (direct) Deployment responsible for the creation of the ReplicaSet.
    53  func ExampleEnqueueRequestForOwner() {
    54  	// controller is a controller.controller
    55  	err := c.Watch(
    56  		source.Kind(mgr.GetCache(),
    57  			&appsv1.ReplicaSet{},
    58  			handler.TypedEnqueueRequestForOwner[*appsv1.ReplicaSet](mgr.GetScheme(), mgr.GetRESTMapper(), &appsv1.Deployment{}, handler.OnlyControllerOwner()),
    59  		),
    60  	)
    61  	if err != nil {
    62  		// handle it
    63  	}
    64  }
    65  
    66  // This example watches Deployments and enqueues a Request contain the Name and Namespace of different
    67  // objects (of Type: MyKind) using a mapping function defined by the user.
    68  func ExampleEnqueueRequestsFromMapFunc() {
    69  	// controller is a controller.controller
    70  	err := c.Watch(
    71  		source.Kind(mgr.GetCache(), &appsv1.Deployment{},
    72  			handler.TypedEnqueueRequestsFromMapFunc(func(ctx context.Context, a *appsv1.Deployment) []reconcile.Request {
    73  				return []reconcile.Request{
    74  					{NamespacedName: types.NamespacedName{
    75  						Name:      a.Name + "-1",
    76  						Namespace: a.Namespace,
    77  					}},
    78  					{NamespacedName: types.NamespacedName{
    79  						Name:      a.Name + "-2",
    80  						Namespace: a.Namespace,
    81  					}},
    82  				}
    83  			}),
    84  		),
    85  	)
    86  	if err != nil {
    87  		// handle it
    88  	}
    89  }
    90  
    91  // This example implements handler.EnqueueRequestForObject.
    92  func ExampleFuncs() {
    93  	// controller is a controller.controller
    94  	err := c.Watch(
    95  		source.Kind(mgr.GetCache(), &corev1.Pod{},
    96  			handler.TypedFuncs[*corev1.Pod]{
    97  				CreateFunc: func(ctx context.Context, e event.TypedCreateEvent[*corev1.Pod], q workqueue.RateLimitingInterface) {
    98  					q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
    99  						Name:      e.Object.Name,
   100  						Namespace: e.Object.Namespace,
   101  					}})
   102  				},
   103  				UpdateFunc: func(ctx context.Context, e event.TypedUpdateEvent[*corev1.Pod], q workqueue.RateLimitingInterface) {
   104  					q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
   105  						Name:      e.ObjectNew.Name,
   106  						Namespace: e.ObjectNew.Namespace,
   107  					}})
   108  				},
   109  				DeleteFunc: func(ctx context.Context, e event.TypedDeleteEvent[*corev1.Pod], q workqueue.RateLimitingInterface) {
   110  					q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
   111  						Name:      e.Object.Name,
   112  						Namespace: e.Object.Namespace,
   113  					}})
   114  				},
   115  				GenericFunc: func(ctx context.Context, e event.TypedGenericEvent[*corev1.Pod], q workqueue.RateLimitingInterface) {
   116  					q.Add(reconcile.Request{NamespacedName: types.NamespacedName{
   117  						Name:      e.Object.Name,
   118  						Namespace: e.Object.Namespace,
   119  					}})
   120  				},
   121  			},
   122  		),
   123  	)
   124  	if err != nil {
   125  		// handle it
   126  	}
   127  }
   128  

View as plain text