12
12
*/
13
13
package io .kubernetes .client .examples ;
14
14
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
-
26
15
import io .kubernetes .client .extended .controller .Controller ;
27
16
import io .kubernetes .client .extended .controller .builder .ControllerBuilder ;
28
17
import io .kubernetes .client .extended .controller .builder .DefaultControllerBuilder ;
41
30
import io .kubernetes .client .openapi .models .V1Pod ;
42
31
import io .kubernetes .client .openapi .models .V1PodList ;
43
32
import 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 ;
44
41
45
42
@ SpringBootApplication
46
43
public class SpringControllerExample {
@@ -53,8 +50,8 @@ public static void main(String[] args) {
53
50
public static class AppConfig {
54
51
55
52
@ Bean
56
- public CommandLineRunner commandLineRunner (SharedInformerFactory sharedInformerFactory ,
57
- Controller nodePrintingController ) {
53
+ public CommandLineRunner commandLineRunner (
54
+ SharedInformerFactory sharedInformerFactory , Controller nodePrintingController ) {
58
55
return args -> {
59
56
System .out .println ("starting informers.." );
60
57
sharedInformerFactory .startAllRegisteredInformers ();
@@ -65,40 +62,45 @@ public CommandLineRunner commandLineRunner(SharedInformerFactory sharedInformerF
65
62
}
66
63
67
64
@ Bean
68
- public Controller nodePrintingController (SharedInformerFactory sharedInformerFactory ,
69
- NodePrintingReconciler reconciler ) {
65
+ public Controller nodePrintingController (
66
+ SharedInformerFactory sharedInformerFactory , NodePrintingReconciler reconciler ) {
70
67
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
+ });
75
75
builder .withWorkerCount (2 );
76
76
builder .withReadyFunc (reconciler ::informerReady );
77
77
return builder .withReconciler (reconciler ).withName ("nodePrintingController" ).build ();
78
78
}
79
79
80
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 );
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 );
86
87
}
87
88
88
89
@ 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 );
93
95
}
94
96
95
97
@ 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 );
100
103
}
101
-
102
104
}
103
105
104
106
@ Component
@@ -115,7 +117,8 @@ public static class NodePrintingReconciler implements Reconciler {
115
117
116
118
private Lister <V1Pod > podLister ;
117
119
118
- public NodePrintingReconciler (SharedIndexInformer <V1Node > nodeInformer , SharedIndexInformer <V1Pod > podInformer ) {
120
+ public NodePrintingReconciler (
121
+ SharedIndexInformer <V1Node > nodeInformer , SharedIndexInformer <V1Pod > podInformer ) {
119
122
this .nodeInformer = nodeInformer ;
120
123
this .podInformer = podInformer ;
121
124
this .nodeLister = new Lister <>(nodeInformer .getIndexer (), namespace );
@@ -133,7 +136,8 @@ public Result reconcile(Request request) {
133
136
V1Node node = nodeLister .get (request .getName ());
134
137
135
138
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 ())
137
141
.forEach (System .out ::println );
138
142
139
143
System .out .println ("triggered reconciling " + node .getMetadata ().getName ());
0 commit comments