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();
}
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);
}
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()));
}
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());
}
}
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;
}
Aggregations