From 26abf57ffb61cecd9b3d924f33c8fbee347992c6 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Thu, 16 Sep 2021 19:18:54 -0400 Subject: [PATCH 1/2] Add TypeAdapter for JDK 17 protected packages --- ...ventBasedConfigMapChangeDetectorTests.java | 32 +++++++++++++++++-- ...tEventBasedSecretsChangeDetectorTests.java | 31 ++++++++++++++++-- 2 files changed, 59 insertions(+), 4 deletions(-) diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java index dae163f4..88b0a44b 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedConfigMapChangeDetectorTests.java @@ -16,7 +16,10 @@ package org.springframework.cloud.kubernetes.client.config.reload; +import java.io.IOException; +import java.lang.reflect.Modifier; import java.time.Duration; +import java.time.OffsetDateTime; import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -25,6 +28,10 @@ import java.util.concurrent.TimeUnit; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import io.kubernetes.client.informer.EventType; import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.JSON; @@ -89,6 +96,11 @@ class KubernetesClientEventBasedConfigMapChangeDetectorTests { @Test void watch() { + GsonBuilder builder = new GsonBuilder(); + builder.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE) + .registerTypeAdapter(OffsetDateTime.class, new GsonOffsetDateTimeAdapter()); + Gson gson = builder.create(); + Map data = new HashMap<>(); data.put("application.properties", "spring.cloud.kubernetes.configuration.watcher.refreshDelay=0\n" + "logging.level.org.springframework.cloud.kubernetes=TRACE"); @@ -101,8 +113,7 @@ class KubernetesClientEventBasedConfigMapChangeDetectorTests { .items(Arrays.asList(applicationConfig)); stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).inScenario("watch") .whenScenarioStateIs(STARTED).withQueryParam("watch", equalTo("false")) - .willReturn(aResponse().withStatus(200).withBody(new Gson().toJson(configMapList))) - .willSetStateTo("update")); + .willReturn(aResponse().withStatus(200).withBody(gson.toJson(configMapList))).willSetStateTo("update")); Watch.Response watchResponse = new Watch.Response<>(EventType.MODIFIED.name(), new V1ConfigMap() .kind("ConfigMap").metadata(new V1ObjectMeta().namespace("default").name("bar1")).data(updateData)); @@ -156,4 +167,21 @@ class KubernetesClientEventBasedConfigMapChangeDetectorTests { verify(strategy, atLeast(3)).reload(); } + // This is needed when using JDK17 because GSON uses reflection to construct an + // OffsetDateTime but that constructor + // is protected. + public class GsonOffsetDateTimeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter jsonWriter, OffsetDateTime localDateTime) throws IOException { + jsonWriter.value(OffsetDateTime.now().toString()); + } + + @Override + public OffsetDateTime read(JsonReader jsonReader) throws IOException { + return OffsetDateTime.now(); + } + + } + } diff --git a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetectorTests.java b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetectorTests.java index 4827b391..4c438e89 100644 --- a/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetectorTests.java +++ b/spring-cloud-kubernetes-client-config/src/test/java/org/springframework/cloud/kubernetes/client/config/reload/KubernetesClientEventBasedSecretsChangeDetectorTests.java @@ -16,7 +16,10 @@ package org.springframework.cloud.kubernetes.client.config.reload; +import java.io.IOException; +import java.lang.reflect.Modifier; import java.time.Duration; +import java.time.OffsetDateTime; import java.util.Arrays; import java.util.Base64; import java.util.concurrent.TimeUnit; @@ -24,6 +27,10 @@ import java.util.concurrent.TimeUnit; import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; import io.kubernetes.client.informer.EventType; import io.kubernetes.client.openapi.ApiClient; import io.kubernetes.client.openapi.JSON; @@ -88,6 +95,10 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests { @Test void watch() { + GsonBuilder builder = new GsonBuilder(); + builder.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE) + .registerTypeAdapter(OffsetDateTime.class, new GsonOffsetDateTimeAdapter()); + Gson gson = builder.create(); V1Secret dbPassword = new V1Secret().kind("Secret").metadata(new V1ObjectMeta().name("db-password")) .putStringDataItem("password", Base64.getEncoder().encodeToString("p455w0rd".getBytes())) @@ -100,8 +111,7 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests { stubFor(get(urlMatching("^/api/v1/namespaces/default/secrets.*")).inScenario("watch") .whenScenarioStateIs(STARTED).withQueryParam("watch", equalTo("false")) - .willReturn(aResponse().withStatus(200).withBody(new Gson().toJson(secretList))) - .willSetStateTo("update")); + .willReturn(aResponse().withStatus(200).withBody(gson.toJson(secretList))).willSetStateTo("update")); Watch.Response watchResponse = new Watch.Response<>(EventType.MODIFIED.name(), dbPasswordUpdated); stubFor(get(urlMatching("/api/v1/namespaces/default/secrets.*")).inScenario("watch") @@ -155,4 +165,21 @@ class KubernetesClientEventBasedSecretsChangeDetectorTests { verify(strategy, atLeast(3)).reload(); } + // This is needed when using JDK17 because GSON uses reflection to construct an + // OffsetDateTime but that constructor + // is protected. + public class GsonOffsetDateTimeAdapter extends TypeAdapter { + + @Override + public void write(JsonWriter jsonWriter, OffsetDateTime localDateTime) throws IOException { + jsonWriter.value(OffsetDateTime.now().toString()); + } + + @Override + public OffsetDateTime read(JsonReader jsonReader) throws IOException { + return OffsetDateTime.now(); + } + + } + } From 9eb64b25a5c6ede04f514c7e6287463909068b92 Mon Sep 17 00:00:00 2001 From: Robert McNees <86265089+robertmcnees@users.noreply.github.com> Date: Mon, 20 Sep 2021 09:12:16 -0400 Subject: [PATCH 2/2] Fix github issue 736 (#868) --- .../KubernetesClientAutoConfiguration.java | 1 - ...KubernetesClientDefaultApiClientTests.java | 61 +++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/KubernetesClientDefaultApiClientTests.java diff --git a/spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientAutoConfiguration.java b/spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientAutoConfiguration.java index 2e8f3975..39a7dbb4 100644 --- a/spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientAutoConfiguration.java +++ b/spring-cloud-kubernetes-client-autoconfig/src/main/java/org/springframework/cloud/kubernetes/client/KubernetesClientAutoConfiguration.java @@ -42,7 +42,6 @@ public class KubernetesClientAutoConfiguration { @ConditionalOnMissingBean public ApiClient apiClient() { ApiClient apiClient = kubernetesApiClient(); - io.kubernetes.client.openapi.Configuration.setDefaultApiClient(apiClient); return apiClient; } diff --git a/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/KubernetesClientDefaultApiClientTests.java b/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/KubernetesClientDefaultApiClientTests.java new file mode 100644 index 00000000..7258e62d --- /dev/null +++ b/spring-cloud-kubernetes-client-autoconfig/src/test/java/org/springframework/cloud/kubernetes/client/KubernetesClientDefaultApiClientTests.java @@ -0,0 +1,61 @@ +/* + * Copyright 2013-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.kubernetes.client; + +import io.kubernetes.client.openapi.ApiClient; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * @author Robert McNees + * + * This tests that the apiClient created in KubernetesClientAutoConfiguration will not set + * itself as the default apiClient. This is to avoid overwriting the user's + * defaultApiClient if they include this project. + */ +@SpringBootTest(classes = KubernetesClientDefaultApiClientTests.App.class, + properties = { "kubernetes.informer.enabled=false" }) +// kubernetes.informer is disabled because +// io.kubernetes...KubernetesInformerAutoConfiguration +// creates a defaultApiClient that will be autowired instead of the ApiClient +// created in KubernetesClientAutoConfiguration +public class KubernetesClientDefaultApiClientTests { + + @Autowired + private ApiClient apiClient; + + @Test + public void testCreatedApiClientIsNotDefault() { + assertThat(apiClient).isNotNull(); + + ApiClient defaultApiClient = io.kubernetes.client.openapi.Configuration.getDefaultApiClient(); + assertThat(defaultApiClient).isNotNull(); + + assertThat(defaultApiClient).isNotSameAs(apiClient); + } + + @SpringBootApplication + static class App { + + } + +}