From fc5670986f151c242071eb6d1c1d4bc549d7b6f6 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 1 Feb 2021 15:23:41 +0000 Subject: [PATCH] Remove annotation and related post processor Signed-off-by: Dave Syer --- ...ernetesEndpointsLoadBalancerProcessor.java | 89 ------------------- .../KubernetesEndpointsLoadBalanced.java | 35 -------- ...tionalOnKubernetesLoadBalancerEnabled.java | 28 ------ ...bernetesLoadBalancerAutoConfiguration.java | 29 ------ .../endpoints/PollingEndpointsGetter.java | 11 ++- .../main/resources/META-INF/spring.factories | 1 - .../KubernetesInformerCreatorTest.java | 25 +++--- .../extended/network/TestApplication.java | 5 -- 8 files changed, 21 insertions(+), 202 deletions(-) delete mode 100644 spring/src/main/java/io/kubernetes/client/spring/extended/network/KubernetesEndpointsLoadBalancerProcessor.java delete mode 100644 spring/src/main/java/io/kubernetes/client/spring/extended/network/annotation/KubernetesEndpointsLoadBalanced.java delete mode 100644 spring/src/main/java/io/kubernetes/client/spring/extended/network/config/ConditionalOnKubernetesLoadBalancerEnabled.java delete mode 100644 spring/src/main/java/io/kubernetes/client/spring/extended/network/config/KubernetesLoadBalancerAutoConfiguration.java diff --git a/spring/src/main/java/io/kubernetes/client/spring/extended/network/KubernetesEndpointsLoadBalancerProcessor.java b/spring/src/main/java/io/kubernetes/client/spring/extended/network/KubernetesEndpointsLoadBalancerProcessor.java deleted file mode 100644 index 9e64cf7fec..0000000000 --- a/spring/src/main/java/io/kubernetes/client/spring/extended/network/KubernetesEndpointsLoadBalancerProcessor.java +++ /dev/null @@ -1,89 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at -http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -package io.kubernetes.client.spring.extended.network; - -import io.kubernetes.client.extended.network.EndpointsLoadBalancer; -import io.kubernetes.client.extended.network.LoadBalanceStrategy; -import io.kubernetes.client.spring.extended.network.annotation.KubernetesEndpointsLoadBalanced; -import io.kubernetes.client.spring.extended.network.endpoints.EndpointsGetter; -import java.lang.reflect.Field; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.BeanCreationException; -import org.springframework.beans.factory.NoSuchBeanDefinitionException; -import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.util.ReflectionUtils; - -public class KubernetesEndpointsLoadBalancerProcessor - implements InstantiationAwareBeanPostProcessor, ApplicationContextAware { - - private ApplicationContext applicationContext; - - @Override - public Object postProcessBeforeInitialization(Object bean, String beanName) - throws BeansException { - Class beanClass = bean.getClass(); - for (Field field : beanClass.getDeclaredFields()) { - if (!field.isAnnotationPresent(KubernetesEndpointsLoadBalanced.class)) { - continue; - } - - KubernetesEndpointsLoadBalanced loadBalanced = - field.getAnnotation(KubernetesEndpointsLoadBalanced.class); - - EndpointsGetter epGetter; - try { - epGetter = - applicationContext - .getAutowireCapableBeanFactory() - .getBean(loadBalanced.endpointsGetter()); - } catch (NoSuchBeanDefinitionException ne) { - try { - epGetter = loadBalanced.endpointsGetter().newInstance(); - } catch (IllegalAccessException | InstantiationException e) { - throw new BeanCreationException("failed creating endpoint getter instance", e); - } - applicationContext.getAutowireCapableBeanFactory().autowireBean(epGetter); - applicationContext - .getAutowireCapableBeanFactory() - .initializeBean( - epGetter, "endpoints-getter-" + loadBalanced.endpointsGetter().getSimpleName()); - } - - LoadBalanceStrategy strategy; - try { - strategy = loadBalanced.strategy().newInstance(); - } catch (IllegalAccessException | InstantiationException e) { - throw new BeanCreationException( - "failed creating endpoint load-balance strategy instance", e); - } - - EndpointsGetter finalEpGetter = epGetter; - EndpointsLoadBalancer loadBalancer = - new EndpointsLoadBalancer( - () -> { - return finalEpGetter.get(loadBalanced.namespace(), loadBalanced.name()); - }, - strategy); - ReflectionUtils.makeAccessible(field); - ReflectionUtils.setField(field, bean, loadBalancer); - } - return bean; - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; - } -} diff --git a/spring/src/main/java/io/kubernetes/client/spring/extended/network/annotation/KubernetesEndpointsLoadBalanced.java b/spring/src/main/java/io/kubernetes/client/spring/extended/network/annotation/KubernetesEndpointsLoadBalanced.java deleted file mode 100644 index 1cc0e84682..0000000000 --- a/spring/src/main/java/io/kubernetes/client/spring/extended/network/annotation/KubernetesEndpointsLoadBalanced.java +++ /dev/null @@ -1,35 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at -http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -package io.kubernetes.client.spring.extended.network.annotation; - -import io.kubernetes.client.extended.network.LoadBalanceStrategy; -import io.kubernetes.client.extended.network.RoundRobinLoadBalanceStrategy; -import io.kubernetes.client.spring.extended.network.endpoints.EndpointsGetter; -import io.kubernetes.client.spring.extended.network.endpoints.InformerEndpointsGetter; -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target({ElementType.FIELD}) -@Retention(RetentionPolicy.RUNTIME) -public @interface KubernetesEndpointsLoadBalanced { - - String namespace(); - - String name(); - - Class strategy() default RoundRobinLoadBalanceStrategy.class; - - Class endpointsGetter() default InformerEndpointsGetter.class; -} diff --git a/spring/src/main/java/io/kubernetes/client/spring/extended/network/config/ConditionalOnKubernetesLoadBalancerEnabled.java b/spring/src/main/java/io/kubernetes/client/spring/extended/network/config/ConditionalOnKubernetesLoadBalancerEnabled.java deleted file mode 100644 index 8f676b3e9b..0000000000 --- a/spring/src/main/java/io/kubernetes/client/spring/extended/network/config/ConditionalOnKubernetesLoadBalancerEnabled.java +++ /dev/null @@ -1,28 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at -http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -package io.kubernetes.client.spring.extended.network.config; - -import java.lang.annotation.Documented; -import java.lang.annotation.ElementType; -import java.lang.annotation.Inherited; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; -import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; - -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@Inherited -@ConditionalOnProperty(value = "kubernetes.loadbalancer.enabled", matchIfMissing = true) -public @interface ConditionalOnKubernetesLoadBalancerEnabled {} diff --git a/spring/src/main/java/io/kubernetes/client/spring/extended/network/config/KubernetesLoadBalancerAutoConfiguration.java b/spring/src/main/java/io/kubernetes/client/spring/extended/network/config/KubernetesLoadBalancerAutoConfiguration.java deleted file mode 100644 index e508d0e2cb..0000000000 --- a/spring/src/main/java/io/kubernetes/client/spring/extended/network/config/KubernetesLoadBalancerAutoConfiguration.java +++ /dev/null @@ -1,29 +0,0 @@ -/* -Copyright 2020 The Kubernetes Authors. -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at -http://www.apache.org/licenses/LICENSE-2.0 -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -*/ -package io.kubernetes.client.spring.extended.network.config; - -import io.kubernetes.client.spring.extended.network.KubernetesEndpointsLoadBalancerProcessor; -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -@Configuration(proxyBeanMethods = false) -@ConditionalOnKubernetesLoadBalancerEnabled -public class KubernetesLoadBalancerAutoConfiguration { - - @Bean - @ConditionalOnMissingBean - public KubernetesEndpointsLoadBalancerProcessor kubernetesEndpointsLoadBalancerProcessor() { - return new KubernetesEndpointsLoadBalancerProcessor(); - } -} diff --git a/spring/src/main/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetter.java b/spring/src/main/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetter.java index 03e3d07063..731805ba01 100644 --- a/spring/src/main/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetter.java +++ b/spring/src/main/java/io/kubernetes/client/spring/extended/network/endpoints/PollingEndpointsGetter.java @@ -12,22 +12,27 @@ */ package io.kubernetes.client.spring.extended.network.endpoints; +import java.time.Duration; + import com.github.benmanes.caffeine.cache.Cache; import com.github.benmanes.caffeine.cache.Caffeine; + import io.kubernetes.client.apimachinery.NamespaceName; import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.ApiException; import io.kubernetes.client.openapi.apis.CoreV1Api; import io.kubernetes.client.openapi.models.V1Endpoints; -import java.time.Duration; -import org.springframework.beans.factory.annotation.Autowired; public class PollingEndpointsGetter implements EndpointsGetter { private static final Cache lastObservedEndpoints = Caffeine.newBuilder().expireAfterWrite(Duration.ofMinutes(5)).build(); - @Autowired private ApiClient apiClient; + private final ApiClient apiClient; + + public PollingEndpointsGetter(ApiClient apiClient) { + this.apiClient = apiClient; + } @Override public V1Endpoints get(String namespace, String name) { diff --git a/spring/src/main/resources/META-INF/spring.factories b/spring/src/main/resources/META-INF/spring.factories index aaa853e42c..d89f340072 100644 --- a/spring/src/main/resources/META-INF/spring.factories +++ b/spring/src/main/resources/META-INF/spring.factories @@ -1,5 +1,4 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ io.kubernetes.client.spring.extended.controller.config.KubernetesInformerAutoConfiguration, \ io.kubernetes.client.spring.extended.controller.config.KubernetesReconcilerAutoConfiguration, \ -io.kubernetes.client.spring.extended.network.config.KubernetesLoadBalancerAutoConfiguration, \ io.kubernetes.client.spring.extended.manifests.config.KubernetesManifestsAutoConfiguration diff --git a/spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesInformerCreatorTest.java b/spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesInformerCreatorTest.java index f5589da5fa..ff64279e78 100644 --- a/spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesInformerCreatorTest.java +++ b/spring/src/test/java/io/kubernetes/client/spring/extended/controller/KubernetesInformerCreatorTest.java @@ -22,8 +22,21 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import java.util.Arrays; + import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.google.gson.Gson; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.annotation.Bean; +import org.springframework.test.context.junit4.SpringRunner; + import io.kubernetes.client.informer.SharedInformer; import io.kubernetes.client.informer.SharedInformerFactory; import io.kubernetes.client.informer.cache.Lister; @@ -38,16 +51,6 @@ import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer; import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers; import io.kubernetes.client.util.ClientBuilder; -import java.util.Arrays; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.context.annotation.Bean; -import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest(classes = {KubernetesInformerCreatorTest.App.class}) @@ -99,8 +102,6 @@ static class TestSharedInformerFactory extends SharedInformerFactory {} @Autowired private Lister configMapLister; - @Autowired private ApiClient apiClient; - @Test public void testInformerInjection() throws InterruptedException { assertNotNull(podInformer); diff --git a/spring/src/test/java/io/kubernetes/client/spring/extended/network/TestApplication.java b/spring/src/test/java/io/kubernetes/client/spring/extended/network/TestApplication.java index 1fae0b6bdb..229de37360 100644 --- a/spring/src/test/java/io/kubernetes/client/spring/extended/network/TestApplication.java +++ b/spring/src/test/java/io/kubernetes/client/spring/extended/network/TestApplication.java @@ -12,12 +12,7 @@ */ package io.kubernetes.client.spring.extended.network; -import io.kubernetes.client.spring.extended.network.config.KubernetesLoadBalancerAutoConfiguration; import org.springframework.boot.SpringBootConfiguration; -import org.springframework.boot.autoconfigure.ImportAutoConfiguration; @SpringBootConfiguration -@ImportAutoConfiguration({ - KubernetesLoadBalancerAutoConfiguration.class, -}) public class TestApplication {}