Search in sources :

Example 1 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project che-server by eclipse-che.

the class KubernetesClientFactory method create.

/**
 * Creates instance of {@link KubernetesClient} that uses an {@link OkHttpClient} instance derived
 * from the shared {@code httpClient} instance in which interceptors are overridden to
 * authenticate with the credentials (user/password or Oauth token) contained in the {@code
 * config} parameter.
 */
protected BaseKubernetesClient<?> create(Config config) {
    OkHttpClient clientHttpClient = httpClient.newBuilder().authenticator(Authenticator.NONE).build();
    OkHttpClient.Builder builder = clientHttpClient.newBuilder();
    builder.interceptors().clear();
    builder.addInterceptor(buildKubernetesInterceptor(config)).addInterceptor(new ImpersonatorInterceptor(config));
    initializeRequestTracing(builder);
    clientHttpClient = builder.build();
    return new UnclosableKubernetesClient(clientHttpClient, config);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ImpersonatorInterceptor(io.fabric8.kubernetes.client.utils.ImpersonatorInterceptor)

Example 2 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project devspaces-images by redhat-developer.

the class KubernetesClientFactory method create.

/**
 * Creates instance of {@link KubernetesClient} that uses an {@link OkHttpClient} instance derived
 * from the shared {@code httpClient} instance in which interceptors are overridden to
 * authenticate with the credentials (user/password or Oauth token) contained in the {@code
 * config} parameter.
 */
protected BaseKubernetesClient<?> create(Config config) {
    OkHttpClient clientHttpClient = httpClient.newBuilder().authenticator(Authenticator.NONE).build();
    OkHttpClient.Builder builder = clientHttpClient.newBuilder();
    builder.interceptors().clear();
    builder.addInterceptor(buildKubernetesInterceptor(config)).addInterceptor(new ImpersonatorInterceptor(config));
    initializeRequestTracing(builder);
    clientHttpClient = builder.build();
    return new UnclosableKubernetesClient(clientHttpClient, config);
}
Also used : OkHttpClient(okhttp3.OkHttpClient) ImpersonatorInterceptor(io.fabric8.kubernetes.client.utils.ImpersonatorInterceptor)

Example 3 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kubernetes-client by fabric8io.

the class OpenIDConnectionUtils method getOIDCDiscoveryDocumentAsMap.

/**
 * OpenID providers publish their metadata at a well-known URL OpenID providers publish their metadata
 * at a well-known URL which looks like this: https://[base-server-url]/.well-known/openid-configuration
 * This method performs an Http Get at this public URL and fetches response as a HashMap
 *
 * @param client HttpClient for doing HTTP Get to well known URL of OpenID provider
 * @param issuer OpenID Connect provider issuer URL
 * @return a HashMap of Discovery document
 */
static Map<String, Object> getOIDCDiscoveryDocumentAsMap(HttpClient client, String issuer) {
    HttpRequest request = client.newHttpRequestBuilder().uri(getWellKnownUrlForOpenIDIssuer(issuer)).build();
    try {
        HttpResponse<String> response = client.send(request, String.class);
        if (response.isSuccessful() && response.body() != null) {
            return convertJsonStringToMap(response.body());
        } else {
            // Don't produce an error that's too huge (e.g. if we get HTML back for some reason).
            String responseBody = response.body();
            LOGGER.warn("oidc: failed to query metadata endpoint: {} {}", response.code(), responseBody);
        }
    } catch (IOException e) {
        LOGGER.warn("Could not refresh OIDC token, failure in getting refresh URL", e);
    }
    return Collections.emptyMap();
}
Also used : HttpRequest(io.fabric8.kubernetes.client.http.HttpRequest) IOException(java.io.IOException)

Example 4 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kubernetes-client by fabric8io.

the class OpenIDConnectionUtils method refreshOidcToken.

/**
 * Issue Token Refresh HTTP Request to OIDC Provider
 *
 * @param client Http Client for issuing HTTP request
 * @param clientId client id
 * @param refreshToken refresh token
 * @param clientSecret client secret
 * @param tokenURL OpenID Connection provider's token refresh url
 * @return response as HashMap
 * @throws IOException in case of any error in contacting OIDC provider
 */
static Map<String, Object> refreshOidcToken(HttpClient client, String clientId, String refreshToken, String clientSecret, String tokenURL) throws IOException {
    HttpRequest request = getTokenRefreshHttpRequest(client, tokenURL, clientId, refreshToken, clientSecret);
    HttpResponse<String> response = client.send(request, String.class);
    String body = response.body();
    if (body != null) {
        // Get response body as string
        if (response.isSuccessful()) {
            // Deserialize response body into a Map and return
            return convertJsonStringToMap(body);
        } else {
            // Log error response body
            LOGGER.warn("Response: {}", body);
        }
    }
    return Collections.emptyMap();
}
Also used : HttpRequest(io.fabric8.kubernetes.client.http.HttpRequest)

Example 5 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kubernetes-client by fabric8io.

the class BaseOperationTest method testHttpRetryWithLessFailuresThanRetries.

@Test
void testHttpRetryWithLessFailuresThanRetries() throws MalformedURLException, IOException {
    final AtomicInteger httpExecutionCounter = new AtomicInteger(0);
    HttpClient mockClient = newHttpClientWithSomeFailures(httpExecutionCounter, 2);
    BaseOperation<Pod, PodList, Resource<Pod>> baseOp = new BaseOperation(new OperationContext().withConfig(new ConfigBuilder().withMasterUrl("https://172.17.0.2:8443").withRequestRetryBackoffLimit(3).build()).withPlural("pods").withName("test-pod").withHttpClient(mockClient));
    baseOp.setType(Pod.class);
    // When
    Pod result = baseOp.get();
    // Then
    assertNotNull(result);
    assertEquals(3, httpExecutionCounter.get(), "Expected 3 calls: 2 failures and 1 success!");
}
Also used : OperationContext(io.fabric8.kubernetes.client.dsl.internal.OperationContext) PodOperationContext(io.fabric8.kubernetes.client.dsl.internal.PodOperationContext) PodList(io.fabric8.kubernetes.api.model.PodList) Pod(io.fabric8.kubernetes.api.model.Pod) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpClient(io.fabric8.kubernetes.client.http.HttpClient) Resource(io.fabric8.kubernetes.client.dsl.Resource) ConfigBuilder(io.fabric8.kubernetes.client.ConfigBuilder) BaseOperation(io.fabric8.kubernetes.client.dsl.internal.BaseOperation) Test(org.junit.jupiter.api.Test)

Aggregations

IOException (java.io.IOException)17 HttpClient (io.fabric8.kubernetes.client.http.HttpClient)16 URL (java.net.URL)10 Pod (io.fabric8.kubernetes.api.model.Pod)8 Test (org.junit.jupiter.api.Test)8 HttpRequest (io.fabric8.kubernetes.client.http.HttpRequest)7 Vertx (io.vertx.core.Vertx)7 File (java.io.File)7 OkHttpClient (okhttp3.OkHttpClient)7 ConfigBuilder (io.fabric8.kubernetes.client.ConfigBuilder)6 HttpClient (io.vertx.core.http.HttpClient)6 TreeMap (java.util.TreeMap)6 Service (io.fabric8.kubernetes.api.model.Service)5 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 Resource (io.fabric8.kubernetes.client.dsl.Resource)5 OperationContext (io.fabric8.kubernetes.client.dsl.internal.OperationContext)5 Route (io.fabric8.openshift.api.model.Route)5 RestAssured (io.restassured.RestAssured)5 AbstractTestClass (io.vertx.it.openshift.utils.AbstractTestClass)5 Ensure (io.vertx.it.openshift.utils.Ensure)5