11
11
import io .javaoperatorsdk .operator .api .config .dependent .DependentResourceSpec ;
12
12
import io .javaoperatorsdk .operator .api .config .informer .InformerConfiguration ;
13
13
import io .javaoperatorsdk .operator .api .reconciler .Constants ;
14
+ import io .javaoperatorsdk .operator .api .reconciler .dependent .DependentResource ;
14
15
import io .javaoperatorsdk .operator .processing .GroupVersionKind ;
15
16
import io .javaoperatorsdk .operator .processing .event .source .PrimaryToSecondaryMapper ;
16
17
import io .javaoperatorsdk .operator .processing .event .source .SecondaryToPrimaryMapper ;
19
20
import io .javaoperatorsdk .operator .processing .event .source .filter .OnDeleteFilter ;
20
21
import io .javaoperatorsdk .operator .processing .event .source .filter .OnUpdateFilter ;
21
22
23
+ import static io .javaoperatorsdk .operator .processing .dependent .kubernetes .KubernetesDependentResource .getSecondaryToPrimaryMapper ;
22
24
import static io .javaoperatorsdk .operator .processing .dependent .kubernetes .KubernetesDependentResourceConfig .DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA ;
23
25
24
26
public class KubernetesDependentConverter <R extends HasMetadata , P extends HasMetadata > implements
@@ -55,18 +57,17 @@ private InformerConfiguration<R> createInformerConfiguration(KubernetesDependent
55
57
ControllerConfiguration <?> controllerConfig ,
56
58
DependentResourceSpec <?, ?> spec ) {
57
59
try {
58
- Class <? extends KubernetesDependentResource <?, ?>> originatingClass =
60
+ Class <? extends KubernetesDependentResource <?, ?>> dependentResourceClass =
59
61
(Class <? extends KubernetesDependentResource <?, ?>>) spec .getDependentResourceClass ();
60
- var dependentInstance = originatingClass .getConstructor ().newInstance ();
61
- var resourceType = dependentInstance .resourceType ();
62
+ var resourceType = dependentResourceClass .getConstructor ().newInstance ().resourceType ();
62
63
63
64
InformerConfiguration .InformerConfigurationBuilder informerConfig ;
64
65
if (configAnnotation != null && configAnnotation .informerConfig () != null &&
65
66
!Constants .NO_VALUE_SET .equals (configAnnotation .informerConfig ().groupVersionKind ())) {
66
67
67
68
if (!GenericKubernetesResource .class .isAssignableFrom (resourceType )) {
68
69
throw new IllegalStateException ("If GroupVersionKind is set the resource type must be " +
69
- "GenericKubernetesDependentResource. For: " + originatingClass .getName ());
70
+ "GenericKubernetesDependentResource. For: " + dependentResourceClass .getName ());
70
71
}
71
72
72
73
informerConfig = InformerConfiguration .from (
@@ -77,17 +78,14 @@ private InformerConfiguration<R> createInformerConfiguration(KubernetesDependent
77
78
InformerConfiguration .from (resourceType , controllerConfig .getResourceClass ());
78
79
}
79
80
80
- informerConfig .withSecondaryToPrimaryMapper (
81
- dependentInstance .getSecondaryToPrimaryMapper ().orElse (null ));
82
-
83
81
if (configAnnotation != null && configAnnotation .informerConfig () != null ) {
84
82
85
83
if (!Constants .NO_VALUE_SET .equals (configAnnotation .informerConfig ().name ())) {
86
- informerConfig .withName (dependentInstance .name ());
84
+ informerConfig .withName (configAnnotation . informerConfig () .name ());
87
85
} else if (spec .getName () != null && !Constants .NO_VALUE_SET .equals (spec .getName ())) {
88
86
informerConfig .withName (spec .getName ());
89
87
} else {
90
- informerConfig .withName (dependentInstance . name ( ));
88
+ informerConfig .withName (DependentResource . defaultNameFor ( dependentResourceClass ));
91
89
}
92
90
93
91
var namespaces = Set .of (configAnnotation .informerConfig ().namespaces ());
@@ -97,7 +95,7 @@ private InformerConfiguration<R> createInformerConfiguration(KubernetesDependent
97
95
var labelSelector = Constants .NO_VALUE_SET .equals (fromAnnotation ) ? null : fromAnnotation ;
98
96
informerConfig .withLabelSelector (labelSelector );
99
97
100
- final var context = Utils .contextFor (controllerConfig , originatingClass ,
98
+ final var context = Utils .contextFor (controllerConfig , dependentResourceClass ,
101
99
configAnnotation .annotationType ());
102
100
103
101
var onAddFilter = Utils .instantiate (configAnnotation .informerConfig ().onAddFilter (),
@@ -135,15 +133,24 @@ private InformerConfiguration<R> createInformerConfiguration(KubernetesDependent
135
133
if (secondaryToPrimaryMapper != null ) {
136
134
informerConfig .withSecondaryToPrimaryMapper (secondaryToPrimaryMapper );
137
135
}
138
-
136
+ } else {
137
+ var mapper = getSecondaryToPrimaryMapper (dependentResourceClass ,
138
+ controllerConfig .getResourceClass ()).orElseThrow (
139
+ () -> new IllegalStateException (
140
+ "No suitable SecondaryToPrimaryMapper for informer of dependent resource: "
141
+ + dependentResourceClass .getName ()));
142
+ informerConfig .withSecondaryToPrimaryMapper (mapper );
139
143
}
140
144
145
+
146
+
141
147
return informerConfig .build ();
142
148
} catch (InstantiationException | IllegalAccessException | InvocationTargetException
143
149
| NoSuchMethodException e ) {
144
150
throw new RuntimeException (e );
145
151
}
146
-
147
152
}
148
153
154
+
155
+
149
156
}
0 commit comments