Search in sources :

Example 21 with Watch

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

the class AbstractAssemblyOperator method reconcileAll.

/**
 * Reconcile assembly resources in the given namespace having the given selector.
 * Reconciliation works by getting the assembly ConfigMaps in the given namespace with the given selector and
 * comparing with the corresponding {@linkplain #getResources(String) resource}.
 * <ul>
 * <li>An assembly will be {@linkplain #createOrUpdate(Reconciliation, ConfigMap, Handler) created} for all ConfigMaps without same-named resources</li>
 * <li>An assembly will be {@linkplain #delete(Reconciliation, Handler) deleted} for all resources without same-named ConfigMaps</li>
 * </ul>
 *
 * @param trigger A description of the triggering event (timer or watch), used for logging
 * @param namespace The namespace
 * @param selector The selector
 */
public final CountDownLatch reconcileAll(String trigger, String namespace, Labels selector) {
    Labels selectorWithCluster = selector.withType(assemblyType);
    // get ConfigMaps with kind=cluster&type=kafka (or connect, or connect-s2i) for the corresponding cluster type
    List<ConfigMap> cms = configMapOperations.list(namespace, selectorWithCluster);
    Set<String> cmsNames = cms.stream().map(cm -> cm.getMetadata().getName()).collect(Collectors.toSet());
    log.debug("reconcileAll({}, {}): ConfigMaps with labels {}: {}", assemblyType, trigger, selectorWithCluster, cmsNames);
    // get resources with kind=cluster&type=kafka (or connect, or connect-s2i)
    List<? extends HasMetadata> resources = getResources(namespace);
    // now extract the cluster name from those
    Set<String> resourceNames = resources.stream().filter(// exclude Cluster CM, which won't have a cluster label
    r -> Labels.kind(r) == null).map(Labels::cluster).collect(Collectors.toSet());
    log.debug("reconcileAll({}, {}): Other resources with labels {}: {}", assemblyType, trigger, selectorWithCluster, resourceNames);
    cmsNames.addAll(resourceNames);
    // We use a latch so that callers (specifically, test callers) know when the reconciliation is complete
    // Using futures would be more complex for no benefit
    CountDownLatch latch = new CountDownLatch(cmsNames.size());
    for (String name : cmsNames) {
        Reconciliation reconciliation = new Reconciliation(trigger, assemblyType, namespace, name);
        reconcileAssembly(reconciliation, result -> {
            if (result.succeeded()) {
                log.info("{}: Assembly reconciled", reconciliation);
            } else {
                log.error("{}: Failed to reconcile", reconciliation);
            }
            latch.countDown();
        });
    }
    return latch;
}
Also used : AssemblyType(io.strimzi.controller.cluster.model.AssemblyType) Logger(org.slf4j.Logger) Vertx(io.vertx.core.Vertx) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) ConfigMapOperator(io.strimzi.controller.cluster.operator.resource.ConfigMapOperator) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Labels(io.strimzi.controller.cluster.model.Labels) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Lock(io.vertx.core.shareddata.Lock) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) Reconciliation(io.strimzi.controller.cluster.Reconciliation) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) Reconciliation(io.strimzi.controller.cluster.Reconciliation) Labels(io.strimzi.controller.cluster.model.Labels) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 22 with Watch

use of io.fabric8.kubernetes.client.Watch 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 23 with Watch

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

the class ControllerIT method testCreateTwoConfigMapsManagingOneTopic.

@Test
public void testCreateTwoConfigMapsManagingOneTopic(TestContext context) {
    String topicName = "two-cms-one-topic";
    Topic topic = new Topic.Builder(topicName, 1, (short) 1, emptyMap()).build();
    ConfigMap cm = TopicSerialization.toConfigMap(topic, cmPredicate);
    ConfigMap cm2 = new ConfigMapBuilder(cm).editMetadata().withName(topicName + "-1").endMetadata().build();
    // create one
    createCm(context, cm2);
    // create another
    kubeClient.configMaps().inNamespace(NAMESPACE).create(cm);
    waitForEvent(context, cm, "Failure processing ConfigMap watch event ADDED on map two-cms-one-topic with labels {strimzi.io/kind=topic}: " + "Topic 'two-cms-one-topic' is already managed via ConfigMap 'two-cms-one-topic-1' it cannot also be managed via the ConfiMap 'two-cms-one-topic'", Controller.EventType.WARNING);
}
Also used : ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test)

Example 24 with Watch

use of io.fabric8.kubernetes.client.Watch 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)

Example 25 with Watch

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

the class ResourceMojo method lateInit.

private void lateInit() throws MojoExecutionException {
    if (goalFinder.runningWithGoal(project, session, "fabric8:watch") || goalFinder.runningWithGoal(project, session, "fabric8:watch")) {
        Properties properties = project.getProperties();
        properties.setProperty("fabric8.watch", "true");
    }
    platformMode = clusterAccess.resolvePlatformMode(mode, log);
    log.info("Running in [[B]]%s[[B]] mode", platformMode.getLabel());
    if (isOpenShiftMode()) {
        Properties properties = project.getProperties();
        if (!properties.contains(DOCKER_IMAGE_USER)) {
            String namespace = clusterAccess.getNamespace();
            log.info("Using docker image name of namespace: " + namespace);
            properties.setProperty(DOCKER_IMAGE_USER, namespace);
        }
        if (!properties.contains(PlatformMode.FABRIC8_EFFECTIVE_PLATFORM_MODE)) {
            properties.setProperty(PlatformMode.FABRIC8_EFFECTIVE_PLATFORM_MODE, platformMode.toString());
        }
    }
    openShiftConverters = new HashMap<>();
    openShiftConverters.put("ReplicaSet", new ReplicSetOpenShiftConverter());
    openShiftConverters.put("Deployment", new DeploymentOpenShiftConverter(platformMode, getOpenshiftDeployTimeoutSeconds()));
    // TODO : This converter shouldn't be here. See its javadoc.
    openShiftConverters.put("DeploymentConfig", new DeploymentConfigOpenShiftConverter(getOpenshiftDeployTimeoutSeconds()));
    openShiftConverters.put("Namespace", new NamespaceOpenShiftConverter());
    handlerHub = new HandlerHub(project);
}
Also used : NamespaceOpenShiftConverter(io.fabric8.maven.plugin.converter.NamespaceOpenShiftConverter) DeploymentConfigOpenShiftConverter(io.fabric8.maven.plugin.converter.DeploymentConfigOpenShiftConverter) DeploymentOpenShiftConverter(io.fabric8.maven.plugin.converter.DeploymentOpenShiftConverter) Properties(java.util.Properties) ReplicSetOpenShiftConverter(io.fabric8.maven.plugin.converter.ReplicSetOpenShiftConverter) HandlerHub(io.fabric8.maven.core.handler.HandlerHub)

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