@@ -18,16 +18,21 @@ package controller_test
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
22
+ "strconv"
21
23
22
24
appsv1 "k8s.io/api/apps/v1"
23
25
corev1 "k8s.io/api/core/v1"
24
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
27
"k8s.io/apimachinery/pkg/runtime/schema"
26
28
"k8s.io/apimachinery/pkg/types"
29
+ "k8s.io/utils/ptr"
27
30
"sigs.k8s.io/controller-runtime/pkg/cache"
31
+ "sigs.k8s.io/controller-runtime/pkg/client"
28
32
"sigs.k8s.io/controller-runtime/pkg/controller"
29
33
"sigs.k8s.io/controller-runtime/pkg/controller/controllertest"
30
34
"sigs.k8s.io/controller-runtime/pkg/handler"
35
+ "sigs.k8s.io/controller-runtime/pkg/predicate"
31
36
"sigs.k8s.io/controller-runtime/pkg/reconcile"
32
37
"sigs.k8s.io/controller-runtime/pkg/source"
33
38
@@ -48,30 +53,46 @@ var _ = Describe("controller", func() {
48
53
Describe ("controller" , func () {
49
54
// TODO(directxman12): write a whole suite of controller-client interaction tests
50
55
51
- It ("should reconcile" , func () {
56
+ // Since all tests are run in parallel and share the same testenv, we namespace the objects
57
+ // created by using a namespace per entry, and adding a watch predicate that filters by
58
+ // namespace.
59
+ DescribeTable ("should reconcile" , func (enableWarmup bool ) {
52
60
By ("Creating the Manager" )
53
61
cm , err := manager .New (cfg , manager.Options {})
54
62
Expect (err ).NotTo (HaveOccurred ())
55
63
56
64
By ("Creating the Controller" )
57
- instance , err := controller .New ("foo-controller" , cm , controller.Options {
58
- Reconciler : reconcile .Func (
59
- func (_ context.Context , request reconcile.Request ) (reconcile.Result , error ) {
60
- reconciled <- request
61
- return reconcile.Result {}, nil
62
- }),
63
- })
65
+ instance , err := controller .New (
66
+ fmt .Sprintf ("foo-controller-%t" , enableWarmup ),
67
+ cm ,
68
+ controller.Options {
69
+ Reconciler : reconcile .Func (
70
+ func (_ context.Context , request reconcile.Request ) (reconcile.Result , error ) {
71
+ reconciled <- request
72
+ return reconcile.Result {}, nil
73
+ }),
74
+ EnableWarmup : ptr .To (enableWarmup ),
75
+ },
76
+ )
64
77
Expect (err ).NotTo (HaveOccurred ())
65
78
79
+ testNamespace := strconv .FormatBool (enableWarmup )
80
+
66
81
By ("Watching Resources" )
67
82
err = instance .Watch (
68
83
source .Kind (cm .GetCache (), & appsv1.ReplicaSet {},
69
84
handler .TypedEnqueueRequestForOwner [* appsv1.ReplicaSet ](cm .GetScheme (), cm .GetRESTMapper (), & appsv1.Deployment {}),
85
+ makeNamespacePredicate [* appsv1.ReplicaSet ](testNamespace ),
70
86
),
71
87
)
72
88
Expect (err ).NotTo (HaveOccurred ())
73
89
74
- err = instance .Watch (source .Kind (cm .GetCache (), & appsv1.Deployment {}, & handler.TypedEnqueueRequestForObject [* appsv1.Deployment ]{}))
90
+ err = instance .Watch (
91
+ source .Kind (cm .GetCache (), & appsv1.Deployment {},
92
+ & handler.TypedEnqueueRequestForObject [* appsv1.Deployment ]{},
93
+ makeNamespacePredicate [* appsv1.Deployment ](testNamespace ),
94
+ ),
95
+ )
75
96
Expect (err ).NotTo (HaveOccurred ())
76
97
77
98
err = cm .GetClient ().Get (ctx , types.NamespacedName {Name : "foo" }, & corev1.Namespace {})
@@ -110,19 +131,25 @@ var _ = Describe("controller", func() {
110
131
},
111
132
}
112
133
expectedReconcileRequest := reconcile.Request {NamespacedName : types.NamespacedName {
113
- Namespace : "default" ,
134
+ Namespace : testNamespace ,
114
135
Name : "deployment-name" ,
115
136
}}
116
137
138
+ By ("Creating the test namespace" )
139
+ _ , err = clientset .CoreV1 ().Namespaces ().Create (ctx , & corev1.Namespace {
140
+ ObjectMeta : metav1.ObjectMeta { Name : testNamespace },
141
+ }, metav1.CreateOptions {})
142
+ Expect (err ).NotTo (HaveOccurred ())
143
+
117
144
By ("Invoking Reconciling for Create" )
118
- deployment , err = clientset .AppsV1 ().Deployments ("default" ).Create (ctx , deployment , metav1.CreateOptions {})
145
+ deployment , err = clientset .AppsV1 ().Deployments (testNamespace ).Create (ctx , deployment , metav1.CreateOptions {})
119
146
Expect (err ).NotTo (HaveOccurred ())
120
147
Expect (<- reconciled ).To (Equal (expectedReconcileRequest ))
121
148
122
149
By ("Invoking Reconciling for Update" )
123
150
newDeployment := deployment .DeepCopy ()
124
151
newDeployment .Labels = map [string ]string {"foo" : "bar" }
125
- _ , err = clientset .AppsV1 ().Deployments ("default" ).Update (ctx , newDeployment , metav1.UpdateOptions {})
152
+ _ , err = clientset .AppsV1 ().Deployments (testNamespace ).Update (ctx , newDeployment , metav1.UpdateOptions {})
126
153
Expect (err ).NotTo (HaveOccurred ())
127
154
Expect (<- reconciled ).To (Equal (expectedReconcileRequest ))
128
155
@@ -145,24 +172,24 @@ var _ = Describe("controller", func() {
145
172
Template : deployment .Spec .Template ,
146
173
},
147
174
}
148
- replicaset , err = clientset .AppsV1 ().ReplicaSets ("default" ).Create (ctx , replicaset , metav1.CreateOptions {})
175
+ replicaset , err = clientset .AppsV1 ().ReplicaSets (testNamespace ).Create (ctx , replicaset , metav1.CreateOptions {})
149
176
Expect (err ).NotTo (HaveOccurred ())
150
177
Expect (<- reconciled ).To (Equal (expectedReconcileRequest ))
151
178
152
179
By ("Invoking Reconciling for an OwnedObject when it is updated" )
153
180
newReplicaset := replicaset .DeepCopy ()
154
181
newReplicaset .Labels = map [string ]string {"foo" : "bar" }
155
- _ , err = clientset .AppsV1 ().ReplicaSets ("default" ).Update (ctx , newReplicaset , metav1.UpdateOptions {})
182
+ _ , err = clientset .AppsV1 ().ReplicaSets (testNamespace ).Update (ctx , newReplicaset , metav1.UpdateOptions {})
156
183
Expect (err ).NotTo (HaveOccurred ())
157
184
Expect (<- reconciled ).To (Equal (expectedReconcileRequest ))
158
185
159
186
By ("Invoking Reconciling for an OwnedObject when it is deleted" )
160
- err = clientset .AppsV1 ().ReplicaSets ("default" ).Delete (ctx , replicaset .Name , metav1.DeleteOptions {})
187
+ err = clientset .AppsV1 ().ReplicaSets (testNamespace ).Delete (ctx , replicaset .Name , metav1.DeleteOptions {})
161
188
Expect (err ).NotTo (HaveOccurred ())
162
189
Expect (<- reconciled ).To (Equal (expectedReconcileRequest ))
163
190
164
191
By ("Invoking Reconciling for Delete" )
165
- err = clientset .AppsV1 ().Deployments ("default" ).
192
+ err = clientset .AppsV1 ().Deployments (testNamespace ).
166
193
Delete (ctx , "deployment-name" , metav1.DeleteOptions {})
167
194
Expect (err ).NotTo (HaveOccurred ())
168
195
Expect (<- reconciled ).To (Equal (expectedReconcileRequest ))
@@ -174,7 +201,12 @@ var _ = Describe("controller", func() {
174
201
175
202
By ("Invoking Reconciling for a pod when it is created when adding watcher dynamically" )
176
203
// Add new watcher dynamically
177
- err = instance .Watch (source .Kind (cm .GetCache (), & corev1.Pod {}, & handler.TypedEnqueueRequestForObject [* corev1.Pod ]{}))
204
+ err = instance .Watch (
205
+ source .Kind (cm .GetCache (), & corev1.Pod {},
206
+ & handler.TypedEnqueueRequestForObject [* corev1.Pod ]{},
207
+ makeNamespacePredicate [* corev1.Pod ](testNamespace ),
208
+ ),
209
+ )
178
210
Expect (err ).NotTo (HaveOccurred ())
179
211
180
212
pod := & corev1.Pod {
@@ -194,16 +226,27 @@ var _ = Describe("controller", func() {
194
226
},
195
227
}
196
228
expectedReconcileRequest = reconcile.Request {NamespacedName : types.NamespacedName {
197
- Namespace : "default" ,
229
+ Namespace : testNamespace ,
198
230
Name : "pod-name" ,
199
231
}}
200
- _ , err = clientset .CoreV1 ().Pods ("default" ).Create (ctx , pod , metav1.CreateOptions {})
232
+ _ , err = clientset .CoreV1 ().Pods (testNamespace ).Create (ctx , pod , metav1.CreateOptions {})
201
233
Expect (err ).NotTo (HaveOccurred ())
202
234
Expect (<- reconciled ).To (Equal (expectedReconcileRequest ))
203
- })
235
+ },
236
+ Entry ("with controller warmup enabled" , true ),
237
+ Entry ("with controller warmup not enabled" , false ),
238
+ )
204
239
})
205
240
})
206
241
242
+ // makeNamespacePredicate returns a predicate that filters out all objects not in the passed in
243
+ // namespace.
244
+ func makeNamespacePredicate [object client.Object ](namespace string ) predicate.TypedPredicate [object ] {
245
+ return predicate.NewTypedPredicateFuncs [object ](func (obj object ) bool {
246
+ return obj .GetNamespace () == namespace
247
+ })
248
+ }
249
+
207
250
func truePtr () * bool {
208
251
t := true
209
252
return & t
0 commit comments