Skip to content

Commit

Permalink
fix (jkube-kit/config) : Add workaround for trace level logging probl…
Browse files Browse the repository at this point in the history
…ems with KubernetesClient (eclipse-jkube#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 <[email protected]>
  • Loading branch information
rohanKanojia committed Jan 18, 2023
1 parent 174d189 commit 37fe020
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 37fe020

Please sign in to comment.