Skip to content

Commit

Permalink
Fix fabric8io#3972: removing the need for parameter processing in Ser…
Browse files Browse the repository at this point in the history
…ialization

The methods accepting parameters have been deprecated
Support for Parameterizable has been removed - it should have been
rarely used.
  • Loading branch information
shawkins committed Dec 8, 2022
1 parent d418273 commit 80c9c21
Show file tree
Hide file tree
Showing 27 changed files with 99 additions and 233 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -33,6 +33,7 @@
* Fix #4515: files located at the root of jars named model.properties, e.g. core.properties, have been removed
* Fix #4579: the implicit registration of resource and list types that happens when using the resource(class) methods has been removed. This makes the behavior of the client more predictable as that was an undocumented side-effect. If you expect to see instances of a custom type from an untyped api call - typically KubernetesClient.load, KubernetesClient.resourceList, KubernetesClient.resource(InputStream|String), then you must either create a META-INF/services/io.fabric8.kubernetes.api.model.KubernetesResource file (see above #3923), or make calls to KubernetesDeserializer.registerCustomKind - however since KubernetesDeserializer is an internal class that mechanism is not preferred.
* Fix #4597: remove the deprecated support for `javax.validation.constraints.NotNull` in the `crd-generator`, to mark a property as `required` it needs to be annotated with `io.fabric8.generator.annotation.Required`
* Fix #3973: removed support for Parameterizable - that was only needed as a workaround for non-string parameters. You should instead include those parameter values in the map passed to processLocally.

### 6.2.0 (2022-10-20)

Expand Down
Expand Up @@ -75,7 +75,6 @@
import io.fabric8.kubernetes.client.dsl.NamespaceableResource;
import io.fabric8.kubernetes.client.dsl.NetworkAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.dsl.PolicyAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.RbacAPIGroupDSL;
Expand Down Expand Up @@ -291,15 +290,15 @@ MixedOperation<GenericKubernetesResource, GenericKubernetesResourceList, Resourc
* @param is the input stream containing JSON/YAML content
* @return an operation instance to work on the list of Kubernetes Resource objects
*/
ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> load(InputStream is);
NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> load(InputStream is);

/**
* Load a Kubernetes list object
*
* @param s kubernetes list as string
* @return an operation instance to work on the deserialized KubernetesList objects
*/
ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(String s);
NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(String s);

/**
* KubernetesResourceList operations
Expand Down
Expand Up @@ -98,12 +98,12 @@ public KubernetesClientBuilder withConfig(Config config) {
}

public KubernetesClientBuilder withConfig(String config) {
this.config = Serialization.unmarshal(config);
this.config = Serialization.unmarshal(config, Config.class);
return this;
}

public KubernetesClientBuilder withConfig(InputStream config) {
this.config = Serialization.unmarshal(config);
this.config = Serialization.unmarshal(config, Config.class);
return this;
}

Expand Down
Expand Up @@ -76,7 +76,6 @@
import io.fabric8.kubernetes.client.dsl.NamespaceableResource;
import io.fabric8.kubernetes.client.dsl.NetworkAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.NonNamespaceOperation;
import io.fabric8.kubernetes.client.dsl.ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.PodResource;
import io.fabric8.kubernetes.client.dsl.PolicyAPIGroupDSL;
import io.fabric8.kubernetes.client.dsl.RbacAPIGroupDSL;
Expand Down Expand Up @@ -248,12 +247,12 @@ public NonNamespaceOperation<ComponentStatus, ComponentStatusList, Resource<Comp
}

@Override
public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> load(InputStream is) {
public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> load(InputStream is) {
return getClient().load(is);
}

@Override
public ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(String s) {
public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> resourceList(String s) {
return getClient().resourceList(s);
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Expand Up @@ -75,11 +75,7 @@ public static boolean isJSONValid(String json) {
}

public static String convertYamlToJson(String yaml) throws IOException {
ObjectMapper yamlReader = Serialization.yamlMapper();
Object obj = yamlReader.readValue(yaml, Object.class);

ObjectMapper jsonWriter = Serialization.jsonMapper();
return jsonWriter.writeValueAsString(obj);
return Serialization.asJson(Serialization.unmarshal(yaml));
}

public static String convertToJson(String jsonOrYaml) throws IOException {
Expand Down
Expand Up @@ -35,7 +35,6 @@

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
Expand Down Expand Up @@ -180,17 +179,13 @@ public static <T> T unmarshal(InputStream is) {
* @param parameters A {@link Map} with parameters for placeholder substitution.
* @param <T> The target type.
* @return returns returns de-serialized object
*
* @deprecated
*/
@Deprecated
@SuppressWarnings("unchecked")
public static <T> T unmarshal(InputStream is, Map<String, String> parameters) {
String specFile = readSpecFileFromInputStream(is);
if (containsMultipleDocuments(specFile)) {
return (T) getKubernetesResourceList(parameters, specFile);
} else if (specFile.contains(DOCUMENT_DELIMITER)) {
specFile = specFile.replaceAll("^---([ \\t].*?)?\\r?\\n", "");
specFile = specFile.replaceAll("\\n---([ \\t].*?)?\\r?\\n?$", "\n");
}
return unmarshal(new ByteArrayInputStream(specFile.getBytes()), JSON_MAPPER, parameters);
return unmarshal(is, JSON_MAPPER, parameters);
}

/**
Expand All @@ -217,9 +212,28 @@ public static <T> T unmarshal(InputStream is, ObjectMapper mapper) {
* @param parameters A {@link Map} with parameters for placeholder substitution.
* @param <T> The target type.
* @return returns de-serialized object
*
* @deprecated
*/
@Deprecated
public static <T> T unmarshal(InputStream is, ObjectMapper mapper, Map<String, String> parameters) {
return unmarshal(is, mapper, new TypeReference<T>() {
// TODO: this defeats the purpose of accepting a stream...
// also it's not well documented which Serialization methods are aware of input that can contain
// multiple docs
String specFile;
try {
specFile = IOHelpers.readFully(is);
} catch (IOException e1) {
throw new RuntimeException("Could not read stream");
}
if (containsMultipleDocuments(specFile)) {
return (T) getKubernetesResourceList(Collections.emptyMap(), specFile);
} else if (specFile.contains(DOCUMENT_DELIMITER)) {
specFile = specFile.replaceAll("^---([ \\t].*?)?\\r?\\n", "");
specFile = specFile.replaceAll("\\n---([ \\t].*?)?\\r?\\n?$", "\n");
}

return unmarshal(new ByteArrayInputStream(specFile.getBytes(StandardCharsets.UTF_8)), mapper, new TypeReference<T>() {
@Override
public Type getType() {
return KubernetesResource.class;
Expand All @@ -228,9 +242,8 @@ public Type getType() {
}

private static <T> T unmarshal(InputStream is, ObjectMapper mapper, TypeReference<T> type, Map<String, String> parameters) {
try (
InputStream wrapped = parameters != null && !parameters.isEmpty() ? ReplaceValueStream.replaceValues(is, parameters)
: is;
try (InputStream wrapped = parameters != null && !parameters.isEmpty() ? ReplaceValueStream.replaceValues(is, parameters)
: is;
BufferedInputStream bis = new BufferedInputStream(wrapped)) {
bis.mark(-1);
int intch;
Expand Down Expand Up @@ -296,7 +309,10 @@ public static <T> T unmarshal(String str, final Class<T> type) {
* @param parameters A hashmap containing parameters
*
* @return returns de-serialized object
*
* @deprecated
*/
@Deprecated
public static <T> T unmarshal(String str, final Class<T> type, Map<String, String> parameters) {
try (InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8))) {
return unmarshal(is, new TypeReference<T>() {
Expand Down Expand Up @@ -330,7 +346,10 @@ public static <T> T unmarshal(InputStream is, final Class<T> type) {
* @param parameters A {@link Map} with parameters for placeholder substitution.
* @param <T> Template argument denoting type
* @return returns de-serialized object
*
* @deprecated
*/
@Deprecated
public static <T> T unmarshal(InputStream is, final Class<T> type, Map<String, String> parameters) {
return unmarshal(is, new TypeReference<T>() {
@Override
Expand Down Expand Up @@ -361,7 +380,10 @@ public static <T> T unmarshal(InputStream is, TypeReference<T> type) {
* @param <T> Template argument denoting type
*
* @return returns de-serialized object
*
* @deprecated
*/
@Deprecated
public static <T> T unmarshal(InputStream is, TypeReference<T> type, Map<String, String> parameters) {
return unmarshal(is, JSON_MAPPER, type, parameters);
}
Expand Down Expand Up @@ -401,20 +423,6 @@ private static boolean validate(String document) {
return !document.isEmpty() && keyValueMatcher.find();
}

private static String readSpecFileFromInputStream(InputStream inputStream) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
try {
while ((length = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, length);
}
return outputStream.toString();
} catch (IOException e) {
throw new RuntimeException("Unable to read InputStream." + e);
}
}

/**
* Create a copy of the resource via serialization.
*
Expand Down
Expand Up @@ -21,8 +21,6 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import java.util.Collections;

import static org.assertj.core.api.Assertions.assertThat;

class SerializationSingleDocumentUnmarshalTest {
Expand All @@ -36,8 +34,7 @@ class SerializationSingleDocumentUnmarshalTest {
void unmarshalWithSingleDocumentWithDocumentDelimiterShouldReturnKubernetesResource(String arg) {
// When
final KubernetesResource result = Serialization.unmarshal(
SerializationTest.class.getResourceAsStream(String.format("/serialization/%s", arg)),
Collections.emptyMap());
SerializationTest.class.getResourceAsStream(String.format("/serialization/%s", arg)));
// Then
assertThat(result)
.asInstanceOf(InstanceOfAssertFactories.type(Service.class))
Expand Down
Expand Up @@ -402,7 +402,7 @@ public Type getType() {
return refinedType;
}
};
CompletableFuture<L> futureAnswer = handleResponse(httpClient, requestBuilder, listTypeReference, getParameters());
CompletableFuture<L> futureAnswer = handleResponse(httpClient, requestBuilder, listTypeReference);
return futureAnswer.thenApply(l -> {
updateApiVersion(l);
return l;
Expand Down
Expand Up @@ -33,7 +33,6 @@
import io.fabric8.kubernetes.client.dsl.ListVisitFromServerWritable;
import io.fabric8.kubernetes.client.dsl.NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.NamespaceableResource;
import io.fabric8.kubernetes.client.dsl.ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.Waitable;
import io.fabric8.kubernetes.client.readiness.Readiness;
Expand All @@ -47,7 +46,6 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand All @@ -59,7 +57,7 @@
import java.util.stream.Stream;

public class NamespaceVisitFromServerGetWatchDeleteRecreateWaitApplicableListImpl
implements ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata>,
implements NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata>,
Waitable<List<HasMetadata>, HasMetadata> {

private static final Logger LOGGER = LoggerFactory
Expand All @@ -86,7 +84,7 @@ List<HasMetadata> getItems() {
Object item = context.getItem();

if (item instanceof InputStream) {
item = Serialization.unmarshal((InputStream) item, Collections.emptyMap());
item = Serialization.unmarshal((InputStream) item);
context = context.withItem(item); // late realization of the inputstream
}

Expand Down Expand Up @@ -174,13 +172,6 @@ private static void logAsNotReady(Throwable t, HasMetadata meta) {
LOGGER.debug("The error stack trace:", t);
}

@Override
public NamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> withParameters(
Map<String, String> parameters) {
Object item = Serialization.unmarshal((InputStream) context.getItem(), parameters);
return newInstance(context.withItem(item));
}

@Override
public ListVisitFromServerWritable<HasMetadata> dryRun(boolean isDryRun) {
return newInstance(this.context.withDryRun(isDryRun));
Expand Down

0 comments on commit 80c9c21

Please sign in to comment.