Search in sources :

Example 76 with Template

use of io.fabric8.openshift.api.model.Template in project syndesis-qe by syndesisio.

the class WildFlyS2i method deploy.

@Override
public void deploy() {
    if (!isDeployed()) {
        Template template;
        try (InputStream is = ClassLoader.getSystemResourceAsStream("templates/syndesis-wildfly.yml")) {
            template = OpenShiftUtils.getInstance().templates().load(is).get();
        } catch (IOException ex) {
            throw new IllegalArgumentException("Unable to read template ", ex);
        }
        Map<String, String> templateParams = new HashMap<>();
        templateParams.put("GITHUB_REPO", gitURL);
        templateParams.put("APPLICATION_NAME", appName);
        templateParams.put("SOURCE_REF", branch == null ? "master" : branch);
        OpenShiftUtils.getInstance().templates().withName(appName).delete();
        OpenShiftUtils.getInstance().imageStreams().createOrReplaceWithNew().editOrNewMetadata().withName("wildfly-130-centos7").addToLabels("app", appName).endMetadata().editOrNewSpec().editOrNewLookupPolicy().withLocal(false).endLookupPolicy().addNewTag().addToAnnotations("openshift.io/imported-from", "quay.io/syndesis_qe/wildfly-130-centos7").editOrNewFrom().withKind("DockerImage").withName("quay.io/syndesis_qe/wildfly-130-centos7").endFrom().withName("latest").editOrNewReferencePolicy().withType("Source").endReferencePolicy().endTag().endSpec().done();
        OpenShiftUtils.getInstance().createResources(OpenShiftUtils.getInstance().recreateAndProcessTemplate(template, templateParams));
    }
}
Also used : HashMap(java.util.HashMap) InputStream(java.io.InputStream) IOException(java.io.IOException) Template(io.fabric8.openshift.api.model.Template)

Example 77 with Template

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

the class Controller method doCreateTemplate.

protected void doCreateTemplate(Template entity, String namespace, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient != null && openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.TEMPLATE)) {
        LOG.info("Creating a Template from " + sourceName + " namespace " + namespace + " name " + getName(entity));
        try {
            Object answer = openShiftClient.templates().inNamespace(namespace).create(entity);
            logGeneratedEntity("Created Template: ", namespace, entity, answer);
        } catch (Exception e) {
            onApplyError("Failed to Template entity from " + sourceName + ". " + e + ". " + entity, e);
        }
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) 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)

Example 78 with Template

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

the class Controller method installTemplate.

/**
 * Installs the template into the namespace without processing it
 */
public void installTemplate(Template entity, String sourceName) {
    OpenShiftClient openShiftClient = getOpenShiftClientOrNull();
    if (openShiftClient == null || !openShiftClient.supportsOpenShiftAPIGroup(OpenShiftAPIGroups.TEMPLATE)) {
        // lets not install the template on Kubernetes!
        return;
    }
    if (!isProcessTemplatesLocally()) {
        String namespace = getNamespace();
        String id = getName(entity);
        Objects.notNull(id, "No name for " + entity + " " + sourceName);
        Template old = openShiftClient.templates().inNamespace(namespace).withName(id).get();
        if (isRunning(old)) {
            if (UserConfigurationCompare.configEqual(entity, old)) {
                LOG.info("Template has not changed so not doing anything");
            } else {
                boolean recreateMode = isRecreateMode();
                // TODO seems you can't update templates right now
                recreateMode = true;
                if (recreateMode) {
                    openShiftClient.templates().inNamespace(namespace).withName(id).delete();
                    doCreateTemplate(entity, namespace, sourceName);
                } else {
                    LOG.info("Updating a Template from " + sourceName);
                    try {
                        Object answer = openShiftClient.templates().inNamespace(namespace).withName(id).replace(entity);
                        LOG.info("Updated Template: " + answer);
                    } catch (Exception e) {
                        onApplyError("Failed to update Template from " + sourceName + ". " + e + ". " + entity, e);
                    }
                }
            }
        } else {
            if (!isAllowCreate()) {
                LOG.warn("Creation disabled so not creating a Template from " + sourceName + " namespace " + namespace + " name " + getName(entity));
            } else {
                doCreateTemplate(entity, namespace, sourceName);
            }
        }
    }
}
Also used : OpenShiftClient(io.fabric8.openshift.client.OpenShiftClient) 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) Template(io.fabric8.openshift.api.model.Template)

Example 79 with Template

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

the class KubernetesUtils method getResourceRequirements.

/**
 * Get resource requirements from memory and cpu.
 *
 * @param resourceRequirements resource requirements in pod template
 * @param mem Memory in mb.
 * @param memoryLimitFactor limit factor for the memory, used to set the limit resources.
 * @param cpu cpu.
 * @param cpuLimitFactor limit factor for the cpu, used to set the limit resources.
 * @param externalResources external resources
 * @param externalResourceConfigKeys config keys of external resources
 * @return KubernetesResource requirements.
 */
public static ResourceRequirements getResourceRequirements(ResourceRequirements resourceRequirements, int mem, double memoryLimitFactor, double cpu, double cpuLimitFactor, Map<String, ExternalResource> externalResources, Map<String, String> externalResourceConfigKeys) {
    final Quantity cpuQuantity = new Quantity(String.valueOf(cpu));
    final Quantity cpuLimitQuantity = new Quantity(String.valueOf(cpu * cpuLimitFactor));
    final Quantity memQuantity = new Quantity(mem + Constants.RESOURCE_UNIT_MB);
    final Quantity memQuantityLimit = new Quantity(((int) (mem * memoryLimitFactor)) + Constants.RESOURCE_UNIT_MB);
    ResourceRequirementsBuilder resourceRequirementsBuilder = new ResourceRequirementsBuilder(resourceRequirements).addToRequests(Constants.RESOURCE_NAME_MEMORY, memQuantity).addToRequests(Constants.RESOURCE_NAME_CPU, cpuQuantity).addToLimits(Constants.RESOURCE_NAME_MEMORY, memQuantityLimit).addToLimits(Constants.RESOURCE_NAME_CPU, cpuLimitQuantity);
    // Add the external resources to resource requirement.
    for (Map.Entry<String, ExternalResource> externalResource : externalResources.entrySet()) {
        final String configKey = externalResourceConfigKeys.get(externalResource.getKey());
        if (!StringUtils.isNullOrWhitespaceOnly(configKey)) {
            final Quantity resourceQuantity = new Quantity(String.valueOf(externalResource.getValue().getValue().longValue()));
            resourceRequirementsBuilder.addToRequests(configKey, resourceQuantity).addToLimits(configKey, resourceQuantity);
            LOG.info("Request external resource {} with config key {}.", resourceQuantity.getAmount(), configKey);
        }
    }
    return resourceRequirementsBuilder.build();
}
Also used : ResourceRequirementsBuilder(io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder) Quantity(io.fabric8.kubernetes.api.model.Quantity) KubernetesConfigMap(org.apache.flink.kubernetes.kubeclient.resources.KubernetesConfigMap) Map(java.util.Map) HashMap(java.util.HashMap) ExternalResource(org.apache.flink.api.common.resources.ExternalResource)

Example 80 with Template

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

the class KubernetesUtils method loadPodFromTemplateFile.

public static FlinkPod loadPodFromTemplateFile(FlinkKubeClient kubeClient, File podTemplateFile, String mainContainerName) {
    final KubernetesPod pod = kubeClient.loadPodFromTemplateFile(podTemplateFile);
    final List<Container> otherContainers = new ArrayList<>();
    Container mainContainer = null;
    for (Container container : pod.getInternalResource().getSpec().getContainers()) {
        if (mainContainerName.equals(container.getName())) {
            mainContainer = container;
        } else {
            otherContainers.add(container);
        }
    }
    if (mainContainer == null) {
        LOG.info("Could not find main container {} in pod template, using empty one to initialize.", mainContainerName);
        mainContainer = new ContainerBuilder().build();
    }
    pod.getInternalResource().getSpec().setContainers(otherContainers);
    return new FlinkPod(pod.getInternalResource(), mainContainer);
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) FlinkPod(org.apache.flink.kubernetes.kubeclient.FlinkPod) ArrayList(java.util.ArrayList) KubernetesPod(org.apache.flink.kubernetes.kubeclient.resources.KubernetesPod)

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