Search in sources :

Example 11 with Template

use of io.fabric8.openshift.api.model.Template in project wildfly-camel by wildfly-extras.

the class KubernetesIntegrationTest method testKubernetesProducer.

@Test
public void testKubernetesProducer() throws Exception {
    String kubernetesProducerURI = String.format("kubernetes:%s?category=pods&operation=createPod", getKubernetesMaster());
    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("direct:start").to(kubernetesProducerURI);
        }
    });
    ProducerTemplate template = camelctx.createProducerTemplate();
    String podName = POD_NAME + "-producer";
    camelctx.start();
    KubernetesEndpoint kubernetesEndpoint = camelctx.getEndpoint(kubernetesProducerURI, KubernetesEndpoint.class);
    String kubernetesNamespace = getNamespace(kubernetesEndpoint.getKubernetesClient());
    try {
        Map<String, Object> headers = new HashMap<>();
        headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, kubernetesNamespace);
        headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
        headers.put(KubernetesConstants.KUBERNETES_POD_SPEC, createPodSpec());
        Pod pod = template.requestBodyAndHeaders("direct:start", null, headers, Pod.class);
        Assert.assertNotNull("Expected pod to be not null", pod);
        Assert.assertEquals("Pending", pod.getStatus().getPhase());
    } finally {
        // Clean up
        Map<String, Object> headers = new HashMap<>();
        headers.put(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, kubernetesNamespace);
        headers.put(KubernetesConstants.KUBERNETES_POD_NAME, podName);
        template.requestBodyAndHeaders("kubernetes:" + getKubernetesMaster() + "?category=pods&operation=deletePod", null, headers);
        camelctx.stop();
    }
}
Also used : DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) CamelContext(org.apache.camel.CamelContext) ProducerTemplate(org.apache.camel.ProducerTemplate) KubernetesEndpoint(org.apache.camel.component.kubernetes.KubernetesEndpoint) RouteBuilder(org.apache.camel.builder.RouteBuilder) Pod(io.fabric8.kubernetes.api.model.Pod) HashMap(java.util.HashMap) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8-maven-plugin by fabric8io.

the class DebugEnricher method enableDebugging.

private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
    if (template != null) {
        PodSpec podSpec = template.getSpec();
        if (podSpec != null) {
            List<Container> containers = podSpec.getContainers();
            if (containers.size() > 0) {
                Container container = containers.get(0);
                List<EnvVar> env = container.getEnv();
                if (env == null) {
                    env = new ArrayList<>();
                }
                String remoteDebugPort = KubernetesResourceUtil.getEnvVar(env, ENV_VAR_JAVA_DEBUG_PORT, ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                boolean enabled = false;
                if (KubernetesResourceUtil.setEnvVar(env, ENV_VAR_JAVA_DEBUG, "true")) {
                    container.setEnv(env);
                    enabled = true;
                }
                List<ContainerPort> ports = container.getPorts();
                if (ports == null) {
                    ports = new ArrayList<>();
                }
                if (KubernetesResourceUtil.addPort(ports, remoteDebugPort, "debug", log)) {
                    container.setPorts(ports);
                    enabled = true;
                }
                if (enabled) {
                    log.info("Enabling debug on " + getKind(entity) + " " + KubernetesHelper.getName(entity) + " due to the property: " + ENABLE_DEBUG_MAVEN_PROPERTY);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 13 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8-maven-plugin by fabric8io.

the class KubernetesResourceUtil method loadResources.

public static Set<HasMetadata> loadResources(File manifest) throws IOException {
    Object dto = KubernetesHelper.loadYaml(manifest, KubernetesResource.class);
    if (dto == null) {
        throw new IllegalStateException("Cannot load kubernetes YAML: " + manifest);
    }
    if (dto instanceof Template) {
        Template template = (Template) dto;
        boolean failOnMissingParameterValue = false;
        dto = Templates.processTemplatesLocally(template, failOnMissingParameterValue);
    }
    Set<KubernetesResource<?>> resources = new LinkedHashSet<>();
    Set<HasMetadata> entities = new TreeSet<>(new HasMetadataComparator());
    for (KubernetesResource<?> resource : resources) {
        entities.addAll(KubernetesHelper.toItemList(resource));
    }
    entities.addAll(KubernetesHelper.toItemList(dto));
    return entities;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) HasMetadataComparator(io.fabric8.kubernetes.internal.HasMetadataComparator) TreeSet(java.util.TreeSet) KubernetesResource(io.fabric8.kubernetes.api.model.KubernetesResource) Template(io.fabric8.openshift.api.model.Template)

Example 14 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8-maven-plugin by fabric8io.

the class OpenShiftDependencyResources method addMissingParameters.

public void addMissingParameters(Template template) {
    List<Parameter> parameters = template.getParameters();
    if (parameters == null) {
        parameters = new ArrayList<>();
    }
    Map<String, Parameter> map = new HashMap<>();
    for (Parameter parameter : parameters) {
        map.put(parameter.getName(), parameter);
    }
    mergeParametersIntoMap(map, parameters);
    for (Parameter parameter : map.values()) {
        if (!parameters.contains(parameter)) {
            parameters.add(parameter);
        }
    }
    template.setParameters(parameters);
}
Also used : HashMap(java.util.HashMap) Parameter(io.fabric8.openshift.api.model.Parameter)

Example 15 with Template

use of io.fabric8.openshift.api.model.Template in project fabric8-maven-plugin by fabric8io.

the class OpenShiftDependencyResources method addOpenShiftResources.

public void addOpenShiftResources(List<HasMetadata> items, boolean isAppCatalog) {
    for (HasMetadata item : items) {
        if (item instanceof Template) {
            Template template = (Template) item;
            if (!KubernetesResourceUtil.isAppCatalogResource(template) && !isAppCatalog) {
                List<HasMetadata> objects = notNullList(template.getObjects());
                String sourceUrl = getSourceUrlAnnotation(template);
                if (Strings.isNotBlank(sourceUrl)) {
                    for (HasMetadata object : objects) {
                        setSourceUrlAnnotationIfNotSet(object, sourceUrl);
                    }
                }
                addOpenShiftResources(objects, isAppCatalog);
                mergeParametersIntoMap(templateParameters, notNullList(template.getParameters()));
                continue;
            }
        }
        KindAndName key = new KindAndName(item);
        HasMetadata old = openshiftDependencyResources.get(key);
        if (old != null && !isAppCatalog) {
            log.warn("Duplicate OpenShift resources for %s at %s and %s", key, getSourceUrlAnnotation(old), getSourceUrlAnnotation(item));
        }
        openshiftDependencyResources.put(key, item);
    }
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Template(io.fabric8.openshift.api.model.Template)

Aggregations

Template (io.fabric8.openshift.api.model.Template)32 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)24 IOException (java.io.IOException)20 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)16 HashMap (java.util.HashMap)15 Test (org.junit.Test)15 File (java.io.File)14 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)12 Service (io.fabric8.kubernetes.api.model.Service)11 Parameter (io.fabric8.openshift.api.model.Parameter)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 Container (io.fabric8.kubernetes.api.model.Container)8 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)8 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)8 OpenShiftNotAvailableException (io.fabric8.openshift.client.OpenShiftNotAvailableException)8 FileNotFoundException (java.io.FileNotFoundException)8 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)7 OpenShiftHelper (io.vertx.it.openshift.utils.OpenShiftHelper)7