Search in sources :

Example 1 with ServiceReference

use of io.fabric8.kubernetes.api.model.apiextensions.v1.ServiceReference in project stackgres by ongres.

the class WebhookConfiguratorImplTest method configureWebhook_shouldNotFail.

@Test
void configureWebhook_shouldNotFail() {
    var definition = crdFinder.scanDefinitions().get(0);
    final String certificate = StringUtil.generateRandom();
    ArgumentCaptor<CustomResourceDefinition> crdCaptor = ArgumentCaptor.forClass(CustomResourceDefinition.class);
    when(crdWriter.update(crdCaptor.capture())).thenReturn(definition);
    webhookConfigurator.configureWebhook(definition.getMetadata().getName(), certificate);
    CustomResourceDefinition crd = crdCaptor.getValue();
    final CustomResourceDefinitionSpec spec = crd.getSpec();
    final CustomResourceConversion conversion = spec.getConversion();
    final WebhookClientConfig clientConfig = conversion.getWebhook().getClientConfig();
    final ServiceReference service = clientConfig.getService();
    assertEquals("Webhook", conversion.getStrategy());
    assertEquals(OPERATOR_NAME, service.getName());
    assertEquals(OPERATOR_NAMESPACE, service.getNamespace());
    assertEquals("/stackgres/conversion/" + definition.getSpec().getNames().getSingular(), service.getPath());
    assertEquals(certificate, clientConfig.getCaBundle());
    assertFalse(spec.getPreserveUnknownFields());
}
Also used : CustomResourceConversion(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceConversion) WebhookClientConfig(io.fabric8.kubernetes.api.model.apiextensions.v1.WebhookClientConfig) CustomResourceDefinition(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition) CustomResourceDefinitionSpec(io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionSpec) ServiceReference(io.fabric8.kubernetes.api.model.apiextensions.v1.ServiceReference) Test(org.junit.jupiter.api.Test)

Example 2 with ServiceReference

use of io.fabric8.kubernetes.api.model.apiextensions.v1.ServiceReference in project fabric8 by jboss-fuse.

the class FeatureConfigInstaller method restoreConfigAdminIfNeeded.

public void restoreConfigAdminIfNeeded() {
    // we'll be doing confiadmin checks, so configadmin bundle has to be started and has its services registered
    Bundle b = null;
    try {
        b = new BundleUtils(context).findBundle("org.apache.felix.configadmin");
        if (b.getState() != Bundle.ACTIVE) {
            b.start();
            ServiceReference<ConfigurationAdmin> ref = context.getServiceReference(ConfigurationAdmin.class);
            if (ref != null) {
                configAdmin = context.getService(ref);
            }
        }
    } catch (BundleException e) {
        configAdmin = null;
        LOGGER.warn(e.getMessage());
    }
}
Also used : Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException) BundleUtils(io.fabric8.utils.BundleUtils) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 3 with ServiceReference

use of io.fabric8.kubernetes.api.model.apiextensions.v1.ServiceReference in project fabric8 by jboss-fuse.

the class ManagerTest method testManager.

@Test
public void testManager() throws Exception {
    ZKServerFactoryBean zkServerFactoryBean = null;
    try {
        int zooKeeperPort = getFreePort();
        int serverPort = getFreePort();
        zkServerFactoryBean = new ZKServerFactoryBean();
        zkServerFactoryBean.setPurge(true);
        zkServerFactoryBean.setClientPortAddress(new InetSocketAddress("localhost", zooKeeperPort));
        zkServerFactoryBean.afterPropertiesSet();
        CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zooKeeperPort).retryPolicy(new RetryOneTime(1000)).connectionTimeoutMs(60000);
        CuratorFramework curator = builder.build();
        curator.start();
        curator.getZookeeperClient().blockUntilConnectedOrTimedOut();
        BundleContext bundleContext = createMock(BundleContext.class);
        ServiceRegistration registration = createMock(ServiceRegistration.class);
        Manager manager = new Manager(bundleContext, curator, "tcp://localhost:" + serverPort, "localhost", TimeUnit.MINUTES.toMillis(5));
        bundleContext.addServiceListener(manager, "(service.exported.interfaces=*)");
        expect(bundleContext.getProperty("org.osgi.framework.uuid")).andReturn("the-framework-uuid");
        expect(bundleContext.registerService(EasyMock.<String[]>anyObject(), same(manager), EasyMock.<Dictionary>same(null))).andReturn(registration);
        expect(bundleContext.getServiceReferences((String) null, "(service.exported.interfaces=*)")).andReturn(null);
        replay(bundleContext, registration);
        manager.init();
        verify(bundleContext, registration);
        reset(bundleContext, registration);
        BundleContext expBundleContext = createMock(BundleContext.class);
        Bundle expBundle = createMock(Bundle.class);
        ServiceReference reference = createMock(ServiceReference.class);
        final Properties props = new Properties();
        props.put(Constants.OBJECTCLASS, new String[] { ConfigurationAdmin.class.getName() });
        expect(reference.getProperty(EasyMock.<String>anyObject())).andAnswer(new IAnswer<Object>() {

            public Object answer() throws Throwable {
                return props.get(EasyMock.getCurrentArguments()[0]);
            }
        }).anyTimes();
        expect(reference.getPropertyKeys()).andReturn(props.keySet().toArray(new String[0]));
        expect(reference.getBundle()).andReturn(expBundle).anyTimes();
        expect(expBundle.getBundleContext()).andReturn(expBundleContext).anyTimes();
        expect(expBundle.getState()).andReturn(Bundle.ACTIVE).anyTimes();
        replay(bundleContext, registration, reference, expBundleContext, expBundle);
        manager.serviceChanged(new ServiceEvent(ServiceEvent.REGISTERED, reference));
        Thread.sleep(1000);
        verify(bundleContext, registration, reference, expBundleContext, expBundle);
    } finally {
        try {
            zkServerFactoryBean.destroy();
        } catch (Throwable t) {
        }
    }
}
Also used : RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) InetSocketAddress(java.net.InetSocketAddress) Bundle(org.osgi.framework.Bundle) ZKServerFactoryBean(io.fabric8.zookeeper.spring.ZKServerFactoryBean) Manager(io.fabric8.dosgi.impl.Manager) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) IAnswer(org.easymock.IAnswer) CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceEvent(org.osgi.framework.ServiceEvent) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) BundleContext(org.osgi.framework.BundleContext) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 4 with ServiceReference

use of io.fabric8.kubernetes.api.model.apiextensions.v1.ServiceReference in project fabric8 by jboss-fuse.

the class FabricDiscovery method addingService.

@Override
public CuratorFramework addingService(ServiceReference<CuratorFramework> reference) {
    CuratorFramework curator = context.getService(reference);
    try {
        logger.debug("CuratorFramework found, starting group");
        GroupFactory factory = new ZooKeeperGroupFactory(curator);
        singleton = factory.createGroup("/fabric/registry/clusters/elasticsearch/" + clusterName.value(), ESNode.class);
        singleton.add(this);
        singleton.update(new ESNode(clusterName.value(), localNode, false));
        singleton.start();
    } catch (Exception e) {
        LOG.error("Error starting group", e);
    }
    return curator;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) GroupFactory(io.fabric8.groups.GroupFactory) ZooKeeperGroupFactory(io.fabric8.groups.internal.ZooKeeperGroupFactory) ZooKeeperGroupFactory(io.fabric8.groups.internal.ZooKeeperGroupFactory) ElasticsearchException(org.elasticsearch.ElasticsearchException) ElasticsearchIllegalStateException(org.elasticsearch.ElasticsearchIllegalStateException) IOException(java.io.IOException)

Example 5 with ServiceReference

use of io.fabric8.kubernetes.api.model.apiextensions.v1.ServiceReference in project fabric8 by jboss-fuse.

the class ServiceFactoryTest method testDiscoveryOnRestartCurator.

@Test
public void testDiscoveryOnRestartCurator() throws Exception {
    underTest = new ActiveMQServiceFactory();
    underTest.curator = curator;
    Properties props = new Properties();
    props.put("config", "amq.xml");
    props.put("broker-name", "amq");
    props.put("group", "amq");
    props.put("connectors", "openwire");
    props.put("openwire-port", "0");
    props.put("container.ip", "localhost");
    underTest.updated("b", props);
    final AtomicReference<CuratorFramework> curatorFrameworkAtomicReference = new AtomicReference<>(curator);
    OsgiFabricDiscoveryAgent osgiFabricDiscoveryAgent = new OsgiFabricDiscoveryAgent(new BundleContext() {

        @Override
        public String getProperty(String s) {
            return null;
        }

        @Override
        public Bundle getBundle() {
            return null;
        }

        @Override
        public Bundle installBundle(String s, InputStream inputStream) throws BundleException {
            return null;
        }

        @Override
        public Bundle installBundle(String s) throws BundleException {
            return null;
        }

        @Override
        public Bundle getBundle(long l) {
            return null;
        }

        @Override
        public Bundle[] getBundles() {
            return new Bundle[0];
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener, String s) throws InvalidSyntaxException {
        }

        @Override
        public void addServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void removeServiceListener(ServiceListener serviceListener) {
        }

        @Override
        public void addBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void removeBundleListener(BundleListener bundleListener) {
        }

        @Override
        public void addFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public void removeFrameworkListener(FrameworkListener frameworkListener) {
        }

        @Override
        public ServiceRegistration<?> registerService(String[] strings, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceRegistration<?> registerService(String s, Object o, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public <S> ServiceRegistration<S> registerService(Class<S> aClass, S s, Dictionary<String, ?> dictionary) {
            return null;
        }

        @Override
        public ServiceReference<?>[] getServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?>[] getAllServiceReferences(String s, String s1) throws InvalidSyntaxException {
            return new ServiceReference<?>[0];
        }

        @Override
        public ServiceReference<?> getServiceReference(String s) {
            return null;
        }

        @Override
        public <S> ServiceReference<S> getServiceReference(Class<S> aClass) {
            return null;
        }

        @Override
        public <S> Collection<ServiceReference<S>> getServiceReferences(Class<S> aClass, String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public <S> S getService(ServiceReference<S> serviceReference) {
            return (S) curatorFrameworkAtomicReference.get();
        }

        @Override
        public boolean ungetService(ServiceReference<?> serviceReference) {
            return false;
        }

        @Override
        public File getDataFile(String s) {
            return null;
        }

        @Override
        public Filter createFilter(String s) throws InvalidSyntaxException {
            return null;
        }

        @Override
        public Bundle getBundle(String s) {
            return null;
        }
    });
    final LinkedBlockingQueue<DiscoveryEvent> discoveryEvents = new LinkedBlockingQueue<DiscoveryEvent>(10);
    osgiFabricDiscoveryAgent.setDiscoveryListener(new DiscoveryListener() {

        @Override
        public void onServiceAdd(DiscoveryEvent discoveryEvent) {
            discoveryEvents.offer(discoveryEvent);
        }

        @Override
        public void onServiceRemove(DiscoveryEvent discoveryEvent) {
        }
    });
    // will call into dummy bundle and get curator
    osgiFabricDiscoveryAgent.addingService(null);
    osgiFabricDiscoveryAgent.setGroupName("amq");
    osgiFabricDiscoveryAgent.start();
    DiscoveryEvent event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("event: " + event);
    assertNotNull("got added service", event);
    underTest.deleted("b");
    // swap curator ref
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + zkPort).sessionTimeoutMs(15000).retryPolicy(new RetryNTimes(5000, 1000));
    curator = builder.build();
    LOG.debug("Starting new curator " + curator);
    curator.start();
    curatorFrameworkAtomicReference.get().close();
    curatorFrameworkAtomicReference.set(curator);
    // will call into dummy bundle and get new curator ref
    osgiFabricDiscoveryAgent.addingService(null);
    // start broker again
    underTest.curator = curator;
    underTest.updated("b", props);
    event = discoveryEvents.poll(5, TimeUnit.SECONDS);
    LOG.info("new event: " + event);
    assertNotNull("got newly added service", event);
    underTest.deleted("b");
}
Also used : CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) DiscoveryEvent(org.apache.activemq.command.DiscoveryEvent) Properties(java.util.Properties) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryNTimes(org.apache.curator.retry.RetryNTimes) InputStream(java.io.InputStream) AtomicReference(java.util.concurrent.atomic.AtomicReference) OsgiFabricDiscoveryAgent(io.fabric8.mq.fabric.discovery.OsgiFabricDiscoveryAgent) Collection(java.util.Collection) File(java.io.File) DiscoveryListener(org.apache.activemq.transport.discovery.DiscoveryListener) Test(org.junit.Test)

Aggregations

IOException (java.io.IOException)5 ServiceReference (org.osgi.framework.ServiceReference)5 BundleException (org.osgi.framework.BundleException)4 File (java.io.File)3 CuratorFramework (org.apache.curator.framework.CuratorFramework)3 Agent (io.fabric8.agent.service.Agent)2 FabricService (io.fabric8.api.FabricService)2 BootstrapConfiguration (io.fabric8.zookeeper.bootstrap.BootstrapConfiguration)2 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 Properties (java.util.Properties)2 Set (java.util.Set)2 TreeSet (java.util.TreeSet)2 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)2 Properties (org.apache.felix.utils.properties.Properties)2 Test (org.junit.Test)2 Bundle (org.osgi.framework.Bundle)2 Configuration (org.osgi.service.cm.Configuration)2 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)2 ConfigurationException (org.osgi.service.cm.ConfigurationException)2