1212*/
1313package io .kubernetes .client .spring .extended .controller ;
1414
15+ import static org .junit .Assert .assertEquals ;
1516import static org .junit .Assert .assertNotNull ;
17+ import static org .junit .Assert .fail ;
1618
1719import com .github .tomakehurst .wiremock .junit .WireMockRule ;
20+ import io .kubernetes .client .common .KubernetesObject ;
1821import io .kubernetes .client .extended .controller .Controller ;
22+ import io .kubernetes .client .extended .controller .DefaultController ;
1923import io .kubernetes .client .extended .controller .reconciler .Reconciler ;
2024import io .kubernetes .client .extended .controller .reconciler .Request ;
2125import io .kubernetes .client .extended .controller .reconciler .Result ;
26+ import io .kubernetes .client .extended .workqueue .WorkQueue ;
2227import io .kubernetes .client .informer .SharedInformer ;
28+ import io .kubernetes .client .informer .SharedInformerFactory ;
29+ import io .kubernetes .client .informer .cache .DeltaFIFO ;
2330import 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 ;
2434import io .kubernetes .client .openapi .models .V1Pod ;
25- import io .kubernetes .client .spring . extended . controller . annotation .* ;
35+ import io .kubernetes .client .openapi . models . V1PodList ;
2636import io .kubernetes .client .spring .extended .controller .annotation .AddWatchEventFilter ;
2737import 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 ;
2842import 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 ;
2947import org .junit .Rule ;
3048import org .junit .Test ;
3149import org .junit .runner .RunWith ;
32- import org .springframework .beans .factory .annotation .Autowired ;
3350import org .springframework .boot .test .context .SpringBootTest ;
3451import org .springframework .boot .test .context .TestConfiguration ;
3552import org .springframework .context .annotation .Bean ;
@@ -47,8 +64,12 @@ public class KubernetesReconcilerCreatorTest {
4764 static class TestConfig {
4865
4966 @ 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 );
5273 }
5374 }
5475
@@ -58,20 +79,32 @@ public TestReconciler podReconciler() {
5879 @ KubernetesReconcilerWatches ({
5980 @ KubernetesReconcilerWatch (
6081 apiTypeClass = V1Pod .class ,
82+ workQueueKeyFunc = CustomWorkQueueKeyFunc .class ,
6183 resyncPeriodMillis = 60 * 1000L // resync every 60s
6284 )
6385 }))
6486 static class TestReconciler implements Reconciler {
6587
66- private int observedPodCount ;
88+ private Request receivedRequest ;
6789
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 ;
69100
70- @ Autowired private Lister <V1Pod > podLister ;
101+ private final Lister <V1Pod > podLister ;
102+
103+ private final Lister <V1ConfigMap > configMapLister ;
71104
72105 @ Override
73106 public Result reconcile (Request request ) {
74- observedPodCount = podLister . list (). size () ;
107+ receivedRequest = request ;
75108 return new Result (false );
76109 }
77110
@@ -96,10 +129,53 @@ private boolean podInformerCacheReady() {
96129 }
97130 }
98131
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+ }
100153
101154 @ Test
102- public void testSimplePodController () {
155+ public void testSimplePodController () throws InterruptedException {
103156 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 ());
104180 }
105181}
0 commit comments