|
5 | 5 | import org.slf4j.Logger;
|
6 | 6 | import org.slf4j.LoggerFactory;
|
7 | 7 |
|
8 |
| -import io.fabric8.kubernetes.api.model.GenericKubernetesResource; |
9 | 8 | import io.fabric8.kubernetes.api.model.HasMetadata;
|
10 | 9 | import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
|
11 | 10 | import io.javaoperatorsdk.operator.api.config.Utils;
|
12 | 11 | import io.javaoperatorsdk.operator.api.config.dependent.ConfigurationConverter;
|
13 | 12 | import io.javaoperatorsdk.operator.api.config.dependent.DependentResourceSpec;
|
14 | 13 | import io.javaoperatorsdk.operator.api.config.informer.InformerConfiguration;
|
15 | 14 | import io.javaoperatorsdk.operator.api.reconciler.Constants;
|
16 |
| -import io.javaoperatorsdk.operator.api.reconciler.dependent.DependentResource; |
17 | 15 | import io.javaoperatorsdk.operator.processing.GroupVersionKind;
|
18 |
| -import io.javaoperatorsdk.operator.processing.event.source.PrimaryToSecondaryMapper; |
19 |
| -import io.javaoperatorsdk.operator.processing.event.source.SecondaryToPrimaryMapper; |
20 | 16 | import io.javaoperatorsdk.operator.processing.event.source.filter.GenericFilter;
|
21 | 17 | import io.javaoperatorsdk.operator.processing.event.source.filter.OnAddFilter;
|
22 | 18 | import io.javaoperatorsdk.operator.processing.event.source.filter.OnDeleteFilter;
|
23 | 19 | import io.javaoperatorsdk.operator.processing.event.source.filter.OnUpdateFilter;
|
24 | 20 |
|
25 |
| -import static io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResource.getSecondaryToPrimaryMapper; |
26 | 21 | import static io.javaoperatorsdk.operator.processing.dependent.kubernetes.KubernetesDependentResourceConfig.DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA;
|
27 | 22 |
|
28 | 23 | public class KubernetesDependentConverter<R extends HasMetadata, P extends HasMetadata> implements
|
@@ -54,98 +49,75 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
|
54 | 49 | }
|
55 | 50 |
|
56 | 51 | @SuppressWarnings({"unchecked", "rawtypes"})
|
57 |
| - private InformerConfiguration<R> createInformerConfig(KubernetesDependent configAnnotation, |
| 52 | + private InformerConfiguration.InformerConfigurationBuilder<R> createInformerConfig( |
| 53 | + KubernetesDependent configAnnotation, |
58 | 54 | DependentResourceSpec<R, P, KubernetesDependentResourceConfig<R>> spec,
|
59 | 55 | ControllerConfiguration<? extends HasMetadata> controllerConfig) {
|
60 | 56 | Class<? extends KubernetesDependentResource<?, ?>> dependentResourceClass =
|
61 | 57 | (Class<? extends KubernetesDependentResource<?, ?>>) spec.getDependentResourceClass();
|
62 |
| - final var resourceType = spec.getResourceClass(); |
63 | 58 |
|
64 |
| - InformerConfiguration.InformerConfigurationBuilder informerConfig; |
| 59 | + InformerConfiguration.InformerConfigurationBuilder informerBuilder; |
65 | 60 | if (configAnnotation != null && configAnnotation.informerConfig() != null &&
|
66 | 61 | !Constants.NO_VALUE_SET.equals(configAnnotation.informerConfig().groupVersionKind())) {
|
67 | 62 |
|
68 |
| - if (!GenericKubernetesResource.class.isAssignableFrom(resourceType)) { |
69 |
| - throw new IllegalStateException( |
70 |
| - "If GroupVersionKind is set the resource type must be GenericKubernetesDependentResource for: " |
71 |
| - + dependentResourceClass.getName()); |
72 |
| - } |
73 |
| - |
74 |
| - informerConfig = InformerConfiguration.from( |
| 63 | + informerBuilder = InformerConfiguration.from( |
75 | 64 | GroupVersionKind.fromString(configAnnotation.informerConfig().groupVersionKind()),
|
76 | 65 | controllerConfig.getResourceClass());
|
77 | 66 | } else {
|
78 |
| - informerConfig = |
79 |
| - InformerConfiguration.from(resourceType, controllerConfig.getResourceClass()); |
| 67 | + informerBuilder = new InternalInformerConfigBuilder(controllerConfig.getResourceClass()); |
80 | 68 | }
|
81 | 69 |
|
82 |
| - // default name should be set even if there's no explicit informer configuration |
83 |
| - informerConfig.withName(DependentResource.defaultNameFor(dependentResourceClass)); |
84 |
| - |
85 | 70 | if (configAnnotation != null && configAnnotation.informerConfig() != null) {
|
86 | 71 | // override default name if more specific one is provided
|
87 | 72 | if (!Constants.NO_VALUE_SET.equals(configAnnotation.informerConfig().name())) {
|
88 |
| - informerConfig.withName(configAnnotation.informerConfig().name()); |
| 73 | + informerBuilder.withName(configAnnotation.informerConfig().name()); |
89 | 74 | } else if (spec.getName() != null && !Constants.NO_VALUE_SET.equals(spec.getName())) {
|
90 |
| - informerConfig.withName(spec.getName()); |
| 75 | + informerBuilder.withName(spec.getName()); |
91 | 76 | }
|
92 | 77 |
|
93 | 78 | var namespaces = Set.of(configAnnotation.informerConfig().namespaces());
|
94 |
| - informerConfig.withNamespaces(namespaces); |
| 79 | + informerBuilder.withNamespaces(namespaces); |
95 | 80 |
|
96 | 81 | final var fromAnnotation = configAnnotation.informerConfig().labelSelector();
|
97 | 82 | var labelSelector = Constants.NO_VALUE_SET.equals(fromAnnotation) ? null : fromAnnotation;
|
98 |
| - informerConfig.withLabelSelector(labelSelector); |
| 83 | + informerBuilder.withLabelSelector(labelSelector); |
99 | 84 |
|
100 | 85 | final var context = Utils.contextFor(controllerConfig, dependentResourceClass,
|
101 | 86 | configAnnotation.annotationType());
|
102 | 87 |
|
103 | 88 | var onAddFilter = Utils.instantiate(configAnnotation.informerConfig().onAddFilter(),
|
104 | 89 | OnAddFilter.class, context);
|
105 |
| - informerConfig.withOnAddFilter((OnAddFilter<? super R>) onAddFilter); |
| 90 | + informerBuilder.withOnAddFilter((OnAddFilter<? super R>) onAddFilter); |
106 | 91 |
|
107 | 92 | var onUpdateFilter =
|
108 | 93 | Utils.instantiate(configAnnotation.informerConfig().onUpdateFilter(),
|
109 | 94 | OnUpdateFilter.class, context);
|
110 |
| - informerConfig.withOnUpdateFilter((OnUpdateFilter<? super R>) onUpdateFilter); |
| 95 | + informerBuilder.withOnUpdateFilter((OnUpdateFilter<? super R>) onUpdateFilter); |
111 | 96 |
|
112 | 97 | var onDeleteFilter =
|
113 | 98 | Utils.instantiate(configAnnotation.informerConfig().onDeleteFilter(),
|
114 | 99 | OnDeleteFilter.class, context);
|
115 |
| - informerConfig.withOnDeleteFilter((OnDeleteFilter<? super R>) onDeleteFilter); |
| 100 | + informerBuilder.withOnDeleteFilter((OnDeleteFilter<? super R>) onDeleteFilter); |
116 | 101 |
|
117 | 102 | var genericFilter =
|
118 | 103 | Utils.instantiate(configAnnotation.informerConfig().genericFilter(),
|
119 | 104 | GenericFilter.class,
|
120 | 105 | context);
|
121 | 106 |
|
122 |
| - informerConfig.withGenericFilter((GenericFilter<? super R>) genericFilter); |
| 107 | + informerBuilder.withGenericFilter((GenericFilter<? super R>) genericFilter); |
123 | 108 |
|
124 |
| - informerConfig.followControllerNamespacesOnChange( |
| 109 | + informerBuilder.followControllerNamespacesOnChange( |
125 | 110 | configAnnotation.informerConfig().followControllerNamespacesOnChange());
|
126 | 111 |
|
127 |
| - var primaryToSecondaryMapper = |
128 |
| - Utils.instantiate(configAnnotation.informerConfig().primaryToSecondaryMapper(), |
129 |
| - PrimaryToSecondaryMapper.class, context); |
130 |
| - informerConfig.withPrimaryToSecondaryMapper(primaryToSecondaryMapper); |
| 112 | + } |
| 113 | + return informerBuilder; |
| 114 | + } |
131 | 115 |
|
132 |
| - var secondaryToPrimaryMapper = |
133 |
| - Utils.instantiate(configAnnotation.informerConfig().secondaryToPrimaryMapper(), |
134 |
| - SecondaryToPrimaryMapper.class, context); |
135 |
| - if (secondaryToPrimaryMapper != null) { |
136 |
| - informerConfig.withSecondaryToPrimaryMapper(secondaryToPrimaryMapper); |
137 |
| - } |
138 |
| - } else { |
139 |
| - getSecondaryToPrimaryMapper(dependentResourceClass, |
140 |
| - controllerConfig.getResourceClass()) |
141 |
| - .ifPresentOrElse(informerConfig::withSecondaryToPrimaryMapper, () -> { |
142 |
| - if (spec.getUseEventSourceWithName().isEmpty()) { |
143 |
| - log.warn("No SecondaryToPrimaryMapper set for dependent resource " |
144 |
| - + dependentResourceClass.getSimpleName() + |
145 |
| - ". This might be an issue with the setup of the dependent resource"); |
146 |
| - } |
147 |
| - }); |
| 116 | + private class InternalInformerConfigBuilder |
| 117 | + extends InformerConfiguration.InformerConfigurationBuilder<R> { |
| 118 | + public InternalInformerConfigBuilder(Class<? extends HasMetadata> primaryResourceClass) { |
| 119 | + this.primaryResourceClass = primaryResourceClass; |
148 | 120 | }
|
149 |
| - return informerConfig.build(); |
150 | 121 | }
|
| 122 | + |
151 | 123 | }
|
0 commit comments