Search in sources :

Example 1 with HostPathVolumeSourceBuilder

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;
}
Also used : Bind(com.spotify.docker.client.messages.HostConfig.Bind) ArrayList(java.util.ArrayList) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) SecurityContextBuilder(io.fabric8.kubernetes.api.model.SecurityContextBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) HostPathVolumeSourceBuilder(io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder) CapabilitiesBuilder(io.fabric8.kubernetes.api.model.CapabilitiesBuilder) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) HostPathVolumeSource(io.fabric8.kubernetes.api.model.HostPathVolumeSource) Pod(io.fabric8.kubernetes.api.model.Pod) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Volume(io.fabric8.kubernetes.api.model.Volume) Capabilities(io.fabric8.kubernetes.api.model.Capabilities) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException)

Example 2 with HostPathVolumeSourceBuilder

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;
}
Also used : HostPathVolumeSourceBuilder(io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder) File(java.io.File) HostPathVolumeSource(io.fabric8.kubernetes.api.model.HostPathVolumeSource) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

Example 3 with HostPathVolumeSourceBuilder

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;
}
Also used : Bind(com.spotify.docker.client.messages.HostConfig.Bind) ArrayList(java.util.ArrayList) VolumeBuilder(io.fabric8.kubernetes.api.model.VolumeBuilder) SecurityContextBuilder(io.fabric8.kubernetes.api.model.SecurityContextBuilder) DaemonSetBuilder(io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) HostPathVolumeSourceBuilder(io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder) CapabilitiesBuilder(io.fabric8.kubernetes.api.model.CapabilitiesBuilder) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) HostPathVolumeSource(io.fabric8.kubernetes.api.model.HostPathVolumeSource) LabelSelectorBuilder(io.fabric8.kubernetes.api.model.LabelSelectorBuilder) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) VolumeMountBuilder(io.fabric8.kubernetes.api.model.VolumeMountBuilder) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) Volume(io.fabric8.kubernetes.api.model.Volume) Capabilities(io.fabric8.kubernetes.api.model.Capabilities) VolumeMount(io.fabric8.kubernetes.api.model.VolumeMount) DaemonSet(io.fabric8.kubernetes.api.model.apps.DaemonSet)

Aggregations

HostPathVolumeSource (io.fabric8.kubernetes.api.model.HostPathVolumeSource)3 HostPathVolumeSourceBuilder (io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 Bind (com.spotify.docker.client.messages.HostConfig.Bind)2 Capabilities (io.fabric8.kubernetes.api.model.Capabilities)2 CapabilitiesBuilder (io.fabric8.kubernetes.api.model.CapabilitiesBuilder)2 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)2 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)2 SecurityContextBuilder (io.fabric8.kubernetes.api.model.SecurityContextBuilder)2 Volume (io.fabric8.kubernetes.api.model.Volume)2 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)2 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)2 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)2 ArrayList (java.util.ArrayList)2 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)1 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)1 Pod (io.fabric8.kubernetes.api.model.Pod)1 PodBuilder (io.fabric8.kubernetes.api.model.PodBuilder)1