sigs.k8s.io/controller-runtime@v0.18.2/pkg/controller/example_test.go (about) 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 controller_test 18 19 import ( 20 "context" 21 "os" 22 23 corev1 "k8s.io/api/core/v1" 24 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 25 "k8s.io/apimachinery/pkg/runtime/schema" 26 "sigs.k8s.io/controller-runtime/pkg/controller" 27 "sigs.k8s.io/controller-runtime/pkg/handler" 28 logf "sigs.k8s.io/controller-runtime/pkg/log" 29 "sigs.k8s.io/controller-runtime/pkg/manager" 30 "sigs.k8s.io/controller-runtime/pkg/manager/signals" 31 "sigs.k8s.io/controller-runtime/pkg/reconcile" 32 "sigs.k8s.io/controller-runtime/pkg/source" 33 ) 34 35 var ( 36 mgr manager.Manager 37 // NB: don't call SetLogger in init(), or else you'll mess up logging in the main suite. 38 log = logf.Log.WithName("controller-examples") 39 ) 40 41 // This example creates a new Controller named "pod-controller" with a no-op reconcile function. The 42 // manager.Manager will be used to Start the Controller, and will provide it a shared Cache and Client. 43 func ExampleNew() { 44 _, err := controller.New("pod-controller", mgr, controller.Options{ 45 Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) { 46 // Your business logic to implement the API by creating, updating, deleting objects goes here. 47 return reconcile.Result{}, nil 48 }), 49 }) 50 if err != nil { 51 log.Error(err, "unable to create pod-controller") 52 os.Exit(1) 53 } 54 } 55 56 // This example starts a new Controller named "pod-controller" to Watch Pods and call a no-op Reconciler. 57 func 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. 88 func ExampleController_unstructured() { 89 // mgr is a manager.Manager 90 91 // Create a new Controller that will call the provided Reconciler function in response 92 // to events. 93 c, err := controller.New("pod-controller", mgr, controller.Options{ 94 Reconciler: reconcile.Func(func(context.Context, reconcile.Request) (reconcile.Result, error) { 95 // Your business logic to implement the API by creating, updating, deleting objects goes here. 96 return reconcile.Result{}, nil 97 }), 98 }) 99 if err != nil { 100 log.Error(err, "unable to create pod-controller") 101 os.Exit(1) 102 } 103 104 u := &unstructured.Unstructured{} 105 u.SetGroupVersionKind(schema.GroupVersionKind{ 106 Kind: "Pod", 107 Group: "", 108 Version: "v1", 109 }) 110 // Watch for Pod create / update / delete events and call Reconcile 111 err = c.Watch(source.Kind(mgr.GetCache(), u, &handler.TypedEnqueueRequestForObject[*unstructured.Unstructured]{})) 112 if err != nil { 113 log.Error(err, "unable to watch pods") 114 os.Exit(1) 115 } 116 117 // Start the Controller through the manager. 118 if err := mgr.Start(signals.SetupSignalHandler()); err != nil { 119 log.Error(err, "unable to continue running manager") 120 os.Exit(1) 121 } 122 } 123 124 // This example creates a new controller named "pod-controller" to watch Pods 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. 127 func 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", mgr, 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 }