Search in sources :

Example 1 with PodOperationsImpl

use of io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl in project kubernetes-client by fabric8io.

the class BaseOperationTest method testSimpleFieldQueryParamConcatenation.

@Test
void testSimpleFieldQueryParamConcatenation() {
    Map<String, String> fieldsMap = new HashMap<>();
    fieldsMap.put("yesKey1", "yesValue1");
    fieldsMap.put("yesKey2", "yesValue2");
    PodOperationsImpl operation = new PodOperationsImpl(new PodOperationContext(), new OperationContext());
    operation = (PodOperationsImpl) operation.withFields(fieldsMap).withField("yesKey2", "overrideValue2").withoutField("noKey1", "noValue1").withoutField("noKey2", "noValue2");
    final String fieldQueryParam = operation.getFieldQueryParam();
    // Use contains to not be depending on map key/value pair ordering
    assertThat(fieldQueryParam, containsString("yesKey1=yesValue1"));
    assertThat(fieldQueryParam, containsString("yesKey2=overrideValue2"));
    assertThat(fieldQueryParam, containsString("noKey1!=noValue1"));
    assertThat(fieldQueryParam, containsString("noKey2!=noValue2"));
}
Also used : OperationContext(io.fabric8.kubernetes.client.dsl.internal.OperationContext) PodOperationContext(io.fabric8.kubernetes.client.dsl.internal.PodOperationContext) HashMap(java.util.HashMap) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl) PodOperationContext(io.fabric8.kubernetes.client.dsl.internal.PodOperationContext) Test(org.junit.jupiter.api.Test)

Example 2 with PodOperationsImpl

use of io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl in project kubernetes-client by fabric8io.

the class BaseOperationTest method testFilterContextModification.

@Test
void testFilterContextModification() {
    PodOperationsImpl operation = new PodOperationsImpl(new PodOperationContext(), new OperationContext());
    operation.withField("x", "y");
    // should not modify the existing context
    assertTrue(Utils.isNullOrEmpty(operation.getFieldQueryParam()));
}
Also used : OperationContext(io.fabric8.kubernetes.client.dsl.internal.OperationContext) PodOperationContext(io.fabric8.kubernetes.client.dsl.internal.PodOperationContext) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl) PodOperationContext(io.fabric8.kubernetes.client.dsl.internal.PodOperationContext) Test(org.junit.jupiter.api.Test)

Example 3 with PodOperationsImpl

use of io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl in project kubernetes-client by fabric8io.

the class ServiceOperationsImpl method matchingPod.

private Pod matchingPod() {
    Service item = requireFromServer();
    Map<String, String> labels = item.getSpec().getSelector();
    PodList list = new PodOperationsImpl(context).inNamespace(item.getMetadata().getNamespace()).withLabels(labels).list();
    return list.getItems().stream().findFirst().orElseThrow(() -> new IllegalStateException("Could not find matching pod for service:" + item + "."));
}
Also used : PodList(io.fabric8.kubernetes.api.model.PodList) Service(io.fabric8.kubernetes.api.model.Service)

Example 4 with PodOperationsImpl

use of io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl in project syndesis by syndesisio.

the class KubernetesSupport method watchLog.

/*
     * Feeds the controller of the given podName to the callback handler for processing.
     *
     * We do this instead of using the watchLog() feature of the k8s client lib because it really sucks due to:
     *  1. You can't configure the timestamps option or the sinceTime option.  Need to resume log downloads.
     *  2. It seems to need extra threads..
     *  3. It might be hiding some of the http failure conditions.
     *
     */
protected void watchLog(String podName, Consumer<InputStream> handler, String sinceTime, Executor executor) throws IOException {
    try {
        PodOperationsImpl pod = (PodOperationsImpl) client.pods().withName(podName);
        List<Container> containers = pod.get().getSpec().getContainers();
        String containerFilter = getSpecificUserContainer(containers).map(n -> "&container=" + n).orElse("");
        StringBuilder url = new StringBuilder().append(pod.getResourceUrl().toString()).append("/log?pretty=false&follow=true&timestamps=true").append(containerFilter);
        if (sinceTime != null) {
            url.append("&sinceTime=").append(sinceTime);
        }
        String podLogUrl = url.toString();
        Thread.currentThread().setName("Logs Controller [running], request: " + podLogUrl);
        Request request = new Request.Builder().url(new URL(podLogUrl)).get().tag("log-watcher").build();
        OkHttpClient clone = okHttpClient.newBuilder().readTimeout(readTimeout).build();
        clone.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Call call, IOException e) {
                LOG.info("Failure occurred getting  controller for pod: {},", podName, e);
                handler.accept(null);
            }

            @Override
            public void onResponse(final Call call, final Response response) throws IOException {
                executor.execute(() -> {
                    Thread.currentThread().setName("Logs Controller [running], streaming: " + podLogUrl);
                    try {
                        if (response.code() == 200) {
                            handler.accept(response.body().byteStream());
                        } else {
                            LOG.info("Failure occurred while processing controller for pod: {}, http status: {}, details: {}", podName, response.code(), response.body().string());
                            handler.accept(null);
                        }
                    } catch (SocketTimeoutException timeout) {
                        LOG.warn("Timed out reading the log stream");
                        LOG.debug("Timed out reading the log stream", timeout);
                    } catch (IOException e) {
                        LOG.error("Unexpected Error", e);
                    } finally {
                        Thread.currentThread().setName(ActivityTrackingController.IDLE_THREAD_NAME);
                    }
                });
            }
        });
    } catch (RuntimeException t) {
        throw new IOException("Unexpected Error", t);
    } finally {
        Thread.currentThread().setName(ActivityTrackingController.IDLE_THREAD_NAME);
    }
}
Also used : Request(okhttp3.Request) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl) Logger(org.slf4j.Logger) Container(io.fabric8.kubernetes.api.model.Container) Executor(java.util.concurrent.Executor) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) Dispatcher(okhttp3.Dispatcher) Consumer(java.util.function.Consumer) List(java.util.List) OkHttpClient(okhttp3.OkHttpClient) SocketTimeoutException(java.net.SocketTimeoutException) Duration(java.time.Duration) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Response(okhttp3.Response) Optional(java.util.Optional) Call(okhttp3.Call) Callback(okhttp3.Callback) Collections(java.util.Collections) HttpClientUtils(io.fabric8.kubernetes.client.utils.HttpClientUtils) InputStream(java.io.InputStream) Call(okhttp3.Call) OkHttpClient(okhttp3.OkHttpClient) Request(okhttp3.Request) IOException(java.io.IOException) URL(java.net.URL) Response(okhttp3.Response) Container(io.fabric8.kubernetes.api.model.Container) Callback(okhttp3.Callback) SocketTimeoutException(java.net.SocketTimeoutException) PodOperationsImpl(io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl)

Example 5 with PodOperationsImpl

use of io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl 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)

Aggregations

Test (org.junit.jupiter.api.Test)6 OperationContext (io.fabric8.kubernetes.client.dsl.internal.OperationContext)5 PodOperationContext (io.fabric8.kubernetes.client.dsl.internal.PodOperationContext)5 PodOperationsImpl (io.fabric8.kubernetes.client.dsl.internal.core.v1.PodOperationsImpl)4 PodOperationsImpl (io.fabric8.kubernetes.client.dsl.internal.PodOperationsImpl)3 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)2 HttpClientUtils (io.fabric8.kubernetes.client.utils.HttpClientUtils)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 Optional (java.util.Optional)2 OkHttpClient (okhttp3.OkHttpClient)2 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)1 Container (io.fabric8.kubernetes.api.model.Container)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 ObjectMeta (io.fabric8.kubernetes.api.model.ObjectMeta)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodList (io.fabric8.kubernetes.api.model.PodList)1 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)1