Skip to content

Commit b6cc739

Browse files
authored
fix: setSpec NoSuchMethodException for custom resource (#1589) (#1591)
1 parent 4ce2dca commit b6cc739

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import io.fabric8.kubernetes.api.builder.Builder;
1414
import io.fabric8.kubernetes.api.model.HasMetadata;
15+
import io.fabric8.kubernetes.client.CustomResource;
1516
import io.fabric8.kubernetes.client.KubernetesClientException;
1617
import io.fabric8.kubernetes.client.utils.Serialization;
1718
import io.javaoperatorsdk.operator.api.reconciler.Constants;
@@ -103,7 +104,9 @@ public static Object getSpec(HasMetadata resource) {
103104

104105
public static Object setSpec(HasMetadata resource, Object spec) {
105106
try {
106-
Method setSpecMethod = resource.getClass().getMethod("setSpec", spec.getClass());
107+
Class<? extends HasMetadata> resourceClass = resource.getClass();
108+
Method setSpecMethod =
109+
resource.getClass().getMethod("setSpec", getSpecClass(resourceClass, spec));
107110
return setSpecMethod.invoke(resource, spec);
108111
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
109112
throw new IllegalStateException("No spec found on resource", e);
@@ -167,4 +170,13 @@ private static boolean matchesResourceType(String resourceTypeName,
167170
return false;
168171
}
169172

173+
// CustomResouce has a parameterized parameter type
174+
private static Class getSpecClass(Class<? extends HasMetadata> resourceClass, Object spec) {
175+
if (CustomResource.class.isAssignableFrom(resourceClass)) {
176+
return Object.class;
177+
} else {
178+
return spec.getClass();
179+
}
180+
}
181+
170182
}

operator-framework-core/src/test/java/io/javaoperatorsdk/operator/ReconcilerUtilsTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,19 @@ void setsSpecWithReflection() {
9898
assertThat(deployment.getSpec().getReplicas()).isEqualTo(1);
9999
}
100100

101+
@Test
102+
void setsSpecCustomResourceWithReflection() {
103+
Tomcat tomcat = new Tomcat();
104+
tomcat.setSpec(new TomcatSpec());
105+
tomcat.getSpec().setReplicas(5);
106+
TomcatSpec newSpec = new TomcatSpec();
107+
newSpec.setReplicas(1);
108+
109+
ReconcilerUtils.setSpec(tomcat, newSpec);
110+
111+
assertThat(tomcat.getSpec().getReplicas()).isEqualTo(1);
112+
}
113+
101114
@Test
102115
void loadYamlAsBuilder() {
103116
DeploymentBuilder builder =
@@ -134,7 +147,19 @@ void handleKubernetesExceptionShouldThrowMissingCRDExceptionWhenAppropriate() {
134147
@Group("tomcatoperator.io")
135148
@Version("v1")
136149
@ShortNames("tc")
137-
private static class Tomcat extends CustomResource<Void, Void> implements Namespaced {
150+
private static class Tomcat extends CustomResource<TomcatSpec, Void> implements Namespaced {
151+
152+
}
153+
154+
private class TomcatSpec {
155+
private Integer replicas;
156+
157+
public Integer getReplicas() {
158+
return replicas;
159+
}
138160

161+
public void setReplicas(Integer replicas) {
162+
this.replicas = replicas;
163+
}
139164
}
140165
}

0 commit comments

Comments
 (0)