Search in sources :

Example 21 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.

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 22 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.

the class KubernetesEnvironmentProvisionerTest method shouldProvisionComponentObjectsIntoExistingKubernetesRecipe.

@Test
public void shouldProvisionComponentObjectsIntoExistingKubernetesRecipe() throws Exception {
    // given
    workspaceConfig.setDefaultEnv("default");
    RecipeImpl existingRecipe = new RecipeImpl(KUBERNETES_COMPONENT_TYPE, "yaml", "existing-content", null);
    workspaceConfig.getEnvironments().put("default", new EnvironmentImpl(existingRecipe, emptyMap()));
    List<HasMetadata> recipeObjects = new ArrayList<>();
    Deployment recipeDeployment = new DeploymentBuilder().withNewMetadata().withName("db").endMetadata().withNewSpec().endSpec().build();
    recipeObjects.add(new DeploymentBuilder(recipeDeployment).build());
    doReturn(recipeObjects).when(k8sRecipeParser).parse(anyString());
    List<HasMetadata> componentsObject = new ArrayList<>();
    Deployment componentDeployment = new DeploymentBuilder().withNewMetadata().withName("web-app").endMetadata().withNewSpec().endSpec().build();
    componentsObject.add(new DeploymentBuilder(componentDeployment).build());
    // when
    k8sEnvProvisioner.provision(workspaceConfig, KubernetesEnvironment.TYPE, componentsObject, emptyMap());
    // then
    // it is expected that applier wrap original recipes objects in new Kubernetes list
    KubernetesList expectedKubernetesList = new KubernetesListBuilder().withItems(Arrays.asList(recipeDeployment, componentDeployment)).build();
    EnvironmentImpl resultEnv = workspaceConfig.getEnvironments().get(workspaceConfig.getDefaultEnv());
    assertEquals(toK8SList(resultEnv.getRecipe().getContent()).getItems(), expectedKubernetesList.getItems());
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) RecipeImpl(org.eclipse.che.api.workspace.server.model.impl.RecipeImpl) ArrayList(java.util.ArrayList) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) EnvironmentImpl(org.eclipse.che.api.workspace.server.model.impl.EnvironmentImpl) KubernetesList(io.fabric8.kubernetes.api.model.KubernetesList) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Example 23 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.

the class KubernetesEnvironmentFactoryTest 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 = k8sEnvFactory.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) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Example 24 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.

the class KubernetesEnvironmentFactoryTest 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 = k8sEnvFactory.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) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

Example 25 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project devspaces-images by redhat-developer.

the class KubernetesDeploymentsTest method deploymentShouldHaveImagePullSecretsOfSAAndSelf.

@Test
public void deploymentShouldHaveImagePullSecretsOfSAAndSelf() throws InfrastructureException {
    doReturn(serverMock.getClient()).when(clientFactory).create(anyString());
    final String serviceAccountName = "workspace-sa";
    LocalObjectReference pullSecretOfSA = new LocalObjectReferenceBuilder().withName("pullsecret-sa").build();
    LocalObjectReference pullSecretOfPod = new LocalObjectReferenceBuilder().withName("pullsecret-pod").build();
    List<LocalObjectReference> imagePullSecrets = new ArrayList<>();
    imagePullSecrets.add(pullSecretOfPod);
    ServiceAccount serviceAccount = new ServiceAccountBuilder().withMetadata(new ObjectMetaBuilder().withName(serviceAccountName).withNamespace(kubernetesDeployments.namespace).build()).withImagePullSecrets(pullSecretOfSA).build();
    serverMock.getClient().serviceAccounts().inNamespace(kubernetesDeployments.namespace).create(serviceAccount);
    ObjectMeta objectMeta = new ObjectMetaBuilder().withName("test-pod").withNamespace(kubernetesDeployments.namespace).build();
    Deployment deployment = new DeploymentBuilder().withMetadata(objectMeta).withSpec(new DeploymentSpecBuilder().withNewTemplate().withMetadata(objectMeta).withNewSpec().withImagePullSecrets(imagePullSecrets).withServiceAccountName(serviceAccountName).endSpec().endTemplate().build()).build();
    kubernetesDeployments.addPullSecretsOfSA(deployment);
    imagePullSecrets.add(pullSecretOfSA);
    assertTrue(deployment.getSpec().getTemplate().getSpec().getImagePullSecrets().containsAll(imagePullSecrets));
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) DeploymentSpecBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentSpecBuilder) LocalObjectReference(io.fabric8.kubernetes.api.model.LocalObjectReference) ArrayList(java.util.ArrayList) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ObjectMetaBuilder(io.fabric8.kubernetes.api.model.ObjectMetaBuilder) LocalObjectReferenceBuilder(io.fabric8.kubernetes.api.model.LocalObjectReferenceBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.testng.annotations.Test)

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