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