Search in sources :

Example 1 with DockerProject

use of io.elastest.epm.client.json.DockerProject in project elastest-torm by elastest.

the class K8sService method deleteResources.

public boolean deleteResources(String projectName) {
    DockerProject project = projects.get(projectName);
    logger.debug("Remove resources described in this yml: {}", project.getYml());
    if (checkIfExistNamespace(projectName)) {
        return deleteNamespace(projectName);
    } else {
        return deleteResourcesFromYmlString(project.getYml(), null);
    }
}
Also used : DockerProject(io.elastest.epm.client.json.DockerProject)

Example 2 with DockerProject

use of io.elastest.epm.client.json.DockerProject in project elastest-torm by elastest.

the class K8sService method createk8sProject.

public DockerProject createk8sProject(String projectName, String serviceDescriptor, Map<String, String> envs, List<String> extraHosts, Map<String, String> labels) {
    logger.debug("Store new project \"{}\" with this deployment manifest -> {}", projectName, serviceDescriptor);
    DockerProject project = new DockerProject(projectName, serviceDescriptor);
    if (envs != null) {
        project.getEnv().putAll(envs);
    }
    if (extraHosts != null) {
        project.getExtraHosts().addAll(extraHosts);
    }
    if (labels != null) {
        project.getLabels().putAll(labels);
    }
    projects.put(projectName, project);
    return project;
}
Also used : DockerProject(io.elastest.epm.client.json.DockerProject)

Example 3 with DockerProject

use of io.elastest.epm.client.json.DockerProject 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)

Aggregations

DockerProject (io.elastest.epm.client.json.DockerProject)3 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)1 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)1 PersistentVolumeClaimBuilder (io.fabric8.kubernetes.api.model.PersistentVolumeClaimBuilder)1 Service (io.fabric8.kubernetes.api.model.Service)1 ServiceBuilder (io.fabric8.kubernetes.api.model.ServiceBuilder)1 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)1 DeploymentBuilder (io.fabric8.kubernetes.api.model.apps.DeploymentBuilder)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 TarArchiveInputStream (org.apache.commons.compress.archivers.tar.TarArchiveInputStream)1