use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project che-server by eclipse-che.
the class DockerimageComponentToWorkspaceApplier method buildDeployment.
private Deployment buildDeployment(String name, String image, String memoryRequest, String memoryLimit, String cpuRequest, String cpuLimit, List<EnvVar> env, List<String> command, List<String> args) {
Container container = new ContainerBuilder().withImage(image).withImagePullPolicy(imagePullPolicy).withName(name).withEnv(env).withCommand(command).withArgs(args).build();
Containers.addRamLimit(container, memoryLimit);
if (!isNullOrEmpty(memoryRequest)) {
Containers.addRamRequest(container, memoryRequest);
}
if (!isNullOrEmpty(cpuRequest)) {
Containers.addCpuRequest(container, KubernetesSize.toCores(cpuRequest));
}
if (!isNullOrEmpty(cpuLimit)) {
Containers.addCpuLimit(container, KubernetesSize.toCores(cpuLimit));
}
return new DeploymentBuilder().withNewMetadata().addToLabels(CHE_COMPONENT_NAME_LABEL, name).withName(name).endMetadata().withNewSpec().withNewSelector().addToMatchLabels(CHE_COMPONENT_NAME_LABEL, name).endSelector().withNewTemplate().withNewMetadata().withName(name).addToLabels(CHE_COMPONENT_NAME_LABEL, name).addToAnnotations(Names.createMachineNameAnnotations(name, name)).endMetadata().withNewSpec().withContainers(container).endSpec().endTemplate().endSpec().build();
}
use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.
the class AsyncStorageProvisioner method createAsyncStoragePodIfNotExist.
/**
* Create storage Pod with container with mounted volume for storing project source backups, SSH
* key and exposed port for rsync connection
*/
private void createAsyncStoragePodIfNotExist(KubernetesClient k8sClient, String namespace, String configMap, String userId) {
RollableScalableResource<Deployment> resource = k8sClient.apps().deployments().inNamespace(namespace).withName(ASYNC_STORAGE);
if (resource.get() != null) {
// deployment already exist
return;
}
String containerName = Names.generateName(ASYNC_STORAGE);
Volume storageVolume = new VolumeBuilder().withName(STORAGE_VOLUME).withPersistentVolumeClaim(new PersistentVolumeClaimVolumeSourceBuilder().withClaimName(pvcName).withReadOnly(false).build()).build();
Volume sshKeyVolume = new VolumeBuilder().withName(CONFIG_MAP_VOLUME_NAME).withConfigMap(new ConfigMapVolumeSourceBuilder().withName(configMap).withDefaultMode(0600).build()).build();
VolumeMount storageVolumeMount = new VolumeMountBuilder().withMountPath(ASYNC_STORAGE_DATA_PATH).withName(STORAGE_VOLUME).withReadOnly(false).build();
VolumeMount sshVolumeMount = new VolumeMountBuilder().withMountPath(SSH_KEY_PATH).withSubPath(AUTHORIZED_KEYS).withName(CONFIG_MAP_VOLUME_NAME).withReadOnly(true).build();
Container container = new ContainerBuilder().withName(containerName).withImage(asyncStorageImage).withImagePullPolicy(sidecarImagePullPolicy).withNewResources().addToLimits("memory", new Quantity("512Mi")).addToRequests("memory", new Quantity("256Mi")).endResources().withPorts(new ContainerPortBuilder().withContainerPort(SERVICE_PORT).withProtocol("TCP").build()).withVolumeMounts(storageVolumeMount, sshVolumeMount).build();
PodSpecBuilder podSpecBuilder = new PodSpecBuilder();
PodSpec podSpec = podSpecBuilder.withContainers(container).withVolumes(storageVolume, sshKeyVolume).build();
ObjectMetaBuilder metaBuilder = new ObjectMetaBuilder();
ObjectMeta meta = metaBuilder.withLabels(of("app", "che", CHE_USER_ID_LABEL, userId, CHE_DEPLOYMENT_NAME_LABEL, ASYNC_STORAGE)).withNamespace(namespace).withName(ASYNC_STORAGE).build();
Deployment deployment = new DeploymentBuilder().withMetadata(meta).withNewSpec().withNewSelector().withMatchLabels(meta.getLabels()).endSelector().withReplicas(1).withNewTemplate().withMetadata(meta).withSpec(podSpec).endTemplate().endSpec().build();
k8sClient.apps().deployments().inNamespace(namespace).create(deployment);
}
use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.
the class OpenShiftEnvironmentFactoryTest method shouldMergeDeploymentAndPodIntoOneDeployment.
@Test
public void shouldMergeDeploymentAndPodIntoOneDeployment() throws Exception {
// given
PodTemplateSpec podTemplate = new PodTemplateSpecBuilder().withNewMetadata().withName("deployment-pod").endMetadata().withNewSpec().endSpec().build();
Deployment deployment = new DeploymentBuilder().withNewMetadata().withName("deployment-test").endMetadata().withNewSpec().withTemplate(podTemplate).endSpec().build();
Pod pod = new PodBuilder().withNewMetadata().withName("bare-pod").endMetadata().withNewSpec().endSpec().build();
when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(deployment, pod));
Deployment merged = createEmptyDeployment("merged");
when(podMerger.merge(any())).thenReturn(merged);
// when
final KubernetesEnvironment k8sEnv = osEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());
// then
verify(podMerger).merge(asList(new PodData(pod), new PodData(deployment)));
assertEquals(k8sEnv.getPodsData().size(), 1);
assertTrue(k8sEnv.getPodsCopy().isEmpty());
assertEquals(k8sEnv.getDeploymentsCopy().size(), 1);
assertEquals(k8sEnv.getDeploymentsCopy().get("merged"), merged);
}
use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.
the class OpenShiftEnvironmentFactoryTest method shouldUseDeploymentNameAsPodTemplateNameIfItIsMissing.
@Test
public void shouldUseDeploymentNameAsPodTemplateNameIfItIsMissing() throws Exception {
// given
PodTemplateSpec podTemplate = new PodTemplateSpecBuilder().withNewSpec().endSpec().build();
Deployment deployment = new DeploymentBuilder().withNewMetadata().withName("deployment-test").endMetadata().withNewSpec().withTemplate(podTemplate).endSpec().build();
when(k8sRecipeParser.parse(any(InternalRecipe.class))).thenReturn(asList(deployment));
// when
final KubernetesEnvironment k8sEnv = osEnvFactory.doCreate(internalRecipe, emptyMap(), emptyList());
// then
Deployment deploymentTest = k8sEnv.getDeploymentsCopy().get("deployment-test");
assertNotNull(deploymentTest);
PodTemplateSpec resultPodTemplate = deploymentTest.getSpec().getTemplate();
assertEquals(resultPodTemplate.getMetadata().getName(), "deployment-test");
}
use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.
the class KubernetesDeployments method deploy.
/**
* Starts the specified Pod via a Deployment.
*
* @param pod pod to deploy
* @return created pod
* @throws InfrastructureException when any exception occurs
*/
public Pod deploy(Pod pod) throws InfrastructureException {
putLabel(pod, CHE_WORKSPACE_ID_LABEL, workspaceId);
// Since we use the pod's metadata as the deployment's metadata
// This is used to identify the pod in CreateWatcher.
String originalName = pod.getMetadata().getName();
putLabel(pod, CHE_DEPLOYMENT_NAME_LABEL, originalName);
ObjectMeta metadata = pod.getMetadata();
PodSpec podSpec = pod.getSpec();
// Only allowable value
podSpec.setRestartPolicy("Always");
Deployment deployment = new DeploymentBuilder().withMetadata(metadata).withNewSpec().withNewSelector().withMatchLabels(metadata.getLabels()).endSelector().withReplicas(1).withNewTemplate().withMetadata(metadata).withSpec(podSpec).endTemplate().endSpec().build();
return createDeployment(deployment, workspaceId);
}
Aggregations