Search in sources :

Example 16 with DeploymentBuilder

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();
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 17 with DeploymentBuilder

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);
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) Quantity(io.fabric8.kubernetes.api.model.Quantity) PersistentVolumeClaimVolumeSourceBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimVolumeSourceBuilder) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Volume(io.fabric8.kubernetes.api.model.Volume) ContainerPortBuilder(io.fabric8.kubernetes.api.model.ContainerPortBuilder) ConfigMapVolumeSourceBuilder(io.fabric8.kubernetes.api.model.ConfigMapVolumeSourceBuilder) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 18 with DeploymentBuilder

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);
}
Also used : PodData(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment.PodData) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Pod(io.fabric8.kubernetes.api.model.Pod) InternalRecipe(org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Example 19 with DeploymentBuilder

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");
}
Also used : PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) InternalRecipe(org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe) PodTemplateSpecBuilder(io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder) KubernetesEnvironment(org.eclipse.che.workspace.infrastructure.kubernetes.environment.KubernetesEnvironment) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Example 20 with DeploymentBuilder

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);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Aggregations

DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)153 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)80 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)60 Test (org.junit.Test)60 Test (org.junit.jupiter.api.Test)32 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)26 Expectations (mockit.Expectations)26 Test (org.testng.annotations.Test)22 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)17 ArrayList (java.util.ArrayList)17 Container (io.fabric8.kubernetes.api.model.Container)16 HashMap (java.util.HashMap)16 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)15 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)15 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)14 Properties (java.util.Properties)14 Pod (io.fabric8.kubernetes.api.model.Pod)13 PodTemplateSpecBuilder (io.fabric8.kubernetes.api.model.PodTemplateSpecBuilder)13 ObjectMetaBuilder (io.fabric8.kubernetes.api.model.ObjectMetaBuilder)12 InternalRecipe (org.eclipse.che.api.workspace.server.spi.environment.InternalRecipe)12