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