Search in sources :

Example 11 with Watch

use of io.fabric8.kubernetes.client.Watch in project strimzi by strimzi.

the class StatefulSetOperator method restartPod.

private Future<Void> restartPod(String namespace, String name, Predicate<String> isReady, String podName) {
    Future<Void> result = Future.future();
    log.info("Roll {}/{}: Rolling pod {}", namespace, name, podName);
    Future<Void> deleted = Future.future();
    Future<CompositeFuture> deleteFinished = Future.future();
    Watcher<Pod> watcher = new RollingUpdateWatcher(deleted);
    Watch watch = podOperations.watch(namespace, podName, watcher);
    // Delete the pod
    log.debug("Roll {}/{}: Waiting for pod {} to be deleted", namespace, name, podName);
    Future podReconcileFuture = podOperations.reconcile(namespace, podName, null);
    CompositeFuture.join(podReconcileFuture, deleted).setHandler(deleteResult -> {
        watch.close();
        if (deleteResult.succeeded()) {
            log.debug("Roll {}/{}: Pod {} was deleted", namespace, name, podName);
        }
        deleteFinished.handle(deleteResult);
    });
    deleteFinished.compose(ix -> {
        log.debug("Roll {}/{}: Waiting for new pod {} to get ready", namespace, name, podName);
        Future<Void> readyFuture = Future.future();
        vertx.setPeriodic(1_000, timerId -> {
            p(isReady, podName).setHandler(x -> {
                if (x.succeeded()) {
                    if (x.result()) {
                        vertx.cancelTimer(timerId);
                        readyFuture.complete();
                    }
                // else not ready
                } else {
                    vertx.cancelTimer(timerId);
                    readyFuture.fail(x.cause());
                }
            });
        });
        return readyFuture;
    }).setHandler(result);
    return result;
}
Also used : CompositeFuture(io.vertx.core.CompositeFuture) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) Logger(org.slf4j.Logger) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) Predicate(java.util.function.Predicate) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) Pod(io.fabric8.kubernetes.api.model.Pod) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) Future(io.vertx.core.Future) ArrayList(java.util.ArrayList) CompositeFuture(io.vertx.core.CompositeFuture) List(java.util.List) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Pod(io.fabric8.kubernetes.api.model.Pod) Watch(io.fabric8.kubernetes.client.Watch) Future(io.vertx.core.Future) CompositeFuture(io.vertx.core.CompositeFuture)

Example 12 with Watch

use of io.fabric8.kubernetes.client.Watch in project strimzi by strimzi.

the class ClusterControllerTest method startStop.

/**
 * Does the CC start and then stop a verticle per namespace?
 * @param context
 * @param namespaces
 */
private void startStop(TestContext context, String namespaces) {
    AtomicInteger numWatchers = new AtomicInteger(0);
    KubernetesClient client = mock(KubernetesClient.class);
    MixedOperation mockCms = mock(MixedOperation.class);
    when(client.configMaps()).thenReturn(mockCms);
    List<String> namespaceList = asList(namespaces.split(" *,+ *"));
    for (String namespace : namespaceList) {
        MixedOperation mockNamespacedCms = mock(MixedOperation.class);
        when(mockNamespacedCms.watch(any())).thenAnswer(invo -> {
            numWatchers.incrementAndGet();
            Watch mockWatch = mock(Watch.class);
            doAnswer(invo2 -> {
                ((Watcher) invo.getArgument(0)).onClose(null);
                return null;
            }).when(mockWatch).close();
            return mockWatch;
        });
        when(mockNamespacedCms.withLabels(any())).thenReturn(mockNamespacedCms);
        when(mockCms.inNamespace(namespace)).thenReturn(mockNamespacedCms);
    }
    Async async = context.async();
    Map<String, String> env = new HashMap<>();
    env.put(ClusterControllerConfig.STRIMZI_NAMESPACE, namespaces);
    env.put(ClusterControllerConfig.STRIMZI_CONFIGMAP_LABELS, STRIMZI_IO_KIND_CLUSTER);
    env.put(ClusterControllerConfig.STRIMZI_FULL_RECONCILIATION_INTERVAL_MS, "120000");
    Main.run(vertx, client, true, env).setHandler(ar -> {
        context.assertNull(ar.cause(), "Expected all verticles to start OK");
        async.complete();
    });
    async.await();
    context.assertEquals(namespaceList.size(), vertx.deploymentIDs().size(), "A verticle per namespace");
    List<Async> asyncs = new ArrayList<>();
    for (String deploymentId : vertx.deploymentIDs()) {
        Async async2 = context.async();
        asyncs.add(async2);
        vertx.undeploy(deploymentId, ar -> {
            context.assertNull(ar.cause(), "Didn't expect error when undeploying verticle " + deploymentId);
            async2.complete();
        });
    }
    for (Async async2 : asyncs) {
        async2.await();
    }
    if (numWatchers.get() > namespaceList.size()) {
        context.fail("Looks like there were more watchers than namespaces");
    }
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashMap(java.util.HashMap) Async(io.vertx.ext.unit.Async) Watch(io.fabric8.kubernetes.client.Watch) ArrayList(java.util.ArrayList) Watcher(io.fabric8.kubernetes.client.Watcher) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation)

Example 13 with Watch

use of io.fabric8.kubernetes.client.Watch in project strimzi by strimzi.

the class MockKube method buildStatefulSets.

private MixedOperation<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>> buildStatefulSets(MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods) {
    return new AbstractMockBuilder<StatefulSet, StatefulSetList, DoneableStatefulSet, RollableScalableResource<StatefulSet, DoneableStatefulSet>>(StatefulSet.class, StatefulSetList.class, DoneableStatefulSet.class, castClass(RollableScalableResource.class), ssDb) {

        @Override
        protected void nameScopedMocks(RollableScalableResource<StatefulSet, DoneableStatefulSet> resource, String resourceName) {
            mockGet(resourceName, resource);
            // mockCreate("endpoint", endpointDb, resourceName, resource);
            mockCascading(resource);
            mockPatch(resourceName, resource);
            mockDelete(resourceName, resource);
            mockIsReady(resourceName, resource);
            when(resource.create(any())).thenAnswer(cinvocation -> {
                checkNotExists(resourceName);
                StatefulSet argument = cinvocation.getArgument(0);
                LOGGER.debug("create {} {} -> {}", resourceType, resourceName, argument);
                ssDb.put(resourceName, copyResource(argument));
                for (int i = 0; i < argument.getSpec().getReplicas(); i++) {
                    String podName = argument.getMetadata().getName() + "-" + i;
                    podDb.put(podName, new PodBuilder().withNewMetadata().withNamespace(argument.getMetadata().getNamespace()).withName(podName).endMetadata().build());
                }
                return argument;
            });
            EditReplacePatchDeletable<StatefulSet, StatefulSet, DoneableStatefulSet, Boolean> c = mock(EditReplacePatchDeletable.class);
            when(resource.cascading(false)).thenReturn(c);
            when(c.patch(any())).thenAnswer(patchInvocation -> {
                StatefulSet argument = patchInvocation.getArgument(0);
                return doPatch(resourceName, argument);
            });
            when(resource.scale(anyInt(), anyBoolean())).thenAnswer(invocation -> {
                checkDoesExist(resourceName);
                StatefulSet ss = copyResource(ssDb.get(resourceName));
                int newScale = invocation.getArgument(0);
                ss.getSpec().setReplicas(newScale);
                return doPatch(resourceName, ss);
            });
            when(resource.scale(anyInt())).thenAnswer(invocation -> {
                checkDoesExist(resourceName);
                StatefulSet ss = copyResource(ssDb.get(resourceName));
                int newScale = invocation.getArgument(0);
                ss.getSpec().setReplicas(newScale);
                return doPatch(resourceName, ss);
            });
            when(resource.isReady()).thenAnswer(i -> {
                LOGGER.debug("{} {} is ready", resourceType, resourceName);
                return true;
            });
            mockPods.inNamespace(any()).withName(any()).watch(new Watcher<Pod>() {

                @Override
                public void eventReceived(Action action, Pod resource) {
                    if (action == Action.DELETED) {
                        String podName = resource.getMetadata().getName();
                        String podNamespace = resource.getMetadata().getNamespace();
                        StatefulSet statefulSet = ssDb.get(resourceName);
                        if (podName.startsWith(resourceName + "-") && Integer.parseInt(podName.substring(podName.lastIndexOf("-") + 1)) < statefulSet.getSpec().getReplicas()) {
                            mockPods.inNamespace(podNamespace).withName(podName).create(resource);
                        }
                    }
                }

                @Override
                public void onClose(KubernetesClientException e) {
                }
            });
        }

        private StatefulSet doPatch(String resourceName, StatefulSet argument) {
            int oldScale = ssDb.get(resourceName).getSpec().getReplicas();
            int newScale = argument.getSpec().getReplicas();
            if (newScale > oldScale) {
                LOGGER.debug("scaling up {} {} from {} to {}", resourceType, resourceName, oldScale, newScale);
                Pod examplePod = mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(argument.getMetadata().getName() + "-0").get();
                for (int i = oldScale; i < newScale; i++) {
                    String newPodName = argument.getMetadata().getName() + "-" + i;
                    mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(newPodName).create(new PodBuilder(examplePod).editMetadata().withName(newPodName).endMetadata().build());
                }
                ssDb.put(resourceName, copyResource(argument));
            } else if (newScale < oldScale) {
                ssDb.put(resourceName, copyResource(argument));
                LOGGER.debug("scaling down {} {} from {} to {}", resourceType, resourceName, oldScale, newScale);
                for (int i = oldScale - 1; i >= newScale; i--) {
                    String newPodName = argument.getMetadata().getName() + "-" + i;
                    mockPods.inNamespace(argument.getMetadata().getNamespace()).withName(newPodName).delete();
                }
            } else {
                ssDb.put(resourceName, copyResource(argument));
            }
            return argument;
        }
    }.build();
}
Also used : DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) KubernetesResourceList(io.fabric8.kubernetes.api.model.KubernetesResourceList) Doneable(io.fabric8.kubernetes.api.model.Doneable) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) LoggerFactory(org.slf4j.LoggerFactory) Watcher(io.fabric8.kubernetes.client.Watcher) DoneablePersistentVolumeClaim(io.fabric8.kubernetes.api.model.DoneablePersistentVolumeClaim) Resource(io.fabric8.kubernetes.client.dsl.Resource) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) Map(java.util.Map) Mockito.doAnswer(org.mockito.Mockito.doAnswer) PersistentVolumeClaimList(io.fabric8.kubernetes.api.model.PersistentVolumeClaimList) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) ExtensionsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.ExtensionsAPIGroupDSL) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Set(java.util.Set) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) DoneableDeployment(io.fabric8.kubernetes.api.model.extensions.DoneableDeployment) DoneableEndpoints(io.fabric8.kubernetes.api.model.DoneableEndpoints) PersistentVolumeClaim(io.fabric8.kubernetes.api.model.PersistentVolumeClaim) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) DeploymentList(io.fabric8.kubernetes.api.model.extensions.DeploymentList) EndpointsList(io.fabric8.kubernetes.api.model.EndpointsList) Watch(io.fabric8.kubernetes.client.Watch) HashMap(java.util.HashMap) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) ScalableResource(io.fabric8.kubernetes.client.dsl.ScalableResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) Service(io.fabric8.kubernetes.api.model.Service) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) OngoingStubbing(org.mockito.stubbing.OngoingStubbing) ConfigMapList(io.fabric8.kubernetes.api.model.ConfigMapList) Pod(io.fabric8.kubernetes.api.model.Pod) Mockito.when(org.mockito.Mockito.when) DoneableConfigMap(io.fabric8.kubernetes.api.model.DoneableConfigMap) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) PodList(io.fabric8.kubernetes.api.model.PodList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) AppsAPIGroupDSL(io.fabric8.kubernetes.client.dsl.AppsAPIGroupDSL) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) EditReplacePatchDeletable(io.fabric8.kubernetes.client.dsl.EditReplacePatchDeletable) Watcher(io.fabric8.kubernetes.client.Watcher) RollableScalableResource(io.fabric8.kubernetes.client.dsl.RollableScalableResource) StatefulSetList(io.fabric8.kubernetes.api.model.extensions.StatefulSetList) DoneableStatefulSet(io.fabric8.kubernetes.api.model.extensions.DoneableStatefulSet) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 14 with Watch

use of io.fabric8.kubernetes.client.Watch in project strimzi by strimzi.

the class ConfigMapWatcher method eventReceived.

public void eventReceived(Action action, ConfigMap configMap) {
    ObjectMeta metadata = configMap.getMetadata();
    Map<String, String> labels = metadata.getLabels();
    if (cmPredicate.test(configMap)) {
        String name = metadata.getName();
        LOGGER.info("ConfigMap watch received event {} on map {} with labels {}", action, name, labels);
        Handler<AsyncResult<Void>> resultHandler = ar -> {
            if (ar.succeeded()) {
                LOGGER.info("Success processing ConfigMap watch event {} on map {} with labels {}", action, name, labels);
            } else {
                String message;
                if (ar.cause() instanceof InvalidConfigMapException) {
                    message = "ConfigMap " + name + " has an invalid 'data' section: " + ar.cause().getMessage();
                    LOGGER.error("{}", message);
                } else {
                    message = "Failure processing ConfigMap watch event " + action + " on map " + name + " with labels " + labels + ": " + ar.cause().getMessage();
                    LOGGER.error("{}", message, ar.cause());
                }
                controller.enqueue(controller.new Event(configMap, message, Controller.EventType.WARNING, errorResult -> {
                }));
            }
        };
        switch(action) {
            case ADDED:
                controller.onConfigMapAdded(configMap, resultHandler);
                break;
            case MODIFIED:
                controller.onConfigMapModified(configMap, resultHandler);
                break;
            case DELETED:
                controller.onConfigMapDeleted(configMap, resultHandler);
                break;
            case ERROR:
                LOGGER.error("Watch received action=ERROR for ConfigMap " + name);
        }
    }
}
Also used : KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Logger(org.slf4j.Logger) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Map(java.util.Map) LoggerFactory(org.slf4j.LoggerFactory) Watcher(io.fabric8.kubernetes.client.Watcher) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) AsyncResult(io.vertx.core.AsyncResult)

Example 15 with Watch

use of io.fabric8.kubernetes.client.Watch in project fabric8 by fabric8io.

the class WatchBuildsExample method main.

public static void main(String... args) throws Exception {
    OpenShiftClient client = new DefaultOpenShiftClient();
    client.builds().watch(new Watcher<Build>() {

        @Override
        public void eventReceived(Action action, Build build) {
            System.out.println(action + ": " + build);
        }

        @Override
        public void onClose(KubernetesClientException e) {
            System.out.println("Closed: " + e);
        }
    });
    client.close();
}
Also used : Build(io.fabric8.openshift.api.model.Build) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) DefaultOpenShiftClient(io.fabric8.openshift.client.DefaultOpenShiftClient) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Aggregations

KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)13 Watch (io.fabric8.kubernetes.client.Watch)12 Pod (io.fabric8.kubernetes.api.model.Pod)10 Watcher (io.fabric8.kubernetes.client.Watcher)10 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)8 PodList (io.fabric8.kubernetes.api.model.PodList)7 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)6 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)6 ArrayList (java.util.ArrayList)5 Test (org.junit.Test)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 Logger (io.fabric8.maven.docker.util.Logger)4 IOException (java.io.IOException)4 DoneablePod (io.fabric8.kubernetes.api.model.DoneablePod)3 MixedOperation (io.fabric8.kubernetes.client.dsl.MixedOperation)3 Build (io.fabric8.openshift.api.model.Build)3 Map (java.util.Map)3 ConfigMapList (io.fabric8.kubernetes.api.model.ConfigMapList)2