Search in sources :

Example 71 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project strimzi by strimzi.

the class AbstractModel method createDeployment.

protected Deployment createDeployment(List<ContainerPort> ports, Probe livenessProbe, Probe readinessProbe, DeploymentStrategy updateStrategy, Map<String, String> deploymentAnnotations, Map<String, String> podAnnotations) {
    Container container = new ContainerBuilder().withName(name).withImage(getImage()).withEnv(getEnvVars()).withPorts(ports).withLivenessProbe(livenessProbe).withReadinessProbe(readinessProbe).build();
    Deployment dep = new DeploymentBuilder().withNewMetadata().withName(name).withLabels(getLabelsWithName()).withNamespace(namespace).withAnnotations(deploymentAnnotations).endMetadata().withNewSpec().withStrategy(updateStrategy).withReplicas(replicas).withNewTemplate().withNewMetadata().withLabels(getLabelsWithName()).withAnnotations(podAnnotations).endMetadata().withNewSpec().withServiceAccountName(getServiceAccountName()).withContainers(container).endSpec().endTemplate().endSpec().build();
    return dep;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 72 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project elastest-torm by elastest.

the class K8sService method deployResourcesFromProject.

public List<PodInfo> deployResourcesFromProject(String projectName, String namespace) throws Exception {
    PodInfo podInfo = new PodInfo();
    List<PodInfo> podsInfoList = new ArrayList<>();
    DockerProject project = projects.get(projectName);
    logger.debug("Project to deploy: {}", projectName);
    logger.debug("Resources as string: {}", project.getYml());
    namespace = namespace == null || namespace.isEmpty() ? DEFAULT_NAMESPACE : namespace;
    try (InputStream is = IOUtils.toInputStream(project.getYml(), CharEncoding.UTF_8)) {
        List<HasMetadata> resourcesMetadata = client.load(is).inNamespace(namespace).get();
        project.getEnv().put("ETM_K8S_API_TOKEN", getServiceAccountToken());
        project.getEnv().put("ETM_K8S_API_URL", client.getConfiguration().getMasterUrl());
        logger.debug("Add these environment variables:");
        project.getEnv().forEach((key, value) -> {
            logger.debug("Env var {} with value {}", key, value);
        });
        logger.debug("Number of deployments loaded: {}", resourcesMetadata.size());
        for (HasMetadata metadata : resourcesMetadata) {
            switch(metadata.getKind()) {
                case "Deployment":
                    String deploymentName = ((Deployment) metadata).getMetadata().getName();
                    logger.debug("Editing and deploying service {}:", deploymentName);
                    DeploymentBuilder deploymentBuilder = new DeploymentBuilder(((Deployment) metadata));
                    Deployment deployment = deploymentBuilder.editSpec().editTemplate().editMetadata().addToLabels(project.getLabels()).endMetadata().editSpec().editContainer(0).addAllToEnv(getEnvVarListFromMap(project.getEnv())).endContainer().endSpec().endTemplate().endSpec().build();
                    logger.debug("Deployment {} content: {}", deploymentName, deployment.toString());
                    client.apps().deployments().inNamespace(namespace).createOrReplace(deployment);
                    client.apps().deployments().inNamespace(namespace).withName(deploymentName).waitUntilReady(5, TimeUnit.MINUTES);
                    break;
                case "PersistentVolumeClaim":
                    PersistentVolumeClaimBuilder pVCBuilder = new PersistentVolumeClaimBuilder((PersistentVolumeClaim) metadata);
                    if (client.persistentVolumeClaims().inNamespace(namespace).withName("elastest-" + projectName + "-data-claim").get() == null) {
                        client.persistentVolumeClaims().inNamespace(namespace).create(pVCBuilder.build());
                    }
                    break;
                case "Service":
                    logger.debug("Creating k8s service \"{}\" with data {}", ((Service) metadata).getMetadata().getName(), metadata.toString());
                    ServiceBuilder serviceBuilder = new ServiceBuilder((Service) metadata);
                    if (client.services().inNamespace(namespace).withName(projectName).get() == null) {
                        client.services().inNamespace(namespace).create(serviceBuilder.build());
                    }
                default:
                    break;
            }
        }
    } catch (IOException | InterruptedException e) {
        logger.error("Error loading deployment from a yaml string");
        e.printStackTrace();
        throw e;
    }
    return podsInfoList;
}
Also used : HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) PersistentVolumeClaimBuilder(io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder) DoneableService(io.fabric8.kubernetes.api.model.DoneableService) Service(io.fabric8.kubernetes.api.model.Service) IOException(java.io.IOException) ServiceBuilder(io.fabric8.kubernetes.api.model.ServiceBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) DockerProject(io.elastest.epm.client.json.DockerProject)

Example 73 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project strimzi by strimzi.

the class KafkaClientsTemplates method kafkaClients.

public static DeploymentBuilder kafkaClients(String namespaceName, boolean tlsListener, String kafkaClientsName, boolean hostnameVerification, String listenerName, String secretPrefix, KafkaUser... kafkaUsers) {
    Map<String, String> label = new HashMap<>();
    label.put(Constants.KAFKA_CLIENTS_LABEL_KEY, Constants.KAFKA_CLIENTS_LABEL_VALUE);
    label.put(Constants.DEPLOYMENT_TYPE, DeploymentTypes.KafkaClients.name());
    DeploymentBuilder kafkaClient = new DeploymentBuilder().withNewMetadata().withName(kafkaClientsName).withLabels(label).withNamespace(namespaceName).endMetadata().withNewSpec().withNewSelector().addToMatchLabels("app", kafkaClientsName).addToMatchLabels(label).endSelector().withReplicas(1).withNewTemplate().withNewMetadata().addToLabels("app", kafkaClientsName).addToLabels(label).endMetadata().withSpec(createClientSpec(namespaceName, tlsListener, kafkaClientsName, hostnameVerification, listenerName, secretPrefix, kafkaUsers)).endTemplate().endSpec();
    return kafkaClient;
}
Also used : HashMap(java.util.HashMap) TestUtils.toYamlString(io.strimzi.test.TestUtils.toYamlString) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 74 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project strimzi by strimzi.

the class ScraperTemplates method scraperPod.

public static DeploymentBuilder scraperPod(String namespaceName, String podName) {
    Map<String, String> label = new HashMap<>();
    label.put(Constants.SCRAPER_LABEL_KEY, Constants.SCRAPER_LABEL_VALUE);
    label.put(Constants.DEPLOYMENT_TYPE, DeploymentTypes.Scraper.name());
    return new DeploymentBuilder().withNewMetadata().withName(podName).withLabels(label).withNamespace(namespaceName).endMetadata().withNewSpec().withNewSelector().addToMatchLabels("app", podName).addToMatchLabels(label).endSelector().withReplicas(1).withNewTemplate().withNewMetadata().addToLabels("app", podName).addToLabels(label).endMetadata().withNewSpec().withContainers(new ContainerBuilder().withName(podName).withImage("registry.access.redhat.com/ubi8/openjdk-11:latest").withCommand("sleep").withArgs("infinity").withImagePullPolicy(Environment.COMPONENTS_IMAGE_PULL_POLICY).withResources(new ResourceRequirementsBuilder().addToRequests("memory", new Quantity("200M")).build()).build()).endSpec().endTemplate().endSpec();
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) HashMap(java.util.HashMap) ResourceRequirementsBuilder(io.fabric8.kubernetes.api.model.ResourceRequirementsBuilder) Quantity(io.fabric8.kubernetes.api.model.Quantity) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)

Example 75 with DeploymentBuilder

use of io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder in project strimzi by strimzi.

the class MockKube2ControllersTest method testDeploymentController.

@Test
public void testDeploymentController() {
    final String deploymentName = "my-deployment";
    client.apps().deployments().inNamespace(NAMESPACE).create(new DeploymentBuilder().withNewMetadata().withNamespace(NAMESPACE).withName(deploymentName).endMetadata().withNewSpec().withReplicas(3).withSelector(new LabelSelector(null, Map.of("app", "my-app"))).withNewTemplate().withNewSpec().withContainers(new ContainerBuilder().withName("nginx").withImage("nginx:1.14.2").build()).endSpec().endTemplate().endSpec().build());
    TestUtils.waitFor("Wait for deployment to have status", 100L, 10_000L, () -> client.apps().deployments().inNamespace(NAMESPACE).withName(deploymentName).get() != null && client.apps().deployments().inNamespace(NAMESPACE).withName(deploymentName).get().getStatus() != null);
    Deployment createdDeployment = client.apps().deployments().inNamespace(NAMESPACE).withName(deploymentName).get();
    assertThat(createdDeployment, is(notNullValue()));
    assertThat(createdDeployment.getStatus(), is(notNullValue()));
    assertThat(createdDeployment.getStatus().getReplicas(), is(3));
    assertThat(createdDeployment.getStatus().getAvailableReplicas(), is(3));
    assertThat(createdDeployment.getStatus().getObservedGeneration(), is(1L));
}
Also used : ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) IntOrString(io.fabric8.kubernetes.api.model.IntOrString) DeploymentBuilder(io.fabric8.kubernetes.api.model.apps.DeploymentBuilder) Test(org.junit.jupiter.api.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