Skip to content

Commit

Permalink
fix fabric8io#4662: refining the client to not use static deserializa…
Browse files Browse the repository at this point in the history
…tion logic
  • Loading branch information
shawkins committed Apr 3, 2023
1 parent 5b5a7bc commit f498b4a
Show file tree
Hide file tree
Showing 64 changed files with 801 additions and 377 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#### _**Note**_: Breaking changes
* Fix #4998: Serialization.yamlMapper and Serialization.clearYamlMapper have been deprecated

* Fix #3973: deprecated Serialization methods that take parameters and methods for the json and yaml mappers.
* Fix #4875: Removed unused options from the java-generator

### 6.5.1 (2023-03-20)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.fabric8.kubernetes.client.dsl.internal.OperationSupport;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.impl.KubernetesClientImpl;
import io.fabric8.kubernetes.client.utils.Serialization;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
Expand All @@ -46,7 +47,7 @@ class ServiceCatalogClientAdaptTest {
public void setUp() {
HttpClient mockClient = Mockito.mock(HttpClient.class, Mockito.RETURNS_DEEP_STUBS);
Config config = new ConfigBuilder().withMasterUrl("https://localhost:8443/").build();
kubernetesClient = new KubernetesClientImpl(mockClient, config);
kubernetesClient = new KubernetesClientImpl(mockClient, config, () -> Runnable::run, Serialization.getKubernetesSerialization());
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.Watcher;
import io.fabric8.kubernetes.client.WatcherException;
import io.fabric8.kubernetes.client.server.mock.KubernetesCrudDispatcher;
import io.fabric8.kubernetes.client.server.mock.KubernetesMockServer;
import io.fabric8.kubernetes.client.server.mock.crud.crd.Owl;
Expand All @@ -35,7 +37,12 @@
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class KubernetesCrudDispatcherFinalizerTest {

Expand Down Expand Up @@ -112,6 +119,20 @@ void removeFinalizerByReplace() {
// Given:
Owl owl = createOwlWithFinalizer("owl-with-finalizer");

client.resources(Owl.class).watch(new Watcher<Owl>() {

@Override
public void eventReceived(Action action, Owl resource) {
System.out.println(action + " " + Serialization.asYaml(resource));
}

@Override
public void onClose(WatcherException cause) {

}

});

// When owl with finalizer is deleted:
client.resources(Owl.class).resource(owl).delete();
Owl owlThatShouldBeMarkedForDeletion = client.resources(Owl.class).resource(owl).get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
import io.fabric8.kubernetes.client.http.HttpClient.Factory;
import io.fabric8.kubernetes.client.http.StandardHttpClientBuilder;
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
import io.fabric8.kubernetes.client.utils.Serialization;

import java.io.InputStream;

/**
* Class for Default Kubernetes Client implementing KubernetesClient interface.
Expand All @@ -36,14 +33,6 @@ public class DefaultKubernetesClient extends NamespacedKubernetesClientAdapter<N

public static final String KUBERNETES_VERSION_ENDPOINT = "version";

public static DefaultKubernetesClient fromConfig(String config) {
return new DefaultKubernetesClient(Serialization.unmarshal(config, Config.class));
}

public static DefaultKubernetesClient fromConfig(InputStream is) {
return new DefaultKubernetesClient(Serialization.unmarshal(is, Config.class));
}

public DefaultKubernetesClient() {
this(new ConfigBuilder().build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,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 @@ -300,15 +299,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
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@

package io.fabric8.kubernetes.client;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.fabric8.kubernetes.client.http.HttpClient;
import io.fabric8.kubernetes.client.informers.ResourceEventHandler;
import io.fabric8.kubernetes.client.utils.HttpClientUtils;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -47,6 +48,7 @@ default void onClose(Executor executor) {
private Class<KubernetesClient> clazz;
private ExecutorSupplier executorSupplier;
private Consumer<HttpClient.Builder> builderConsumer;
private KubernetesSerialization kubernetesSerialization = new KubernetesSerialization(new ObjectMapper());

public KubernetesClientBuilder() {
// basically the same logic as in KubernetesResourceUtil for finding list types
Expand Down Expand Up @@ -75,8 +77,9 @@ public KubernetesClient build() {
this.factory = HttpClientUtils.getHttpClientFactory();
}
HttpClient client = getHttpClient();
return clazz.getConstructor(HttpClient.class, Config.class, ExecutorSupplier.class).newInstance(client, config,
executorSupplier);
return clazz.getConstructor(HttpClient.class, Config.class, ExecutorSupplier.class, KubernetesSerialization.class)
.newInstance(client, config,
executorSupplier, kubernetesSerialization);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
throw KubernetesClientException.launderThrowable(e);
Expand All @@ -96,13 +99,18 @@ public KubernetesClientBuilder withConfig(Config config) {
return this;
}

public KubernetesClientBuilder withKubernetesSerialization(KubernetesSerialization kubernetesSerialization) {
this.kubernetesSerialization = kubernetesSerialization;
return this;
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,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 @@ -255,12 +254,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.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import io.fabric8.kubernetes.api.model.GenericKubernetesResource;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.kubernetes.client.utils.KubernetesSerialization;

import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -42,6 +42,7 @@ public class ReducedStateItemStore<V extends HasMetadata> implements ItemStore<V
private final List<String[]> fields = new ArrayList<>();
private final Class<V> typeClass;
private final KeyState keyState;
private KubernetesSerialization serialization;

public static class KeyState {

Expand Down Expand Up @@ -101,7 +102,8 @@ public KeyState(Function<HasMetadata, String> keyFunction, Function<String, Stri
* @param typeClass the expected type
* @param valueFields the additional fields to save
*/
public ReducedStateItemStore(KeyState keyState, Class<V> typeClass, String... valueFields) {
public ReducedStateItemStore(KeyState keyState, Class<V> typeClass, KubernetesSerialization serialization,
String... valueFields) {
this.keyState = keyState;
fields.add(new String[] { METADATA, "resourceVersion" });
if (valueFields != null) {
Expand All @@ -110,13 +112,14 @@ public ReducedStateItemStore(KeyState keyState, Class<V> typeClass, String... va
}
}
this.typeClass = typeClass;
this.serialization = serialization;
}

Object[] store(V value) {
if (value == null) {
return null;
}
Map<String, Object> raw = Serialization.jsonMapper().convertValue(value, Map.class);
Map<String, Object> raw = serialization.convertValue(value, Map.class);
return fields.stream().map(f -> GenericKubernetesResource.get(raw, (Object[]) f)).toArray();
}

Expand All @@ -129,7 +132,7 @@ V restore(String key, Object[] values) {
String[] keyParts = this.keyState.keyFieldFunction.apply(key);
applyFields(keyParts, raw, this.keyState.keyFields);

return Serialization.jsonMapper().convertValue(raw, typeClass);
return serialization.convertValue(raw, typeClass);
}

private static void applyFields(Object[] values, Map<String, Object> raw, List<String[]> fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ public static List<EnvVar> convertMapToEnvVarList(Map<String, String> envVarMap)
* Check whether a Kubernetes resource is Ready or not. Applicable only to
* Deployment, ReplicaSet, Pod, ReplicationController, Endpoints, Node and
* StatefulSet
*
*
* @param item item which needs to be checked
* @return boolean value indicating it's status
*/
Expand All @@ -393,7 +393,7 @@ public static boolean isResourceReady(HasMetadata item) {

/**
* Calculates age of a kubernetes resource
*
*
* @param kubernetesResource
* @return a positive duration indicating age of the kubernetes resource
*/
Expand Down Expand Up @@ -435,7 +435,7 @@ private static Class<?> loadRelated(Class<?> type, String suffix, Class<?> defau
public static Secret createDockerRegistrySecret(String dockerServer, String username, String password)
throws JsonProcessingException {
Map<String, Object> dockerConfigMap = createDockerRegistryConfigMap(dockerServer, username, password);
String dockerConfigAsStr = Serialization.jsonMapper().writeValueAsString(dockerConfigMap);
String dockerConfigAsStr = Serialization.asJson(dockerConfigMap);

return createDockerSecret(DEFAULT_CONTAINER_IMAGE_REGISTRY_SECRET_NAME, dockerConfigAsStr);
}
Expand All @@ -452,7 +452,7 @@ public static Secret createDockerRegistrySecret(String dockerServer, String user
public static Secret createDockerRegistrySecret(String dockerServer, String username, String password, String secretName)
throws JsonProcessingException {
Map<String, Object> dockerConfigMap = createDockerRegistryConfigMap(dockerServer, username, password);
String dockerConfigAsStr = Serialization.jsonMapper().writeValueAsString(dockerConfigMap);
String dockerConfigAsStr = Serialization.asJson(dockerConfigMap);

return createDockerSecret(secretName, dockerConfigAsStr);
}
Expand Down
Loading

0 comments on commit f498b4a

Please sign in to comment.