Skip to content

Update client builder to be more robust by default #1498

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

Merged
Merged
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
50 changes: 47 additions & 3 deletions util/src/main/java/io/kubernetes/client/util/ClientBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@
*/
package io.kubernetes.client.util;

import static io.kubernetes.client.util.Config.*;
import static io.kubernetes.client.util.Config.ENV_KUBECONFIG;
import static io.kubernetes.client.util.Config.ENV_SERVICE_HOST;
import static io.kubernetes.client.util.Config.ENV_SERVICE_PORT;
import static io.kubernetes.client.util.Config.SERVICEACCOUNT_CA_PATH;
import static io.kubernetes.client.util.Config.SERVICEACCOUNT_TOKEN_PATH;
import static io.kubernetes.client.util.KubeConfig.ENV_HOME;
import static io.kubernetes.client.util.KubeConfig.KUBECONFIG;
import static io.kubernetes.client.util.KubeConfig.KUBEDIR;
Expand Down Expand Up @@ -41,7 +45,9 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.PrivateKey;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import okhttp3.Protocol;
import org.apache.commons.compress.utils.IOUtils;
import org.slf4j.Logger;
Expand All @@ -55,6 +61,12 @@ public class ClientBuilder {
private byte[] caCertBytes = null;
private boolean verifyingSsl = true;
private Authentication authentication;
// defaulting client protocols to HTTP1.1 and HTTP 2
private List<Protocol> protocols = Arrays.asList(Protocol.HTTP_2, Protocol.HTTP_1_1);
// default to unlimited read timeout
private Duration readTimeout = Duration.ZERO;
// default health check is once a minute
private Duration pingInterval = Duration.ofMinutes(1);

/**
* Creates an {@link ApiClient} by calling {@link #standard()} and {@link #build()}.
Expand Down Expand Up @@ -367,12 +379,44 @@ public ClientBuilder setVerifyingSsl(boolean verifyingSsl) {
return this;
}

public ClientBuilder setProtocols(List<Protocol> protocols) {
this.protocols = protocols;
return this;
}

public List<Protocol> getProtocols() {
return protocols;
}

public ClientBuilder setReadTimeout(Duration readTimeout) {
this.readTimeout = readTimeout;
return this;
}

public Duration getReadTimeout() {
return this.readTimeout;
}

public ClientBuilder setPingInterval(Duration pingInterval) {
this.pingInterval = pingInterval;
return this;
}

public Duration getPingInterval() {
return this.pingInterval;
}

public ApiClient build() {
final ApiClient client = new ApiClient();

// defaulting client protocols to HTTP1.1
client.setHttpClient(
client.getHttpClient().newBuilder().protocols(Arrays.asList(Protocol.HTTP_1_1)).build());
client
.getHttpClient()
.newBuilder()
.protocols(protocols)
.readTimeout(this.readTimeout)
.pingInterval(pingInterval)
.build());

if (basePath != null) {
if (basePath.endsWith("/")) {
Expand Down