Search in sources :

Example 6 with DaemonSet

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

the class KubernetesResourceUtil method getPodLabelSelector.

public static LabelSelector getPodLabelSelector(HasMetadata entity) {
    LabelSelector selector = null;
    if (entity instanceof Deployment) {
        Deployment resource = (Deployment) entity;
        DeploymentSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof ReplicaSet) {
        ReplicaSet resource = (ReplicaSet) entity;
        ReplicaSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) entity;
        DeploymentConfigSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof ReplicationController) {
        ReplicationController resource = (ReplicationController) entity;
        ReplicationControllerSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof DaemonSet) {
        DaemonSet resource = (DaemonSet) entity;
        DaemonSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof StatefulSet) {
        StatefulSet resource = (StatefulSet) entity;
        StatefulSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof Job) {
        Job resource = (Job) entity;
        JobSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    }
    return selector;
}
Also used : StatefulSetSpec(io.fabric8.kubernetes.api.model.apps.StatefulSetSpec) ReplicaSetSpec(io.fabric8.kubernetes.api.model.apps.ReplicaSetSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DaemonSetSpec(io.fabric8.kubernetes.api.model.apps.DaemonSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.apps.DeploymentSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DaemonSet(io.fabric8.kubernetes.api.model.apps.DaemonSet) JobSpec(io.fabric8.kubernetes.api.model.batch.JobSpec) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Job(io.fabric8.kubernetes.api.model.batch.Job) ReplicaSet(io.fabric8.kubernetes.api.model.apps.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Example 7 with DaemonSet

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

the class DefaultControllerEnricher method create.

@Override
public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
    final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getContext().getGav().getSanitizedArtifactId()));
    ResourceConfig xmlResourceConfig = getConfiguration().getResource().orElse(null);
    ResourceConfig config = new ResourceConfig.Builder(xmlResourceConfig).controllerName(name).imagePullPolicy(getImagePullPolicy(xmlResourceConfig, getConfig(Config.pullPolicy))).withReplicas(getReplicaCount(builder, xmlResourceConfig, Configs.asInt(getConfig(Config.replicaCount)))).build();
    final List<ImageConfiguration> images = getImages().orElse(Collections.emptyList());
    // Check if at least a replica set is added. If not add a default one
    if (!KubernetesResourceUtil.checkForKind(builder, POD_CONTROLLER_KINDS)) {
        // At least one image must be present, otherwise the resulting config will be invalid
        if (!images.isEmpty()) {
            String type = getConfig(Config.type);
            if ("deployment".equalsIgnoreCase(type) || "deploymentConfig".equalsIgnoreCase(type)) {
                if (platformMode == PlatformMode.kubernetes || (platformMode == PlatformMode.openshift && useDeploymentforOpenShift())) {
                    log.info("Adding a default Deployment");
                    Deployment deployment = deployHandler.getDeployment(config, images);
                    builder.addToItems(deployment);
                    setProcessingInstruction(FABRIC8_GENERATED_CONTAINERS, getContainersFromPodSpec(deployment.getSpec().getTemplate()));
                } else {
                    log.info("Adding a default DeploymentConfig");
                    DeploymentConfig deploymentConfig = deployConfigHandler.getDeploymentConfig(config, images, getOpenshiftDeployTimeoutInSeconds(3600L), getValueFromConfig(IMAGE_CHANGE_TRIGGERS, true), getValueFromConfig(OPENSHIFT_ENABLE_AUTOMATIC_TRIGGER, true), isOpenShiftMode(), getProcessingInstructionViaKey(FABRIC8_GENERATED_CONTAINERS));
                    builder.addToItems(deploymentConfig);
                    setProcessingInstruction(FABRIC8_GENERATED_CONTAINERS, getContainersFromPodSpec(deploymentConfig.getSpec().getTemplate()));
                }
            } else if ("statefulSet".equalsIgnoreCase(type)) {
                log.info("Adding a default StatefulSet");
                StatefulSet statefulSet = statefulSetHandler.getStatefulSet(config, images);
                builder.addToItems(statefulSet);
                setProcessingInstruction(FABRIC8_GENERATED_CONTAINERS, getContainersFromPodSpec(statefulSet.getSpec().getTemplate()));
            } else if ("daemonSet".equalsIgnoreCase(type)) {
                log.info("Adding a default DaemonSet");
                DaemonSet daemonSet = daemonSetHandler.getDaemonSet(config, images);
                builder.addToItems(daemonSet);
                setProcessingInstruction(FABRIC8_GENERATED_CONTAINERS, getContainersFromPodSpec(daemonSet.getSpec().getTemplate()));
            } else if ("replicaSet".equalsIgnoreCase(type)) {
                log.info("Adding a default ReplicaSet");
                ReplicaSet replicaSet = rsHandler.getReplicaSet(config, images);
                builder.addToItems(replicaSet);
                setProcessingInstruction(FABRIC8_GENERATED_CONTAINERS, getContainersFromPodSpec(replicaSet.getSpec().getTemplate()));
            } else if ("replicationController".equalsIgnoreCase(type)) {
                log.info("Adding a default ReplicationController");
                ReplicationController replicationController = rcHandler.getReplicationController(config, images);
                builder.addToReplicationControllerItems(replicationController);
                setProcessingInstruction(FABRIC8_GENERATED_CONTAINERS, getContainersFromPodSpec(replicationController.getSpec().getTemplate()));
            } else if ("job".equalsIgnoreCase(type)) {
                log.info("Adding a default Job");
                Job job = jobHandler.getJob(config, images);
                builder.addToItems(job);
                setProcessingInstruction(FABRIC8_GENERATED_CONTAINERS, getContainersFromPodSpec(job.getSpec().getTemplate()));
            }
        }
    }
}
Also used : ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) Deployment(io.fabric8.kubernetes.api.model.apps.Deployment) DaemonSet(io.fabric8.kubernetes.api.model.apps.DaemonSet) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) StatefulSet(io.fabric8.kubernetes.api.model.apps.StatefulSet) Job(io.fabric8.kubernetes.api.model.batch.Job) ReplicaSet(io.fabric8.kubernetes.api.model.apps.ReplicaSet)

Example 8 with DaemonSet

use of io.fabric8.kubernetes.api.model.apps.DaemonSet in project kubernetes-client by fabric8io.

the class DaemonSetIT method update.

@Test
public void update() {
    DaemonSet daemonSet = client.apps().daemonSets().inNamespace(session.getNamespace()).withName("daemonset-update").edit(c -> new DaemonSetBuilder(c).editSpec().editTemplate().editSpec().editContainer(0).withImage("quay.io/fluentd_elasticsearch/fluentd:v3.0.0").endContainer().endSpec().endTemplate().endSpec().build());
    assertNotNull(daemonSet);
    assertEquals("quay.io/fluentd_elasticsearch/fluentd:v3.0.0", daemonSet.getSpec().getTemplate().getSpec().getContainers().get(0).getImage());
}
Also used : DaemonSetBuilder(io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder) DaemonSet(io.fabric8.kubernetes.api.model.apps.DaemonSet) Test(org.junit.Test)

Example 9 with DaemonSet

use of io.fabric8.kubernetes.api.model.apps.DaemonSet 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 10 with DaemonSet

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

the class KubernetesResourceUtil method getPodLabelSelector.

public static LabelSelector getPodLabelSelector(HasMetadata entity) {
    LabelSelector selector = null;
    if (entity instanceof Deployment) {
        Deployment resource = (Deployment) entity;
        DeploymentSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof ReplicaSet) {
        ReplicaSet resource = (ReplicaSet) entity;
        ReplicaSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof DeploymentConfig) {
        DeploymentConfig resource = (DeploymentConfig) entity;
        DeploymentConfigSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof ReplicationController) {
        ReplicationController resource = (ReplicationController) entity;
        ReplicationControllerSpec spec = resource.getSpec();
        if (spec != null) {
            selector = toLabelSelector(spec.getSelector());
        }
    } else if (entity instanceof DaemonSet) {
        DaemonSet resource = (DaemonSet) entity;
        DaemonSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof StatefulSet) {
        StatefulSet resource = (StatefulSet) entity;
        StatefulSetSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    } else if (entity instanceof Job) {
        Job resource = (Job) entity;
        JobSpec spec = resource.getSpec();
        if (spec != null) {
            selector = spec.getSelector();
        }
    }
    return selector;
}
Also used : StatefulSetSpec(io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec) ReplicaSetSpec(io.fabric8.kubernetes.api.model.extensions.ReplicaSetSpec) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DaemonSetSpec(io.fabric8.kubernetes.api.model.extensions.DaemonSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) ReplicationController(io.fabric8.kubernetes.api.model.ReplicationController) DaemonSet(io.fabric8.kubernetes.api.model.extensions.DaemonSet) JobSpec(io.fabric8.kubernetes.api.model.JobSpec) DeploymentConfig(io.fabric8.openshift.api.model.DeploymentConfig) DeploymentConfigSpec(io.fabric8.openshift.api.model.DeploymentConfigSpec) StatefulSet(io.fabric8.kubernetes.api.model.extensions.StatefulSet) Job(io.fabric8.kubernetes.api.model.Job) ReplicaSet(io.fabric8.kubernetes.api.model.extensions.ReplicaSet) ReplicationControllerSpec(io.fabric8.kubernetes.api.model.ReplicationControllerSpec)

Aggregations

DaemonSet (io.fabric8.kubernetes.api.model.apps.DaemonSet)10 ReplicationController (io.fabric8.kubernetes.api.model.ReplicationController)9 DeploymentConfig (io.fabric8.openshift.api.model.DeploymentConfig)8 DaemonSetBuilder (io.fabric8.kubernetes.api.model.apps.DaemonSetBuilder)7 Pod (io.fabric8.kubernetes.api.model.Pod)6 Deployment (io.fabric8.kubernetes.api.model.apps.Deployment)5 ReplicaSet (io.fabric8.kubernetes.api.model.apps.ReplicaSet)5 StatefulSet (io.fabric8.kubernetes.api.model.apps.StatefulSet)5 Template (io.fabric8.openshift.api.model.Template)5 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)4 KubernetesListBuilder (io.fabric8.kubernetes.api.model.KubernetesListBuilder)4 PersistentVolumeClaim (io.fabric8.kubernetes.api.model.PersistentVolumeClaim)4 Secret (io.fabric8.kubernetes.api.model.Secret)4 Service (io.fabric8.kubernetes.api.model.Service)4 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)4 DaemonSet (io.fabric8.kubernetes.api.model.extensions.DaemonSet)4 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)4 Ingress (io.fabric8.kubernetes.api.model.extensions.Ingress)4 ReplicaSet (io.fabric8.kubernetes.api.model.extensions.ReplicaSet)4 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)4