12
12
*/
13
13
package io .kubernetes .client .spring .extended .controller ;
14
14
15
+ import static org .junit .Assert .assertEquals ;
15
16
import static org .junit .Assert .assertNotNull ;
17
+ import static org .junit .Assert .fail ;
16
18
17
19
import com .github .tomakehurst .wiremock .junit .WireMockRule ;
20
+ import io .kubernetes .client .common .KubernetesObject ;
18
21
import io .kubernetes .client .extended .controller .Controller ;
22
+ import io .kubernetes .client .extended .controller .DefaultController ;
19
23
import io .kubernetes .client .extended .controller .reconciler .Reconciler ;
20
24
import io .kubernetes .client .extended .controller .reconciler .Request ;
21
25
import io .kubernetes .client .extended .controller .reconciler .Result ;
26
+ import io .kubernetes .client .extended .workqueue .WorkQueue ;
22
27
import io .kubernetes .client .informer .SharedInformer ;
28
+ import io .kubernetes .client .informer .SharedInformerFactory ;
29
+ import io .kubernetes .client .informer .cache .DeltaFIFO ;
23
30
import io .kubernetes .client .informer .cache .Lister ;
31
+ import io .kubernetes .client .informer .impl .DefaultSharedIndexInformer ;
32
+ import io .kubernetes .client .openapi .models .V1ConfigMap ;
33
+ import io .kubernetes .client .openapi .models .V1ObjectMeta ;
24
34
import io .kubernetes .client .openapi .models .V1Pod ;
25
- import io .kubernetes .client .spring . extended . controller . annotation .* ;
35
+ import io .kubernetes .client .openapi . models . V1PodList ;
26
36
import io .kubernetes .client .spring .extended .controller .annotation .AddWatchEventFilter ;
27
37
import io .kubernetes .client .spring .extended .controller .annotation .DeleteWatchEventFilter ;
38
+ import io .kubernetes .client .spring .extended .controller .annotation .KubernetesReconciler ;
39
+ import io .kubernetes .client .spring .extended .controller .annotation .KubernetesReconcilerReadyFunc ;
40
+ import io .kubernetes .client .spring .extended .controller .annotation .KubernetesReconcilerWatch ;
41
+ import io .kubernetes .client .spring .extended .controller .annotation .KubernetesReconcilerWatches ;
28
42
import io .kubernetes .client .spring .extended .controller .annotation .UpdateWatchEventFilter ;
43
+ import java .util .LinkedList ;
44
+ import java .util .function .Function ;
45
+ import javax .annotation .Resource ;
46
+ import org .apache .commons .lang3 .tuple .MutablePair ;
29
47
import org .junit .Rule ;
30
48
import org .junit .Test ;
31
49
import org .junit .runner .RunWith ;
32
- import org .springframework .beans .factory .annotation .Autowired ;
33
50
import org .springframework .boot .test .context .SpringBootTest ;
34
51
import org .springframework .boot .test .context .TestConfiguration ;
35
52
import org .springframework .context .annotation .Bean ;
@@ -47,8 +64,12 @@ public class KubernetesReconcilerCreatorTest {
47
64
static class TestConfig {
48
65
49
66
@ Bean
50
- public TestReconciler podReconciler () {
51
- return new TestReconciler ();
67
+ public TestReconciler podReconciler (
68
+ SharedInformer <V1Pod > podInformer ,
69
+ Lister <V1Pod > podLister ,
70
+ SharedInformer <V1ConfigMap > configMapInformer ,
71
+ Lister <V1ConfigMap > configMapLister ) {
72
+ return new TestReconciler (podInformer , podLister , configMapLister );
52
73
}
53
74
}
54
75
@@ -58,20 +79,32 @@ public TestReconciler podReconciler() {
58
79
@ KubernetesReconcilerWatches ({
59
80
@ KubernetesReconcilerWatch (
60
81
apiTypeClass = V1Pod .class ,
82
+ workQueueKeyFunc = CustomWorkQueueKeyFunc .class ,
61
83
resyncPeriodMillis = 60 * 1000L // resync every 60s
62
84
)
63
85
}))
64
86
static class TestReconciler implements Reconciler {
65
87
66
- private int observedPodCount ;
88
+ private Request receivedRequest ;
67
89
68
- @ Autowired private SharedInformer <V1Pod > podInformer ;
90
+ public TestReconciler (
91
+ SharedInformer <V1Pod > podInformer ,
92
+ Lister <V1Pod > podLister ,
93
+ Lister <V1ConfigMap > configMapLister ) {
94
+ this .podInformer = podInformer ;
95
+ this .podLister = podLister ;
96
+ this .configMapLister = configMapLister ;
97
+ }
98
+
99
+ private final SharedInformer <V1Pod > podInformer ;
69
100
70
- @ Autowired private Lister <V1Pod > podLister ;
101
+ private final Lister <V1Pod > podLister ;
102
+
103
+ private final Lister <V1ConfigMap > configMapLister ;
71
104
72
105
@ Override
73
106
public Result reconcile (Request request ) {
74
- observedPodCount = podLister . list (). size () ;
107
+ receivedRequest = request ;
75
108
return new Result (false );
76
109
}
77
110
@@ -96,10 +129,53 @@ private boolean podInformerCacheReady() {
96
129
}
97
130
}
98
131
99
- @ Autowired private Controller testController ;
132
+ @ Resource (name = "test-reconcile" )
133
+ private Controller testController ;
134
+
135
+ @ Resource private TestReconciler testReconciler ;
136
+
137
+ @ Resource private SharedInformerFactory sharedInformerFactory ;
138
+
139
+ public static class CustomWorkQueueKeyFunc implements Function <KubernetesObject , Request > {
140
+
141
+ public CustomWorkQueueKeyFunc (WorkQueue <Request > workQueue ) {
142
+ this .workQueue = workQueue ;
143
+ }
144
+
145
+ private final WorkQueue <Request > workQueue ;
146
+
147
+ @ Override
148
+ public Request apply (KubernetesObject item ) {
149
+ workQueue .add (new Request ("foo" ));
150
+ return null ;
151
+ }
152
+ }
100
153
101
154
@ Test
102
- public void testSimplePodController () {
155
+ public void testSimplePodController () throws InterruptedException {
103
156
assertNotNull (testController );
157
+ assertNotNull (testReconciler );
158
+
159
+ sharedInformerFactory .startAllRegisteredInformers ();
160
+
161
+ ((DefaultSharedIndexInformer <V1Pod , V1PodList >) testReconciler .podInformer )
162
+ .handleDeltas (
163
+ new LinkedList <MutablePair <DeltaFIFO .DeltaType , KubernetesObject >>() {
164
+ {
165
+ add (
166
+ new MutablePair <>(
167
+ DeltaFIFO .DeltaType .Added ,
168
+ new V1Pod ().metadata (new V1ObjectMeta ().namespace ("a" ).name ("b" ))));
169
+ }
170
+ });
171
+
172
+ Thread .sleep (500 );
173
+
174
+ WorkQueue <Request > workQueue = ((DefaultController ) testController ).getWorkQueue ();
175
+ assertEquals (1 , workQueue .length ());
176
+ if (workQueue .length () != 1 ) {
177
+ fail ();
178
+ }
179
+ assertEquals ("foo" , workQueue .get ().getName ());
104
180
}
105
181
}
0 commit comments