1
1
package io .javaoperatorsdk .operator .processing .dependent .kubernetes ;
2
2
3
- import java .lang .reflect .InvocationTargetException ;
4
3
import java .util .Set ;
5
4
6
5
import org .slf4j .Logger ;
@@ -35,7 +34,7 @@ public class KubernetesDependentConverter<R extends HasMetadata, P extends HasMe
35
34
@ SuppressWarnings ({"unchecked" , "rawtypes" })
36
35
public KubernetesDependentResourceConfig <R > configFrom (KubernetesDependent configAnnotation ,
37
36
ControllerConfiguration <?> controllerConfig ,
38
- DependentResourceSpec <?, ?> spec ) {
37
+ DependentResourceSpec <?, ?> spec , DependentResource <?, ?> dependentResource ) {
39
38
40
39
var createResourceOnlyIfNotExistingWithSSA =
41
40
DEFAULT_CREATE_RESOURCE_ONLY_IF_NOT_EXISTING_WITH_SSA ;
@@ -50,7 +49,8 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
50
49
51
50
var informerConfiguration = createInformerConfiguration (configAnnotation ,
52
51
controllerConfig ,
53
- spec );
52
+ (DependentResourceSpec <R , P >) spec ,
53
+ (DependentResource <R , P >) dependentResource );
54
54
55
55
return new KubernetesDependentResourceConfig (useSSA , createResourceOnlyIfNotExistingWithSSA ,
56
56
informerConfiguration );
@@ -60,105 +60,93 @@ public KubernetesDependentResourceConfig<R> configFrom(KubernetesDependent confi
60
60
@ SuppressWarnings ({"unchecked" , "rawtypes" })
61
61
private InformerConfiguration <R > createInformerConfiguration (KubernetesDependent configAnnotation ,
62
62
ControllerConfiguration <?> controllerConfig ,
63
- DependentResourceSpec <?, ?> spec ) {
64
- try {
65
- Class <? extends KubernetesDependentResource <?, ?>> dependentResourceClass =
66
- (Class <? extends KubernetesDependentResource <?, ?>>) spec .getDependentResourceClass ();
67
- // this is quite ugly but getting the resource type would be quite cumbersome
68
- var resourceType = dependentResourceClass .getConstructor ().newInstance ().resourceType ();
69
-
70
- InformerConfiguration .InformerConfigurationBuilder informerConfig ;
71
- if (configAnnotation != null && configAnnotation .informerConfig () != null &&
72
- !Constants .NO_VALUE_SET .equals (configAnnotation .informerConfig ().groupVersionKind ())) {
73
-
74
- if (!GenericKubernetesResource .class .isAssignableFrom (resourceType )) {
75
- throw new IllegalStateException ("If GroupVersionKind is set the resource type must be " +
76
- "GenericKubernetesDependentResource. For: " + dependentResourceClass .getName ());
77
- }
78
-
79
- informerConfig = InformerConfiguration .from (
80
- GroupVersionKind .fromString (configAnnotation .informerConfig ().groupVersionKind ()),
81
- controllerConfig .getResourceClass ());
82
- } else {
83
- informerConfig =
84
- InformerConfiguration .from (resourceType , controllerConfig .getResourceClass ());
63
+ DependentResourceSpec <R , P > spec , DependentResource <R , P > dependentResource ) {
64
+ Class <? extends KubernetesDependentResource <?, ?>> dependentResourceClass =
65
+ (Class <? extends KubernetesDependentResource <?, ?>>) spec .getDependentResourceClass ();
66
+ var resourceType = dependentResource .resourceType ();
67
+
68
+ InformerConfiguration .InformerConfigurationBuilder informerConfig ;
69
+ if (configAnnotation != null && configAnnotation .informerConfig () != null &&
70
+ !Constants .NO_VALUE_SET .equals (configAnnotation .informerConfig ().groupVersionKind ())) {
71
+
72
+ if (!GenericKubernetesResource .class .isAssignableFrom (resourceType )) {
73
+ throw new IllegalStateException ("If GroupVersionKind is set the resource type must be " +
74
+ "GenericKubernetesDependentResource. For: " + dependentResourceClass .getName ());
85
75
}
86
76
87
- if (configAnnotation != null && configAnnotation .informerConfig () != null ) {
88
-
89
- if (!Constants .NO_VALUE_SET .equals (configAnnotation .informerConfig ().name ())) {
90
- informerConfig .withName (configAnnotation .informerConfig ().name ());
91
- } else if (spec .getName () != null && !Constants .NO_VALUE_SET .equals (spec .getName ())) {
92
- informerConfig .withName (spec .getName ());
93
- } else {
94
- informerConfig .withName (DependentResource .defaultNameFor (dependentResourceClass ));
95
- }
96
-
97
- var namespaces = Set .of (configAnnotation .informerConfig ().namespaces ());
98
- informerConfig .withNamespaces (namespaces );
99
-
100
- final var fromAnnotation = configAnnotation .informerConfig ().labelSelector ();
101
- var labelSelector = Constants .NO_VALUE_SET .equals (fromAnnotation ) ? null : fromAnnotation ;
102
- informerConfig .withLabelSelector (labelSelector );
103
-
104
- final var context = Utils .contextFor (controllerConfig , dependentResourceClass ,
105
- configAnnotation .annotationType ());
106
-
107
- var onAddFilter = Utils .instantiate (configAnnotation .informerConfig ().onAddFilter (),
108
- OnAddFilter .class , context );
109
- informerConfig .withOnAddFilter ((OnAddFilter <? super R >) onAddFilter );
110
-
111
- var onUpdateFilter =
112
- Utils .instantiate (configAnnotation .informerConfig ().onUpdateFilter (),
113
- OnUpdateFilter .class , context );
114
- informerConfig .withOnUpdateFilter ((OnUpdateFilter <? super R >) onUpdateFilter );
115
-
116
- var onDeleteFilter =
117
- Utils .instantiate (configAnnotation .informerConfig ().onDeleteFilter (),
118
- OnDeleteFilter .class , context );
119
- informerConfig .withOnDeleteFilter ((OnDeleteFilter <? super R >) onDeleteFilter );
120
-
121
- var genericFilter =
122
- Utils .instantiate (configAnnotation .informerConfig ().genericFilter (),
123
- GenericFilter .class ,
124
- context );
125
-
126
- informerConfig .withGenericFilter ((GenericFilter <? super R >) genericFilter );
127
-
128
- informerConfig .followControllerNamespacesOnChange (
129
- configAnnotation .informerConfig ().followControllerNamespacesOnChange ());
130
-
131
- var primaryToSecondaryMapper =
132
- Utils .instantiate (configAnnotation .informerConfig ().primaryToSecondaryMapper (),
133
- PrimaryToSecondaryMapper .class , context );
134
- informerConfig .withPrimaryToSecondaryMapper (primaryToSecondaryMapper );
135
-
136
- var secondaryToPrimaryMapper =
137
- Utils .instantiate (configAnnotation .informerConfig ().secondaryToPrimaryMapper (),
138
- SecondaryToPrimaryMapper .class , context );
139
- if (secondaryToPrimaryMapper != null ) {
140
- informerConfig .withSecondaryToPrimaryMapper (secondaryToPrimaryMapper );
141
- }
77
+ informerConfig = InformerConfiguration .from (
78
+ GroupVersionKind .fromString (configAnnotation .informerConfig ().groupVersionKind ()),
79
+ controllerConfig .getResourceClass ());
80
+ } else {
81
+ informerConfig =
82
+ InformerConfiguration .from (resourceType , controllerConfig .getResourceClass ());
83
+ }
84
+
85
+ if (configAnnotation != null && configAnnotation .informerConfig () != null ) {
86
+
87
+ if (!Constants .NO_VALUE_SET .equals (configAnnotation .informerConfig ().name ())) {
88
+ informerConfig .withName (configAnnotation .informerConfig ().name ());
89
+ } else if (spec .getName () != null && !Constants .NO_VALUE_SET .equals (spec .getName ())) {
90
+ informerConfig .withName (spec .getName ());
142
91
} else {
143
- getSecondaryToPrimaryMapper (dependentResourceClass ,
144
- controllerConfig .getResourceClass ())
145
- .ifPresentOrElse (informerConfig ::withSecondaryToPrimaryMapper , () -> {
146
- if (spec .getUseEventSourceWithName ().isEmpty ()) {
147
- log .warn ("No SecondaryToPrimaryMapper going to be set for dependent resource." +
148
- " This might be an issue with the setup of the dependent resource" );
149
- }
150
- });
92
+ informerConfig .withName (DependentResource .defaultNameFor (dependentResourceClass ));
151
93
}
152
94
95
+ var namespaces = Set .of (configAnnotation .informerConfig ().namespaces ());
96
+ informerConfig .withNamespaces (namespaces );
153
97
98
+ final var fromAnnotation = configAnnotation .informerConfig ().labelSelector ();
99
+ var labelSelector = Constants .NO_VALUE_SET .equals (fromAnnotation ) ? null : fromAnnotation ;
100
+ informerConfig .withLabelSelector (labelSelector );
154
101
155
- return informerConfig . build ();
156
- } catch ( InstantiationException | IllegalAccessException | InvocationTargetException
157
- | NoSuchMethodException e ) {
158
- throw new RuntimeException ( e );
159
- }
160
- }
102
+ final var context = Utils . contextFor ( controllerConfig , dependentResourceClass ,
103
+ configAnnotation . annotationType ());
104
+
105
+ var onAddFilter = Utils . instantiate ( configAnnotation . informerConfig (). onAddFilter (),
106
+ OnAddFilter . class , context );
107
+ informerConfig . withOnAddFilter (( OnAddFilter <? super R >) onAddFilter );
161
108
109
+ var onUpdateFilter =
110
+ Utils .instantiate (configAnnotation .informerConfig ().onUpdateFilter (),
111
+ OnUpdateFilter .class , context );
112
+ informerConfig .withOnUpdateFilter ((OnUpdateFilter <? super R >) onUpdateFilter );
162
113
114
+ var onDeleteFilter =
115
+ Utils .instantiate (configAnnotation .informerConfig ().onDeleteFilter (),
116
+ OnDeleteFilter .class , context );
117
+ informerConfig .withOnDeleteFilter ((OnDeleteFilter <? super R >) onDeleteFilter );
163
118
119
+ var genericFilter =
120
+ Utils .instantiate (configAnnotation .informerConfig ().genericFilter (),
121
+ GenericFilter .class ,
122
+ context );
123
+
124
+ informerConfig .withGenericFilter ((GenericFilter <? super R >) genericFilter );
125
+
126
+ informerConfig .followControllerNamespacesOnChange (
127
+ configAnnotation .informerConfig ().followControllerNamespacesOnChange ());
128
+
129
+ var primaryToSecondaryMapper =
130
+ Utils .instantiate (configAnnotation .informerConfig ().primaryToSecondaryMapper (),
131
+ PrimaryToSecondaryMapper .class , context );
132
+ informerConfig .withPrimaryToSecondaryMapper (primaryToSecondaryMapper );
133
+
134
+ var secondaryToPrimaryMapper =
135
+ Utils .instantiate (configAnnotation .informerConfig ().secondaryToPrimaryMapper (),
136
+ SecondaryToPrimaryMapper .class , context );
137
+ if (secondaryToPrimaryMapper != null ) {
138
+ informerConfig .withSecondaryToPrimaryMapper (secondaryToPrimaryMapper );
139
+ }
140
+ } else {
141
+ getSecondaryToPrimaryMapper (dependentResourceClass ,
142
+ controllerConfig .getResourceClass ())
143
+ .ifPresentOrElse (informerConfig ::withSecondaryToPrimaryMapper , () -> {
144
+ if (spec .getUseEventSourceWithName ().isEmpty ()) {
145
+ log .warn ("No SecondaryToPrimaryMapper going to be set for dependent resource." +
146
+ " This might be an issue with the setup of the dependent resource" );
147
+ }
148
+ });
149
+ }
150
+ return informerConfig .build ();
151
+ }
164
152
}
0 commit comments