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
+
15
26
import io .kubernetes .client .extended .controller .Controller ;
16
27
import io .kubernetes .client .extended .controller .builder .ControllerBuilder ;
17
28
import io .kubernetes .client .extended .controller .builder .DefaultControllerBuilder ;
18
29
import io .kubernetes .client .extended .controller .reconciler .Reconciler ;
19
30
import io .kubernetes .client .extended .controller .reconciler .Request ;
20
31
import io .kubernetes .client .extended .controller .reconciler .Result ;
32
+ import io .kubernetes .client .informer .SharedIndexInformer ;
21
33
import io .kubernetes .client .informer .SharedInformer ;
22
34
import io .kubernetes .client .informer .SharedInformerFactory ;
23
35
import io .kubernetes .client .informer .cache .Lister ;
36
+ import io .kubernetes .client .openapi .ApiClient ;
24
37
import io .kubernetes .client .openapi .models .V1Endpoints ;
25
38
import io .kubernetes .client .openapi .models .V1EndpointsList ;
26
39
import io .kubernetes .client .openapi .models .V1Node ;
27
40
import io .kubernetes .client .openapi .models .V1NodeList ;
28
41
import io .kubernetes .client .openapi .models .V1Pod ;
29
42
import 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 ;
42
44
43
45
@ SpringBootApplication
44
46
public class SpringControllerExample {
@@ -51,8 +53,8 @@ public static void main(String[] args) {
51
53
public static class AppConfig {
52
54
53
55
@ Bean
54
- public CommandLineRunner commandLineRunner (
55
- SharedInformerFactory sharedInformerFactory , Controller nodePrintingController ) {
56
+ public CommandLineRunner commandLineRunner (SharedInformerFactory sharedInformerFactory ,
57
+ Controller nodePrintingController ) {
56
58
return args -> {
57
59
System .out .println ("starting informers.." );
58
60
sharedInformerFactory .startAllRegisteredInformers ();
@@ -63,53 +65,62 @@ public CommandLineRunner commandLineRunner(
63
65
}
64
66
65
67
@ Bean
66
- public Controller nodePrintingController (
67
- SharedInformerFactory sharedInformerFactory , NodePrintingReconciler reconciler ) {
68
+ public Controller nodePrintingController (SharedInformerFactory sharedInformerFactory ,
69
+ NodePrintingReconciler reconciler ) {
68
70
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
+ });
76
75
builder .withWorkerCount (2 );
77
76
builder .withReadyFunc (reconciler ::informerReady );
78
77
return builder .withReconciler (reconciler ).withName ("nodePrintingController" ).build ();
79
78
}
80
- }
81
79
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
+ }
102
103
103
104
@ Component
104
105
public static class NodePrintingReconciler implements Reconciler {
105
106
106
107
@ Value ("${namespace}" )
107
108
private String namespace ;
108
109
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
+ }
113
124
114
125
// *OPTIONAL*
115
126
// If you want to hold the controller from running util some condition..
@@ -122,8 +133,7 @@ public Result reconcile(Request request) {
122
133
V1Node node = nodeLister .get (request .getName ());
123
134
124
135
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 ())
127
137
.forEach (System .out ::println );
128
138
129
139
System .out .println ("triggered reconciling " + node .getMetadata ().getName ());
0 commit comments