Skip to content

Commit e0bcba9

Browse files
committed
wip
Signed-off-by: Attila Mészáros <[email protected]>
1 parent 423ba0f commit e0bcba9

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/ReconcilerUtils.java

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

operator-framework-core/src/main/java/io/javaoperatorsdk/operator/processing/event/ReconciliationDispatcher.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010
import io.fabric8.kubernetes.api.model.KubernetesResourceList;
1111
import io.fabric8.kubernetes.api.model.Namespaced;
1212
import io.fabric8.kubernetes.api.model.ObjectMeta;
13-
import io.fabric8.kubernetes.client.CustomResource;
1413
import io.fabric8.kubernetes.client.KubernetesClientException;
1514
import io.fabric8.kubernetes.client.dsl.MixedOperation;
1615
import io.fabric8.kubernetes.client.dsl.Resource;
1716
import io.fabric8.kubernetes.client.dsl.base.PatchContext;
1817
import io.fabric8.kubernetes.client.dsl.base.PatchType;
1918
import io.javaoperatorsdk.operator.OperatorException;
19+
import io.javaoperatorsdk.operator.ReconcilerUtils;
2020
import io.javaoperatorsdk.operator.api.config.Cloner;
2121
import io.javaoperatorsdk.operator.api.config.ControllerConfiguration;
2222
import io.javaoperatorsdk.operator.api.reconciler.BaseControl;
@@ -490,7 +490,7 @@ private R editStatus(R resource, R originalResource) {
490490
var res = resource(clonedOriginal);
491491
return res.editStatus(
492492
r -> {
493-
((CustomResource) r).setStatus(((CustomResource) resource).getStatus());
493+
ReconcilerUtils.setStatus(r, ReconcilerUtils.getStatus(resource));
494494
return r;
495495
});
496496
} finally {

0 commit comments

Comments
 (0)