|
| 1 | +package io.kubernetes.client.examples; |
| 2 | + |
| 3 | +import io.kubernetes.client.extended.controller.Controller; |
| 4 | +import io.kubernetes.client.extended.controller.reconciler.Reconciler; |
| 5 | +import io.kubernetes.client.extended.controller.reconciler.Request; |
| 6 | +import io.kubernetes.client.extended.controller.reconciler.Result; |
| 7 | +import io.kubernetes.client.informer.SharedInformer; |
| 8 | +import io.kubernetes.client.informer.SharedInformerFactory; |
| 9 | +import io.kubernetes.client.informer.cache.Lister; |
| 10 | +import io.kubernetes.client.openapi.ApiClient; |
| 11 | +import io.kubernetes.client.openapi.models.V1Node; |
| 12 | +import io.kubernetes.client.openapi.models.V1NodeList; |
| 13 | +import io.kubernetes.client.openapi.models.V1Pod; |
| 14 | +import io.kubernetes.client.openapi.models.V1PodList; |
| 15 | +import io.kubernetes.client.spring.extended.controller.annotation.*; |
| 16 | +import io.kubernetes.client.util.ClientBuilder; |
| 17 | +import java.io.IOException; |
| 18 | +import java.time.Duration; |
| 19 | +import org.springframework.beans.factory.annotation.Qualifier; |
| 20 | +import org.springframework.boot.CommandLineRunner; |
| 21 | +import org.springframework.boot.SpringApplication; |
| 22 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 23 | +import org.springframework.context.annotation.Bean; |
| 24 | +import org.springframework.context.annotation.ComponentScan; |
| 25 | +import org.springframework.context.annotation.Configuration; |
| 26 | + |
| 27 | +@SpringBootApplication |
| 28 | +public class SpringControllerExample { |
| 29 | + |
| 30 | + public static void main(String[] args) { |
| 31 | + SpringApplication.run(SpringControllerExample.class, args); |
| 32 | + } |
| 33 | + |
| 34 | + @Configuration |
| 35 | + @ComponentScan( |
| 36 | + basePackages = "io.kubernetes.client.spring.extended.controller" |
| 37 | + ) // Scanning beans under this package is *REQUIRED* for informers/reconciler injections. |
| 38 | + public static class AppConfig { |
| 39 | + |
| 40 | + @Bean |
| 41 | + public CommandLineRunner commandLineRunner( |
| 42 | + SharedInformerFactory sharedInformerFactory, |
| 43 | + @Qualifier("node-printing-controller") Controller nodePrintingController) { |
| 44 | + return args -> { |
| 45 | + System.out.println("starting informers.."); |
| 46 | + sharedInformerFactory.startAllRegisteredInformers(); |
| 47 | + |
| 48 | + System.out.println("running controller.."); |
| 49 | + nodePrintingController.run(); |
| 50 | + }; |
| 51 | + } |
| 52 | + |
| 53 | + // *OPTIONAL* |
| 54 | + // Injecting and customize your ApiClient, if not specified, fallbacks to {@link |
| 55 | + // io.kubernetes.client.util.ClientBuilder#standard} |
| 56 | + @Bean |
| 57 | + public ApiClient myApiClient() throws IOException { |
| 58 | + ApiClient apiClient = ClientBuilder.standard().build(); |
| 59 | + return apiClient.setHttpClient( |
| 60 | + apiClient.getHttpClient().newBuilder().readTimeout(Duration.ZERO).build()); |
| 61 | + } |
| 62 | + |
| 63 | + // *REQUIRED* |
| 64 | + // Injecting your SharedInformerFactory class annotated `@KubernetesInformers` |
| 65 | + @Bean("sharedInformerFactory") |
| 66 | + public SharedInformerFactory sharedInformerFactory() { |
| 67 | + return new MySharedInformerFactory(); |
| 68 | + } |
| 69 | + |
| 70 | + @Bean |
| 71 | + public NodePrintingReconciler nodePrintingReconciler( |
| 72 | + Lister<V1Pod> podLister, Lister<V1Node> nodeLister, SharedInformer<V1Node> nodeInformer) { |
| 73 | + return new NodePrintingReconciler(podLister, nodeLister, nodeInformer); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @KubernetesInformers({ // Defining what resources is the informer-factory actually watching. |
| 78 | + @KubernetesInformer( |
| 79 | + apiTypeClass = V1Node.class, |
| 80 | + apiListTypeClass = V1NodeList.class, |
| 81 | + groupVersionResource = |
| 82 | + @GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "nodes"), |
| 83 | + resyncPeriodMillis = 60 * 1000L |
| 84 | + ), |
| 85 | + @KubernetesInformer( |
| 86 | + apiTypeClass = V1Pod.class, |
| 87 | + apiListTypeClass = V1PodList.class, |
| 88 | + groupVersionResource = |
| 89 | + @GroupVersionResource(apiGroup = "", apiVersion = "v1", resourcePlural = "pods") |
| 90 | + ), |
| 91 | + }) |
| 92 | + public static class MySharedInformerFactory extends SharedInformerFactory {} |
| 93 | + |
| 94 | + // As long as a reconciler bean attached `@KubernetesReconciler` detected in the context, we will |
| 95 | + // be automatically creating a conresponding controller bean implementing {@link |
| 96 | + // io.kubernetes.client.extended.controller.Controller} |
| 97 | + // with the name specified and registering it to the spring bean-factory. |
| 98 | + @KubernetesReconciler( |
| 99 | + value = "node-printing-controller", |
| 100 | + watches = |
| 101 | + @KubernetesReconcilerWatches({ |
| 102 | + @KubernetesReconcilerWatch( |
| 103 | + apiTypeClass = V1Node.class, |
| 104 | + resyncPeriodMillis = 60 * 1000L // fully resync every 1 minute |
| 105 | + ), |
| 106 | + }) |
| 107 | + ) |
| 108 | + public static class NodePrintingReconciler implements Reconciler { |
| 109 | + |
| 110 | + public NodePrintingReconciler( |
| 111 | + Lister<V1Pod> podLister, Lister<V1Node> nodeLister, SharedInformer<V1Node> nodeInformer) { |
| 112 | + this.nodeLister = nodeLister; |
| 113 | + this.podLister = podLister; |
| 114 | + this.nodeInformer = nodeInformer; |
| 115 | + } |
| 116 | + |
| 117 | + private SharedInformer<V1Node> nodeInformer; |
| 118 | + |
| 119 | + private Lister<V1Node> nodeLister; |
| 120 | + |
| 121 | + private Lister<V1Pod> podLister; |
| 122 | + |
| 123 | + // *OPTIONAL* |
| 124 | + // If you feed like hold the controller from running util some condition.. |
| 125 | + @KubernetesReconcilerReadyFunc |
| 126 | + boolean informerReady() { |
| 127 | + return nodeInformer.hasSynced(); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public Result reconcile(Request request) { |
| 132 | + V1Node node = nodeLister.get(request.getName()); |
| 133 | + System.out.println("triggered reconciling " + node.getMetadata().getName()); |
| 134 | + return new Result(false); |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments