Search in sources :

Example 26 with Event

use of io.fabric8.kubernetes.api.model.Event in project strimzi by strimzi.

the class ControllerIT method waitForEvent.

private void waitForEvent(TestContext context, ConfigMap cm, String expectedMessage, Controller.EventType expectedType) {
    waitFor(context, () -> {
        List<Event> items = kubeClient.events().inNamespace(NAMESPACE).withLabels(cmPredicate.labels()).list().getItems();
        List<Event> filtered = items.stream().filter(evt -> !preExistingEvents.contains(evt.getMetadata().getUid()) && "ConfigMap".equals(evt.getInvolvedObject().getKind()) && cm.getMetadata().getName().equals(evt.getInvolvedObject().getName())).collect(Collectors.toList());
        LOGGER.debug("Waiting for events: {}", filtered.stream().map(evt -> evt.getMessage()).collect(Collectors.toList()));
        if (!filtered.isEmpty()) {
            assertEquals(1, filtered.size());
            Event event = filtered.get(0);
            assertEquals(expectedMessage, event.getMessage());
            assertEquals(expectedType.name, event.getType());
            assertNotNull(event.getInvolvedObject());
            assertEquals("ConfigMap", event.getInvolvedObject().getKind());
            assertEquals(cm.getMetadata().getName(), event.getInvolvedObject().getName());
            return true;
        } else {
            return false;
        }
    }, timeout, "Expected an error event");
}
Also used : TestContext(io.vertx.ext.unit.TestContext) ZookeeperServer(io.debezium.kafka.ZookeeperServer) LoggerFactory(org.slf4j.LoggerFactory) ConfigEntry(org.apache.kafka.clients.admin.ConfigEntry) BooleanSupplier(java.util.function.BooleanSupplier) Collections.singletonList(java.util.Collections.singletonList) AdminClient(org.apache.kafka.clients.admin.AdminClient) Locale(java.util.Locale) After(org.junit.After) Map(java.util.Map) DeleteTopicsResult(org.apache.kafka.clients.admin.DeleteTopicsResult) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) ClassRule(org.junit.ClassRule) KafkaCluster(io.debezium.kafka.KafkaCluster) AfterClass(org.junit.AfterClass) Event(io.fabric8.kubernetes.api.model.Event) Namespace(io.strimzi.test.Namespace) Set(java.util.Set) Future(io.vertx.core.Future) Collectors(java.util.stream.Collectors) List(java.util.List) AlterConfigsResult(org.apache.kafka.clients.admin.AlterConfigsResult) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) Async(io.vertx.ext.unit.Async) BeforeClass(org.junit.BeforeClass) NewPartitions(org.apache.kafka.clients.admin.NewPartitions) RunWith(org.junit.runner.RunWith) HashMap(java.util.HashMap) ConfigResource(org.apache.kafka.common.config.ConfigResource) KubeClusterResource(io.strimzi.test.k8s.KubeClusterResource) CreateTopicsResult(org.apache.kafka.clients.admin.CreateTopicsResult) Collections.singletonMap(java.util.Collections.singletonMap) Before(org.junit.Before) Collections.emptyMap(java.util.Collections.emptyMap) Logger(org.slf4j.Logger) Files(java.nio.file.Files) Assert.assertNotNull(org.junit.Assert.assertNotNull) Vertx(io.vertx.core.Vertx) NewTopic(org.apache.kafka.clients.admin.NewTopic) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) CreatePartitionsResult(org.apache.kafka.clients.admin.CreatePartitionsResult) Field(java.lang.reflect.Field) ConfigMap(io.fabric8.kubernetes.api.model.ConfigMap) ConfigMapBuilder(io.fabric8.kubernetes.api.model.ConfigMapBuilder) ExecutionException(java.util.concurrent.ExecutionException) Ignore(org.junit.Ignore) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Assert.assertEquals(org.junit.Assert.assertEquals) Event(io.fabric8.kubernetes.api.model.Event)

Example 27 with Event

use of io.fabric8.kubernetes.api.model.Event 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 28 with Event

use of io.fabric8.kubernetes.api.model.Event in project vertx-openshift-it by cescoffier.

the class EventBusIT method initialize.

@BeforeClass
public static void initialize() throws IOException {
    initializeServiceAccount();
    SortedMap<String, File> dependencies = new TreeMap<>();
    dependencies.put("A-EventBus", new File("../" + APPLICATION_NAME + "/target/classes/META-INF/fabric8/openshift.yml"));
    dependencies.forEach((name, template) -> Ensure.ensureThat(String.format("template file %s can be deployed", template), () -> deploymentAssistant.deploy(name, template)));
    Ensure.ensureThat("The event-bus app is up and running", () -> await().atMost(5, TimeUnit.MINUTES).catchUncaughtExceptions().untilAsserted(() -> {
        Service service = client.services().withName(APPLICATION_NAME).get();
        Assertions.assertThat(service).isNotNull();
        route = client.routes().withName(APPLICATION_NAME).get();
        Assertions.assertThat(route).isNotNull();
        get(Kube.urlForRoute(route, "/health")).then().statusCode(200);
    }));
    clusterEventBusHelper = new OpenShiftHelper(client, APPLICATION_NAME);
}
Also used : Service(io.fabric8.kubernetes.api.model.Service) TreeMap(java.util.TreeMap) File(java.io.File) OpenShiftHelper(io.vertx.it.openshift.utils.OpenShiftHelper) BeforeClass(org.junit.BeforeClass)

Example 29 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class ZooKeeperClusterBootstrapImpl method cleanInternal.

private BootstrapConfiguration cleanInternal(final BundleContext syscontext, final BootstrapConfiguration bootConfig, RuntimeProperties runtimeProps) throws TimeoutException {
    LOGGER.debug("Begin clean fabric");
    try {
        Configuration zkClientCfg = null;
        Configuration zkServerCfg = null;
        Configuration[] configsSet = configAdmin.get().listConfigurations("(|(service.factoryPid=io.fabric8.zookeeper.server)(service.pid=io.fabric8.zookeeper))");
        if (configsSet != null) {
            for (Configuration cfg : configsSet) {
                // let's explicitly delete client config first
                if ("io.fabric8.zookeeper".equals(cfg.getPid())) {
                    zkClientCfg = cfg;
                }
                if ("io.fabric8.zookeeper.server".equals(cfg.getFactoryPid())) {
                    zkServerCfg = cfg;
                }
            }
        }
        File karafData = new File(data);
        // Setup the listener for unregistration of {@link BootstrapConfiguration}
        final CountDownLatch unregisterLatch = new CountDownLatch(1);
        ServiceListener listener = new ServiceListener() {

            @Override
            public void serviceChanged(ServiceEvent event) {
                if (event.getType() == ServiceEvent.UNREGISTERING) {
                    LOGGER.debug("Unregistering BootstrapConfiguration");
                    bootConfig.getComponentContext().getBundleContext().removeServiceListener(this);
                    unregisterLatch.countDown();
                }
            }
        };
        String filter = "(objectClass=" + BootstrapConfiguration.class.getName() + ")";
        // FABRIC-1052: register listener using the same bundle context that is used for listeners related to SCR
        bootConfig.getComponentContext().getBundleContext().addServiceListener(listener, filter);
        CountDownLatch unregisterLatch2 = null;
        if (syscontext.getServiceReference(CuratorComplete.class) != null) {
            unregisterLatch2 = new CountDownLatch(1);
            final CountDownLatch finalUnregisterLatch = unregisterLatch2;
            listener = new ServiceListener() {

                @Override
                public void serviceChanged(ServiceEvent event) {
                    if (event.getType() == ServiceEvent.UNREGISTERING) {
                        LOGGER.debug("Unregistering CuratorComplete");
                        bootConfig.getComponentContext().getBundleContext().removeServiceListener(this);
                        finalUnregisterLatch.countDown();
                    }
                }
            };
            bootConfig.getComponentContext().getBundleContext().addServiceListener(listener, "(objectClass=" + CuratorComplete.class.getName() + ")");
        }
        // Disable the BootstrapConfiguration component
        // ENTESB-4827: disabling BootstrapConfiguration leads to deactivation of FabricService and ProfileUrlHandler
        // and we have race condition if we're --cleaning after recently created fabric. previous fabric
        // started FabricConfigAdminBridge which scheduled CM updates for tens of PIDs - among others,
        // org.ops4j.pax.web, which leads to an attempt to reconfigure Jetty with "profile:jetty.xml"
        // and if we disable ProfileUrlHandler we may loose Jetty instance
        LOGGER.debug("Disable BootstrapConfiguration");
        ComponentContext componentContext = bootConfig.getComponentContext();
        componentContext.disableComponent(BootstrapConfiguration.COMPONENT_NAME);
        if (!unregisterLatch.await(30, TimeUnit.SECONDS))
            throw new TimeoutException("Timeout for unregistering BootstrapConfiguration service");
        if (unregisterLatch2 != null && !unregisterLatch2.await(30, TimeUnit.SECONDS))
            throw new TimeoutException("Timeout for unregistering CuratorComplete service");
        // Do the cleanup
        runtimeProps.clearRuntimeAttributes();
        cleanConfigurations(syscontext, zkClientCfg, zkServerCfg);
        cleanZookeeperDirectory(karafData);
        cleanGitDirectory(karafData);
        // Setup the registration listener for the new {@link BootstrapConfiguration}
        final CountDownLatch registerLatch = new CountDownLatch(1);
        final AtomicReference<ServiceReference<?>> sref = new AtomicReference<ServiceReference<?>>();
        listener = new ServiceListener() {

            @Override
            public void serviceChanged(ServiceEvent event) {
                if (event.getType() == ServiceEvent.REGISTERED) {
                    LOGGER.debug("Registered BootstrapConfiguration");
                    syscontext.removeServiceListener(this);
                    sref.set(event.getServiceReference());
                    registerLatch.countDown();
                }
            }
        };
        syscontext.addServiceListener(listener, "(objectClass=" + BootstrapConfiguration.class.getName() + ")");
        // Enable the {@link BootstrapConfiguration} component and await the registration of the respective service
        LOGGER.debug("Enable BootstrapConfiguration");
        componentContext.enableComponent(BootstrapConfiguration.COMPONENT_NAME);
        if (!registerLatch.await(30, TimeUnit.SECONDS))
            throw new TimeoutException("Timeout for registering BootstrapConfiguration service");
        return (BootstrapConfiguration) syscontext.getService(sref.get());
    } catch (RuntimeException rte) {
        throw rte;
    } catch (TimeoutException toe) {
        throw toe;
    } catch (Exception ex) {
        throw new FabricException("Unable to delete zookeeper configuration", ex);
    } finally {
        LOGGER.debug("End clean fabric");
    }
}
Also used : ServiceListener(org.osgi.framework.ServiceListener) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) Configuration(org.osgi.service.cm.Configuration) ComponentContext(org.osgi.service.component.ComponentContext) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) FabricException(io.fabric8.api.FabricException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CuratorComplete(io.fabric8.api.CuratorComplete) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) ServiceReference(org.osgi.framework.ServiceReference) ServiceEvent(org.osgi.framework.ServiceEvent) File(java.io.File) TimeoutException(java.util.concurrent.TimeoutException)

Example 30 with Event

use of io.fabric8.kubernetes.api.model.Event in project fabric8 by jboss-fuse.

the class ZooKeeperClusterBootstrapImpl method create.

@Override
public void create(CreateEnsembleOptions options) {
    assertValid();
    try {
        // Wait for bootstrap to be complete
        ServiceLocator.awaitService(BootstrapComplete.class);
        LOGGER.info("Create fabric with: {}", options);
        stopBundles();
        BundleContext syscontext = bundleContext.getBundle(0).getBundleContext();
        long bootstrapTimeout = options.getBootstrapTimeout();
        RuntimeProperties runtimeProps = runtimeProperties.get();
        BootstrapConfiguration bootConfig = bootstrapConfiguration.get();
        if (options.isClean()) {
            bootConfig = cleanInternal(syscontext, bootConfig, runtimeProps);
        }
        // before we start fabric, register CM listener that'll mark end of work of FabricConfigAdminBridge
        final CountDownLatch fcabLatch = new CountDownLatch(1);
        final ServiceRegistration<ConfigurationListener> registration = bundleContext.registerService(ConfigurationListener.class, new ConfigurationListener() {

            @Override
            public void configurationEvent(ConfigurationEvent event) {
                if (event.getType() == ConfigurationEvent.CM_UPDATED && event.getPid() != null && event.getPid().equals(Constants.CONFIGADMIN_BRIDGE_PID)) {
                    fcabLatch.countDown();
                }
            }
        }, null);
        BootstrapCreateHandler createHandler = new BootstrapCreateHandler(syscontext, bootConfig, runtimeProps);
        createHandler.bootstrapFabric(name, homeDir, options);
        startBundles(options);
        long startTime = System.currentTimeMillis();
        ServiceLocator.awaitService(FabricComplete.class, bootstrapTimeout, TimeUnit.MILLISECONDS);
        // FabricComplete is registered somewhere in the middle of registering CuratorFramework (SCR activates
        // it when CuratorFramework is registered), but CuratorFramework leads to activation of >100 SCR
        // components, so let's wait for new CuratorComplete service - it is registered after registration
        // of CuratorFramework finishes
        ServiceLocator.awaitService(CuratorComplete.class, bootstrapTimeout, TimeUnit.MILLISECONDS);
        CuratorFramework curatorFramework = ServiceLocator.getService(CuratorFramework.class);
        Map<String, String> dataStoreProperties = options.getDataStoreProperties();
        if (ZooKeeperUtils.exists(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL.getPath()) == null)
            ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL.getPath());
        if (dataStoreProperties != null) {
            String remoteUrl = dataStoreProperties.get(Constants.GIT_REMOTE_URL);
            if (remoteUrl != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_URL.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_URL.getPath(), remoteUrl);
            }
            String remoteUser = dataStoreProperties.get("gitRemoteUser");
            if (remoteUser != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_USER.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_USER.getPath(), remoteUser);
            }
            String remotePassword = dataStoreProperties.get("gitRemotePassword");
            if (remotePassword != null) {
                ZooKeeperUtils.create(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_PASSWORD.getPath());
                ZooKeeperUtils.add(curatorFramework, ZkPath.CONFIG_GIT_EXTERNAL_PASSWORD.getPath(), remotePassword);
            }
        }
        // HttpService is registered differently. not as SCR component activation, but after
        // FabricConfigAdminBridge updates (or doesn't touch) org.ops4j.pax.web CM configuration
        // however in fabric, this PID contains URL to jetty configuration in the form "profile:jetty.xml"
        // so we have to have FabricService and ProfileUrlHandler active
        // some ARQ (single container instance) tests failed because tests ended without http service running
        // of course we have to think if all fabric instances need http service
        ServiceLocator.awaitService("org.osgi.service.http.HttpService", bootstrapTimeout, TimeUnit.MILLISECONDS);
        // and last wait - too much synchronization never hurts
        fcabLatch.await(bootstrapTimeout, TimeUnit.MILLISECONDS);
        registration.unregister();
        long timeDiff = System.currentTimeMillis() - startTime;
        createHandler.waitForContainerAlive(name, syscontext, bootstrapTimeout);
        if (options.isWaitForProvision() && options.isAgentEnabled()) {
            long currentTime = System.currentTimeMillis();
            createHandler.waitForSuccessfulDeploymentOf(name, syscontext, bootstrapTimeout - (currentTime - startTime));
        }
    } catch (RuntimeException rte) {
        throw rte;
    } catch (Exception ex) {
        throw new FabricException("Unable to create zookeeper server configuration", ex);
    }
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) BootstrapConfiguration(io.fabric8.zookeeper.bootstrap.BootstrapConfiguration) FabricException(io.fabric8.api.FabricException) CountDownLatch(java.util.concurrent.CountDownLatch) TimeoutException(java.util.concurrent.TimeoutException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) FabricException(io.fabric8.api.FabricException) IOException(java.io.IOException) ConfigurationListener(org.osgi.service.cm.ConfigurationListener) CuratorFramework(org.apache.curator.framework.CuratorFramework) RuntimeProperties(io.fabric8.api.RuntimeProperties) BundleContext(org.osgi.framework.BundleContext)

Aggregations

Test (org.junit.Test)14 IOException (java.io.IOException)11 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)8 ArrayList (java.util.ArrayList)6 ConnectionParameters (io.fabric8.gateway.handlers.loadbalancer.ConnectionParameters)5 File (java.io.File)5 Map (java.util.Map)5 CuratorFramework (org.apache.curator.framework.CuratorFramework)5 Logger (org.slf4j.Logger)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 LoggerFactory (org.slf4j.LoggerFactory)4 ServiceDTO (io.fabric8.gateway.ServiceDTO)3 FutureHandler (io.fabric8.gateway.handlers.detecting.FutureHandler)3 HttpGatewayHandler (io.fabric8.gateway.handlers.http.HttpGatewayHandler)3 LogEvent (io.fabric8.insight.log.LogEvent)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 URI (java.net.URI)3 URISyntaxException (java.net.URISyntaxException)3 HashMap (java.util.HashMap)3 ChildData (org.apache.curator.framework.recipes.cache.ChildData)3