Search in sources :

Example 11 with PodSpec

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

the class KubernetesPodsProducer method doCreatePod.

protected void doCreatePod(Exchange exchange, String operation) throws Exception {
    Pod pod = null;
    String podName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_POD_NAME, String.class);
    String namespaceName = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_NAMESPACE_NAME, String.class);
    PodSpec podSpec = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_POD_SPEC, PodSpec.class);
    if (ObjectHelper.isEmpty(podName)) {
        LOG.error("Create a specific pod require specify a pod name");
        throw new IllegalArgumentException("Create a specific pod require specify a pod name");
    }
    if (ObjectHelper.isEmpty(namespaceName)) {
        LOG.error("Create a specific pod require specify a namespace name");
        throw new IllegalArgumentException("Create a specific pod require specify a namespace name");
    }
    if (ObjectHelper.isEmpty(podSpec)) {
        LOG.error("Create a specific pod require specify a pod spec bean");
        throw new IllegalArgumentException("Create a specific pod require specify a pod spec bean");
    }
    Map<String, String> labels = exchange.getIn().getHeader(KubernetesConstants.KUBERNETES_PODS_LABELS, Map.class);
    Pod podCreating = new PodBuilder().withNewMetadata().withName(podName).withLabels(labels).endMetadata().withSpec(podSpec).build();
    pod = getEndpoint().getKubernetesClient().pods().inNamespace(namespaceName).create(podCreating);
    MessageHelper.copyHeaders(exchange.getIn(), exchange.getOut(), true);
    exchange.getOut().setBody(pod);
}
Also used : DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) PodBuilder(io.fabric8.kubernetes.api.model.PodBuilder)

Example 12 with PodSpec

use of io.fabric8.kubernetes.api.model.PodSpec in project jointware by isdream.

the class PerfComparator method createByObject.

public static void createByObject() {
    Deployment dm = new Deployment();
    {
        ObjectMeta md = new ObjectMeta();
        ;
        md.setName("busybox-dm");
        md.setNamespace("wuheng");
        {
            Map<String, String> labels = new HashMap<String, String>();
            labels.put("app", "busybox-dm");
            md.setLabels(labels);
        }
        dm.setMetadata(md);
    }
    {
        DeploymentSpec spec = new DeploymentSpec();
        spec.setReplicas(2);
        {
            PodTemplateSpec template = new PodTemplateSpec();
            {
                PodSpec pc = new PodSpec();
                {
                    List<Container> containers = new ArrayList<Container>();
                    {
                        Container c = new Container();
                        {
                            c.setImage("dcr.io:5000/busybox:latest");
                            c.setImagePullPolicy("IfNotPresent");
                            c.setName("busybox-dm");
                            List<String> commands = new ArrayList<String>();
                            {
                                commands.add("sleep");
                                commands.add("3600");
                            }
                            c.setCommand(commands);
                        }
                        containers.add(c);
                    }
                    pc.setContainers(containers);
                }
                template.setSpec(pc);
            }
            spec.setTemplate(template);
        }
        dm.setSpec(spec);
    }
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodTemplateSpec(io.fabric8.kubernetes.api.model.PodTemplateSpec) Container(io.fabric8.kubernetes.api.model.Container) DeploymentSpec(io.fabric8.kubernetes.api.model.extensions.DeploymentSpec) HashMap(java.util.HashMap) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ArrayList(java.util.ArrayList) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment)

Example 13 with PodSpec

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

the class DebugMojo method enableDebugging.

private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
    if (template != null) {
        PodSpec podSpec = template.getSpec();
        if (podSpec != null) {
            List<Container> containers = podSpec.getContainers();
            boolean enabled = false;
            for (int i = 0; i < containers.size(); i++) {
                Container container = containers.get(i);
                List<EnvVar> env = container.getEnv();
                if (env == null) {
                    env = new ArrayList<>();
                }
                remoteDebugPort = KubernetesResourceUtil.getEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                if (KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG, "true")) {
                    container.setEnv(env);
                    enabled = true;
                }
                if (KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SUSPEND, String.valueOf(debugSuspend))) {
                    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 (debugSuspend) {
                    // Setting a random session value to force pod restart
                    this.debugSuspendValue = String.valueOf(new Random().nextLong());
                    KubernetesResourceUtil.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION, this.debugSuspendValue);
                    container.setEnv(env);
                    if (container.getReadinessProbe() != null) {
                        log.info("Readiness probe will be disabled on " + getKind(entity) + " " + getName(entity) + " to allow attaching a remote debugger during suspension");
                        container.setReadinessProbe(null);
                    }
                    enabled = true;
                } else {
                    if (KubernetesResourceUtil.removeEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION)) {
                        container.setEnv(env);
                        enabled = true;
                    }
                }
            }
            if (enabled) {
                log.info("Enabling debug on " + getKind(entity) + " " + getName(entity));
                return true;
            }
        }
    }
    return false;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) Random(java.util.Random) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 14 with PodSpec

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

the class DebugMojo method podHasEnvVarValue.

private boolean podHasEnvVarValue(Pod pod, String envVarName, String envVarValue) {
    PodSpec spec = pod.getSpec();
    if (spec != null) {
        List<Container> containers = spec.getContainers();
        if (containers != null && !containers.isEmpty()) {
            Container container = containers.get(0);
            List<EnvVar> env = container.getEnv();
            if (env != null) {
                for (EnvVar envVar : env) {
                    if (Objects.equal(envVar.getName(), envVarName) && Objects.equal(envVar.getValue(), envVarValue)) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) PodSpec(io.fabric8.kubernetes.api.model.PodSpec) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

Example 15 with PodSpec

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

the class KubernetesResourceUtil method mergePodSpec.

public static void mergePodSpec(PodSpecBuilder builder, PodSpec defaultPodSpec, String defaultName) {
    List<Container> containers = builder.buildContainers();
    List<Container> defaultContainers = defaultPodSpec.getContainers();
    int size = defaultContainers.size();
    if (size > 0) {
        if (containers == null || containers.isEmpty()) {
            builder.addToContainers(defaultContainers.toArray(new Container[size]));
        } else {
            int idx = 0;
            for (Container defaultContainer : defaultContainers) {
                Container container;
                if (idx < containers.size()) {
                    container = containers.get(idx);
                } else {
                    container = new Container();
                    containers.add(container);
                }
                mergeSimpleFields(container, defaultContainer);
                List<EnvVar> defaultEnv = defaultContainer.getEnv();
                if (defaultEnv != null) {
                    for (EnvVar envVar : defaultEnv) {
                        ensureHasEnv(container, envVar);
                    }
                }
                List<ContainerPort> defaultPorts = defaultContainer.getPorts();
                if (defaultPorts != null) {
                    for (ContainerPort port : defaultPorts) {
                        ensureHasPort(container, port);
                    }
                }
                if (container.getReadinessProbe() == null) {
                    container.setReadinessProbe(defaultContainer.getReadinessProbe());
                }
                if (container.getLivenessProbe() == null) {
                    container.setLivenessProbe(defaultContainer.getLivenessProbe());
                }
                if (container.getSecurityContext() == null) {
                    container.setSecurityContext(defaultContainer.getSecurityContext());
                }
                idx++;
            }
            builder.withContainers(containers);
        }
    } else if (!containers.isEmpty()) {
        // lets default the container name if there's none specified in the custom yaml file
        Container container = containers.get(0);
        if (isNullOrBlank(container.getName())) {
            container.setName(defaultName);
        }
        builder.withContainers(containers);
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) EnvVar(io.fabric8.kubernetes.api.model.EnvVar)

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