Search in sources :

Example 1 with DaemonSetBuilder

use of io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder 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)

Example 2 with DaemonSetBuilder

use of io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder in project fabric8-maven-plugin by fabric8io.

the class ImageEnricherTest method checkEnrichDaemonSet.

@Test
public void checkEnrichDaemonSet() throws Exception {
    KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new DaemonSetBuilder().build());
    imageEnricher.create(PlatformMode.kubernetes, builder);
    assertCorrectlyGeneratedResources(builder.build(), "DaemonSet");
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) DaemonSetBuilder(io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder) Test(org.junit.Test)

Example 3 with DaemonSetBuilder

use of io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder in project fabric8-maven-plugin by fabric8io.

the class TriggersAnnotationEnricherTest method testDaemonSetEnrichment.

@Test
public void testDaemonSetEnrichment() throws IOException {
    KubernetesListBuilder builder = new KubernetesListBuilder().addToItems(new DaemonSetBuilder().withNewMetadata().addToAnnotations("annkey", "annvalue").endMetadata().withNewSpec().withNewTemplate().withNewSpec().withContainers(createContainers("c1", "iss:1.1.0", "c2", "docker.io/a-docker-user/is:latest")).endSpec().endTemplate().endSpec().build());
    TriggersAnnotationEnricher enricher = new TriggersAnnotationEnricher(context);
    enricher.enrich(PlatformMode.kubernetes, builder);
    DaemonSet res = (DaemonSet) builder.build().getItems().get(0);
    String triggers = res.getMetadata().getAnnotations().get("image.openshift.io/triggers");
    assertNotNull(triggers);
    List<ImageChangeTrigger> triggerList = OBJECT_MAPPER.readValue(triggers, OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, ImageChangeTrigger.class));
    assertEquals(1, triggerList.size());
    ImageChangeTrigger trigger = triggerList.get(0);
    assertEquals("ImageStreamTag", trigger.getFrom().getKind());
    assertEquals("iss:1.1.0", trigger.getFrom().getName());
    assertTrue(trigger.getAdditionalProperties().containsKey("fieldPath"));
    assertEquals("annvalue", res.getMetadata().getAnnotations().get("annkey"));
}
Also used : KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) DaemonSetBuilder(io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder) DaemonSet(io.fabric8.kubernetes.api.model.apps.DaemonSet) ArrayList(java.util.ArrayList) List(java.util.List) ImageChangeTrigger(io.fabric8.openshift.api.model.ImageChangeTrigger) Test(org.junit.Test)

Aggregations

DaemonSetBuilder (io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder)3 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)2 DaemonSet (io.fabric8.kubernetes.api.model.apps.DaemonSet)2 ArrayList (java.util.ArrayList)2 Test (org.junit.Test)2 Bind (com.spotify.docker.client.messages.HostConfig.Bind)1 Capabilities (io.fabric8.kubernetes.api.model.Capabilities)1 CapabilitiesBuilder (io.fabric8.kubernetes.api.model.CapabilitiesBuilder)1 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)1 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)1 HostPathVolumeSource (io.fabric8.kubernetes.api.model.HostPathVolumeSource)1 HostPathVolumeSourceBuilder (io.fabric8.kubernetes.api.model.HostPathVolumeSourceBuilder)1 LabelSelector (io.fabric8.kubernetes.api.model.LabelSelector)1 LabelSelectorBuilder (io.fabric8.kubernetes.api.model.LabelSelectorBuilder)1 SecurityContextBuilder (io.fabric8.kubernetes.api.model.SecurityContextBuilder)1 Volume (io.fabric8.kubernetes.api.model.Volume)1 VolumeBuilder (io.fabric8.kubernetes.api.model.VolumeBuilder)1 VolumeMount (io.fabric8.kubernetes.api.model.VolumeMount)1 VolumeMountBuilder (io.fabric8.kubernetes.api.model.VolumeMountBuilder)1 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)1