From 37fe0201909cd5b879296f3f6e4f145245bc38e6 Mon Sep 17 00:00:00 2001 From: Rohan Kumar Date: Wed, 18 Jan 2023 17:56:51 +0530 Subject: [PATCH] fix (jkube-kit/config) : Add workaround for trace level logging problems with KubernetesClient (#1950) Add a workaround to forcefully set `org.slf4j.simpleLogger.log.okhttp3.logging.HttpLoggingInterceptor` property to `BASIC` to prevent OkHttp's HttpLoggingInterceptor to interfere with KubernetesClient operations. Signed-off-by: Rohan Kumar --- CHANGELOG.md | 1 + .../kit/config/service/JKubeServiceHub.java | 20 +++++++++++++- .../config/service/JKubeServiceHubTest.java | 27 +++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f3a45f862..c284122486 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ Usage: * Fix #1546: Migrate to JUnit5 testing framework * Fix #1858: Properties in image name not replaced * Fix #1935: `oc:remote-dev` goal / `ocRemoteDev` task have wrong log prefixes +* Fix #1950: Add workaround to set OkHttp's `HttpLoggingInterceptor` log level to `BASIC` when trace enabled * Fix #1966: Old reference to fmp in documentation * Fix #1974: Remove unused methods in KubernetesResourceUtil diff --git a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/JKubeServiceHub.java b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/JKubeServiceHub.java index 6a2e8ed705..d91ab7173d 100644 --- a/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/JKubeServiceHub.java +++ b/jkube-kit/config/service/src/main/java/org/eclipse/jkube/kit/config/service/JKubeServiceHub.java @@ -21,6 +21,7 @@ import lombok.Builder; import lombok.Getter; import lombok.Setter; +import org.apache.commons.lang3.StringUtils; import org.eclipse.jkube.kit.build.service.docker.access.DockerAccess; import org.eclipse.jkube.kit.common.service.MigrateService; import org.eclipse.jkube.kit.build.service.docker.DockerServiceHub; @@ -102,7 +103,7 @@ private void init() { private void initLazyBuilders() { clusterAccessLazyBuilder = new LazyBuilder<>(this::initClusterAccessIfNecessary); - kubernetesClientLazyBuilder = new LazyBuilder<>(() -> getClusterAccess().createDefaultClient()); + kubernetesClientLazyBuilder = new LazyBuilder<>(this::createKubernetesClient); buildServiceManager = new LazyBuilder<>(() -> new BuildServiceManager(this)); pluginManager = new LazyBuilder<>(() -> new PluginManager(this)); applyService = new LazyBuilder<>(() -> new ApplyService(getClient(), log)); @@ -181,4 +182,21 @@ public KubernetesClient getClient() { public ClusterAccess getClusterAccess() { return clusterAccessLazyBuilder.get(); } + + private KubernetesClient createKubernetesClient() { + // Workaround for https://github.com/eclipse/jkube/issues/1950 + // Forcefully set HttpLoggingInterceptor logging level to basic to not read requests/response bodies + setOkHttpLoggingLevelToBasicIfTraceEnabled(); + return getClusterAccess().createDefaultClient(); + } + + private void setOkHttpLoggingLevelToBasicIfTraceEnabled() { + JKubeConfiguration config = getConfiguration(); + if (config != null && config.getProject() != null && config.getProperties() != null) { + String logLevel = config.getProperties().getProperty("org.slf4j.simpleLogger.defaultLogLevel"); + if (StringUtils.isNotBlank(logLevel) && logLevel.equals("trace")) { + System.setProperty("org.slf4j.simpleLogger.log.okhttp3.logging.HttpLoggingInterceptor", "BASIC"); + } + } + } } diff --git a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/JKubeServiceHubTest.java b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/JKubeServiceHubTest.java index c9dbbe11a4..ef6508df30 100644 --- a/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/JKubeServiceHubTest.java +++ b/jkube-kit/config/service/src/test/java/org/eclipse/jkube/kit/config/service/JKubeServiceHubTest.java @@ -13,8 +13,10 @@ */ package org.eclipse.jkube.kit.config.service; +import io.fabric8.kubernetes.client.KubernetesClient; import org.eclipse.jkube.kit.build.service.docker.DockerServiceHub; import org.eclipse.jkube.kit.common.JKubeConfiguration; +import org.eclipse.jkube.kit.common.JavaProject; import org.eclipse.jkube.kit.common.KitLogger; import org.eclipse.jkube.kit.common.service.MigrateService; import org.eclipse.jkube.kit.common.util.LazyBuilder; @@ -31,9 +33,12 @@ import io.fabric8.openshift.client.OpenShiftClient; import org.eclipse.jkube.kit.resource.helm.HelmService; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import org.mockito.MockedConstruction; +import java.util.Properties; + import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatNullPointerException; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; @@ -286,6 +291,28 @@ void getClientWithOfflineConnectionIsNotAllowed() { } } + @DisplayName("https://github.com/eclipse/jkube/issues/1950") + @Test + void getClient_whenTraceLoggingEnabled_thenSetHttpLoggingInterceptorLevelToBasic() { + // Given + Properties properties = new Properties(); + properties.put("org.slf4j.simpleLogger.defaultLogLevel", "trace"); + jKubeServiceHubBuilder.configuration(JKubeConfiguration.builder() + .project(JavaProject.builder() + .properties(properties) + .build()) + .build()); + jKubeServiceHubBuilder.clusterAccess(mock(ClusterAccess.class)); + + // When + Then + try (final JKubeServiceHub jKubeServiceHub = jKubeServiceHubBuilder.build()) { + KubernetesClient kubernetesClient = jKubeServiceHub.getClient(); + + assertThat(System.getProperty("org.slf4j.simpleLogger.log.okhttp3.logging.HttpLoggingInterceptor")) + .isEqualTo("BASIC"); + } + } + @Test void getApplyServiceWithOfflineThrowsException() { // Given