Skip to content

Commit

Permalink
fix #3460: support for deserializing templates with non-string params
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Oct 11, 2021
1 parent e1d9756 commit 6583357
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.JSONSchemaProps;
import io.fabric8.kubernetes.api.model.apps.Deployment;
import io.fabric8.kubernetes.api.model.batch.v1.Job;
import io.fabric8.kubernetes.client.CustomResource;
import io.fabric8.kubernetes.model.annotation.Group;
import io.fabric8.kubernetes.model.annotation.Version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import io.fabric8.kubernetes.api.KubernetesResourceMappingProvider;
import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
Expand All @@ -50,6 +50,9 @@ public class KubernetesDeserializer extends JsonDeserializer<KubernetesResource>

private static final Mapping mapping = new Mapping();

// template deserialization can fail if unless we use generic resources
private static ThreadLocal<Boolean> IN_TEMPLATE = ThreadLocal.withInitial(() -> false);

@Override
public KubernetesResource deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
JsonNode node = jp.readValueAsTree();
Expand Down Expand Up @@ -84,7 +87,23 @@ private static KubernetesResource fromObjectNode(JsonParser jp, JsonNode node) t
if (resourceType == null) {
return jp.getCodec().treeToValue(node, GenericKubernetesResource.class);
} else if (KubernetesResource.class.isAssignableFrom(resourceType)){
return jp.getCodec().treeToValue(node, resourceType);
boolean inTemplate = IN_TEMPLATE.get();
if (!inTemplate && "io.fabric8.openshift.api.model.Template".equals(resourceType.getName())) {
inTemplate = true;
IN_TEMPLATE.set(true);
}
try {
return jp.getCodec().treeToValue(node, resourceType);
} catch (InvalidFormatException e) {
if (!inTemplate) {
throw e;
}
return jp.getCodec().treeToValue(node, GenericKubernetesResource.class);
} finally {
if (inTemplate) {
IN_TEMPLATE.set(false);
}
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import io.fabric8.kubernetes.api.model.ServiceSpec;
import io.fabric8.kubernetes.api.model.StatusBuilder;
import io.fabric8.kubernetes.client.utils.IOHelpers;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.openshift.api.model.ParameterBuilder;
import io.fabric8.openshift.api.model.Template;
import io.fabric8.openshift.api.model.TemplateBuilder;
Expand Down Expand Up @@ -185,6 +186,13 @@ void shouldLoadTemplateWithNumberParameters() {
assertListIsServiceWithPort8080(list);
}

@Test
void shouldGetTemplateWithNumberParameters() {
OpenShiftClient client = new DefaultOpenShiftClient(new OpenShiftConfigBuilder().withDisableApiGroupCheck(true).build());
Template template = client.templates().load(getClass().getResourceAsStream("/template-with-number-params.yml")).get();
assertTrue(Serialization.asYaml(template.getObjects().get(0)).contains("{PORT}"));
}

protected void assertListIsServiceWithPort8080(KubernetesList list) {
assertNotNull(list);
assertListIsServiceWithPort8080(list.getItems());
Expand Down

0 comments on commit 6583357

Please sign in to comment.