Search in sources :

Example 71 with Configuration

use of io.fabric8.annotations.Configuration in project fabric8 by fabric8io.

the class Fabric8Extension method onInjectionPoint.

public <T, X> void onInjectionPoint(@Observes ProcessInjectionPoint<T, X> event, BeanManager beanManager) {
    final InjectionPoint injectionPoint = event.getInjectionPoint();
    if (isServiceInjectionPoint(injectionPoint)) {
        Annotated annotated = injectionPoint.getAnnotated();
        ServiceName name = annotated.getAnnotation(ServiceName.class);
        Protocol protocol = annotated.getAnnotation(Protocol.class);
        PortName port = annotated.getAnnotation(PortName.class);
        Path path = annotated.getAnnotation(Path.class);
        Alias alias = annotated.getAnnotation(Alias.class);
        Endpoint endpoint = annotated.getAnnotation(Endpoint.class);
        External external = annotated.getAnnotation(External.class);
        String serviceName = name.value();
        String serviceProtocol = protocol != null ? protocol.value() : null;
        String servicePort = port != null ? port.value() : null;
        String servicePath = path != null ? path.value() : null;
        String serviceAlias = alias != null ? alias.value() : null;
        Boolean serviceExternal = external != null ? external.value() : false;
        Boolean serviceEndpoint = endpoint != null ? endpoint.value() : false;
        Type type = annotated.getBaseType();
        if (type instanceof ParameterizedType && Instance.class.equals(((ParameterizedType) type).getRawType())) {
            type = ((ParameterizedType) type).getActualTypeArguments()[0];
        }
        if (type.equals(String.class)) {
            ServiceUrlBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal);
        } else if (isGenericOf(type, List.class, String.class)) {
            ServiceUrlCollectionBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, Types.LIST_OF_STRINGS);
        } else if (isGenericOf(type, List.class, null)) {
        // TODO: Integrate with Factories(?)
        } else if (isGenericOf(type, Set.class, String.class)) {
            ServiceUrlCollectionBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, Types.SET_OF_STRINGS);
        } else if (isGenericOf(type, Set.class, null)) {
        // TODO: Integrate with Factories(?)
        } else if (type instanceof Class) {
            ServiceBean.getBean(serviceName, serviceProtocol, servicePort, servicePath, serviceAlias, serviceEndpoint, serviceExternal, type);
        } else {
            throw new RuntimeException(String.format(INJECTION_POINT_UNKNOWN_TYPE, injectionPoint.getBean().getBeanClass(), type));
        }
        if (protocol == null) {
            setDefaultProtocol(event);
        }
        if (port == null) {
            setDefaultPort(event);
        }
        if (path == null) {
            setDefaultPath(event);
        }
        if (endpoint == null) {
            setDefaultEndpoint(event);
        }
        if (external == null) {
            setDefaultExternal(event);
        }
    } else if (isConfigurationInjectionPoint(injectionPoint)) {
        Annotated annotated = injectionPoint.getAnnotated();
        Configuration configuration = annotated.getAnnotation(Configuration.class);
        Type type = injectionPoint.getType();
        String configurationId = configuration.value();
        ConfigurationBean.getBean(configurationId, type);
    }
}
Also used : Path(io.fabric8.annotations.Path) Utils.getFactoryMethodPath(io.fabric8.cdi.Utils.getFactoryMethodPath) LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) Configuration(io.fabric8.annotations.Configuration) ProcessInjectionPoint(javax.enterprise.inject.spi.ProcessInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) Instance(javax.enterprise.inject.Instance) PortName(io.fabric8.annotations.PortName) ParameterizedType(java.lang.reflect.ParameterizedType) Annotated(javax.enterprise.inject.spi.Annotated) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Endpoint(io.fabric8.annotations.Endpoint) ServiceName(io.fabric8.annotations.ServiceName) Alias(io.fabric8.annotations.Alias) External(io.fabric8.annotations.External) ArrayList(java.util.ArrayList) List(java.util.List) Utils.getFactoryMethodProtocol(io.fabric8.cdi.Utils.getFactoryMethodProtocol) Protocol(io.fabric8.annotations.Protocol)

Example 72 with Configuration

use of io.fabric8.annotations.Configuration in project fabric8 by fabric8io.

the class SessionListener method loadDependency.

public void loadDependency(Logger log, List<KubernetesList> kubeConfigs, String dependency, Controller controller, Configuration configuration, String namespace) throws Exception {
    // lets test if the dependency is a local string
    String baseDir = System.getProperty("basedir", ".");
    String path = baseDir + "/" + dependency;
    File file = new File(path);
    if (file.exists()) {
        loadDependency(log, kubeConfigs, file, controller, configuration, log, namespace);
    } else {
        String text = readAsString(createURL(dependency));
        Object resources;
        if (text.trim().startsWith("---") || dependency.endsWith(".yml") || dependency.endsWith(".yaml")) {
            resources = loadYaml(text);
        } else {
            resources = loadJson(text);
        }
        addConfig(kubeConfigs, resources, controller, configuration, log, namespace, dependency);
    }
}
Also used : Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString) File(java.io.File)

Example 73 with Configuration

use of io.fabric8.annotations.Configuration in project fabric8 by fabric8io.

the class SuiteListener method start.

public void start(@Observes(precedence = 100) BeforeSuite event, Configuration configuration, Logger logger) {
    session = new Session(configuration.getSessionId(), configuration.getNamespace(), logger);
    session.init();
    sessionProducer.set(session);
    controlEvent.fire(new Start(session));
}
Also used : Start(io.fabric8.arquillian.kubernetes.event.Start)

Example 74 with Configuration

use of io.fabric8.annotations.Configuration in project fabric8 by fabric8io.

the class Configuration method fromMap.

public static Configuration fromMap(Map<String, String> map, KubernetesClient testKubernetesClient) {
    Configuration configuration = new Configuration();
    try {
        configuration.masterUrl = getStringProperty(KUBERNETES_MASTER, map, FALLBACK_CONFIG.getMasterUrl());
        configuration.environment = getStringProperty(FABRIC8_ENVIRONMENT, map, null);
        configuration.environmentInitEnabled = getBooleanProperty(ENVIRONMENT_INIT_ENABLED, map, true);
        configuration.environmentConfigUrl = getKubernetesConfigurationUrl(map);
        configuration.environmentDependencies = Strings.splitAndTrimAsList(getStringProperty(ENVIRONMENT_DEPENDENCIES, map, ""), "\\s+");
        configuration.namespaceLazyCreateEnabled = getBooleanProperty(NAMESPACE_LAZY_CREATE_ENABLED, map, DEFAULT_NAMESPACE_LAZY_CREATE_ENABLED);
        configuration.properties = map;
        String existingNamespace = getStringProperty(NAMESPACE_TO_USE, map, null);
        configuration.sessionId = UUID.randomUUID().toString();
        configuration.namespaceCleanupConfirmationEnabled = getBooleanProperty(NAMESPACE_CLEANUP_CONFIRM_ENABLED, map, false);
        configuration.deleteAllResourcesOnExit = getBooleanProperty(NAMESPACE_DELETE_ALL_RESOURCES_ON_EXIT, map, false);
        configuration.namespaceCleanupTimeout = getLongProperty(NAMESPACE_CLEANUP_TIMEOUT, map, DEFAULT_NAMESPACE_CLEANUP_TIMEOUT);
        configuration.waitTimeout = getLongProperty(WAIT_TIMEOUT, map, DEFAULT_WAIT_TIMEOUT);
        configuration.waitPollInterval = getLongProperty(WAIT_POLL_INTERVAL, map, DEFAULT_WAIT_POLL_INTERVAL);
        configuration.waitForServiceList = Strings.splitAndTrimAsList(getStringProperty(WAIT_FOR_SERVICE_LIST, map, ""), "\\s+");
        configuration.waitForServiceConnectionEnabled = getBooleanProperty(WAIT_FOR_SERVICE_CONNECTION_ENABLED, map, DEFAULT_WAIT_FOR_SERVICE_CONNECTION_ENABLED);
        configuration.waitForServiceConnectionTimeout = getLongProperty(WAIT_FOR_SERVICE_CONNECTION_TIMEOUT, map, DEFAULT_NAMESPACE_CLEANUP_TIMEOUT);
        configuration.ansiLoggerEnabled = getBooleanProperty(ANSI_LOGGER_ENABLED, map, true);
        configuration.kubernetesDomain = getStringProperty(KUBERNETES_DOMAIN, map, "");
        configuration.gofabric8Enabled = getBooleanProperty(GOFABRIC8_ENABLED, map, false);
        configuration.createNamespaceForTest = getBooleanProperty(CREATE_NAMESPACE_FOR_TEST, map, false);
        KubernetesClient kubernetesClient = getOrCreateKubernetesClient(configuration, testKubernetesClient);
        boolean failOnMissingEnvironmentNamespace = getBooleanProperty(FAIL_ON_MISSING_ENVIRONMENT_NAMESPACE, map, false);
        String defaultDevelopNamespace = existingNamespace;
        if (Strings.isNullOrBlank(defaultDevelopNamespace)) {
            defaultDevelopNamespace = kubernetesClient.getNamespace();
        }
        String developNamespace = getStringProperty(DEVELOPMENT_NAMESPACE, map, defaultDevelopNamespace);
        configuration.kubernetesClient = kubernetesClient;
        String environmentNamespace = findNamespaceForEnvironment(configuration.environment, map, kubernetesClient, developNamespace, failOnMissingEnvironmentNamespace);
        String providedNamespace = selectNamespace(environmentNamespace, existingNamespace);
        if (configuration.createNamespaceForTest) {
            configuration.namespace = NAMESPACE_PREFIX + configuration.sessionId;
        } else {
            String namespace = Strings.isNotBlank(providedNamespace) ? providedNamespace : developNamespace;
            ;
            if (Strings.isNullOrBlank(namespace)) {
                namespace = kubernetesClient.getNamespace();
                if (Strings.isNullOrBlank(namespace)) {
                    namespace = KubernetesHelper.defaultNamespace();
                }
            }
            configuration.namespace = namespace;
        }
        // We default to "cleanup=true" when generating namespace and "cleanup=false" when using existing namespace.
        configuration.namespaceCleanupEnabled = getBooleanProperty(NAMESPACE_CLEANUP_ENABLED, map, Strings.isNullOrBlank(providedNamespace));
    } catch (Throwable t) {
        if (t instanceof RuntimeException) {
            throw (RuntimeException) t;
        } else {
            throw new RuntimeException(t);
        }
    }
    return configuration;
}
Also used : KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient)

Example 75 with Configuration

use of io.fabric8.annotations.Configuration in project fabric8 by fabric8io.

the class Controller method applyReplicationController.

public void applyReplicationController(ReplicationController replicationController, String sourceName) throws Exception {
    String namespace = getNamespace();
    String id = getName(replicationController);
    Objects.notNull(id, "No name for " + replicationController + " " + sourceName);
    if (isServicesOnlyMode()) {
        LOG.debug("Only processing Services right now so ignoring ReplicationController: " + namespace + ":" + id);
        return;
    }
    ReplicationController old = kubernetesClient.replicationControllers().inNamespace(namespace).withName(id).get();
    if (isRunning(old)) {
        if (UserConfigurationCompare.configEqual(replicationController, old)) {
            LOG.info("ReplicationController has not changed so not doing anything");
        } else {
            ReplicationControllerSpec newSpec = replicationController.getSpec();
            ReplicationControllerSpec oldSpec = old.getSpec();
            if (rollingUpgrade) {
                LOG.info("Rolling upgrade of the ReplicationController: " + namespace + "/" + id);
                // lets preserve the number of replicas currently running in the environment we are about to upgrade
                if (rollingUpgradePreserveScale && newSpec != null && oldSpec != null) {
                    Integer replicas = oldSpec.getReplicas();
                    if (replicas != null) {
                        newSpec.setReplicas(replicas);
                    }
                }
                LOG.info("rollingUpgradePreserveScale " + rollingUpgradePreserveScale + " new replicas is " + (newSpec != null ? newSpec.getReplicas() : "<null>"));
                kubernetesClient.replicationControllers().inNamespace(namespace).withName(id).rolling().replace(replicationController);
            } else if (isRecreateMode()) {
                LOG.info("Deleting ReplicationController: " + id);
                kubernetesClient.replicationControllers().inNamespace(namespace).withName(id).delete();
                doCreateReplicationController(replicationController, namespace, sourceName);
            } else {
                LOG.info("Updating ReplicationController from " + sourceName + " namespace " + namespace + " name " + getName(replicationController));
                try {
                    Object answer = kubernetesClient.replicationControllers().inNamespace(namespace).withName(id).replace(replicationController);
                    logGeneratedEntity("Updated replicationController: ", namespace, replicationController, answer);
                    if (deletePodsOnReplicationControllerUpdate) {
                        kubernetesClient.pods().inNamespace(namespace).withLabels(newSpec.getSelector()).delete();
                        LOG.info("Deleting any pods for the replication controller to ensure they use the new configuration");
                    } else {
                        LOG.info("Warning not deleted any pods so they could well be running with the old configuration!");
                    }
                } catch (Exception e) {
                    onApplyError("Failed to update ReplicationController from " + sourceName + ". " + e + ". " + replicationController, e);
                }
            }
        }
    } else {
        if (!isAllowCreate()) {
            LOG.warn("Creation disabled so not creating a ReplicationController from " + sourceName + " namespace " + namespace + " name " + getName(replicationController));
        } else {
            doCreateReplicationController(replicationController, namespace, sourceName);
        }
    }
}
Also used : ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) JSONObject(org.json.JSONObject) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) FileNotFoundException(java.io.FileNotFoundException) OpenShiftNotAvailableException(io.fabric8.openshift.client.OpenShiftNotAvailableException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Aggregations

IOException (java.io.IOException)29 HashMap (java.util.HashMap)23 File (java.io.File)22 Configuration (org.osgi.service.cm.Configuration)20 Map (java.util.Map)16 BootstrapConfiguration (io.fabric8.zookeeper.bootstrap.BootstrapConfiguration)15 Test (org.junit.Test)13 ArrayList (java.util.ArrayList)12 Container (io.fabric8.api.Container)11 Profile (io.fabric8.api.Profile)11 RuntimeProperties (io.fabric8.api.RuntimeProperties)9 HashSet (java.util.HashSet)9 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)8 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)8 FabricException (io.fabric8.api.FabricException)7 FabricService (io.fabric8.api.FabricService)7 Properties (java.util.Properties)7 DefaultKubernetesClient (io.fabric8.kubernetes.client.DefaultKubernetesClient)6 Util.readAsString (io.fabric8.arquillian.utils.Util.readAsString)5 URL (java.net.URL)5