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