Skip to content

Commit 5d1bfc2

Browse files
mingjie-liMingjieAmadeus
authored andcommitted
add spring factorybean and example
1 parent 7bcb2eb commit 5d1bfc2

File tree

5 files changed

+481
-227
lines changed

5 files changed

+481
-227
lines changed

examples/src/main/java/io/kubernetes/client/examples/SpringControllerExample.java renamed to examples/src/main/java/io/kubernetes/client/examples/spring/SpringControllerExample.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
See the License for the specific language governing permissions and
1111
limitations under the License.
1212
*/
13-
package io.kubernetes.client.examples;
13+
package io.kubernetes.client.examples.spring;
1414

1515
import io.kubernetes.client.extended.controller.Controller;
1616
import io.kubernetes.client.extended.controller.reconciler.Reconciler;
@@ -145,8 +145,9 @@ public NodePrintingReconciler(
145145

146146
// *OPTIONAL*
147147
// If you feed like hold the controller from running util some condition..
148+
// need to be a public function
148149
@KubernetesReconcilerReadyFunc
149-
boolean informerReady() {
150+
public boolean informerReady() {
150151
return nodeInformer.hasSynced();
151152
}
152153

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.examples.springfactorybean;
14+
15+
import io.kubernetes.client.extended.controller.Controller;
16+
import io.kubernetes.client.extended.controller.reconciler.Reconciler;
17+
import io.kubernetes.client.extended.controller.reconciler.Request;
18+
import io.kubernetes.client.extended.controller.reconciler.Result;
19+
import io.kubernetes.client.informer.SharedInformer;
20+
import io.kubernetes.client.informer.SharedInformerFactory;
21+
import io.kubernetes.client.informer.cache.Lister;
22+
import io.kubernetes.client.openapi.ApiClient;
23+
import io.kubernetes.client.openapi.models.V1Node;
24+
import io.kubernetes.client.openapi.models.V1NodeList;
25+
import io.kubernetes.client.openapi.models.V1Pod;
26+
import io.kubernetes.client.openapi.models.V1PodList;
27+
import io.kubernetes.client.spring.extended.controller.KubernetesInformerConfigurer;
28+
import io.kubernetes.client.spring.extended.controller.KubernetesReconcilerConfigurer;
29+
import io.kubernetes.client.spring.extended.controller.annotation.*;
30+
import io.kubernetes.client.spring.extended.controller.factory.KubernetesControllerFactory;
31+
import io.kubernetes.client.util.ClientBuilder;
32+
import java.io.IOException;
33+
import java.time.Duration;
34+
35+
import org.springframework.beans.factory.annotation.Autowired;
36+
import org.springframework.beans.factory.annotation.Qualifier;
37+
import org.springframework.beans.factory.annotation.Value;
38+
import org.springframework.boot.CommandLineRunner;
39+
import org.springframework.boot.SpringApplication;
40+
import org.springframework.boot.autoconfigure.SpringBootApplication;
41+
import org.springframework.context.annotation.Bean;
42+
import org.springframework.context.annotation.Configuration;
43+
import org.springframework.stereotype.Component;
44+
45+
@SpringBootApplication
46+
public class SpringControllerFactoryBeanExample {
47+
48+
public static void main(String[] args) {
49+
SpringApplication.run(SpringControllerFactoryBeanExample.class, args);
50+
}
51+
52+
@Configuration
53+
public static class AppConfig {
54+
55+
@Bean
56+
public CommandLineRunner commandLineRunner(
57+
SharedInformerFactory sharedInformerFactory,
58+
@Qualifier("node-printing-controller") Controller nodePrintingController) {
59+
return args -> {
60+
System.out.println("starting informers..");
61+
sharedInformerFactory.startAllRegisteredInformers();
62+
63+
System.out.println("running controller..");
64+
nodePrintingController.run();
65+
};
66+
}
67+
68+
// *REQUIRED*
69+
// Configurer components that registers informers to the informer-factory in the context.
70+
@Bean
71+
public KubernetesInformerConfigurer kubernetesInformerConfigurer(ApiClient apiClient) {
72+
return new KubernetesInformerConfigurer(apiClient);
73+
}
74+
75+
// *REQUIRED*
76+
// factorybean to crete controller
77+
@Bean("node-printing-controller")
78+
public KubernetesControllerFactory kubernetesReconcilerConfigurer(
79+
SharedInformerFactory sharedInformerFactory, Reconciler reconciler) {
80+
return new KubernetesControllerFactory(sharedInformerFactory, reconciler);
81+
}
82+
83+
// *OPTIONAL*
84+
// Injecting and customize your ApiClient, if not specified, fallbacks to {@link
85+
// io.kubernetes.client.util.ClientBuilder#standard}
86+
@Bean
87+
public ApiClient myApiClient() throws IOException {
88+
ApiClient apiClient = ClientBuilder.standard().build();
89+
return apiClient.setHttpClient(
90+
apiClient.getHttpClient().newBuilder().readTimeout(Duration.ZERO).build());
91+
}
92+
93+
// *REQUIRED*
94+
// Injecting your SharedInformerFactory class annotated `@KubernetesInformers`
95+
@Bean("sharedInformerFactory")
96+
public SharedInformerFactory sharedInformerFactory() {
97+
return new MySharedInformerFactory();
98+
}
99+
}
100+
101+
@KubernetesInformers({ // Defining what resources is the informer-factory actually watching.
102+
@KubernetesInformer(
103+
apiTypeClass = V1Node.class,
104+
apiListTypeClass = V1NodeList.class,
105+
groupVersionResource =
106+
@GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "nodes"),
107+
resyncPeriodMillis = 60 * 1000L),
108+
@KubernetesInformer(
109+
apiTypeClass = V1Pod.class,
110+
apiListTypeClass = V1PodList.class,
111+
groupVersionResource =
112+
@GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "pods")),
113+
})
114+
public static class MySharedInformerFactory extends SharedInformerFactory {}
115+
116+
// As long as a reconciler bean attached `@KubernetesReconciler` detected in the context, we
117+
// will
118+
// be automatically creating a conresponding controller bean implementing {@link
119+
// io.kubernetes.client.extended.controller.Controller}
120+
// with the name specified and registering it to the spring bean-factory.
121+
@KubernetesReconciler(
122+
value = "node-printing-controller",
123+
watches =
124+
@KubernetesReconcilerWatches({
125+
@KubernetesReconcilerWatch(
126+
apiTypeClass = V1Node.class,
127+
resyncPeriodMillis = 60 * 1000L // fully resync every 1 minute
128+
),
129+
}))
130+
@Component
131+
public static class NodePrintingReconciler implements Reconciler {
132+
133+
@Value("${namespace}")
134+
private String namespace;
135+
@Autowired
136+
private SharedInformer<V1Node> nodeInformer;
137+
@Autowired
138+
private SharedInformer<V1Pod> podInformer;
139+
@Autowired
140+
private Lister<V1Node> nodeLister;
141+
@Autowired
142+
private Lister<V1Pod> podLister;
143+
144+
// *OPTIONAL*
145+
// If you feed like hold the controller from running util some condition..
146+
@KubernetesReconcilerReadyFunc
147+
public boolean informerReady() {
148+
return podInformer.hasSynced() && nodeInformer.hasSynced();
149+
}
150+
151+
@Override
152+
public Result reconcile(Request request) {
153+
V1Node node = nodeLister.get(request.getName());
154+
155+
System.out.println("get all pods in namespace "+namespace);
156+
podLister.namespace(namespace).list().stream()
157+
.map(pod->pod.getMetadata().getName()).forEach(System.out::println);
158+
159+
System.out.println("triggered reconciling " + node.getMetadata().getName());
160+
return new Result(false);
161+
}
162+
}
163+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
namespace=airflow

0 commit comments

Comments
 (0)