|
| 1 | +/* |
| 2 | +Copyright 2021 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.manifests; |
| 14 | + |
| 15 | +import com.github.benmanes.caffeine.cache.CacheLoader; |
| 16 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 17 | +import com.github.benmanes.caffeine.cache.LoadingCache; |
| 18 | +import io.kubernetes.client.openapi.models.V1ConfigMap; |
| 19 | +import io.kubernetes.client.spring.extended.manifests.annotation.FromConfigMap; |
| 20 | +import io.kubernetes.client.spring.extended.manifests.config.KubernetesManifestsProperties; |
| 21 | +import io.kubernetes.client.spring.extended.manifests.configmaps.ConfigMapGetter; |
| 22 | +import java.lang.reflect.Field; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.concurrent.Executors; |
| 25 | +import java.util.concurrent.ScheduledExecutorService; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.function.Supplier; |
| 28 | +import org.checkerframework.checker.nullness.qual.NonNull; |
| 29 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 30 | +import org.slf4j.Logger; |
| 31 | +import org.slf4j.LoggerFactory; |
| 32 | +import org.springframework.beans.BeansException; |
| 33 | +import org.springframework.beans.factory.BeanCreationException; |
| 34 | +import org.springframework.beans.factory.NoSuchBeanDefinitionException; |
| 35 | +import org.springframework.beans.factory.annotation.Autowired; |
| 36 | +import org.springframework.beans.factory.config.BeanPostProcessor; |
| 37 | +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; |
| 38 | +import org.springframework.context.ApplicationContext; |
| 39 | +import org.springframework.context.ApplicationContextAware; |
| 40 | +import org.springframework.util.ReflectionUtils; |
| 41 | + |
| 42 | +public class KubernetesFromConfigMapProcessor |
| 43 | + implements InstantiationAwareBeanPostProcessor, BeanPostProcessor, ApplicationContextAware { |
| 44 | + |
| 45 | + private static final Logger log = LoggerFactory.getLogger(KubernetesFromConfigMapProcessor.class); |
| 46 | + |
| 47 | + private ApplicationContext applicationContext; |
| 48 | + |
| 49 | + private final ScheduledExecutorService configMapKeyRefresher = |
| 50 | + Executors.newSingleThreadScheduledExecutor(); |
| 51 | + |
| 52 | + @Autowired private KubernetesManifestsProperties manifestsProperties; |
| 53 | + |
| 54 | + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { |
| 55 | + |
| 56 | + for (Field field : bean.getClass().getDeclaredFields()) { |
| 57 | + ReflectionUtils.makeAccessible(field); |
| 58 | + try { |
| 59 | + if (field.get(bean) != null) { |
| 60 | + continue; // field already set, skip processing |
| 61 | + } |
| 62 | + } catch (IllegalAccessException e) { |
| 63 | + log.warn("Failed inject resource for @FromConfigMap annotated field {}", field, e); |
| 64 | + continue; |
| 65 | + } |
| 66 | + |
| 67 | + FromConfigMap fromConfigMapAnnotation = field.getAnnotation(FromConfigMap.class); |
| 68 | + if (fromConfigMapAnnotation == null) { |
| 69 | + continue; // skip if the field doesn't have the annotation |
| 70 | + } |
| 71 | + |
| 72 | + if (!Map.class.isAssignableFrom(field.getType())) { |
| 73 | + log.warn( |
| 74 | + "Failed inject resource for @FromConfigMap annotated field {}, the declaring type should be Map<String, String>", |
| 75 | + field); |
| 76 | + continue; |
| 77 | + } |
| 78 | + |
| 79 | + ConfigMapGetter configMapGetter = |
| 80 | + getOrCreateConfigMapGetter(fromConfigMapAnnotation, applicationContext); |
| 81 | + |
| 82 | + LoadingCache<String, String> configMapDataCache = |
| 83 | + Caffeine.newBuilder() |
| 84 | + .expireAfterWrite(manifestsProperties.getRefreshInterval()) |
| 85 | + .build( |
| 86 | + new ConfigMapGetterCacheLoader( |
| 87 | + () -> { |
| 88 | + return configMapGetter.get( |
| 89 | + fromConfigMapAnnotation.namespace(), fromConfigMapAnnotation.name()); |
| 90 | + })); |
| 91 | + fullyRefreshCache(configMapGetter, fromConfigMapAnnotation, configMapDataCache); |
| 92 | + configMapKeyRefresher.scheduleAtFixedRate( |
| 93 | + () -> { |
| 94 | + fullyRefreshCache(configMapGetter, fromConfigMapAnnotation, configMapDataCache); |
| 95 | + }, |
| 96 | + manifestsProperties.getRefreshInterval().getSeconds(), |
| 97 | + manifestsProperties.getRefreshInterval().getSeconds(), |
| 98 | + TimeUnit.SECONDS); |
| 99 | + ReflectionUtils.setField(field, bean, configMapDataCache.asMap()); |
| 100 | + } |
| 101 | + |
| 102 | + return bean; |
| 103 | + } |
| 104 | + |
| 105 | + private static void fullyRefreshCache( |
| 106 | + ConfigMapGetter configMapGetter, |
| 107 | + FromConfigMap fromConfigMapAnnotation, |
| 108 | + LoadingCache<String, String> configMapDataCache) { |
| 109 | + V1ConfigMap configMap = |
| 110 | + configMapGetter.get(fromConfigMapAnnotation.namespace(), fromConfigMapAnnotation.name()); |
| 111 | + if (configMap == null || configMap.getData() == null) { |
| 112 | + return; |
| 113 | + } |
| 114 | + // TODO: make the cache data refreshment atomic |
| 115 | + configMap.getData().keySet().stream().forEach(key -> configMapDataCache.refresh(key)); |
| 116 | + } |
| 117 | + |
| 118 | + private ConfigMapGetter getOrCreateConfigMapGetter( |
| 119 | + FromConfigMap fromConfigMapAnnotation, ApplicationContext applicationContext) { |
| 120 | + ConfigMapGetter configMapGetter; |
| 121 | + try { |
| 122 | + configMapGetter = |
| 123 | + applicationContext |
| 124 | + .getAutowireCapableBeanFactory() |
| 125 | + .getBean(fromConfigMapAnnotation.configMapGetter()); |
| 126 | + } catch (NoSuchBeanDefinitionException ne) { |
| 127 | + try { |
| 128 | + configMapGetter = fromConfigMapAnnotation.configMapGetter().newInstance(); |
| 129 | + } catch (IllegalAccessException | InstantiationException e) { |
| 130 | + throw new BeanCreationException("failed creating configmap getter instance", e); |
| 131 | + } |
| 132 | + applicationContext.getAutowireCapableBeanFactory().autowireBean(configMapGetter); |
| 133 | + applicationContext |
| 134 | + .getAutowireCapableBeanFactory() |
| 135 | + .initializeBean( |
| 136 | + configMapGetter, |
| 137 | + "configmap-getter-" + fromConfigMapAnnotation.configMapGetter().getSimpleName()); |
| 138 | + } |
| 139 | + return configMapGetter; |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 144 | + this.applicationContext = applicationContext; |
| 145 | + } |
| 146 | + |
| 147 | + static class ConfigMapGetterCacheLoader implements CacheLoader<String, String> { |
| 148 | + |
| 149 | + ConfigMapGetterCacheLoader(Supplier<V1ConfigMap> configMapSupplier) { |
| 150 | + this.configMapSupplier = configMapSupplier; |
| 151 | + } |
| 152 | + |
| 153 | + private final Supplier<V1ConfigMap> configMapSupplier; |
| 154 | + |
| 155 | + @Override |
| 156 | + public @Nullable String load(@NonNull String key) throws Exception { |
| 157 | + V1ConfigMap configMap = this.configMapSupplier.get(); |
| 158 | + if (configMap == null || configMap.getData() == null) { |
| 159 | + return null; |
| 160 | + } |
| 161 | + return configMap.getData().get(key); |
| 162 | + } |
| 163 | + } |
| 164 | +} |
0 commit comments