Search in sources :

Example 11 with HttpClient

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

the class BuildConfigOperationsImplTest method setUp.

@BeforeEach
public void setUp() {
    this.httpClient = Mockito.mock(HttpClient.class, Mockito.RETURNS_DEEP_STUBS);
    HttpRequest response = Mockito.mock(HttpRequest.class, Mockito.CALLS_REAL_METHODS);
    when(response.method()).thenReturn("POST");
    when(response.uri()).thenReturn(URI.create("https://localhost:8443/"));
    when(this.httpClient.newBuilder().readTimeout(anyLong(), any()).writeTimeout(anyLong(), any()).build()).thenReturn(httpClient);
    when(this.httpClient.newHttpRequestBuilder().post(any(), any(), anyLong()).header(any(), any()).uri(any(String.class)).build()).thenReturn(response);
    this.config = new OpenShiftConfigBuilder().withMasterUrl("https://localhost:8443/").build();
}
Also used : HttpRequest(io.fabric8.kubernetes.client.http.HttpRequest) HttpClient(io.fabric8.kubernetes.client.http.HttpClient) OpenShiftConfigBuilder(io.fabric8.openshift.client.OpenShiftConfigBuilder) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 12 with HttpClient

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

the class BuildConfigOperationsImplTest method testWriteShouldCompleteSuccessfully.

@Test
void testWriteShouldCompleteSuccessfully() throws IOException {
    // Given
    BuildConfigOperationsImpl impl = new BuildConfigOperationsImpl(new OpenshiftClientContext() {

        @Override
        public HttpClient getHttpClient() {
            return httpClient;
        }

        @Override
        public OpenShiftConfig getConfiguration() {
            return config;
        }
    }) {

        @Override
        protected String getRecentEvents() {
            throw new AssertionError();
        }
    };
    HttpResponse<InputStream> response = Mockito.mock(HttpResponse.class, Mockito.CALLS_REAL_METHODS);
    when(response.code()).thenReturn(200);
    when(response.body()).thenReturn(new ByteArrayInputStream(new byte[0]));
    when(httpClient.send(any(), eq(InputStream.class))).thenReturn(response);
    impl.submitToApiServer(new ByteArrayInputStream(new byte[0]), 0);
    Mockito.verify(response, Mockito.times(1)).body();
}
Also used : OpenshiftClientContext(io.fabric8.openshift.client.OpenshiftClientContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpClient(io.fabric8.kubernetes.client.http.HttpClient) OpenShiftConfig(io.fabric8.openshift.client.OpenShiftConfig) Test(org.junit.jupiter.api.Test)

Example 13 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project kas-fleetshard by bf2fc6cc711aee1a0c2a.

the class OMB method deployWorkers.

/**
 * Deploy workers to run producers and consumers.
 *
 * @param workers The number of workers to deploy.
 * @return List of worker hostnames.
 */
public List<String> deployWorkers(int workers) throws Exception {
    LOGGER.info("Deploying {} workers, container memory: {}, cpu: {}", workers, workerContainerMemory, workerCpu);
    // we are now on java 11 which defaults to https://www.eclipse.org/openj9/docs/xxusecontainersupport/ and -XX:+PreferContainerQuotaForCPUCount
    String jvmOpts = String.format("-XX:+ExitOnOutOfMemoryError");
    List<Future<Void>> futures = new ArrayList<>();
    List<Node> nodes = ombCluster.getWorkerNodes();
    ExecutorService executorService = Executors.newFixedThreadPool(N_THREADS);
    try {
        for (int i = 0; i < workers; i++) {
            String name = String.format("worker-%d", i);
            final int nodeIdx = i % nodes.size();
            futures.add(executorService.submit(() -> {
                workerNames.add(name);
                createWorker(jvmOpts, name, this.useSingleNode ? nodes.get(0) : nodes.get(nodeIdx));
                return null;
            }));
        }
    } finally {
        executorService.shutdown();
        awaitAllFutures(futures);
    }
    LOGGER.info("Collecting hosts");
    TreeMap<Integer, String> sortedHostnames = new TreeMap<>();
    ombCluster.kubeClient().client().adapt(OpenShiftClient.class).routes().inNamespace(Constants.OMB_NAMESPACE).withLabel("app", "worker").list().getItems().forEach(r -> {
        String host = r.getSpec().getHost();
        if (host == null || host.isEmpty()) {
            throw new IllegalStateException("Host node not defined");
        }
        sortedHostnames.put(workerNames.indexOf(r.getMetadata().getLabels().get("app.kubernetes.io/name")), String.format("http://%s", host));
    });
    List<String> hostnames = new ArrayList<>(sortedHostnames.values());
    LOGGER.info("Waiting for worker pods to run");
    // Wait until workers are running
    List<Pod> pods = ombCluster.kubeClient().client().pods().inNamespace(Constants.OMB_NAMESPACE).withLabel("app", "worker").list().getItems();
    while (pods.size() != workers) {
        pods = ombCluster.kubeClient().client().pods().inNamespace(Constants.OMB_NAMESPACE).withLabel("app", "worker").list().getItems();
        LOGGER.info("Found {} pods, expecting {}", pods.size(), workers);
        Thread.sleep(5000);
    }
    CompletableFuture<?>[] ready = new CompletableFuture<?>[pods.size()];
    for (int i = 0; i < pods.size(); i++) {
        Pod pod = pods.get(i);
        ready[i] = TestUtils.asyncWaitFor("pod ready", 1_000, 600_000, () -> ombCluster.kubeClient().client().pods().inNamespace(Constants.OMB_NAMESPACE).withName(pod.getMetadata().getName()).isReady());
    }
    CompletableFuture.allOf(ready).get();
    HttpClient client = HttpClient.newHttpClient();
    List<URI> notReady = hostnames.stream().map(u -> u + "/counters-stats").map(URI::create).collect(Collectors.toList());
    do {
        Iterator<URI> itr = notReady.iterator();
        LOGGER.info("Awaiting {} OMB endpoint(s) to become ready.", notReady.size());
        while (itr.hasNext()) {
            HttpRequest request = HttpRequest.newBuilder().uri(itr.next()).timeout(Duration.ofSeconds(10)).GET().build();
            HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
            if (response.statusCode() == 200) {
                itr.remove();
            }
        }
        Thread.sleep(1000);
    } while (!notReady.isEmpty());
    LOGGER.info("Deployed {} workers: {}", workers, hostnames);
    return hostnames;
}
Also used : HttpRequest(java.net.http.HttpRequest) Pod(io.fabric8.kubernetes.api.model.Pod) Node(io.fabric8.kubernetes.api.model.Node) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) TreeMap(java.util.TreeMap) URI(java.net.URI) CompletableFuture(java.util.concurrent.CompletableFuture) HttpClient(java.net.http.HttpClient) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 14 with HttpClient

use of io.fabric8.kubernetes.client.http.HttpClient in project cloud-pipeline by epam.

the class PipelineExecutor method launchRootPod.

public void launchRootPod(String command, PipelineRun run, List<EnvVar> envVars, List<String> endpoints, String pipelineId, String nodeIdLabel, String secretName, String clusterId, boolean pullImage) {
    try (KubernetesClient client = new DefaultKubernetesClient()) {
        Map<String, String> labels = new HashMap<>();
        labels.put("spawned_by", "pipeline-api");
        labels.put("pipeline_id", pipelineId);
        addWorkerLabel(clusterId, labels, run);
        LOGGER.debug("Root pipeline task ID: {}", run.getPodId());
        Map<String, String> nodeSelector = new HashMap<>();
        if (preferenceManager.getPreference(SystemPreferences.CLUSTER_ENABLE_AUTOSCALING)) {
            nodeSelector.put("runid", nodeIdLabel);
            // id pod ip == pipeline id we have a root pod, otherwise we prefer to skip pod in autoscaler
            if (run.getPodId().equals(pipelineId)) {
                labels.put("type", "pipeline");
            }
            labels.put("runid", nodeIdLabel);
        } else {
            nodeSelector.put("skill", "luigi");
        }
        labels.putAll(getServiceLabels(endpoints));
        OkHttpClient httpClient = HttpClientUtils.createHttpClient(client.getConfiguration());
        ObjectMeta metadata = getObjectMeta(run, labels);
        PodSpec spec = getPodSpec(run, envVars, secretName, nodeSelector, run.getDockerImage(), command, pullImage);
        Pod pod = new Pod("v1", "Pod", metadata, spec, null);
        Pod created = new PodOperationsImpl(httpClient, client.getConfiguration(), kubeNamespace).create(pod);
        LOGGER.debug("Created POD: {}", created.toString());
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) OkHttpClient(okhttp3.OkHttpClient) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl)

Example 15 with HttpClient

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

the class JolokiaFabricConnector method connect.

/**
 * connects to a fabric
 */
public void connect() {
    if (this.j4p != null || this.fabricServiceFacade != null) {
        disconnect();
    }
    this.j4p = J4pClient.url(this.url).user(this.userName).password(this.password).build();
    /* This needs further investigation...
        DefaultHttpClient httpClient = (DefaultHttpClient) j4p.getHttpClient();
        httpClient.setRedirectStrategy(new DefaultRedirectStrategy() {
            @Override
            public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
                return true;
            }
        });
        */
    this.fabricServiceFacade = new FabricServiceFacade(this);
    this.fabricMBeanFacade = new FabricMBean(this);
}
Also used : FabricMBean(io.fabric8.jolokia.facade.mbeans.FabricMBean) FabricServiceFacade(io.fabric8.jolokia.facade.facades.FabricServiceFacade)

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