Search in sources :

Example 66 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 67 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)

Example 68 with Template

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

the class DecoratorWithPodTemplateTestBase method testPodTolerationsMerging.

@Test
public void testPodTolerationsMerging() {
    final List<Toleration> expectedTolerations = Arrays.asList(new Toleration("NoSchedule", "key1", "Equal", null, "value1"), // The toleration from pod template
    new Toleration("NoExecute", "key2-of-pod-template", "Exists", 6000L, null));
    assertThat(this.resultPod.getPodWithoutMainContainer().getSpec().getTolerations(), Matchers.containsInAnyOrder(expectedTolerations.toArray()));
}
Also used : Toleration(io.fabric8.kubernetes.api.model.Toleration) Test(org.junit.Test)

Example 69 with Template

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

the class K8sRemoteInterpreterProcess method apply.

/**
 * Apply spec file(s) in the path.
 * @param path Path to the K8s resources
 * @param delete set to true, the K8s resources are deleted
 * @param templateProperties properties to enrich the template
 */
void apply(File path, boolean delete, Properties templateProperties) throws IOException {
    if (path.getName().startsWith(".") || path.isHidden() || path.getName().endsWith("~")) {
        LOGGER.info("Skip {}", path.getAbsolutePath());
        return;
    }
    if (path.isDirectory()) {
        File[] files = path.listFiles();
        Arrays.sort(files);
        if (delete) {
            ArrayUtils.reverse(files);
        }
        for (File f : files) {
            apply(f, delete, templateProperties);
        }
    } else if (path.isFile()) {
        K8sSpecTemplate specTemplate = new K8sSpecTemplate();
        specTemplate.loadProperties(templateProperties);
        String template = specTemplate.render(path);
        ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable<HasMetadata> k8sObjects = client.load(IOUtils.toInputStream(template, StandardCharsets.UTF_8));
        LOGGER.info("Apply {} with {} K8s Objects", path.getAbsolutePath(), k8sObjects.get().size());
        LOGGER.debug(template);
        if (delete) {
            k8sObjects.inNamespace(interpreterNamespace).delete();
        } else {
            k8sObjects.inNamespace(interpreterNamespace).createOrReplace();
        }
    } else {
        LOGGER.error("Can't apply {}", path.getAbsolutePath());
    }
}
Also used : ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable(io.fabric8.kubernetes.client.dsl.ParameterNamespaceListVisitFromServerGetDeleteRecreateWaitApplicable) File(java.io.File)

Example 70 with Template

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

the class SessionListener method expandTemplate.

protected Object expandTemplate(Controller controller, Configuration configuration, Logger log, String namespace, String sourceName, Object dto) {
    if (dto instanceof Template) {
        Template template = (Template) dto;
        KubernetesHelper.setNamespace(template, namespace);
        String parameterNamePrefix = "";
        overrideTemplateParameters(template, configuration.getProperties(), parameterNamePrefix);
        log.status("Applying template in namespace " + namespace);
        controller.installTemplate(template, sourceName);
        dto = controller.processTemplate(template, sourceName);
        if (dto == null) {
            throw new IllegalArgumentException("Failed to process Template!");
        }
    }
    return dto;
}
Also used : Util.readAsString(io.fabric8.arquillian.utils.Util.readAsString) Template(io.fabric8.openshift.api.model.Template)

Aggregations

Template (io.fabric8.openshift.api.model.Template)23 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)19 File (java.io.File)14 IOException (java.io.IOException)13 HashMap (java.util.HashMap)12 Test (org.junit.Test)12 KubernetesList (io.fabric8.kubernetes.api.model.KubernetesList)11 Service (io.fabric8.kubernetes.api.model.Service)10 Container (io.fabric8.kubernetes.api.model.Container)8 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)7 OpenShiftHelper (io.vertx.it.openshift.utils.OpenShiftHelper)7 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 BeforeClass (org.junit.BeforeClass)7 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)6 TreeMap (java.util.TreeMap)6 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)5 Parameter (io.fabric8.openshift.api.model.Parameter)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4