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-
2615import io .kubernetes .client .extended .controller .Controller ;
2716import io .kubernetes .client .extended .controller .builder .ControllerBuilder ;
2817import io .kubernetes .client .extended .controller .builder .DefaultControllerBuilder ;
4130import io .kubernetes .client .openapi .models .V1Pod ;
4231import io .kubernetes .client .openapi .models .V1PodList ;
4332import io .kubernetes .client .util .generic .GenericKubernetesApi ;
33+ import java .time .Duration ;
34+ import org .springframework .beans .factory .annotation .Value ;
35+ import org .springframework .boot .CommandLineRunner ;
36+ import org .springframework .boot .SpringApplication ;
37+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
38+ import org .springframework .context .annotation .Bean ;
39+ import org .springframework .context .annotation .Configuration ;
40+ import org .springframework .stereotype .Component ;
4441
4542@ SpringBootApplication
4643public class SpringControllerExample {
@@ -53,8 +50,8 @@ public static void main(String[] args) {
5350 public static class AppConfig {
5451
5552 @ Bean
56- public CommandLineRunner commandLineRunner (SharedInformerFactory sharedInformerFactory ,
57- Controller nodePrintingController ) {
53+ public CommandLineRunner commandLineRunner (
54+ SharedInformerFactory sharedInformerFactory , Controller nodePrintingController ) {
5855 return args -> {
5956 System .out .println ("starting informers.." );
6057 sharedInformerFactory .startAllRegisteredInformers ();
@@ -65,40 +62,45 @@ public CommandLineRunner commandLineRunner(SharedInformerFactory sharedInformerF
6562 }
6663
6764 @ Bean
68- public Controller nodePrintingController (SharedInformerFactory sharedInformerFactory ,
69- NodePrintingReconciler reconciler ) {
65+ public Controller nodePrintingController (
66+ SharedInformerFactory sharedInformerFactory , NodePrintingReconciler reconciler ) {
7067 DefaultControllerBuilder builder = ControllerBuilder .defaultBuilder (sharedInformerFactory );
71- builder = builder .watch ((q ) -> {
72- return ControllerBuilder .controllerWatchBuilder (V1Node .class , q ).withResyncPeriod (Duration .ofMinutes (1 ))
73- .build ();
74- });
68+ builder =
69+ builder .watch (
70+ (q ) -> {
71+ return ControllerBuilder .controllerWatchBuilder (V1Node .class , q )
72+ .withResyncPeriod (Duration .ofMinutes (1 ))
73+ .build ();
74+ });
7575 builder .withWorkerCount (2 );
7676 builder .withReadyFunc (reconciler ::informerReady );
7777 return builder .withReconciler (reconciler ).withName ("nodePrintingController" ).build ();
7878 }
7979
8080 @ 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 );
81+ public SharedIndexInformer <V1Endpoints > endpointsInformer (
82+ ApiClient apiClient , SharedInformerFactory sharedInformerFactory ) {
83+ GenericKubernetesApi <V1Endpoints , V1EndpointsList > genericApi =
84+ new GenericKubernetesApi <>(
85+ V1Endpoints .class , V1EndpointsList .class , "" , "v1" , "endpoints" , apiClient );
86+ return sharedInformerFactory .sharedIndexInformerFor (genericApi , V1Endpoints .class , 0 );
8687 }
8788
8889 @ 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 );
90+ public SharedIndexInformer <V1Node > nodeInformer (
91+ ApiClient apiClient , SharedInformerFactory sharedInformerFactory ) {
92+ GenericKubernetesApi <V1Node , V1NodeList > genericApi =
93+ new GenericKubernetesApi <>(V1Node .class , V1NodeList .class , "" , "v1" , "nodes" , apiClient );
94+ return sharedInformerFactory .sharedIndexInformerFor (genericApi , V1Node .class , 60 * 1000L );
9395 }
9496
9597 @ 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 );
98+ public SharedIndexInformer <V1Pod > podInformer (
99+ ApiClient apiClient , SharedInformerFactory sharedInformerFactory ) {
100+ GenericKubernetesApi <V1Pod , V1PodList > genericApi =
101+ new GenericKubernetesApi <>(V1Pod .class , V1PodList .class , "" , "v1" , "pods" , apiClient );
102+ return sharedInformerFactory .sharedIndexInformerFor (genericApi , V1Pod .class , 0 );
100103 }
101-
102104 }
103105
104106 @ Component
@@ -115,7 +117,8 @@ public static class NodePrintingReconciler implements Reconciler {
115117
116118 private Lister <V1Pod > podLister ;
117119
118- public NodePrintingReconciler (SharedIndexInformer <V1Node > nodeInformer , SharedIndexInformer <V1Pod > podInformer ) {
120+ public NodePrintingReconciler (
121+ SharedIndexInformer <V1Node > nodeInformer , SharedIndexInformer <V1Pod > podInformer ) {
119122 this .nodeInformer = nodeInformer ;
120123 this .podInformer = podInformer ;
121124 this .nodeLister = new Lister <>(nodeInformer .getIndexer (), namespace );
@@ -133,7 +136,8 @@ public Result reconcile(Request request) {
133136 V1Node node = nodeLister .get (request .getName ());
134137
135138 System .out .println ("get all pods in namespace " + namespace );
136- podLister .namespace (namespace ).list ().stream ().map (pod -> pod .getMetadata ().getName ())
139+ podLister .namespace (namespace ).list ().stream ()
140+ .map (pod -> pod .getMetadata ().getName ())
137141 .forEach (System .out ::println );
138142
139143 System .out .println ("triggered reconciling " + node .getMetadata ().getName ());
0 commit comments