Skip to content

Commit c535c9b

Browse files
authored
Merge pull request #1687 from dsyer/remove-annotations
Removes Kubernetes annotations from Spring integration
2 parents adcbb61 + 2676f2d commit c535c9b

14 files changed

+116
-62
lines changed

examples/examples-release-13/src/main/java/io/kubernetes/client/examples/SpringControllerExample.java

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,24 @@
1313
package io.kubernetes.client.examples;
1414

1515
import io.kubernetes.client.extended.controller.Controller;
16+
import io.kubernetes.client.extended.controller.builder.ControllerBuilder;
17+
import io.kubernetes.client.extended.controller.builder.DefaultControllerBuilder;
1618
import io.kubernetes.client.extended.controller.reconciler.Reconciler;
1719
import io.kubernetes.client.extended.controller.reconciler.Request;
1820
import io.kubernetes.client.extended.controller.reconciler.Result;
21+
import io.kubernetes.client.informer.SharedIndexInformer;
1922
import io.kubernetes.client.informer.SharedInformer;
2023
import io.kubernetes.client.informer.SharedInformerFactory;
2124
import io.kubernetes.client.informer.cache.Lister;
25+
import io.kubernetes.client.openapi.ApiClient;
2226
import io.kubernetes.client.openapi.models.V1Endpoints;
2327
import io.kubernetes.client.openapi.models.V1EndpointsList;
2428
import io.kubernetes.client.openapi.models.V1Node;
2529
import io.kubernetes.client.openapi.models.V1NodeList;
2630
import io.kubernetes.client.openapi.models.V1Pod;
2731
import io.kubernetes.client.openapi.models.V1PodList;
28-
import io.kubernetes.client.spring.extended.controller.annotation.GroupVersionResource;
29-
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer;
30-
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers;
31-
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconciler;
32-
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerReadyFunc;
33-
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatch;
34-
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesReconcilerWatches;
35-
import io.kubernetes.client.spring.extended.controller.factory.KubernetesControllerFactory;
36-
import org.springframework.beans.factory.annotation.Autowired;
37-
import org.springframework.beans.factory.annotation.Qualifier;
32+
import io.kubernetes.client.util.generic.GenericKubernetesApi;
33+
import java.time.Duration;
3834
import org.springframework.beans.factory.annotation.Value;
3935
import org.springframework.boot.CommandLineRunner;
4036
import org.springframework.boot.SpringApplication;
@@ -55,8 +51,7 @@ public static class AppConfig {
5551

5652
@Bean
5753
public CommandLineRunner commandLineRunner(
58-
SharedInformerFactory sharedInformerFactory,
59-
@Qualifier("node-printing-controller") Controller nodePrintingController) {
54+
SharedInformerFactory sharedInformerFactory, Controller nodePrintingController) {
6055
return args -> {
6156
System.out.println("starting informers..");
6257
sharedInformerFactory.startAllRegisteredInformers();
@@ -66,63 +61,72 @@ public CommandLineRunner commandLineRunner(
6661
};
6762
}
6863

69-
// *REQUIRED*
70-
// factorybean to crete controller
71-
@Bean("node-printing-controller")
72-
public KubernetesControllerFactory kubernetesReconcilerConfigurer(
73-
SharedInformerFactory sharedInformerFactory, Reconciler reconciler) {
74-
return new KubernetesControllerFactory(sharedInformerFactory, reconciler);
64+
@Bean
65+
public Controller nodePrintingController(
66+
SharedInformerFactory sharedInformerFactory, NodePrintingReconciler reconciler) {
67+
DefaultControllerBuilder builder = ControllerBuilder.defaultBuilder(sharedInformerFactory);
68+
builder =
69+
builder.watch(
70+
(q) -> {
71+
return ControllerBuilder.controllerWatchBuilder(V1Node.class, q)
72+
.withResyncPeriod(Duration.ofMinutes(1))
73+
.build();
74+
});
75+
builder.withWorkerCount(2);
76+
builder.withReadyFunc(reconciler::informerReady);
77+
return builder.withReconciler(reconciler).withName("nodePrintingController").build();
78+
}
79+
80+
@Bean
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);
87+
}
88+
89+
@Bean
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);
95+
}
96+
97+
@Bean
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);
75103
}
76104
}
77105

78-
@KubernetesInformers({ // Defining what resources is the informer-factory actually watching.
79-
@KubernetesInformer(
80-
apiTypeClass = V1Endpoints.class,
81-
apiListTypeClass = V1EndpointsList.class,
82-
groupVersionResource =
83-
@GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "endpoints")),
84-
@KubernetesInformer(
85-
apiTypeClass = V1Node.class,
86-
apiListTypeClass = V1NodeList.class,
87-
groupVersionResource =
88-
@GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "nodes"),
89-
resyncPeriodMillis = 60 * 1000L),
90-
@KubernetesInformer(
91-
apiTypeClass = V1Pod.class,
92-
apiListTypeClass = V1PodList.class,
93-
groupVersionResource =
94-
@GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "pods")),
95-
})
96-
@Component
97-
public static class MySharedInformerFactory extends SharedInformerFactory {}
98-
99-
// As long as a reconciler bean attached `@KubernetesReconciler` detected in the context, we
100-
// will
101-
// be automatically creating a conresponding controller bean implementing {@link
102-
// io.kubernetes.client.extended.controller.Controller}
103-
// with the name specified and registering it to the spring bean-factory.
104-
@KubernetesReconciler(
105-
watches =
106-
@KubernetesReconcilerWatches({
107-
@KubernetesReconcilerWatch(
108-
apiTypeClass = V1Node.class,
109-
resyncPeriodMillis = 60 * 1000L // fully resync every 1 minute
110-
),
111-
}))
112106
@Component
113107
public static class NodePrintingReconciler implements Reconciler {
114108

115109
@Value("${namespace}")
116110
private String namespace;
117111

118-
@Autowired private SharedInformer<V1Node> nodeInformer;
119-
@Autowired private SharedInformer<V1Pod> podInformer;
120-
@Autowired private Lister<V1Node> nodeLister;
121-
@Autowired private Lister<V1Pod> podLister;
112+
private SharedInformer<V1Node> nodeInformer;
113+
114+
private SharedInformer<V1Pod> podInformer;
115+
116+
private Lister<V1Node> nodeLister;
117+
118+
private Lister<V1Pod> podLister;
119+
120+
public NodePrintingReconciler(
121+
SharedIndexInformer<V1Node> nodeInformer, SharedIndexInformer<V1Pod> podInformer) {
122+
this.nodeInformer = nodeInformer;
123+
this.podInformer = podInformer;
124+
this.nodeLister = new Lister<>(nodeInformer.getIndexer(), namespace);
125+
this.podLister = new Lister<>(podInformer.getIndexer(), namespace);
126+
}
122127

123128
// *OPTIONAL*
124-
// If you feed like hold the controller from running util some condition..
125-
@KubernetesReconcilerReadyFunc
129+
// If you want to hold the controller from running util some condition..
126130
public boolean informerReady() {
127131
return podInformer.hasSynced() && nodeInformer.hasSynced();
128132
}

examples/examples-release-13/src/main/java/io/kubernetes/client/examples/SpringLoadBalancerExample.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.kubernetes.client.extended.network.EndpointsLoadBalancer;
1616
import io.kubernetes.client.extended.network.LoadBalancer;
1717
import io.kubernetes.client.extended.network.RoundRobinLoadBalanceStrategy;
18+
import io.kubernetes.client.informer.SharedIndexInformer;
1819
import io.kubernetes.client.informer.SharedInformerFactory;
1920
import io.kubernetes.client.informer.cache.Lister;
2021
import io.kubernetes.client.openapi.models.V1Endpoints;
@@ -48,8 +49,8 @@ public CommandLineRunner loadBalancerCommandLineRunner(
4849
}
4950

5051
@Bean
51-
public MyService myService(Lister<V1Endpoints> lister) {
52-
return new MyService(lister);
52+
public MyService myService(SharedIndexInformer<V1Endpoints> lister) {
53+
return new MyService(new Lister<>(lister.getIndexer()));
5354
}
5455
}
5556

spring/src/main/java/io/kubernetes/client/spring/extended/controller/KubernetesInformerFactoryProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@
4444
* new ApiClient if there's no user-specified one for override. 3. By reading from {@link
4545
* io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers}, instantiates and
4646
* injects informers to spring context with the underlying constructing process hidden from users.
47+
*
48+
* @deprecated instead of declaring via the annotation create the informers manually as {@link
49+
* org.springframework.context.annotation.Bean @Beans}
4750
*/
51+
@Deprecated
4852
public class KubernetesInformerFactoryProcessor
4953
implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware, Ordered {
5054

spring/src/main/java/io/kubernetes/client/spring/extended/controller/KubernetesReconcilerProcessor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
*
3232
* <p>It will create a {@link io.kubernetes.client.extended.controller.Controller} for every
3333
* reconciler instances registered in the spring bean-factory.
34+
*
35+
* @deprecated instead of declaring via the annotation create the controller manually as a {@link
36+
* org.springframework.context.annotation.Bean @Bean}
3437
*/
38+
@Deprecated
3539
public class KubernetesReconcilerProcessor implements BeanDefinitionRegistryPostProcessor, Ordered {
3640

3741
public static final String DEFAULT_SHARED_INFORMER_FACTORY_BEAN_NAME = "sharedInformerFactory";

spring/src/main/java/io/kubernetes/client/spring/extended/controller/annotation/AddWatchEventFilter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@
2323
* from watches.
2424
*
2525
* <p>A add-event filter must have the signature as {@link java.util.function.Predicate<ApiType>}
26+
*
27+
* @deprecated register the watcher via the {@link
28+
* io.kubernetes.client.extended.controller.builder.ControllerBuilder} API
2629
*/
30+
@Deprecated
2731
@Target({ElementType.METHOD})
2832
@Retention(RetentionPolicy.RUNTIME)
2933
public @interface AddWatchEventFilter {

spring/src/main/java/io/kubernetes/client/spring/extended/controller/annotation/DeleteWatchEventFilter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
* java.util.function.BiPredicate<ApiType, Boolean>} where the 1st parameter is the
2727
* "before-deletion" state of the resource and the 2nd indicates whether the cache entry for the
2828
* resource is stale.
29+
*
30+
* @deprecated register the watcher via the {@link
31+
* io.kubernetes.client.extended.controller.builder.ControllerBuilder} API
2932
*/
33+
@Deprecated
3034
@Target({ElementType.METHOD})
3135
@Retention(RetentionPolicy.RUNTIME)
3236
public @interface DeleteWatchEventFilter {

spring/src/main/java/io/kubernetes/client/spring/extended/controller/annotation/GroupVersionResource.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import java.lang.annotation.RetentionPolicy;
1818
import java.lang.annotation.Target;
1919

20-
/** The standard Kubernetes group-version-resource. */
20+
/**
21+
* The standard Kubernetes group-version-resource.
22+
*
23+
* @deprecated use the {@link io.kubernetes.client.util.generic.GenericKubernetesApi} instead
24+
*/
25+
@Deprecated
2126
@Target({ElementType.TYPE})
2227
@Retention(RetentionPolicy.RUNTIME)
2328
public @interface GroupVersionResource {

spring/src/main/java/io/kubernetes/client/spring/extended/controller/annotation/KubernetesInformer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
* The interface Kubernetes informer specifies the arguments for injecting an informer into the
2727
* informer-factory. The default argument list works for instantiating an informer list-watching
2828
* namespace resources.
29+
*
30+
* @deprecated create a {@link io.kubernetes.client.informer.SharedIndexInformer} as a bean instead
2931
*/
32+
@Deprecated
3033
@Target({ElementType.TYPE})
3134
@Retention(RetentionPolicy.RUNTIME)
3235
public @interface KubernetesInformer {

spring/src/main/java/io/kubernetes/client/spring/extended/controller/annotation/KubernetesInformers.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
* <p>This annotation is specifically used for joining a list of {@link
2626
* io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer} onto one bean
2727
* method.
28+
*
29+
* @deprecated create a {@link io.kubernetes.client.informer.SharedIndexInformer} beans instead
2830
*/
31+
@Deprecated
2932
@Target({ElementType.TYPE, ElementType.METHOD})
3033
@Retention(RetentionPolicy.RUNTIME)
3134
public @interface KubernetesInformers {

spring/src/main/java/io/kubernetes/client/spring/extended/controller/annotation/KubernetesReconciler.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
* KubernetesReconciler#value()} specifies.
2727
*
2828
* <p>Note that the automatically created controller is not started by default.
29+
*
30+
* @deprecated create a controller as a bean instead using {@link
31+
* io.kubernetes.client.extended.controller.builder.ControllerBuilder}
2932
*/
33+
@Deprecated
3034
@Target({ElementType.TYPE})
3135
@Retention(RetentionPolicy.RUNTIME)
3236
public @interface KubernetesReconciler {

0 commit comments

Comments
 (0)