Search in sources :

Example 1 with PodSpec

use of io.fabric8.kubernetes.api.model.PodSpec in project che by eclipse.

the class OpenShiftConnector method createOpenShiftDeployment.

private String createOpenShiftDeployment(String workspaceID, String imageName, String sanitizedContainerName, Set<String> exposedPorts, String[] envVariables, String[] volumes, boolean runContainerAsRoot) {
    String deploymentName = CHE_OPENSHIFT_RESOURCES_PREFIX + workspaceID;
    LOG.info("Creating OpenShift deployment {}", deploymentName);
    Map<String, String> selector = Collections.singletonMap(OPENSHIFT_DEPLOYMENT_LABEL, deploymentName);
    LOG.info("Adding container {} to OpenShift deployment {}", sanitizedContainerName, deploymentName);
    Long UID = runContainerAsRoot ? UID_ROOT : UID_USER;
    Container container = new ContainerBuilder().withName(sanitizedContainerName).withImage(imageName).withEnv(KubernetesEnvVar.getEnvFrom(envVariables)).withPorts(KubernetesContainer.getContainerPortsFrom(exposedPorts)).withImagePullPolicy(OPENSHIFT_IMAGE_PULL_POLICY_IFNOTPRESENT).withNewSecurityContext().withRunAsUser(UID).withPrivileged(true).endSecurityContext().withLivenessProbe(getLivenessProbeFrom(exposedPorts)).withVolumeMounts(getVolumeMountsFrom(volumes, workspaceID)).build();
    PodSpec podSpec = new PodSpecBuilder().withContainers(container).withVolumes(getVolumesFrom(volumes, workspaceID)).withServiceAccountName(this.openShiftCheServiceAccount).build();
    Deployment deployment = new DeploymentBuilder().withNewMetadata().withName(deploymentName).withNamespace(this.openShiftCheProjectName).endMetadata().withNewSpec().withReplicas(1).withNewSelector().withMatchLabels(selector).endSelector().withNewTemplate().withNewMetadata().withLabels(selector).endMetadata().withSpec(podSpec).endTemplate().endSpec().build();
    deployment = openShiftClient.extensions().deployments().inNamespace(this.openShiftCheProjectName).create(deployment);
    LOG.info("OpenShift deployment {} created", deploymentName);
    return deployment.getMetadata().getName();
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) KubernetesContainer(org.eclipse.che.plugin.openshift.client.kubernetes.KubernetesContainer) Container(io.fabric8.kubernetes.api.model.Container) ContainerBuilder(io.fabric8.kubernetes.api.model.ContainerBuilder) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 2 with PodSpec

use of io.fabric8.kubernetes.api.model.PodSpec in project camel by apache.

the class KubernetesPodsConsumerTest method createAndDeletePod.

@Test
public void createAndDeletePod() throws Exception {
    if (ObjectHelper.isEmpty(authToken)) {
        return;
    }
    mockResultEndpoint.expectedMessageCount(3);
    mockResultEndpoint.expectedHeaderValuesReceivedInAnyOrder(KubernetesConstants.KUBERNETES_EVENT_ACTION, "ADDED", "MODIFIED", "MODIFIED");
    Exchange ex = template.request("direct:createPod", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_NAME, "test");
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("this", "rocks");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_PODS_LABELS, labels);
            PodSpec podSpec = new PodSpec();
            podSpec.setHostname("localhost");
            Container cont = new Container();
            cont.setImage("docker.io/jboss/wildfly:latest");
            cont.setName("pippo");
            List<ContainerPort> containerPort = new ArrayList<ContainerPort>();
            ContainerPort port = new ContainerPort();
            port.setHostIP("0.0.0.0");
            port.setHostPort(8080);
            port.setContainerPort(8080);
            containerPort.add(port);
            cont.setPorts(containerPort);
            List<Container> list = new ArrayList<Container>();
            list.add(cont);
            podSpec.setContainers(list);
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_SPEC, podSpec);
        }
    });
    ex = template.request("direct:deletePod", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, "default");
            exchange.getIn().setHeader(KubernetesConstants.KUBERNETES_POD_NAME, "test");
        }
    });
    boolean podDeleted = ex.getOut().getBody(Boolean.class);
    assertTrue(podDeleted);
    Thread.sleep(3000);
    mockResultEndpoint.assertIsSatisfied();
}
Also used : Processor(org.apache.camel.Processor) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Exchange(org.apache.camel.Exchange) Container(io.fabric8.kubernetes.api.model.Container) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with PodSpec

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

the class DefaultControllerEnricher method addMissingResources.

@Override
public void addMissingResources(KubernetesListBuilder builder) {
    final String name = getConfig(Config.name, MavenUtil.createDefaultResourceName(getProject()));
    final ResourceConfig config = new ResourceConfig.Builder().controllerName(name).imagePullPolicy(getConfig(Config.pullPolicy)).withReplicas(Configs.asInt(getConfig(Config.replicaCount))).build();
    final List<ImageConfiguration> images = getImages();
    // 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 (!Lists.isNullOrEmpty(images)) {
            String type = getConfig(Config.type);
            if ("deployment".equalsIgnoreCase(type)) {
                log.info("Adding a default Deployment");
                builder.addToDeploymentItems(deployHandler.getDeployment(config, images));
            } else if ("statefulSet".equalsIgnoreCase(type)) {
                log.info("Adding a default StatefulSet");
                builder.addToStatefulSetItems(statefulSetHandler.getStatefulSet(config, images));
            } else if ("daemonSet".equalsIgnoreCase(type)) {
                log.info("Adding a default DaemonSet");
                builder.addToDaemonSetItems(daemonSetHandler.getDaemonSet(config, images));
            } else if ("replicaSet".equalsIgnoreCase(type)) {
                log.info("Adding a default ReplicaSet");
                builder.addToReplicaSetItems(rsHandler.getReplicaSet(config, images));
            } else if ("replicationController".equalsIgnoreCase(type)) {
                log.info("Adding a default ReplicationController");
                builder.addToReplicationControllerItems(rcHandler.getReplicationController(config, images));
            } else if ("job".equalsIgnoreCase(type)) {
                log.info("Adding a default Job");
                builder.addToJobItems(jobHandler.getJob(config, images));
            }
        }
    } else if (KubernetesResourceUtil.checkForKind(builder, "StatefulSet")) {
        final StatefulSetSpec spec = statefulSetHandler.getStatefulSet(config, images).getSpec();
        if (spec != null) {
            builder.accept(new TypedVisitor<StatefulSetBuilder>() {

                @Override
                public void visit(StatefulSetBuilder statefulSetBuilder) {
                    statefulSetBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                    mergeStatefulSetSpec(statefulSetBuilder, spec);
                }
            });
            if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                final PodSpec podSpec = spec.getTemplate().getSpec();
                builder.accept(new TypedVisitor<PodSpecBuilder>() {

                    @Override
                    public void visit(PodSpecBuilder builder) {
                        KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
                    }
                });
            }
        }
    } else {
        final DeploymentSpec spec = deployHandler.getDeployment(config, images).getSpec();
        if (spec != null) {
            builder.accept(new TypedVisitor<DeploymentBuilder>() {

                @Override
                public void visit(DeploymentBuilder deploymentBuilder) {
                    deploymentBuilder.editOrNewSpec().editOrNewTemplate().editOrNewSpec().endSpec().endTemplate().endSpec();
                    mergeDeploymentSpec(deploymentBuilder, spec);
                }
            });
            if (spec.getTemplate() != null && spec.getTemplate().getSpec() != null) {
                final PodSpec podSpec = spec.getTemplate().getSpec();
                builder.accept(new TypedVisitor<PodSpecBuilder>() {

                    @Override
                    public void visit(PodSpecBuilder builder) {
                        KubernetesResourceUtil.mergePodSpec(builder, podSpec, name);
                    }
                });
            }
        }
    }
}
Also used : TypedVisitor(io.fabric8.kubernetes.api.builder.TypedVisitor) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) StatefulSetSpec(io.fabric8.kubernetes.api.model.extensions.StatefulSetSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ImageConfiguration(io.fabric8.maven.docker.config.ImageConfiguration) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) KubernetesListBuilder(io.fabric8.kubernetes.api.model.KubernetesListBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder) ResourceConfig(io.fabric8.maven.core.config.ResourceConfig) StatefulSetBuilder(io.fabric8.kubernetes.api.model.extensions.StatefulSetBuilder) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 4 with PodSpec

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

the class DebugEnricher method enableDebugging.

private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
    if (template != null) {
        PodSpec podSpec = template.getSpec();
        if (podSpec != null) {
            List<Container> containers = podSpec.getContainers();
            if (containers.size() > 0) {
                Container container = containers.get(0);
                List<EnvVar> env = container.getEnv();
                if (env == null) {
                    env = new ArrayList<>();
                }
                String remoteDebugPort = KubernetesResourceUtil.getEnvVar(env, ENV_VAR_JAVA_DEBUG_PORT, ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                boolean enabled = false;
                if (KubernetesResourceUtil.setEnvVar(env, ENV_VAR_JAVA_DEBUG, "true")) {
                    container.setEnv(env);
                    enabled = true;
                }
                List<ContainerPort> ports = container.getPorts();
                if (ports == null) {
                    ports = new ArrayList<>();
                }
                if (KubernetesResourceUtil.addPort(ports, remoteDebugPort, "debug", log)) {
                    container.setPorts(ports);
                    enabled = true;
                }
                if (enabled) {
                    log.info("Enabling debug on " + getKind(entity) + " " + KubernetesHelper.getName(entity) + " due to the property: " + ENABLE_DEBUG_MAVEN_PROPERTY);
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 5 with PodSpec

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

the class KubernetesResourceUtil method mergeDeployments.

protected static HasMetadata mergeDeployments(Deployment resource1, Deployment resource2, Logger log, boolean switchOnLocalCustomisation) {
    Deployment resource1OrCopy = resource1;
    if (!switchOnLocalCustomisation) {
        // lets copy the original to avoid modifying it
        resource1OrCopy = new DeploymentBuilder(resource1OrCopy).build();
    }
    HasMetadata answer = resource1OrCopy;
    DeploymentSpec spec1 = resource1OrCopy.getSpec();
    DeploymentSpec spec2 = resource2.getSpec();
    if (spec1 == null) {
        resource1OrCopy.setSpec(spec2);
    } else {
        PodTemplateSpec template1 = spec1.getTemplate();
        PodTemplateSpec template2 = null;
        if (spec2 != null) {
            template2 = spec2.getTemplate();
        }
        if (template1 != null && template2 != null) {
            mergeMetadata(template1, template2);
        }
        if (template1 == null) {
            spec1.setTemplate(template2);
        } else {
            PodSpec podSpec1 = template1.getSpec();
            PodSpec podSpec2 = null;
            if (template2 != null) {
                podSpec2 = template2.getSpec();
            }
            if (podSpec1 == null) {
                template1.setSpec(podSpec2);
            } else {
                String defaultName = null;
                PodTemplateSpec updateTemplate = template1;
                if (switchOnLocalCustomisation) {
                    HasMetadata override = resource2;
                    if (isLocalCustomisation(podSpec1)) {
                        updateTemplate = template2;
                        PodSpec tmp = podSpec1;
                        podSpec1 = podSpec2;
                        podSpec2 = tmp;
                    } else {
                        answer = resource2;
                        override = resource1OrCopy;
                    }
                    mergeMetadata(answer, override);
                } else {
                    mergeMetadata(resource1OrCopy, resource2);
                }
                if (updateTemplate != null) {
                    if (podSpec2 == null) {
                        updateTemplate.setSpec(podSpec1);
                    } else {
                        PodSpecBuilder podSpecBuilder = new PodSpecBuilder(podSpec1);
                        mergePodSpec(podSpecBuilder, podSpec2, defaultName);
                        updateTemplate.setSpec(podSpecBuilder.build());
                    }
                }
                return answer;
            }
        }
    }
    log.info("Merging 2 resources for " + getKind(resource1OrCopy) + " " + getName(resource1OrCopy) + " from " + getSourceUrlAnnotation(resource1OrCopy) + " and " + getSourceUrlAnnotation(resource2) + " and removing " + getSourceUrlAnnotation(resource1OrCopy));
    return resource1OrCopy;
}
Also used : PodSpecBuilder(io.fabric8.kubernetes.api.model.PodSpecBuilder) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Aggregations

PodSpec (io.fabric8.kubernetes.api.model.PodSpec)22 Container (io.fabric8.kubernetes.api.model.Container)18 Map (java.util.Map)10 HashMap (java.util.HashMap)9 List (java.util.List)8 ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)7 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)5 Test (org.junit.Test)5 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)4 Collectors (java.util.stream.Collectors)4 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)3 Pod (io.fabric8.kubernetes.api.model.Pod)3 PodSpecBuilder (io.fabric8.kubernetes.api.model.PodSpecBuilder)3 PodTemplateSpec (io.fabric8.kubernetes.api.model.PodTemplateSpec)3 Service (io.fabric8.kubernetes.api.model.Service)3 Volume (io.fabric8.kubernetes.api.model.Volume)3 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)3 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)3 Collections (java.util.Collections)3