use of io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder in project elastest-torm by elastest.
the class K8sService method deployPod.
public PodInfo deployPod(DockerContainer container, String namespace) throws Exception {
PodInfo podInfo = new PodInfo();
Pod pod = null;
try {
namespace = namespace != null ? namespace : DEFAULT_NAMESPACE;
String podName = container.getContainerName().get();
logger.info("Deploying pod with name {} in namespace {}", podName, namespace);
if (container.getCmd().isPresent()) {
logger.info(String.join(",", container.getCmd().get()));
}
Map<String, String> k8sPobLabels = container.getLabels().get();
k8sPobLabels.put(LABEL_POD_NAME, podName);
String podNameWithoutUnderscore = podName.replace("_", "-");
k8sPobLabels.put(LABEL_COMPONENT, podNameWithoutUnderscore);
k8sPobLabels.put(LABEL_COMPONENT_TYPE, getETComponentType(podNameWithoutUnderscore));
// Create Container
ContainerBuilder containerBuilder = new ContainerBuilder();
containerBuilder.withName(podNameWithoutUnderscore).withImage(container.getImageId()).withEnv(getEnvVarListFromStringList(container.getEnvs().get()));
// Add ports
if (container.getExposedPorts().isPresent() && !container.getExposedPorts().get().isEmpty()) {
List<ContainerPort> ports = new ArrayList<>();
container.getExposedPorts().get().forEach(port -> {
ContainerPort containerPort = new ContainerPort();
containerPort.setContainerPort(new Integer(port));
ports.add(containerPort);
});
containerBuilder.withPorts(ports);
}
if (container.getCapAdd().isPresent() && !container.getCapAdd().get().isEmpty()) {
SecurityContextBuilder securityContextBuilder = new SecurityContextBuilder();
List<String> stringCapabilities = new ArrayList<>();
container.getCapAdd().get().forEach(cap -> {
stringCapabilities.add(cap);
});
Capabilities capabilities = new CapabilitiesBuilder().withAdd(stringCapabilities).build();
securityContextBuilder.withCapabilities(capabilities);
containerBuilder.withSecurityContext(securityContextBuilder.build());
}
// Add volumes if there are
List<Volume> volumes = new ArrayList<>();
List<VolumeMount> volumeMounts = new ArrayList<>();
if (container.getVolumeBindList().isPresent() && !container.getVolumeBindList().get().isEmpty()) {
int count = 0;
for (Bind dockerVolume : container.getVolumeBindList().get()) {
VolumeMount volumeMount = new VolumeMountBuilder().withName("v-" + count).withMountPath(dockerVolume.to()).build();
volumeMounts.add(volumeMount);
HostPathVolumeSource hostPath = new HostPathVolumeSourceBuilder().withPath(dockerVolume.to()).build();
Volume volume = new VolumeBuilder().withName("v-" + count).withHostPath(hostPath).build();
volumes.add(volume);
count++;
}
containerBuilder.withVolumeMounts(volumeMounts);
}
PodBuilder podBuilder = new PodBuilder();
// Set Labels if there are
if (container.getLabels().isPresent() && container.getLabels().get().size() > 0) {
k8sPobLabels.putAll(container.getLabels().get());
}
podBuilder.withNewMetadata().withName(podNameWithoutUnderscore).withLabels(k8sPobLabels).endMetadata().withNewSpec().addNewContainerLike(containerBuilder.build()).endContainer().withVolumes(volumes).endSpec();
podBuilder.buildSpec().getContainers().get(0);
pod = client.pods().inNamespace(namespace).createOrReplace(podBuilder.build());
logger.info("Pod with name {} has been created in namespace {}", podName, namespace);
logger.info("Waiting for Pod with name {} in namespace {}...", podName, namespace);
while (!isReady(podNameWithoutUnderscore, namespace)) {
UtilTools.sleep(1);
}
pod = client.pods().inNamespace(namespace).withName(podNameWithoutUnderscore).get();
if (pod == null) {
throw new Exception("the pod with name " + podName + " could not be obtained. Is null");
}
logger.debug("Pod with name {} ip: {}", podName, pod.getStatus().getPodIP());
} catch (final KubernetesClientException e) {
logger.error("Unable to create job", e);
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
podInfo.setPodIp(pod.getStatus().getPodIP());
podInfo.setPodName(pod.getMetadata().getName());
return podInfo;
}
use of io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder in project elastest-torm by elastest.
the class K8sService method createEtToolsVolume.
public HostPathVolumeSource createEtToolsVolume() throws IOException {
// TODO
String etToolsVolumePath = etDataInHost + "/" + etToolsInternalPath;
String etToolsVolumePathIntoEtm = etSharedFolder + "/" + etToolsInternalPath;
HostPathVolumeSource etToolsVolume = new HostPathVolumeSourceBuilder().withPath(etToolsVolumePath).build();
// Copy to volume
try {
// Method 1
// InputStream a = filesService
// .getResourceAsInputStream("/" + etToolsResourceFolderPath);
// logger.debug("aaaaaaaaaaaaaa {}", a);
//
// final InputStreamReader isr = new InputStreamReader(a,
// StandardCharsets.UTF_8);
// logger.debug("bbbbb");
// try (BufferedReader br = new BufferedReader(isr, 1024)) {
// String line;
// while ((line = br.readLine()) != null) {
// logger.info("File name (dev mode):" + line);
// // if (line.equals(fileName)) {
// // file = new ClassPathResource(path + line).getFile();
// // return file;
// // }
// }
// logger.debug("dddd");
// } catch (Exception e) {
// logger.error("cccc", e);
// }
// Method 2
List<File> tools = filesService.getFilesFromResources("/" + etToolsResourceFolderPath);
if (tools == null || tools.size() == 0) {
logger.debug("No tools found in {}", etToolsResourceFolderPath);
} else {
filesService.saveFilesInPath(tools, etToolsVolumePathIntoEtm);
logger.debug("EtTools copied to {} successfully", etToolsVolumePathIntoEtm);
}
// File sourceDirectoryFile = ResourceUtils
// .getFile(etToolsResourceFolderPath);
//
// if (!sourceDirectoryFile.exists()) { // Dev mode
// Resource resource = new ClassPathResource(
// etToolsResourceFolderPath);
// sourceDirectoryFile = resource.getFile();
// }
//
// File targetDirectoryFile = new File(etToolsVolumePathIntoEtm);
//
// FileUtils.copyDirectory(sourceDirectoryFile,
// targetDirectoryFile);
} catch (Exception e) {
logger.error("Error on create EtTools Volume", e);
}
return etToolsVolume;
}
use of io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder in project elastest-torm by elastest.
the class K8sService method createDaemonSetFromContainerInfo.
public boolean createDaemonSetFromContainerInfo(DockerContainer container, String namespace) {
Boolean created = false;
try {
logger.info("Creating Daemon Set from container name {} in namespace {}", container.getContainerName().get(), namespace);
if (container.getCmd().isPresent()) {
logger.info(String.join(",", container.getCmd().get()));
}
String containerNameWithoutUnderscore = container.getContainerName().get().replace("_", "-");
Map<String, String> k8sPobLabels = container.getLabels().get();
k8sPobLabels.put(LABEL_POD_NAME, container.getContainerName().get());
k8sPobLabels.put(LABEL_COMPONENT, containerNameWithoutUnderscore);
// Create Container
ContainerBuilder containerBuilder = new ContainerBuilder();
containerBuilder.withName(containerNameWithoutUnderscore).withImage(container.getImageId()).withEnv(getEnvVarListFromStringList(container.getEnvs().get()));
// Add ports
if (container.getExposedPorts().isPresent() && !container.getExposedPorts().get().isEmpty()) {
List<ContainerPort> ports = new ArrayList<>();
container.getExposedPorts().get().forEach(port -> {
ContainerPort containerPort = new ContainerPort();
containerPort.setContainerPort(new Integer(port));
ports.add(containerPort);
});
containerBuilder.withPorts(ports);
}
if (container.getCapAdd().isPresent() && !container.getCapAdd().get().isEmpty()) {
SecurityContextBuilder securityContextBuilder = new SecurityContextBuilder();
List<String> stringCapabilities = new ArrayList<>();
container.getCapAdd().get().forEach(cap -> {
stringCapabilities.add(cap);
});
Capabilities capabilities = new CapabilitiesBuilder().withAdd(stringCapabilities).build();
securityContextBuilder.withCapabilities(capabilities);
containerBuilder.withSecurityContext(securityContextBuilder.build());
}
// Add volumes if there are
List<Volume> volumes = new ArrayList<>();
List<VolumeMount> volumeMounts = new ArrayList<>();
if (container.getVolumeBindList().isPresent() && !container.getVolumeBindList().get().isEmpty()) {
int count = 0;
for (Bind dockerVolume : container.getVolumeBindList().get()) {
VolumeMount volumeMount = new VolumeMountBuilder().withName("v-" + count).withMountPath(dockerVolume.to()).build();
volumeMounts.add(volumeMount);
HostPathVolumeSource hostPath = new HostPathVolumeSourceBuilder().withPath(dockerVolume.to()).build();
Volume volume = new VolumeBuilder().withName("v-" + count).withHostPath(hostPath).build();
volumes.add(volume);
count++;
}
containerBuilder.withVolumeMounts(volumeMounts);
}
LabelSelectorBuilder selectorBuilder = new LabelSelectorBuilder();
LabelSelector labelSelector = selectorBuilder.withMatchLabels(Collections.singletonMap(LABEL_COMPONENT, containerNameWithoutUnderscore)).build();
// String dName = "daemonset-" + containerNameWithoutUnderscore;
DaemonSet daemonSet = new DaemonSetBuilder().withNewMetadata().withName(containerNameWithoutUnderscore).withNamespace(namespace).withLabels(k8sPobLabels).endMetadata().withNewSpec().withSelector(labelSelector).withNewTemplate().withNewMetadata().withLabels(k8sPobLabels).endMetadata().withNewSpec().addNewContainerLike(containerBuilder.build()).endContainer().withVolumes(volumes).endSpec().endTemplate().endSpec().build();
client.apps().daemonSets().create(daemonSet);
client.apps().daemonSets().inNamespace(namespace).withName(containerNameWithoutUnderscore).cascading(true);
} catch (Exception e) {
logger.error("Error deploying DaemonSet");
e.printStackTrace();
throw e;
}
return created;
}
Aggregations