@@ -28,6 +28,8 @@ public class ReconcilerUtils {
28
28
protected static final String MISSING_GROUP_SUFFIX = ".javaoperatorsdk.io" ;
29
29
private static final String GET_SPEC = "getSpec" ;
30
30
private static final String SET_SPEC = "setSpec" ;
31
+ private static final String SET_STATUS = "setStatus" ;
32
+ private static final String GET_STATUS = "getStatus" ;
31
33
private static final Pattern API_URI_PATTERN =
32
34
Pattern .compile (".*http(s?)://[^/]*/api(s?)/(\\ S*).*" ); // NOSONAR: input is controlled
33
35
@@ -135,11 +137,23 @@ public static Object getSpec(HasMetadata resource) {
135
137
return cr .getSpec ();
136
138
}
137
139
140
+ return getSpecOrStatus (resource , GET_SPEC );
141
+ }
142
+
143
+ public static Object getStatus (HasMetadata resource ) {
144
+ // optimize CustomResource case
145
+ if (resource instanceof CustomResource cr ) {
146
+ return cr .getStatus ();
147
+ }
148
+ return getSpecOrStatus (resource , GET_STATUS );
149
+ }
150
+
151
+ private static Object getSpecOrStatus (HasMetadata resource , String getMethod ) {
138
152
try {
139
- Method getSpecMethod = resource .getClass ().getMethod (GET_SPEC );
153
+ Method getSpecMethod = resource .getClass ().getMethod (getMethod );
140
154
return getSpecMethod .invoke (resource );
141
155
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e ) {
142
- throw noSpecException (resource , e );
156
+ throw noMethodException (resource , e , getMethod );
143
157
}
144
158
}
145
159
@@ -151,31 +165,46 @@ public static Object setSpec(HasMetadata resource, Object spec) {
151
165
return null ;
152
166
}
153
167
168
+ return setSpecOrStatus (resource , spec , SET_SPEC );
169
+ }
170
+
171
+ @ SuppressWarnings ("unchecked" )
172
+ public static Object setStatus (HasMetadata resource , Object status ) {
173
+ // optimize CustomResource case
174
+ if (resource instanceof CustomResource cr ) {
175
+ cr .setStatus (status );
176
+ return null ;
177
+ }
178
+ return setSpecOrStatus (resource , status , SET_STATUS );
179
+ }
180
+
181
+ private static Object setSpecOrStatus (
182
+ HasMetadata resource , Object spec , String setterMethodName ) {
154
183
try {
155
184
Class <? extends HasMetadata > resourceClass = resource .getClass ();
156
185
157
186
// if given spec is null, find the method just using its name
158
- Method setSpecMethod ;
187
+ Method setMethod ;
159
188
if (spec != null ) {
160
- setSpecMethod = resourceClass .getMethod (SET_SPEC , spec .getClass ());
189
+ setMethod = resourceClass .getMethod (setterMethodName , spec .getClass ());
161
190
} else {
162
- setSpecMethod =
191
+ setMethod =
163
192
Arrays .stream (resourceClass .getMethods ())
164
- .filter (method -> SET_SPEC .equals (method .getName ()))
193
+ .filter (method -> setterMethodName .equals (method .getName ()))
165
194
.findFirst ()
166
- .orElseThrow (() -> noSpecException (resource , null ));
195
+ .orElseThrow (() -> noMethodException (resource , null , setterMethodName ));
167
196
}
168
197
169
- return setSpecMethod .invoke (resource , spec );
198
+ return setMethod .invoke (resource , spec );
170
199
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e ) {
171
- throw noSpecException (resource , e );
200
+ throw noMethodException (resource , e , setterMethodName );
172
201
}
173
202
}
174
203
175
- private static IllegalStateException noSpecException (
176
- HasMetadata resource , ReflectiveOperationException e ) {
204
+ private static IllegalStateException noMethodException (
205
+ HasMetadata resource , ReflectiveOperationException e , String methodName ) {
177
206
return new IllegalStateException (
178
- "No spec found on resource " + resource .getClass ().getName (), e );
207
+ "No method: " + methodName + " found on resource " + resource .getClass ().getName (), e );
179
208
}
180
209
181
210
public static <T > T loadYaml (Class <T > clazz , Class loader , String yaml ) {
0 commit comments