Skip to content

Chore: Support KubernetesInformers annotation on bean constructor #1603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer;
import io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformers;
import io.kubernetes.client.spring.extended.controller.config.KubernetesInformerProperties;
import io.kubernetes.client.spring.extended.controller.factory.KubernetesControllerFactory;
import io.kubernetes.client.util.generic.GenericKubernetesApi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
Expand All @@ -46,21 +46,20 @@
* injects informers to spring context with the underlying constructing process hidden from users.
*/
public class KubernetesInformerFactoryProcessor
implements BeanDefinitionRegistryPostProcessor, Ordered {
implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware, Ordered {

public static final int ORDER = 0;
private static final Logger log =
LoggerFactory.getLogger(KubernetesInformerFactoryProcessor.class);

private static final Logger log = LoggerFactory.getLogger(KubernetesControllerFactory.class);
public static final int ORDER = 0;

@Autowired private KubernetesInformerProperties informerProperties;

private ConfigurableListableBeanFactory beanFactory;

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
this.beanFactory = beanFactory;
}
throws BeansException {}

@Override
public int getOrder() {
Expand All @@ -74,15 +73,22 @@ public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry)
return;
}
for (String name : registry.getBeanDefinitionNames()) {
KubernetesInformers kubernetesInformers = null;
Class<?> cls = ((BeanFactory) registry).getType(name);
if (cls != null) {
KubernetesInformers kubernetesInformers =
kubernetesInformers =
AnnotatedElementUtils.getMergedAnnotation(cls, KubernetesInformers.class);
if (kubernetesInformers != null && kubernetesInformers.value().length > 0) {
for (KubernetesInformer kubernetesInformer : kubernetesInformers.value()) {
registerInformer(registry, kubernetesInformer);
registerLister(registry, kubernetesInformer);
}
}
if (kubernetesInformers == null) {
kubernetesInformers = beanFactory.findAnnotationOnBean(name, KubernetesInformers.class);
}
if (kubernetesInformers == null) {
continue;
}
if (kubernetesInformers.value().length > 0) {
for (KubernetesInformer kubernetesInformer : kubernetesInformers.value()) {
registerInformer(registry, kubernetesInformer);
registerLister(registry, kubernetesInformer);
}
}
}
Expand Down Expand Up @@ -160,4 +166,9 @@ private <T extends KubernetesObject> SharedInformer<T> informer(
api, type, kubernetesInformer.resyncPeriodMillis(), kubernetesInformer.namespace());
return sharedIndexInformer;
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* io.kubernetes.client.spring.extended.controller.annotation.KubernetesInformer} onto one bean
* method.
*/
@Target({ElementType.TYPE})
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface KubernetesInformers {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ public ApiClient testingApiClient() {
}

@Bean
public TestSharedInformerFactory testSharedInformerFactory() {
return new TestSharedInformerFactory();
}

@KubernetesInformers({
@KubernetesInformer(
apiTypeClass = V1Pod.class,
Expand All @@ -86,7 +82,9 @@ public TestSharedInformerFactory testSharedInformerFactory() {
apiVersion = "v1",
resourcePlural = "configmaps")),
})
static class TestSharedInformerFactory {}
public SharedInformerFactory testSharedInformerFactory() {
return new SharedInformerFactory();
}
}

@Autowired private SharedInformerFactory informerFactory;
Expand Down