Search in sources :

Example 1 with KubernetesException

use of com.marcnuri.yakc.api.KubernetesException in project yakc by manusa.

the class WipIT method namespaceWatchBlocksUntilOneIsAdded.

@Test
void namespaceWatchBlocksUntilOneIsAdded() throws KubernetesException {
    final AtomicInteger count = new AtomicInteger(0);
    final Disposable namespaceWatch = KC.create(CoreV1Api.class).listNamespace().watch().takeUntil(we -> we.getType() == Type.ADDED).subscribe(we -> count.incrementAndGet());
    assertThat(namespaceWatch.isDisposed()).isEqualTo(true);
    assertThat(count.get()).isEqualTo(1);
}
Also used : Disposable(io.reactivex.disposables.Disposable) APIResourceList(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.APIResourceList) LabelSelector(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.LabelSelector) ListPodForAllNamespaces(com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Container(com.marcnuri.yakc.model.io.k8s.api.core.v1.Container) PodSpec(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodSpec) ObjectMeta(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.ObjectMeta) DeleteOptions(com.marcnuri.yakc.model.io.k8s.apimachinery.pkg.apis.meta.v1.DeleteOptions) NotFoundException(com.marcnuri.yakc.api.NotFoundException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigMap(com.marcnuri.yakc.model.io.k8s.api.core.v1.ConfigMap) Schedulers(io.reactivex.schedulers.Schedulers) Type(com.marcnuri.yakc.api.WatchEvent.Type) KC(com.marcnuri.yakc.KubernetesClientExtension.KC) IOException(java.io.IOException) Pod(com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod) Test(org.junit.jupiter.api.Test) TimeUnit(java.util.concurrent.TimeUnit) CoreV1Api(com.marcnuri.yakc.api.core.v1.CoreV1Api) CountDownLatch(java.util.concurrent.CountDownLatch) Disposable(io.reactivex.disposables.Disposable) AppsV1Api(com.marcnuri.yakc.api.apps.v1.AppsV1Api) Deployment(com.marcnuri.yakc.model.io.k8s.api.apps.v1.Deployment) Stream(java.util.stream.Stream) PodList(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodList) PodTemplateSpec(com.marcnuri.yakc.model.io.k8s.api.core.v1.PodTemplateSpec) KubernetesException(com.marcnuri.yakc.api.KubernetesException) Collections(java.util.Collections) DeploymentSpec(com.marcnuri.yakc.model.io.k8s.api.apps.v1.DeploymentSpec) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 2 with KubernetesException

use of com.marcnuri.yakc.api.KubernetesException in project yakc by manusa.

the class ExecOnSubscribe method subscribe.

@Override
public void subscribe(ObservableEmitter<ExecMessage> emitter) throws Exception {
    emitter.setDisposable(this);
    final CountDownLatch cdl = new CountDownLatch(1);
    final HttpUrl updatedUrl = request.url().newBuilder().addQueryParameter("stdout", "true").addQueryParameter("stderr", "true").build();
    final Request updatedRequest = request.newBuilder().url(updatedUrl).build();
    webSocket.set(kubernetesClient.getOkHttpClient().newWebSocket(updatedRequest, new WebSocketListener() {

        @Override
        public void onMessage(WebSocket webSocket, String text) {
            emitter.onNext(ExecMessage.builder().standardStream(StandardStream.STDOUT).message(text).build());
        }

        @Override
        public void onMessage(WebSocket webSocket, ByteString bytes) {
            emitter.onNext(ExecMessage.builder().standardStream(StandardStream.fromByte(bytes.getByte(0))).message(bytes.substring(1).utf8()).build());
        }

        @Override
        public void onClosing(WebSocket webSocket, int code, String reason) {
            dispose();
        }

        @Override
        public void onClosed(WebSocket webSocket, int code, String reason) {
            emitter.onComplete();
            close();
        }

        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            final String message = Optional.ofNullable(response).map(Response::body).map(rb -> {
                try {
                    return rb.string();
                } catch (IOException ex) {
                    return null;
                }
            }).orElse(t.getMessage());
            emitter.onError(new KubernetesException(message, response));
            close();
        }

        private void close() {
            disposed.set(true);
            cdl.countDown();
        }
    }));
    cdl.await();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ObservableEmitter(io.reactivex.ObservableEmitter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KubernetesClient(com.marcnuri.yakc.KubernetesClient) WebSocket(okhttp3.WebSocket) WebSocketListener(okhttp3.WebSocketListener) IOException(java.io.IOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Disposable(io.reactivex.disposables.Disposable) ByteString(okio.ByteString) Response(okhttp3.Response) Optional(java.util.Optional) ExecMessage(com.marcnuri.yakc.api.ExecMessage) KubernetesException(com.marcnuri.yakc.api.KubernetesException) HttpUrl(okhttp3.HttpUrl) StandardStream(com.marcnuri.yakc.api.ExecMessage.StandardStream) WebSocketListener(okhttp3.WebSocketListener) ByteString(okio.ByteString) Request(okhttp3.Request) ByteString(okio.ByteString) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpUrl(okhttp3.HttpUrl) KubernetesException(com.marcnuri.yakc.api.KubernetesException) WebSocket(okhttp3.WebSocket)

Aggregations

KubernetesException (com.marcnuri.yakc.api.KubernetesException)2 Disposable (io.reactivex.disposables.Disposable)2 IOException (java.io.IOException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 KubernetesClient (com.marcnuri.yakc.KubernetesClient)1 KC (com.marcnuri.yakc.KubernetesClientExtension.KC)1 ExecMessage (com.marcnuri.yakc.api.ExecMessage)1 StandardStream (com.marcnuri.yakc.api.ExecMessage.StandardStream)1 NotFoundException (com.marcnuri.yakc.api.NotFoundException)1 Type (com.marcnuri.yakc.api.WatchEvent.Type)1 AppsV1Api (com.marcnuri.yakc.api.apps.v1.AppsV1Api)1 CoreV1Api (com.marcnuri.yakc.api.core.v1.CoreV1Api)1 ListPodForAllNamespaces (com.marcnuri.yakc.api.core.v1.CoreV1Api.ListPodForAllNamespaces)1 Deployment (com.marcnuri.yakc.model.io.k8s.api.apps.v1.Deployment)1 DeploymentSpec (com.marcnuri.yakc.model.io.k8s.api.apps.v1.DeploymentSpec)1 ConfigMap (com.marcnuri.yakc.model.io.k8s.api.core.v1.ConfigMap)1 Container (com.marcnuri.yakc.model.io.k8s.api.core.v1.Container)1 Pod (com.marcnuri.yakc.model.io.k8s.api.core.v1.Pod)1 PodList (com.marcnuri.yakc.model.io.k8s.api.core.v1.PodList)1 PodSpec (com.marcnuri.yakc.model.io.k8s.api.core.v1.PodSpec)1