10
10
See the License for the specific language governing permissions and
11
11
limitations under the License.
12
12
*/
13
- package io .kubernetes .client .examples . spring ;
13
+ package io .kubernetes .client .examples ;
14
14
15
15
import io .kubernetes .client .extended .controller .Controller ;
16
16
import io .kubernetes .client .extended .controller .reconciler .Reconciler ;
25
25
import io .kubernetes .client .openapi .models .V1Pod ;
26
26
import io .kubernetes .client .openapi .models .V1PodList ;
27
27
import io .kubernetes .client .spring .extended .controller .KubernetesInformerConfigurer ;
28
- import io .kubernetes .client .spring .extended .controller .KubernetesReconcilerConfigurer ;
29
28
import io .kubernetes .client .spring .extended .controller .annotation .*;
29
+ import io .kubernetes .client .spring .extended .controller .factory .KubernetesControllerFactory ;
30
30
import io .kubernetes .client .util .ClientBuilder ;
31
31
import java .io .IOException ;
32
32
import java .time .Duration ;
33
+ import org .springframework .beans .factory .annotation .Autowired ;
33
34
import org .springframework .beans .factory .annotation .Qualifier ;
35
+ import org .springframework .beans .factory .annotation .Value ;
34
36
import org .springframework .boot .CommandLineRunner ;
35
37
import org .springframework .boot .SpringApplication ;
36
38
import org .springframework .boot .autoconfigure .SpringBootApplication ;
37
39
import org .springframework .context .annotation .Bean ;
38
40
import org .springframework .context .annotation .Configuration ;
41
+ import org .springframework .stereotype .Component ;
39
42
40
43
@ SpringBootApplication
41
44
public class SpringControllerExample {
@@ -68,11 +71,11 @@ public KubernetesInformerConfigurer kubernetesInformerConfigurer(ApiClient apiCl
68
71
}
69
72
70
73
// *REQUIRED*
71
- // Configurer components that registers reconciler to the controller-manager in the context.
72
- @ Bean
73
- public KubernetesReconcilerConfigurer kubernetesReconcilerConfigurer (
74
- SharedInformerFactory sharedInformerFactory ) {
75
- return new KubernetesReconcilerConfigurer (sharedInformerFactory );
74
+ // factorybean to crete controller
75
+ @ Bean ( "node-printing-controller" )
76
+ public KubernetesControllerFactory kubernetesReconcilerConfigurer (
77
+ SharedInformerFactory sharedInformerFactory , Reconciler reconciler ) {
78
+ return new KubernetesControllerFactory (sharedInformerFactory , reconciler );
76
79
}
77
80
78
81
// *OPTIONAL*
@@ -91,12 +94,6 @@ public ApiClient myApiClient() throws IOException {
91
94
public SharedInformerFactory sharedInformerFactory () {
92
95
return new MySharedInformerFactory ();
93
96
}
94
-
95
- @ Bean
96
- public NodePrintingReconciler nodePrintingReconciler (
97
- Lister <V1Pod > podLister , Lister <V1Node > nodeLister , SharedInformer <V1Node > nodeInformer ) {
98
- return new NodePrintingReconciler (podLister , nodeLister , nodeInformer );
99
- }
100
97
}
101
98
102
99
@ KubernetesInformers ({ // Defining what resources is the informer-factory actually watching.
@@ -128,32 +125,33 @@ public static class MySharedInformerFactory extends SharedInformerFactory {}
128
125
resyncPeriodMillis = 60 * 1000L // fully resync every 1 minute
129
126
),
130
127
}))
128
+ @ Component
131
129
public static class NodePrintingReconciler implements Reconciler {
132
130
133
- public NodePrintingReconciler (
134
- Lister <V1Pod > podLister , Lister <V1Node > nodeLister , SharedInformer <V1Node > nodeInformer ) {
135
- this .nodeLister = nodeLister ;
136
- this .podLister = podLister ;
137
- this .nodeInformer = nodeInformer ;
138
- }
131
+ @ Value ("${namespace}" )
132
+ private String namespace ;
139
133
140
- private SharedInformer <V1Node > nodeInformer ;
141
-
142
- private Lister <V1Node > nodeLister ;
143
-
144
- private Lister <V1Pod > podLister ;
134
+ @ Autowired private SharedInformer <V1Node > nodeInformer ;
135
+ @ Autowired private SharedInformer <V1Pod > podInformer ;
136
+ @ Autowired private Lister <V1Node > nodeLister ;
137
+ @ Autowired private Lister <V1Pod > podLister ;
145
138
146
139
// *OPTIONAL*
147
140
// If you feed like hold the controller from running util some condition..
148
- // need to be a public function
149
141
@ KubernetesReconcilerReadyFunc
150
142
public boolean informerReady () {
151
- return nodeInformer .hasSynced ();
143
+ return podInformer . hasSynced () && nodeInformer .hasSynced ();
152
144
}
153
145
154
146
@ Override
155
147
public Result reconcile (Request request ) {
156
148
V1Node node = nodeLister .get (request .getName ());
149
+
150
+ System .out .println ("get all pods in namespace " + namespace );
151
+ podLister .namespace (namespace ).list ().stream ()
152
+ .map (pod -> pod .getMetadata ().getName ())
153
+ .forEach (System .out ::println );
154
+
157
155
System .out .println ("triggered reconciling " + node .getMetadata ().getName ());
158
156
return new Result (false );
159
157
}
0 commit comments