1
1
package io .javaoperatorsdk .operator .config .runtime ;
2
2
3
+ import java .lang .reflect .InvocationTargetException ;
4
+ import java .util .ArrayList ;
5
+ import java .util .Collections ;
6
+ import java .util .List ;
3
7
import java .util .Set ;
4
8
import java .util .function .Function ;
5
9
6
10
import io .fabric8 .kubernetes .api .model .HasMetadata ;
11
+ import io .fabric8 .kubernetes .client .CustomResource ;
7
12
import io .javaoperatorsdk .operator .ControllerUtils ;
8
13
import io .javaoperatorsdk .operator .api .config .ConfigurationService ;
14
+ import io .javaoperatorsdk .operator .api .config .dependent .DefaultDependentResourceConfiguration ;
9
15
import io .javaoperatorsdk .operator .api .reconciler .ControllerConfiguration ;
10
16
import io .javaoperatorsdk .operator .api .reconciler .Reconciler ;
17
+ import io .javaoperatorsdk .operator .api .reconciler .dependent .DependentResource ;
18
+ import io .javaoperatorsdk .operator .api .reconciler .dependent .DependentResourceConfiguration ;
11
19
import io .javaoperatorsdk .operator .processing .event .source .ResourceEventFilter ;
12
20
import io .javaoperatorsdk .operator .processing .event .source .ResourceEventFilters ;
13
21
@@ -81,7 +89,7 @@ public ResourceEventFilter<R, io.javaoperatorsdk.operator.api.config.ControllerC
81
89
82
90
var filterTypes =
83
91
(Class <ResourceEventFilter <R , io .javaoperatorsdk .operator .api .config .ControllerConfiguration <R >>>[]) valueOrDefault (
84
- annotation , ControllerConfiguration ::eventFilters , new Object [] {});
92
+ annotation , ControllerConfiguration ::eventFilters , new Class [] {});
85
93
if (filterTypes .length > 0 ) {
86
94
for (var filterType : filterTypes ) {
87
95
try {
@@ -102,13 +110,77 @@ public ResourceEventFilter<R, io.javaoperatorsdk.operator.api.config.ControllerC
102
110
: ResourceEventFilters .passthrough ();
103
111
}
104
112
105
- public static <T > T valueOrDefault (ControllerConfiguration controllerConfiguration ,
106
- Function <ControllerConfiguration , T > mapper ,
107
- T defaultValue ) {
108
- if (controllerConfiguration == null ) {
109
- return defaultValue ;
113
+ @ Override
114
+ public List <? extends DependentResource > getDependentResources () {
115
+ final var dependentConfigs = valueOrDefault (annotation ,
116
+ ControllerConfiguration ::dependents , new DependentResourceConfiguration [] {});
117
+ if (dependentConfigs .length > 0 ) {
118
+ final List <DependentResource > result = new ArrayList <>(dependentConfigs .length );
119
+ for (DependentResourceConfiguration dependentConfig : dependentConfigs ) {
120
+ final var creatable = valueOrDefault (dependentConfig ,
121
+ DependentResourceConfiguration ::creatable ,
122
+ DependentResourceConfiguration .CREATABLE_DEFAULT );
123
+ final var updatable = valueOrDefault (dependentConfig ,
124
+ DependentResourceConfiguration ::updatable ,
125
+ DependentResourceConfiguration .UPDATABLE_DEFAULT );
126
+ final var owned = valueOrDefault (dependentConfig ,
127
+ DependentResourceConfiguration ::owned ,
128
+ DependentResourceConfiguration .OWNED_DEFAULT );
129
+
130
+ final var resourceType =
131
+ valueOrDefault (dependentConfig , DependentResourceConfiguration ::resourceType , null );
132
+ final var crdName = CustomResource .getCRDName (resourceType );
133
+ final var namespaces = Set .of (
134
+ valueOrDefault (dependentConfig , DependentResourceConfiguration ::namespaces ,
135
+ new String [] {}));
136
+ final var labelSelector =
137
+ valueOrDefault (dependentConfig , DependentResourceConfiguration ::labelSelector , "" );
138
+
139
+ final DefaultDependentResourceConfiguration <? extends HasMetadata > configuration =
140
+ new DefaultDependentResourceConfiguration <>(
141
+ crdName , resourceType , namespaces , labelSelector , service , creatable , updatable ,
142
+ owned ,
143
+ null , null , true );
144
+
145
+ final var builder =
146
+ valueIfPresentOrNull (dependentConfig , DependentResourceConfiguration ::builder ,
147
+ DependentResourceConfiguration .DEFAULT_BUILDER .class );
148
+ final var updater =
149
+ valueIfPresentOrNull (dependentConfig , DependentResourceConfiguration ::updater ,
150
+ DependentResourceConfiguration .DEFAULT_UPDATER .class );
151
+ final var fetcher =
152
+ valueIfPresentOrNull (dependentConfig , DependentResourceConfiguration ::fetcher ,
153
+ DependentResourceConfiguration .DEFAULT_FETCHER .class );
154
+
155
+ final var dependent = new DependentResource (configuration , builder , updater , fetcher );
156
+ result .add (dependent );
157
+ }
158
+ return result ;
110
159
} else {
111
- return mapper .apply (controllerConfiguration );
160
+ return Collections .emptyList ();
161
+ }
162
+ }
163
+
164
+ private static <C , T > T valueOrDefault (C annotation , Function <C , T > mapper , T defaultValue ) {
165
+ return annotation == null ? defaultValue : mapper .apply (annotation );
166
+ }
167
+
168
+ private static <T > T valueIfPresentOrNull (DependentResourceConfiguration annotation ,
169
+ Function <DependentResourceConfiguration , Class <? extends T >> mapper ,
170
+ Class <? extends T > defaultValue ) {
171
+ if (annotation == null ) {
172
+ return null ;
173
+ }
174
+
175
+ final var value = mapper .apply (annotation );
176
+ if (defaultValue .equals (value )) {
177
+ return null ;
178
+ }
179
+ try {
180
+ return value .getConstructor ().newInstance ();
181
+ } catch (InstantiationException | NoSuchMethodException | InvocationTargetException
182
+ | IllegalAccessException e ) {
183
+ throw new RuntimeException (e );
112
184
}
113
185
}
114
186
}
0 commit comments