Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix (jkube-kit/config) : Add workaround for trace level logging problems with KubernetesClient (#1950) #2006

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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