|
| 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.spring.extended.controller; |
| 14 | + |
| 15 | +import io.kubernetes.client.common.KubernetesObject; |
| 16 | +import io.kubernetes.client.informer.SharedIndexInformer; |
| 17 | +import io.kubernetes.client.informer.SharedInformer; |
| 18 | +import io.kubernetes.client.informer.SharedInformerFactory; |
| 19 | +import io.kubernetes.client.informer.cache.Lister; |
| 20 | +import io.kubernetes.client.openapi.ApiClient; |
| 21 | +import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer; |
| 22 | +import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers; |
| 23 | +import io.kubernetes.client.spring.extended.controller.config.KubernetesInformerProperties; |
| 24 | +import io.kubernetes.client.util.generic.GenericKubernetesApi; |
| 25 | +import org.slf4j.Logger; |
| 26 | +import org.slf4j.LoggerFactory; |
| 27 | +import org.springframework.beans.BeansException; |
| 28 | +import org.springframework.beans.factory.BeanFactory; |
| 29 | +import org.springframework.beans.factory.BeanFactoryAware; |
| 30 | +import org.springframework.beans.factory.annotation.Autowired; |
| 31 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 32 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 33 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 34 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
| 35 | +import org.springframework.beans.factory.support.RootBeanDefinition; |
| 36 | +import org.springframework.core.Ordered; |
| 37 | +import org.springframework.core.ResolvableType; |
| 38 | +import org.springframework.core.annotation.AnnotatedElementUtils; |
| 39 | + |
| 40 | +/** |
| 41 | + * The type Kubernetes informer factory processor which basically does the following things: |
| 42 | + * |
| 43 | + * <p>1. By-pass further processing if there's no SharedInformerFactory registered. 2. Instansiate a |
| 44 | + * new ApiClient if there's no user-specified one for override. 3. By reading from {@link |
| 45 | + * io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers}, instantiates and |
| 46 | + * 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} |
| 50 | + */ |
| 51 | +@Deprecated |
| 52 | +public class KubernetesInformerFactoryProcessor |
| 53 | + implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware, Ordered { |
| 54 | + |
| 55 | + private static final Logger log = |
| 56 | + LoggerFactory.getLogger(KubernetesInformerFactoryProcessor.class); |
| 57 | + |
| 58 | + public static final int ORDER = 0; |
| 59 | + |
| 60 | + @Autowired private KubernetesInformerProperties informerProperties; |
| 61 | + |
| 62 | + private ConfigurableListableBeanFactory beanFactory; |
| 63 | + |
| 64 | + @Override |
| 65 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) |
| 66 | + throws BeansException {} |
| 67 | + |
| 68 | + @Override |
| 69 | + public int getOrder() { |
| 70 | + return 0; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) |
| 75 | + throws BeansException { |
| 76 | + if (!(registry instanceof BeanFactory)) { |
| 77 | + return; |
| 78 | + } |
| 79 | + for (String name : registry.getBeanDefinitionNames()) { |
| 80 | + KubernetesInformers kubernetesInformers = null; |
| 81 | + Class<?> cls = ((BeanFactory) registry).getType(name); |
| 82 | + if (cls != null) { |
| 83 | + kubernetesInformers = |
| 84 | + AnnotatedElementUtils.getMergedAnnotation(cls, KubernetesInformers.class); |
| 85 | + } |
| 86 | + if (kubernetesInformers == null) { |
| 87 | + kubernetesInformers = beanFactory.findAnnotationOnBean(name, KubernetesInformers.class); |
| 88 | + } |
| 89 | + if (kubernetesInformers == null) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + if (kubernetesInformers.value().length > 0) { |
| 93 | + for (KubernetesInformer kubernetesInformer : kubernetesInformers.value()) { |
| 94 | + registerInformer(registry, kubernetesInformer); |
| 95 | + registerLister(registry, kubernetesInformer); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + private void registerInformer( |
| 102 | + BeanDefinitionRegistry registry, KubernetesInformer kubernetesInformer) { |
| 103 | + RootBeanDefinition informerBean = |
| 104 | + (RootBeanDefinition) |
| 105 | + BeanDefinitionBuilder.rootBeanDefinition(SharedInformer.class).getBeanDefinition(); |
| 106 | + informerBean.setInstanceSupplier( |
| 107 | + () -> informer(kubernetesInformer.apiTypeClass(), kubernetesInformer)); |
| 108 | + ResolvableType informerType = |
| 109 | + ResolvableType.forClassWithGenerics( |
| 110 | + SharedIndexInformer.class, kubernetesInformer.apiTypeClass()); |
| 111 | + informerBean.setTargetType(informerType); |
| 112 | + registry.registerBeanDefinition( |
| 113 | + getInformerBeanName(kubernetesInformer.apiTypeClass()), informerBean); |
| 114 | + } |
| 115 | + |
| 116 | + private String getInformerBeanName(Class<?> type) { |
| 117 | + return type.getName() + "Informer"; |
| 118 | + } |
| 119 | + |
| 120 | + private void registerLister( |
| 121 | + BeanDefinitionRegistry registry, KubernetesInformer kubernetesInformer) { |
| 122 | + RootBeanDefinition listerBean = |
| 123 | + (RootBeanDefinition) |
| 124 | + BeanDefinitionBuilder.rootBeanDefinition(Lister.class).getBeanDefinition(); |
| 125 | + listerBean.setInstanceSupplier(() -> lister(kubernetesInformer.apiTypeClass())); |
| 126 | + ResolvableType listerType = |
| 127 | + ResolvableType.forClassWithGenerics(Lister.class, kubernetesInformer.apiTypeClass()); |
| 128 | + listerBean.setTargetType(listerType); |
| 129 | + registry.registerBeanDefinition(listerType.toString(), listerBean); |
| 130 | + } |
| 131 | + |
| 132 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 133 | + private <T extends KubernetesObject> Lister<T> lister(Class<T> type) { |
| 134 | + SharedIndexInformer sharedInformer = |
| 135 | + this.beanFactory.getBean(getInformerBeanName(type), SharedIndexInformer.class); |
| 136 | + Lister<T> lister = new Lister<>(sharedInformer.getIndexer()); |
| 137 | + return lister; |
| 138 | + } |
| 139 | + |
| 140 | + @SuppressWarnings({"unchecked", "rawtypes"}) |
| 141 | + private <T extends KubernetesObject> SharedInformer<T> informer( |
| 142 | + Class<T> type, KubernetesInformer kubernetesInformer) { |
| 143 | + ApiClient apiClient = this.beanFactory.getBean(ApiClient.class); |
| 144 | + |
| 145 | + if (apiClient.getHttpClient().readTimeoutMillis() > 0) { |
| 146 | + log.warn( |
| 147 | + "Enforcing read-timeout of the ApiClient {} to {} so that the watch connection won't abort from client-side", |
| 148 | + apiClient, |
| 149 | + informerProperties.getClientReadTimeout()); |
| 150 | + apiClient.setHttpClient( |
| 151 | + apiClient |
| 152 | + .getHttpClient() |
| 153 | + .newBuilder() |
| 154 | + .readTimeout(informerProperties.getClientReadTimeout()) |
| 155 | + .build()); |
| 156 | + } |
| 157 | + |
| 158 | + SharedInformerFactory sharedInformerFactory = |
| 159 | + this.beanFactory.getBean(SharedInformerFactory.class); |
| 160 | + final GenericKubernetesApi api = |
| 161 | + new GenericKubernetesApi( |
| 162 | + kubernetesInformer.apiTypeClass(), |
| 163 | + kubernetesInformer.apiListTypeClass(), |
| 164 | + kubernetesInformer.groupVersionResource().apiGroup(), |
| 165 | + kubernetesInformer.groupVersionResource().apiVersion(), |
| 166 | + kubernetesInformer.groupVersionResource().resourcePlural(), |
| 167 | + apiClient); |
| 168 | + SharedIndexInformer<T> sharedIndexInformer = |
| 169 | + sharedInformerFactory.sharedIndexInformerFor( |
| 170 | + api, type, kubernetesInformer.resyncPeriodMillis(), kubernetesInformer.namespace()); |
| 171 | + return sharedIndexInformer; |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { |
| 176 | + this.beanFactory = (ConfigurableListableBeanFactory) beanFactory; |
| 177 | + } |
| 178 | +} |
0 commit comments