Skip to content

Commit 5a0f702

Browse files
committed
fix: fix setSpec NoSuchMethodException for custom resource (operator-framework#1589)
1 parent fb9dc1a commit 5a0f702

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

+13-1
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,13 +104,24 @@ 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 = resource.getClass().getMethod("setSpec", getSpecClass(resourceClass, spec));
107109
return setSpecMethod.invoke(resource, spec);
108110
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
109111
throw new IllegalStateException("No spec found on resource", e);
110112
}
111113
}
112114

115+
// CustomResouce has a parameterized parameter type
116+
private static Class getSpecClass(Class<? extends HasMetadata> resourceClass, Object spec){
117+
if(CustomResource.class.isAssignableFrom(resourceClass)){
118+
return Object.class;
119+
}
120+
else{
121+
return spec.getClass();
122+
}
123+
}
124+
113125
public static <T> T loadYaml(Class<T> clazz, Class loader, String yaml) {
114126
try (InputStream is = loader.getResourceAsStream(yaml)) {
115127
if (Builder.class.isAssignableFrom(clazz)) {

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

+26-1
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().setItemPath("5");
106+
TomcatSpec newSpec = new TomcatSpec();
107+
newSpec.setItemPath("1");
108+
109+
ReconcilerUtils.setSpec(tomcat, newSpec);
110+
111+
assertThat(tomcat.getSpec().getItemPath()).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 String itemPath;
156+
157+
public String getItemPath() {
158+
return itemPath;
159+
}
138160

161+
public void setItemPath(String itemPath) {
162+
this.itemPath = itemPath;
163+
}
139164
}
140165
}

0 commit comments

Comments
 (0)