Search in sources :

Example 81 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testThreadsOnTimeout.

@Test
public void testThreadsOnTimeout() throws Exception {
    curator.close();
    SocketProxy socketProxy = new SocketProxy(new URI("tcp://localhost:" + zkPort));
    final CuratorFramework proxyCurator = CuratorFrameworkFactory.builder().connectString("localhost:" + socketProxy.getUrl().getPort()).sessionTimeoutMs(5000).connectionTimeoutMs(3000).retryPolicy(new RetryNTimes(10, 1000)).build();
    proxyCurator.start();
    proxyCurator.getZookeeperClient().blockUntilConnectedOrTimedOut();
    LOG.info("curator is go: " + proxyCurator);
    final String path = "/singletons/test/threads" + System.currentTimeMillis() + "**";
    final ArrayList<Runnable> members = new ArrayList<Runnable>();
    final int nThreads = 1;
    class GroupRunnable implements Runnable, GroupListener<NodeState> {

        final int id;

        private final BlockingQueue<Integer> jobQueue = new LinkedBlockingDeque<Integer>();

        ZooKeeperGroup<NodeState> group;

        NodeState nodeState;

        public GroupRunnable(int id) {
            this.id = id;
            members.add(this);
            nodeState = new NodeState("foo" + id);
        }

        @Override
        public void run() {
            group = new ZooKeeperGroup<NodeState>(proxyCurator, path, NodeState.class);
            group.add(this);
            LOG.info("run: Added: " + this);
            try {
                while (true) {
                    switch(jobQueue.take()) {
                        case 0:
                            LOG.info("run: close: " + this);
                            try {
                                group.close();
                            } catch (IOException ignored) {
                            }
                            return;
                        case 1:
                            LOG.info("run: start: " + this);
                            group.start();
                            group.update(nodeState);
                            break;
                        case 2:
                            LOG.info("run: update: " + this);
                            nodeState.setId(nodeState.getId() + id);
                            group.update(nodeState);
                            break;
                    }
                }
            } catch (InterruptedException exit) {
            }
        }

        @Override
        public void groupEvent(Group<NodeState> group, GroupEvent event) {
        }
    }
    ;
    ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
    for (int i = 0; i < nThreads; i++) {
        executorService.execute(new GroupRunnable(i));
    }
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(1);
        // wait for registration
        while (groupRunnable.group == null || groupRunnable.group.getId() == null) {
            TimeUnit.MILLISECONDS.sleep(100);
        }
    }
    boolean firsStartedIsMaster = ((GroupRunnable) members.get(0)).group.isMaster();
    assertTrue("first started is master", firsStartedIsMaster);
    LOG.info("got master...");
    // lets see how long they take to notice a no responses to heart beats
    socketProxy.pause();
    // splash in an update
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(2);
    }
    boolean hasMaster = true;
    while (hasMaster) {
        for (Runnable r : members) {
            GroupRunnable groupRunnable = (GroupRunnable) r;
            hasMaster &= groupRunnable.group.isMaster();
        }
        if (hasMaster) {
            LOG.info("Waiting for no master state on proxy pause");
            TimeUnit.SECONDS.sleep(1);
        }
    }
    for (Runnable r : members) {
        GroupRunnable groupRunnable = (GroupRunnable) r;
        groupRunnable.jobQueue.offer(0);
    }
    executorService.shutdown();
    // at a min when the session has expired
    boolean allThreadComplete = executorService.awaitTermination(6, TimeUnit.SECONDS);
    proxyCurator.close();
    socketProxy.close();
    assertTrue("all threads complete", allThreadComplete);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) BlockingQueue(java.util.concurrent.BlockingQueue) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Group(io.fabric8.groups.Group) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) NodeState(io.fabric8.groups.NodeState) GroupListener(io.fabric8.groups.GroupListener) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SocketProxy(org.apache.activemq.util.SocketProxy) URI(java.net.URI) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExecutorService(java.util.concurrent.ExecutorService) ZooKeeperGroup(io.fabric8.groups.internal.ZooKeeperGroup) Test(org.junit.Test)

Example 82 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project strimzi by strimzi.

the class Main method main.

public static void main(String[] args) {
    Vertx vertx = Vertx.vertx();
    KubernetesClient client = new DefaultKubernetesClient();
    isOnOpenShift(vertx, client).setHandler(os -> {
        if (os.succeeded()) {
            run(vertx, client, os.result().booleanValue(), System.getenv()).setHandler(ar -> {
                if (ar.failed()) {
                    log.error("Unable to start controller for 1 or more namespace", ar.cause());
                    System.exit(1);
                }
            });
        } else {
            log.error("Failed to distinguish between Kubernetes and OpenShift", os.cause());
            System.exit(1);
        }
    });
}
Also used : DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) Vertx(io.vertx.core.Vertx)

Example 83 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project strimzi by strimzi.

the class ClusterController method createConfigMapWatch.

private void createConfigMapWatch(Handler<AsyncResult<Watch>> handler) {
    getVertx().executeBlocking(future -> {
        Watch watch = client.configMaps().inNamespace(namespace).withLabels(selector.toMap()).watch(new Watcher<ConfigMap>() {

            @Override
            public void eventReceived(Action action, ConfigMap cm) {
                Labels labels = Labels.fromResource(cm);
                AssemblyType type;
                try {
                    type = labels.type();
                } catch (IllegalArgumentException e) {
                    log.warn("Unknown {} label {} received in Config Map {} in namespace {}", Labels.STRIMZI_TYPE_LABEL, cm.getMetadata().getLabels().get(Labels.STRIMZI_TYPE_LABEL), cm.getMetadata().getName(), namespace);
                    return;
                }
                final AbstractAssemblyOperator cluster;
                if (type == null) {
                    log.warn("Missing label {} in Config Map {} in namespace {}", Labels.STRIMZI_TYPE_LABEL, cm.getMetadata().getName(), namespace);
                    return;
                } else {
                    switch(type) {
                        case KAFKA:
                            cluster = kafkaAssemblyOperator;
                            break;
                        case CONNECT:
                            cluster = kafkaConnectAssemblyOperator;
                            break;
                        case CONNECT_S2I:
                            cluster = kafkaConnectS2IAssemblyOperator;
                            break;
                        default:
                            return;
                    }
                }
                String name = cm.getMetadata().getName();
                switch(action) {
                    case ADDED:
                    case DELETED:
                    case MODIFIED:
                        Reconciliation reconciliation = new Reconciliation("watch", type, namespace, name);
                        log.info("{}: ConfigMap {} in namespace {} was {}", reconciliation, name, namespace, action);
                        cluster.reconcileAssembly(reconciliation, result -> {
                            if (result.succeeded()) {
                                log.info("{}: assembly reconciled", reconciliation);
                            } else {
                                log.error("{}: Failed to reconcile", reconciliation);
                            }
                        });
                        break;
                    case ERROR:
                        log.error("Failed ConfigMap {} in namespace{} ", name, namespace);
                        reconcileAll("watch error");
                        break;
                    default:
                        log.error("Unknown action: {} in namespace {}", name, namespace);
                        reconcileAll("watch unknown");
                }
            }

            @Override
            public void onClose(KubernetesClientException e) {
                if (e != null) {
                    log.error("Watcher closed with exception in namespace {}", namespace, e);
                } else {
                    log.info("Watcher closed in namespace {}", namespace);
                }
                recreateConfigMapWatch();
            }
        });
        future.complete(watch);
    }, res -> {
        if (res.succeeded()) {
            log.info("ConfigMap watcher running for labels {}", selector);
            handler.handle(Future.succeededFuture((Watch) res.result()));
        } else {
            log.info("ConfigMap watcher failed to start", res.cause());
            handler.handle(Future.failedFuture("ConfigMap watcher failed to start"));
        }
    });
}
Also used : KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) AssemblyType(io.strimzi.controller.cluster.model.AssemblyType) KafkaAssemblyOperator(io.strimzi.controller.cluster.operator.assembly.KafkaAssemblyOperator) Logger(org.slf4j.Logger) LoggerFactory(org.slf4j.LoggerFactory) Watcher(io.fabric8.kubernetes.client.Watcher) Watch(io.fabric8.kubernetes.client.Watch) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Labels(io.strimzi.controller.cluster.model.Labels) Future(io.vertx.core.Future) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) TimeUnit(java.util.concurrent.TimeUnit) AbstractVerticle(io.vertx.core.AbstractVerticle) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) AbstractAssemblyOperator(io.strimzi.controller.cluster.operator.assembly.AbstractAssemblyOperator) KafkaConnectS2IAssemblyOperator(io.strimzi.controller.cluster.operator.assembly.KafkaConnectS2IAssemblyOperator) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) KafkaConnectAssemblyOperator(io.strimzi.controller.cluster.operator.assembly.KafkaConnectAssemblyOperator) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Labels(io.strimzi.controller.cluster.model.Labels) AbstractAssemblyOperator(io.strimzi.controller.cluster.operator.assembly.AbstractAssemblyOperator) AssemblyType(io.strimzi.controller.cluster.model.AssemblyType) Watch(io.fabric8.kubernetes.client.Watch) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 84 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project strimzi by strimzi.

the class ControllerTest method testOnConfigMapAdded_invalidCm.

/**
 * Test what happens when a non-topic config map gets created in kubernetes
 */
@Test
public void testOnConfigMapAdded_invalidCm(TestContext context) {
    Map<String, String> data = map(TopicSerialization.CM_KEY_REPLICAS, "1", TopicSerialization.CM_KEY_PARTITIONS, "1", TopicSerialization.CM_KEY_CONFIG, "{null:null}");
    ConfigMap cm = new ConfigMapBuilder().withNewMetadata().withName("invalid").withLabels(cmPredicate.labels()).endMetadata().withData(data).build();
    Async async = context.async();
    controller.onConfigMapAdded(cm, ar -> {
        assertFailed(context, ar);
        context.assertTrue(ar.cause() instanceof InvalidConfigMapException);
        context.assertEquals("ConfigMap's 'data' section has invalid key 'config': Unexpected character ('n' (code 110)): was expecting double-quote to start field name\n" + " at [Source: 'config' key of 'data' section of ConfigMap 'invalid' in namespace 'null'; line: 1, column: 3]", ar.cause().getMessage());
        mockKafka.assertEmpty(context);
        mockTopicStore.assertEmpty(context);
        async.complete();
    });
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 85 with Start

use of io.fabric8.arquillian.kubernetes.event.Start in project fabric8-maven-plugin by fabric8io.

the class ApplyMojo method applyEntities.

protected void applyEntities(Controller controller, KubernetesClient kubernetes, String namespace, String fileName, Set<HasMetadata> entities) throws Exception {
    // Apply all items
    for (HasMetadata entity : entities) {
        if (entity instanceof Pod) {
            Pod pod = (Pod) entity;
            controller.applyPod(pod, fileName);
        } else if (entity instanceof Service) {
            Service service = (Service) entity;
            controller.applyService(service, fileName);
        } else if (entity instanceof ReplicationController) {
            ReplicationController replicationController = (ReplicationController) entity;
            controller.applyReplicationController(replicationController, fileName);
        } else if (entity != null) {
            controller.apply(entity, fileName);
        }
    }
    String command = clusterAccess.isOpenShiftImageStream(log) ? "oc" : "kubectl";
    log.info("[[B]]HINT:[[B]] Use the command `%s get pods -w` to watch your pods start up", command);
    Logger serviceLogger = createExternalProcessLogger("[[G]][SVC][[G]] ");
    long serviceUrlWaitTimeSeconds = this.serviceUrlWaitTimeSeconds;
    for (HasMetadata entity : entities) {
        if (entity instanceof Service) {
            Service service = (Service) entity;
            String name = getName(service);
            Resource<Service, DoneableService> serviceResource = kubernetes.services().inNamespace(namespace).withName(name);
            String url = null;
            // lets wait a little while until there is a service URL in case the exposecontroller is running slow
            for (int i = 0; i < serviceUrlWaitTimeSeconds; i++) {
                if (i > 0) {
                    Thread.sleep(1000);
                }
                Service s = serviceResource.get();
                if (s != null) {
                    url = getExternalServiceURL(s);
                    if (Strings.isNotBlank(url)) {
                        break;
                    }
                }
                if (!isExposeService(service)) {
                    break;
                }
            }
            // lets not wait for other services
            serviceUrlWaitTimeSeconds = 1;
            if (Strings.isNotBlank(url) && url.startsWith("http")) {
                serviceLogger.info("" + name + ": " + url);
            }
        }
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Pod(io.fabric8.kubernetes.api.model.Pod) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) KubernetesHelper.createIntOrString(io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString) Logger(io.fabric8.maven.docker.util.Logger)

Aggregations

IOException (java.io.IOException)27 Test (org.junit.Test)25 File (java.io.File)18 HashMap (java.util.HashMap)16 Map (java.util.Map)10 Git (org.eclipse.jgit.api.Git)10 ArrayList (java.util.ArrayList)9 GitPatchRepository (io.fabric8.patch.management.impl.GitPatchRepository)8 Bundle (org.osgi.framework.Bundle)8 PatchException (io.fabric8.patch.management.PatchException)7 URISyntaxException (java.net.URISyntaxException)7 BundleException (org.osgi.framework.BundleException)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 CuratorFramework (org.apache.curator.framework.CuratorFramework)6 Container (io.fabric8.api.Container)5 CreateContainerMetadata (io.fabric8.api.CreateContainerMetadata)5 FabricService (io.fabric8.api.FabricService)5 GitPatchManagementServiceImpl (io.fabric8.patch.management.impl.GitPatchManagementServiceImpl)5 HashSet (java.util.HashSet)5 ObjectId (org.eclipse.jgit.lib.ObjectId)5