@@ -18,14 +18,21 @@ package controllerruntime_test
18
18
19
19
import (
20
20
"context"
21
+ "encoding/json"
21
22
"fmt"
22
23
"os"
23
24
"time"
24
25
25
26
appsv1 "k8s.io/api/apps/v1"
26
27
corev1 "k8s.io/api/core/v1"
28
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29
+ "k8s.io/apimachinery/pkg/runtime"
30
+ "k8s.io/apimachinery/pkg/types"
31
+
27
32
ctrl "sigs.k8s.io/controller-runtime"
28
33
"sigs.k8s.io/controller-runtime/pkg/client"
34
+ "sigs.k8s.io/controller-runtime/pkg/handler"
35
+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
29
36
30
37
// since we invoke tests with -ginkgo.junit-report we need to import ginkgo.
31
38
_ "github.com/onsi/ginkgo/v2"
@@ -38,7 +45,7 @@ import (
38
45
//
39
46
// * Start the application.
40
47
func Example () {
41
- var log = ctrl .Log .WithName ("builder-examples" )
48
+ log : = ctrl .Log .WithName ("builder-examples" )
42
49
43
50
manager , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {})
44
51
if err != nil {
@@ -62,6 +69,94 @@ func Example() {
62
69
}
63
70
}
64
71
72
+ type ExampleCRDWithConfigMapRef struct {
73
+ metav1.TypeMeta `json:",inline"`
74
+ metav1.ObjectMeta `json:"metadata,omitempty"`
75
+ ConfigMapRef corev1.LocalObjectReference `json:"configMapRef"`
76
+ }
77
+
78
+ func deepCopyObject (arg any ) runtime.Object {
79
+ // DO NOT use this code in production code, this is only for presentation purposes.
80
+ // in real code you should generate DeepCopy methods by using controller-gen CLI tool.
81
+ argBytes , err := json .Marshal (arg )
82
+ if err != nil {
83
+ panic (err )
84
+ }
85
+ out := & ExampleCRDWithConfigMapRefList {}
86
+ if err := json .Unmarshal (argBytes , out ); err != nil {
87
+ panic (err )
88
+ }
89
+ return out
90
+ }
91
+
92
+ // DeepCopyObject implements client.Object.
93
+ func (in * ExampleCRDWithConfigMapRef ) DeepCopyObject () runtime.Object {
94
+ return deepCopyObject (in )
95
+ }
96
+
97
+ type ExampleCRDWithConfigMapRefList struct {
98
+ metav1.TypeMeta `json:",inline"`
99
+ metav1.ListMeta `json:"metadata,omitempty"`
100
+ Items []ExampleCRDWithConfigMapRef `json:"items"`
101
+ }
102
+
103
+ // DeepCopyObject implements client.ObjectList.
104
+ func (in * ExampleCRDWithConfigMapRefList ) DeepCopyObject () runtime.Object {
105
+ return deepCopyObject (in )
106
+ }
107
+
108
+ // This example creates a simple application Controller that is configured for ExampleCRDWithConfigMapRef CRD.
109
+ // Any change in the configMap referenced in this Custom Resource will cause the re-reconcile of the parent ExampleCRDWithConfigMapRef
110
+ // due to the implementation of the .Watches method of "sigs.k8s.io/controller-runtime/pkg/builder".Builder.
111
+ func Example_customHandler () {
112
+ log := ctrl .Log .WithName ("builder-examples" )
113
+
114
+ manager , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {})
115
+ if err != nil {
116
+ log .Error (err , "could not create manager" )
117
+ os .Exit (1 )
118
+ }
119
+
120
+ err = ctrl .
121
+ NewControllerManagedBy (manager ).
122
+ For (& ExampleCRDWithConfigMapRef {}).
123
+ Watches (& corev1.ConfigMap {}, handler .EnqueueRequestsFromMapFunc (func (ctx context.Context , cm client.Object ) []ctrl.Request {
124
+ // map a change from referenced configMap to ExampleCRDWithConfigMapRef, which causes its re-reconcile
125
+ crList := & ExampleCRDWithConfigMapRefList {}
126
+ if err := manager .GetClient ().List (ctx , crList ); err != nil {
127
+ manager .GetLogger ().Error (err , "while listing ExampleCRDWithConfigMapRefs" )
128
+ return nil
129
+ }
130
+
131
+ reqs := make ([]ctrl.Request , 0 , len (crList .Items ))
132
+ for _ , item := range crList .Items {
133
+ if item .ConfigMapRef .Name == cm .GetName () {
134
+ reqs = append (reqs , ctrl.Request {
135
+ NamespacedName : types.NamespacedName {
136
+ Namespace : item .GetNamespace (),
137
+ Name : item .GetName (),
138
+ },
139
+ })
140
+ }
141
+ }
142
+
143
+ return reqs
144
+ })).
145
+ Complete (reconcile .Func (func (ctx context.Context , r reconcile.Request ) (reconcile.Result , error ) {
146
+ // Your business logic to implement the API by creating, updating, deleting objects goes here.
147
+ return reconcile.Result {}, nil
148
+ }))
149
+ if err != nil {
150
+ log .Error (err , "could not create controller" )
151
+ os .Exit (1 )
152
+ }
153
+
154
+ if err := manager .Start (ctrl .SetupSignalHandler ()); err != nil {
155
+ log .Error (err , "could not start manager" )
156
+ os .Exit (1 )
157
+ }
158
+ }
159
+
65
160
// This example creates a simple application Controller that is configured for ReplicaSets and Pods.
66
161
// This application controller will be running leader election with the provided configuration in the manager options.
67
162
// If leader election configuration is not provided, controller runs leader election with default values.
@@ -75,7 +170,7 @@ func Example() {
75
170
//
76
171
// * Start the application.
77
172
func Example_updateLeaderElectionDurations () {
78
- var log = ctrl .Log .WithName ("builder-examples" )
173
+ log : = ctrl .Log .WithName ("builder-examples" )
79
174
leaseDuration := 100 * time .Second
80
175
renewDeadline := 80 * time .Second
81
176
retryPeriod := 20 * time .Second
0 commit comments