@@ -28,6 +28,8 @@ public class ReconcilerUtils {
2828 protected static final String MISSING_GROUP_SUFFIX = ".javaoperatorsdk.io" ;
2929 private static final String GET_SPEC = "getSpec" ;
3030 private static final String SET_SPEC = "setSpec" ;
31+ private static final String SET_STATUS = "setStatus" ;
32+ private static final String GET_STATUS = "getStatus" ;
3133 private static final Pattern API_URI_PATTERN =
3234 Pattern .compile (".*http(s?)://[^/]*/api(s?)/(\\ S*).*" ); // NOSONAR: input is controlled
3335
@@ -135,11 +137,23 @@ public static Object getSpec(HasMetadata resource) {
135137 return cr .getSpec ();
136138 }
137139
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 ) {
138152 try {
139- Method getSpecMethod = resource .getClass ().getMethod (GET_SPEC );
153+ Method getSpecMethod = resource .getClass ().getMethod (getMethod );
140154 return getSpecMethod .invoke (resource );
141155 } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e ) {
142- throw noSpecException (resource , e );
156+ throw noMethodException (resource , e , getMethod );
143157 }
144158 }
145159
@@ -151,31 +165,46 @@ public static Object setSpec(HasMetadata resource, Object spec) {
151165 return null ;
152166 }
153167
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 ) {
154183 try {
155184 Class <? extends HasMetadata > resourceClass = resource .getClass ();
156185
157186 // if given spec is null, find the method just using its name
158- Method setSpecMethod ;
187+ Method setMethod ;
159188 if (spec != null ) {
160- setSpecMethod = resourceClass .getMethod (SET_SPEC , spec .getClass ());
189+ setMethod = resourceClass .getMethod (setterMethodName , spec .getClass ());
161190 } else {
162- setSpecMethod =
191+ setMethod =
163192 Arrays .stream (resourceClass .getMethods ())
164- .filter (method -> SET_SPEC .equals (method .getName ()))
193+ .filter (method -> setterMethodName .equals (method .getName ()))
165194 .findFirst ()
166- .orElseThrow (() -> noSpecException (resource , null ));
195+ .orElseThrow (() -> noMethodException (resource , null , setterMethodName ));
167196 }
168197
169- return setSpecMethod .invoke (resource , spec );
198+ return setMethod .invoke (resource , spec );
170199 } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e ) {
171- throw noSpecException (resource , e );
200+ throw noMethodException (resource , e , setterMethodName );
172201 }
173202 }
174203
175- private static IllegalStateException noSpecException (
176- HasMetadata resource , ReflectiveOperationException e ) {
204+ private static IllegalStateException noMethodException (
205+ HasMetadata resource , ReflectiveOperationException e , String methodName ) {
177206 return new IllegalStateException (
178- "No spec found on resource " + resource .getClass ().getName (), e );
207+ "No method: " + methodName + " found on resource " + resource .getClass ().getName (), e );
179208 }
180209
181210 public static <T > T loadYaml (Class <T > clazz , Class loader , String yaml ) {
0 commit comments