|
| 1 | +/* |
| 2 | +Copyright 2022 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.aot; |
| 14 | + |
| 15 | +import com.google.gson.annotations.JsonAdapter; |
| 16 | +import io.kubernetes.client.extended.controller.Controller; |
| 17 | +import io.swagger.annotations.ApiModel; |
| 18 | +import java.lang.annotation.Annotation; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.Set; |
| 22 | +import org.jetbrains.annotations.NotNull; |
| 23 | +import org.reflections.Reflections; |
| 24 | +import org.slf4j.Logger; |
| 25 | +import org.slf4j.LoggerFactory; |
| 26 | +import org.springframework.aot.hint.MemberCategory; |
| 27 | +import org.springframework.aot.hint.RuntimeHints; |
| 28 | +import org.springframework.aot.hint.TypeReference; |
| 29 | +import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution; |
| 30 | +import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor; |
| 31 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 32 | +import org.springframework.boot.autoconfigure.AutoConfigurationPackages; |
| 33 | + |
| 34 | +/** |
| 35 | + * GraalVM native images need to know about when you might reflectively work with types at runtime. |
| 36 | + * The Kubernetes Java client works with several types reflectively at runtime. This code uses the |
| 37 | + * third-party Reflections library to reflect upon all the code in your Spring Boot application or |
| 38 | + * in the official Kubernetes Java client that has {@link ApiModel}, {@link JsonAdapter}, and |
| 39 | + * registers them. It also registers a few other specific handful of classes that should be |
| 40 | + * accounted for, in specific cases. |
| 41 | + * |
| 42 | + * @author Josh Long |
| 43 | + */ |
| 44 | +@SuppressWarnings("unused") |
| 45 | +public class KubernetesBeanFactoryInitializationAotProcessor |
| 46 | + implements BeanFactoryInitializationAotProcessor { |
| 47 | + |
| 48 | + private static final Logger LOGGER = |
| 49 | + LoggerFactory.getLogger(KubernetesBeanFactoryInitializationAotProcessor.class); |
| 50 | + |
| 51 | + private final MemberCategory[] allMemberCategories = MemberCategory.values(); |
| 52 | + |
| 53 | + @Override |
| 54 | + public BeanFactoryInitializationAotContribution processAheadOfTime( |
| 55 | + @NotNull ConfigurableListableBeanFactory beanFactory) { |
| 56 | + return (generationContext, beanFactoryInitializationCode) -> { |
| 57 | + RuntimeHints hints = generationContext.getRuntimeHints(); |
| 58 | + String[] classNames = |
| 59 | + new String[] { |
| 60 | + "com.google.gson.JsonElement", // |
| 61 | + "io.kubernetes.client.informer.cache.ProcessorListener", // |
| 62 | + "io.kubernetes.client.extended.controller.Controller", // |
| 63 | + "io.kubernetes.client.util.generic.GenericKubernetesApi$StatusPatch", // |
| 64 | + "io.kubernetes.client.util.Watch$Response" // |
| 65 | + }; |
| 66 | + for (String className : classNames) { |
| 67 | + LOGGER.info("registering " + className + " for reflection"); |
| 68 | + hints.reflection().registerType(TypeReference.of(className), allMemberCategories); |
| 69 | + } |
| 70 | + registerForPackage("io.kubernetes", hints); |
| 71 | + Collection<String> packages = AutoConfigurationPackages.get(beanFactory); |
| 72 | + for (String packageName : packages) { |
| 73 | + registerForPackage(packageName, hints); |
| 74 | + } |
| 75 | + }; |
| 76 | + } |
| 77 | + |
| 78 | + private void registerForPackage(String packageName, RuntimeHints hints) { |
| 79 | + Reflections reflections = new Reflections(packageName); |
| 80 | + Set<Class<?>> apiModels = reflections.getTypesAnnotatedWith(ApiModel.class); |
| 81 | + Set<Class<? extends Controller>> controllers = reflections.getSubTypesOf(Controller.class); |
| 82 | + Set<Class<?>> jsonAdapters = findJsonAdapters(reflections); |
| 83 | + Set<Class<?>> all = new HashSet<>(); |
| 84 | + all.addAll(jsonAdapters); |
| 85 | + all.addAll(controllers); |
| 86 | + all.addAll(apiModels); |
| 87 | + for (Class<?> clazz : all) { |
| 88 | + LOGGER.info("registering " + clazz.getName() + " for reflection"); |
| 89 | + hints.reflection().registerType(clazz, allMemberCategories); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private <R extends Annotation> Set<Class<?>> findJsonAdapters(Reflections reflections) { |
| 94 | + Class<JsonAdapter> jsonAdapterClass = JsonAdapter.class; |
| 95 | + Set<Class<?>> classes = new HashSet<>(); |
| 96 | + for (Class<?> clazz : reflections.getTypesAnnotatedWith(jsonAdapterClass)) { |
| 97 | + JsonAdapter annotation = clazz.getAnnotation(jsonAdapterClass); |
| 98 | + if (null != annotation) { |
| 99 | + classes.add(annotation.value()); |
| 100 | + } |
| 101 | + classes.add(clazz); |
| 102 | + } |
| 103 | + return classes; |
| 104 | + } |
| 105 | +} |
0 commit comments