1212*/
1313package io .kubernetes .client .examples ;
1414
15+ import java .time .Duration ;
16+
17+ import org .springframework .beans .factory .annotation .Autowired ;
18+ import org .springframework .beans .factory .annotation .Value ;
19+ import org .springframework .boot .CommandLineRunner ;
20+ import org .springframework .boot .SpringApplication ;
21+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
22+ import org .springframework .context .annotation .Bean ;
23+ import org .springframework .context .annotation .Configuration ;
24+ import org .springframework .stereotype .Component ;
25+
1526import io .kubernetes .client .extended .controller .Controller ;
1627import io .kubernetes .client .extended .controller .builder .ControllerBuilder ;
1728import io .kubernetes .client .extended .controller .builder .DefaultControllerBuilder ;
1829import io .kubernetes .client .extended .controller .reconciler .Reconciler ;
1930import io .kubernetes .client .extended .controller .reconciler .Request ;
2031import io .kubernetes .client .extended .controller .reconciler .Result ;
32+ import io .kubernetes .client .informer .SharedIndexInformer ;
2133import io .kubernetes .client .informer .SharedInformer ;
2234import io .kubernetes .client .informer .SharedInformerFactory ;
2335import io .kubernetes .client .informer .cache .Lister ;
36+ import io .kubernetes .client .openapi .ApiClient ;
2437import io .kubernetes .client .openapi .models .V1Endpoints ;
2538import io .kubernetes .client .openapi .models .V1EndpointsList ;
2639import io .kubernetes .client .openapi .models .V1Node ;
2740import io .kubernetes .client .openapi .models .V1NodeList ;
2841import io .kubernetes .client .openapi .models .V1Pod ;
2942import io .kubernetes .client .openapi .models .V1PodList ;
30- import io .kubernetes .client .spring .extended .controller .annotation .GroupVersionResource ;
31- import io .kubernetes .client .spring .extended .controller .annotation .KubernetesInformer ;
32- import io .kubernetes .client .spring .extended .controller .annotation .KubernetesInformers ;
33- import java .time .Duration ;
34- import org .springframework .beans .factory .annotation .Autowired ;
35- import org .springframework .beans .factory .annotation .Value ;
36- import org .springframework .boot .CommandLineRunner ;
37- import org .springframework .boot .SpringApplication ;
38- import org .springframework .boot .autoconfigure .SpringBootApplication ;
39- import org .springframework .context .annotation .Bean ;
40- import org .springframework .context .annotation .Configuration ;
41- import org .springframework .stereotype .Component ;
43+ import io .kubernetes .client .util .generic .GenericKubernetesApi ;
4244
4345@ SpringBootApplication
4446public class SpringControllerExample {
@@ -51,8 +53,8 @@ public static void main(String[] args) {
5153 public static class AppConfig {
5254
5355 @ Bean
54- public CommandLineRunner commandLineRunner (
55- SharedInformerFactory sharedInformerFactory , Controller nodePrintingController ) {
56+ public CommandLineRunner commandLineRunner (SharedInformerFactory sharedInformerFactory ,
57+ Controller nodePrintingController ) {
5658 return args -> {
5759 System .out .println ("starting informers.." );
5860 sharedInformerFactory .startAllRegisteredInformers ();
@@ -63,53 +65,62 @@ public CommandLineRunner commandLineRunner(
6365 }
6466
6567 @ Bean
66- public Controller nodePrintingController (
67- SharedInformerFactory sharedInformerFactory , NodePrintingReconciler reconciler ) {
68+ public Controller nodePrintingController (SharedInformerFactory sharedInformerFactory ,
69+ NodePrintingReconciler reconciler ) {
6870 DefaultControllerBuilder builder = ControllerBuilder .defaultBuilder (sharedInformerFactory );
69- builder =
70- builder .watch (
71- (q ) -> {
72- return ControllerBuilder .controllerWatchBuilder (V1Node .class , q )
73- .withResyncPeriod (Duration .ofMinutes (1 ))
74- .build ();
75- });
71+ builder = builder .watch ((q ) -> {
72+ return ControllerBuilder .controllerWatchBuilder (V1Node .class , q ).withResyncPeriod (Duration .ofMinutes (1 ))
73+ .build ();
74+ });
7675 builder .withWorkerCount (2 );
7776 builder .withReadyFunc (reconciler ::informerReady );
7877 return builder .withReconciler (reconciler ).withName ("nodePrintingController" ).build ();
7978 }
80- }
8179
82- @ KubernetesInformers ({ // Defining what resources is the informer-factory actually watching.
83- @ KubernetesInformer (
84- apiTypeClass = V1Endpoints .class ,
85- apiListTypeClass = V1EndpointsList .class ,
86- groupVersionResource =
87- @ GroupVersionResource (apiGroup = "" , apiVersion = "v1" , resourcePlural = "endpoints" )),
88- @ KubernetesInformer (
89- apiTypeClass = V1Node .class ,
90- apiListTypeClass = V1NodeList .class ,
91- groupVersionResource =
92- @ GroupVersionResource (apiGroup = "" , apiVersion = "v1" , resourcePlural = "nodes" ),
93- resyncPeriodMillis = 60 * 1000L ),
94- @ KubernetesInformer (
95- apiTypeClass = V1Pod .class ,
96- apiListTypeClass = V1PodList .class ,
97- groupVersionResource =
98- @ GroupVersionResource (apiGroup = "" , apiVersion = "v1" , resourcePlural = "pods" )),
99- })
100- @ Component
101- public static class MySharedInformerFactory extends SharedInformerFactory {}
80+ @ Bean
81+ public SharedIndexInformer <V1Endpoints > endpointsInformer (ApiClient apiClient ,
82+ SharedInformerFactory sharedInformerFactory ) {
83+ GenericKubernetesApi <V1Endpoints , V1EndpointsList > genericApi = new GenericKubernetesApi <>(V1Endpoints .class ,
84+ V1EndpointsList .class , "" , "v1" , "endpoints" , apiClient );
85+ return sharedInformerFactory .sharedIndexInformerFor ( genericApi , V1Endpoints .class , 0 );
86+ }
87+
88+ @ Bean
89+ public SharedIndexInformer <V1Node > nodeInformer (ApiClient apiClient , SharedInformerFactory sharedInformerFactory ) {
90+ GenericKubernetesApi <V1Node , V1NodeList > genericApi = new GenericKubernetesApi <>(V1Node .class ,
91+ V1NodeList .class , "" , "v1" , "nodes" , apiClient );
92+ return sharedInformerFactory .sharedIndexInformerFor ( genericApi , V1Node .class , 60 * 1000L );
93+ }
94+
95+ @ Bean
96+ public SharedIndexInformer <V1Pod > podInformer (ApiClient apiClient , SharedInformerFactory sharedInformerFactory ) {
97+ GenericKubernetesApi <V1Pod , V1PodList > genericApi = new GenericKubernetesApi <>(V1Pod .class , V1PodList .class ,
98+ "" , "v1" , "pods" , apiClient );
99+ return sharedInformerFactory .sharedIndexInformerFor ( genericApi , V1Pod .class , 0 );
100+ }
101+
102+ }
102103
103104 @ Component
104105 public static class NodePrintingReconciler implements Reconciler {
105106
106107 @ Value ("${namespace}" )
107108 private String namespace ;
108109
109- @ Autowired private SharedInformer <V1Node > nodeInformer ;
110- @ Autowired private SharedInformer <V1Pod > podInformer ;
111- @ Autowired private Lister <V1Node > nodeLister ;
112- @ Autowired private Lister <V1Pod > podLister ;
110+ private SharedInformer <V1Node > nodeInformer ;
111+
112+ private SharedInformer <V1Pod > podInformer ;
113+
114+ private Lister <V1Node > nodeLister ;
115+
116+ private Lister <V1Pod > podLister ;
117+
118+ public NodePrintingReconciler (SharedIndexInformer <V1Node > nodeInformer , SharedIndexInformer <V1Pod > podInformer ) {
119+ this .nodeInformer = nodeInformer ;
120+ this .podInformer = podInformer ;
121+ this .nodeLister = new Lister <>(nodeInformer .getIndexer (), namespace );
122+ this .podLister = new Lister <>(podInformer .getIndexer (), namespace );
123+ }
113124
114125 // *OPTIONAL*
115126 // If you want to hold the controller from running util some condition..
@@ -122,8 +133,7 @@ public Result reconcile(Request request) {
122133 V1Node node = nodeLister .get (request .getName ());
123134
124135 System .out .println ("get all pods in namespace " + namespace );
125- podLister .namespace (namespace ).list ().stream ()
126- .map (pod -> pod .getMetadata ().getName ())
136+ podLister .namespace (namespace ).list ().stream ().map (pod -> pod .getMetadata ().getName ())
127137 .forEach (System .out ::println );
128138
129139 System .out .println ("triggered reconciling " + node .getMetadata ().getName ());
0 commit comments