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