Skip to content

fix: fix setSpec NoSuchMethodException for custom resource (#1589) #1591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import io.fabric8.kubernetes.api.builder.Builder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.client.KubernetesClientException;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.javaoperatorsdk.operator.api.reconciler.Constants;
Expand Down Expand Up @@ -103,7 +104,9 @@ public static Object getSpec(HasMetadata resource) {

public static Object setSpec(HasMetadata resource, Object spec) {
try {
Method setSpecMethod = resource.getClass().getMethod("setSpec", spec.getClass());
Class<? extends HasMetadata> resourceClass = resource.getClass();
Method setSpecMethod =
resource.getClass().getMethod("setSpec", getSpecClass(resourceClass, spec));
return setSpecMethod.invoke(resource, spec);
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
throw new IllegalStateException("No spec found on resource", e);
Expand Down Expand Up @@ -167,4 +170,13 @@ private static boolean matchesResourceType(String resourceTypeName,
return false;
}

// CustomResouce has a parameterized parameter type
private static Class getSpecClass(Class<? extends HasMetadata> resourceClass, Object spec) {
if (CustomResource.class.isAssignableFrom(resourceClass)) {
return Object.class;
} else {
return spec.getClass();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,19 @@ void setsSpecWithReflection() {
assertThat(deployment.getSpec().getReplicas()).isEqualTo(1);
}

@Test
void setsSpecCustomResourceWithReflection() {
Tomcat tomcat = new Tomcat();
tomcat.setSpec(new TomcatSpec());
tomcat.getSpec().setReplicas(5);
TomcatSpec newSpec = new TomcatSpec();
newSpec.setReplicas(1);

ReconcilerUtils.setSpec(tomcat, newSpec);

assertThat(tomcat.getSpec().getReplicas()).isEqualTo(1);
}

@Test
void loadYamlAsBuilder() {
DeploymentBuilder builder =
Expand Down Expand Up @@ -134,7 +147,19 @@ void handleKubernetesExceptionShouldThrowMissingCRDExceptionWhenAppropriate() {
@Group("tomcatoperator.io")
@Version("v1")
@ShortNames("tc")
private static class Tomcat extends CustomResource<Void, Void> implements Namespaced {
private static class Tomcat extends CustomResource<TomcatSpec, Void> implements Namespaced {

}

private class TomcatSpec {
private Integer replicas;

public Integer getReplicas() {
return replicas;
}

public void setReplicas(Integer replicas) {
this.replicas = replicas;
}
}
}