Search in sources :

Example 11 with Parameter

use of io.fabric8.openshift.api.model.Parameter in project fabric8 by jboss-fuse.

the class FactoryMethodProducer method produce.

@Override
public T produce(CreationalContext<T> ctx) {
    List<Object> arguments = new ArrayList<>();
    for (AnnotatedParameter<X> parameter : factoryMethod.getParameters()) {
        Type type = parameter.getBaseType();
        ServiceName parameterServiceName = parameter.getAnnotation(ServiceName.class);
        Protocol parameterProtocol = parameter.getAnnotation(Protocol.class);
        PortName parameterPortName = parameter.getAnnotation(PortName.class);
        Path parameterPath = parameter.getAnnotation(Path.class);
        Endpoint paramEndpoint = parameter.getAnnotation(Endpoint.class);
        External paramExternal = parameter.getAnnotation(External.class);
        Configuration configuration = parameter.getAnnotation(Configuration.class);
        // A point without @ServiceName is invalid.
        // Even if method defines @ServiceName, the annotation on the injection point takes precedence
        String serviceName = pointName;
        String serviceProtocol = or(pointProtocol, parameterProtocol != null ? parameterProtocol.value() : null);
        String servicePort = or(pointPort, parameterPortName != null ? parameterPortName.value() : null);
        String servicePath = or(pointPath, parameterPath != null ? parameterPath.value() : null);
        Boolean serviceEndpoint = paramEndpoint != null ? paramEndpoint.value() : false;
        Boolean serviceExternal = paramExternal != null ? paramExternal.value() : false;
        // If the @ServiceName exists on the current String property
        if (parameterServiceName != null && String.class.equals(type)) {
            try {
                String serviceUrl = getServiceUrl(serviceName, serviceProtocol, servicePort, servicePath, serviceEndpoint, serviceExternal, ctx);
                arguments.add(serviceUrl);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current List property
        if (parameterServiceName != null && List.class.equals(Types.asClass(type))) {
            try {
                List<String> endpointList = getEndpointList(serviceName, serviceProtocol, servicePort, servicePath, serviceExternal, ctx);
                arguments.add(endpointList);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current List property
        if (parameterServiceName != null && Set.class.equals(Types.asClass(type))) {
            try {
                List<String> endpointList = getEndpointList(serviceName, serviceProtocol, servicePort, servicePath, serviceExternal, ctx);
                arguments.add(new HashSet<>(endpointList));
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current property which is a non-String
        if (parameterServiceName != null && !String.class.equals(type)) {
            try {
                Object serviceBean = getServiceBean(serviceName, serviceProtocol, servicePort, servicePath, serviceEndpoint, serviceExternal, type, ctx);
                arguments.add(serviceBean);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(BEAN_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), type, serviceName), t);
            }
        } else // If the current parameter is annotated with @Configuration
        if (configuration != null) {
            try {
                Object config = getConfiguration(serviceName, (Class<Object>) type, ctx);
                arguments.add(config);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(CONF_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else {
            try {
                Object other = BeanProvider.getContextualReference(Types.asClass(type), true);
                arguments.add(other);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(PARAMETER_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), parameter.getPosition()), t);
            }
        }
    }
    try {
        return (T) factoryMethod.getJavaMember().invoke(bean.create(ctx), arguments.toArray());
    } catch (Throwable t) {
        throw new RuntimeException(String.format(INVOCATION_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), arguments), t);
    }
}
Also used : Path(io.fabric8.annotations.Path) HashSet(java.util.HashSet) Set(java.util.Set) Configuration(io.fabric8.annotations.Configuration) ArrayList(java.util.ArrayList) PortName(io.fabric8.annotations.PortName) Type(java.lang.reflect.Type) Endpoint(io.fabric8.annotations.Endpoint) ServiceName(io.fabric8.annotations.ServiceName) External(io.fabric8.annotations.External) ArrayList(java.util.ArrayList) List(java.util.List) Protocol(io.fabric8.annotations.Protocol)

Example 12 with Parameter

use of io.fabric8.openshift.api.model.Parameter in project fabric8 by jboss-fuse.

the class Main method main.

public static void main(String[] args) throws Exception {
    File patch;
    File base;
    if (args.length < 1) {
        help();
        return;
    }
    patch = new File(args[0]);
    if (!patch.isFile()) {
        System.err.println("Invalid patch file");
        return;
    }
    if (args.length > 1) {
        base = new File(args[1]);
    } else {
        base = new File(System.getProperty("karaf.base"));
    }
    if (!new File(base, "system").isDirectory() || !new File(base, "etc").isDirectory()) {
        System.err.println("Invalid karaf-base parameter");
        return;
    }
    new Offline(base).apply(patch);
}
Also used : Offline(io.fabric8.patch.impl.Offline) File(java.io.File)

Example 13 with Parameter

use of io.fabric8.openshift.api.model.Parameter in project fabric8 by fabric8io.

the class FactoryMethodProducer method produce.

@Override
public T produce(CreationalContext<T> ctx) {
    List<Object> arguments = new ArrayList<>();
    for (AnnotatedParameter<X> parameter : factoryMethod.getParameters()) {
        Type type = parameter.getBaseType();
        ServiceName parameterServiceName = parameter.getAnnotation(ServiceName.class);
        Protocol parameterProtocol = parameter.getAnnotation(Protocol.class);
        PortName parameterPortName = parameter.getAnnotation(PortName.class);
        Path parameterPath = parameter.getAnnotation(Path.class);
        Endpoint paramEndpoint = parameter.getAnnotation(Endpoint.class);
        External paramExternal = parameter.getAnnotation(External.class);
        Configuration configuration = parameter.getAnnotation(Configuration.class);
        // A point without @ServiceName is invalid.
        // Even if method defines @ServiceName, the annotation on the injection point takes precedence
        String serviceName = pointName;
        String serviceProtocol = or(pointProtocol, parameterProtocol != null ? parameterProtocol.value() : null);
        String servicePort = or(pointPort, parameterPortName != null ? parameterPortName.value() : null);
        String servicePath = or(pointPath, parameterPath != null ? parameterPath.value() : null);
        Boolean serviceEndpoint = paramEndpoint != null ? paramEndpoint.value() : false;
        Boolean serviceExternal = paramExternal != null ? paramExternal.value() : false;
        // If the @ServiceName exists on the current String property
        if (parameterServiceName != null && String.class.equals(type)) {
            try {
                String serviceUrl = getServiceUrl(serviceName, serviceProtocol, servicePort, servicePath, serviceEndpoint, serviceExternal, ctx);
                arguments.add(serviceUrl);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current List property
        if (parameterServiceName != null && List.class.equals(Types.asClass(type))) {
            try {
                List<String> endpointList = getEndpointList(serviceName, serviceProtocol, servicePort, servicePath, serviceExternal, ctx);
                arguments.add(endpointList);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current List property
        if (parameterServiceName != null && Set.class.equals(Types.asClass(type))) {
            try {
                List<String> endpointList = getEndpointList(serviceName, serviceProtocol, servicePort, servicePath, serviceExternal, ctx);
                arguments.add(new HashSet<>(endpointList));
            } catch (Throwable t) {
                throw new RuntimeException(String.format(SERVICE_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else // If the @ServiceName exists on the current property which is a non-String
        if (parameterServiceName != null && !String.class.equals(type)) {
            try {
                Object serviceBean = getServiceBean(serviceName, serviceProtocol, servicePort, servicePath, serviceEndpoint, serviceExternal, type, ctx);
                arguments.add(serviceBean);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(BEAN_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), type, serviceName), t);
            }
        } else // If the current parameter is annotated with @Configuration
        if (configuration != null) {
            try {
                Object config = getConfiguration(serviceName, (Class<Object>) type, ctx);
                arguments.add(config);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(CONF_LOOKUP_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), serviceName), t);
            }
        } else {
            try {
                Object other = BeanProvider.getContextualReference(Types.asClass(type), true);
                arguments.add(other);
            } catch (Throwable t) {
                throw new RuntimeException(String.format(PARAMETER_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), parameter.getPosition()), t);
            }
        }
    }
    try {
        return (T) factoryMethod.getJavaMember().invoke(bean.create(ctx), arguments.toArray());
    } catch (Throwable t) {
        throw new RuntimeException(String.format(INVOCATION_ERROR_FORMAT, factoryMethod.getJavaMember().getName(), factoryMethod.getJavaMember().getDeclaringClass().getName(), arguments), t);
    }
}
Also used : Path(io.fabric8.annotations.Path) HashSet(java.util.HashSet) Set(java.util.Set) Configuration(io.fabric8.annotations.Configuration) ArrayList(java.util.ArrayList) PortName(io.fabric8.annotations.PortName) Type(java.lang.reflect.Type) Endpoint(io.fabric8.annotations.Endpoint) ServiceName(io.fabric8.annotations.ServiceName) External(io.fabric8.annotations.External) ArrayList(java.util.ArrayList) List(java.util.List) Protocol(io.fabric8.annotations.Protocol)

Example 14 with Parameter

use of io.fabric8.openshift.api.model.Parameter in project flink by apache.

the class FlinkKubeClientFactory method fromConfiguration.

/**
 * Create a Flink Kubernetes client with the given configuration.
 *
 * @param flinkConfig Flink configuration
 * @param useCase Flink Kubernetes client use case (e.g. client, resourcemanager,
 *     kubernetes-ha-services)
 * @return Return the Flink Kubernetes client with the specified configuration and dedicated IO
 *     executor.
 */
public FlinkKubeClient fromConfiguration(Configuration flinkConfig, String useCase) {
    final Config config;
    final String kubeContext = flinkConfig.getString(KubernetesConfigOptions.CONTEXT);
    if (kubeContext != null) {
        LOG.info("Configuring kubernetes client to use context {}.", kubeContext);
    }
    final String kubeConfigFile = flinkConfig.getString(KubernetesConfigOptions.KUBE_CONFIG_FILE);
    if (kubeConfigFile != null) {
        LOG.debug("Trying to load kubernetes config from file: {}.", kubeConfigFile);
        try {
            // If kubeContext is null, the default context in the kubeConfigFile will be used.
            // Note: the third parameter kubeconfigPath is optional and is set to null. It is
            // only used to rewrite
            // relative tls asset paths inside kubeconfig when a file is passed, and in the case
            // that the kubeconfig
            // references some assets via relative paths.
            config = Config.fromKubeconfig(kubeContext, FileUtils.readFileUtf8(new File(kubeConfigFile)), null);
        } catch (IOException e) {
            throw new KubernetesClientException("Load kubernetes config failed.", e);
        }
    } else {
        LOG.debug("Trying to load default kubernetes config.");
        config = Config.autoConfigure(kubeContext);
    }
    final String namespace = flinkConfig.getString(KubernetesConfigOptions.NAMESPACE);
    LOG.debug("Setting namespace of Kubernetes client to {}", namespace);
    config.setNamespace(namespace);
    final NamespacedKubernetesClient client = new DefaultKubernetesClient(config);
    final int poolSize = flinkConfig.get(KubernetesConfigOptions.KUBERNETES_CLIENT_IO_EXECUTOR_POOL_SIZE);
    return new Fabric8FlinkKubeClient(flinkConfig, client, createThreadPoolForAsyncIO(poolSize, useCase));
}
Also used : Config(io.fabric8.kubernetes.client.Config) NamespacedKubernetesClient(io.fabric8.kubernetes.client.NamespacedKubernetesClient) IOException(java.io.IOException) DefaultKubernetesClient(io.fabric8.kubernetes.client.DefaultKubernetesClient) File(java.io.File) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 15 with Parameter

use of io.fabric8.openshift.api.model.Parameter in project fabric8 by jboss-fuse.

the class Templates method combineTemplates.

public static Template combineTemplates(Template firstTemplate, Template template) {
    List<HasMetadata> objects = template.getObjects();
    if (objects != null) {
        for (HasMetadata object : objects) {
            addTemplateObject(firstTemplate, object);
        }
    }
    List<Parameter> parameters = firstTemplate.getParameters();
    if (parameters == null) {
        parameters = new ArrayList<>();
        firstTemplate.setParameters(parameters);
    }
    combineParameters(parameters, template.getParameters());
    String name = KubernetesHelper.getName(template);
    if (Strings.isNotBlank(name)) {
        // lets merge all the fabric8 annotations using the template id qualifier as a postfix
        Map<String, String> annotations = KubernetesHelper.getOrCreateAnnotations(firstTemplate);
        Map<String, String> otherAnnotations = KubernetesHelper.getOrCreateAnnotations(template);
        Set<Map.Entry<String, String>> entries = otherAnnotations.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            String key = entry.getKey();
            String value = entry.getValue();
            if (!annotations.containsKey(key)) {
                annotations.put(key, value);
            }
        }
    }
    return firstTemplate;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Parameter(io.fabric8.openshift.api.model.Parameter) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Parameter (io.fabric8.openshift.api.model.Parameter)11 HashMap (java.util.HashMap)6 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)4 File (java.io.File)3 HashSet (java.util.HashSet)3 TreeSet (java.util.TreeSet)3 Configuration (io.fabric8.annotations.Configuration)2 Endpoint (io.fabric8.annotations.Endpoint)2 External (io.fabric8.annotations.External)2 Path (io.fabric8.annotations.Path)2 PortName (io.fabric8.annotations.PortName)2 Protocol (io.fabric8.annotations.Protocol)2 ServiceName (io.fabric8.annotations.ServiceName)2 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)2 Type (java.lang.reflect.Type)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Map (java.util.Map)2 Set (java.util.Set)2 KubernetesHelper.createIntOrString (io.fabric8.kubernetes.api.KubernetesHelper.createIntOrString)1