Search in sources :

Example 1 with ContainerPort

use of io.fabric8.kubernetes.api.model.ContainerPort 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 2 with ContainerPort

use of io.fabric8.kubernetes.api.model.ContainerPort in project kubernetes by ballerinax.

the class DeploymentHandler method generate.

/**
 * Generate kubernetes deployment definition from annotation.
 *
 * @return Generated kubernetes @{@link Deployment} definition
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
public String generate() throws KubernetesPluginException {
    List<ContainerPort> containerPorts = null;
    if (deploymentModel.getPorts() != null) {
        containerPorts = populatePorts(deploymentModel.getPorts());
    }
    Container container = generateContainer(deploymentModel, containerPorts);
    Deployment deployment = new DeploymentBuilder().withNewMetadata().withName(deploymentModel.getName()).withNamespace(deploymentModel.getNamespace()).withLabels(deploymentModel.getLabels()).endMetadata().withNewSpec().withReplicas(deploymentModel.getReplicas()).withNewTemplate().withNewMetadata().addToLabels(deploymentModel.getLabels()).endMetadata().withNewSpec().withContainers(container).withVolumes(populateVolume(deploymentModel)).endSpec().endTemplate().endSpec().build();
    try {
        return SerializationUtils.dumpWithoutRuntimeStateAsYaml(deployment);
    } catch (JsonProcessingException e) {
        String errorMessage = "Error while parsing yaml file for deployment: " + deploymentModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
Also used : Container(io.fabric8.kubernetes.api.model.Container) ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort) Deployment(io.fabric8.kubernetes.api.model.extensions.Deployment) KubernetesPluginException(org.ballerinax.kubernetes.exceptions.KubernetesPluginException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) DeploymentBuilder(io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)

Example 3 with ContainerPort

use of io.fabric8.kubernetes.api.model.ContainerPort 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 4 with ContainerPort

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

the class KubernetesResourceUtil method ensureHasPort.

private static void ensureHasPort(Container container, ContainerPort port) {
    List<ContainerPort> ports = container.getPorts();
    if (ports == null) {
        ports = new ArrayList<>();
        container.setPorts(ports);
    }
    for (ContainerPort cp : ports) {
        String n1 = cp.getName();
        String n2 = port.getName();
        if (n1 != null && n2 != null && n1.equals(n2)) {
            return;
        }
        Integer p1 = cp.getContainerPort();
        Integer p2 = port.getContainerPort();
        if (p1 != null && p2 != null && p1.intValue() == p2.intValue()) {
            return;
        }
    }
    ports.add(port);
}
Also used : ContainerPort(io.fabric8.kubernetes.api.model.ContainerPort)

Example 5 with ContainerPort

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

the class SpringBootWatcher method getPortForwardUrl.

private String getPortForwardUrl(final Set<HasMetadata> resources) throws Exception {
    LabelSelector selector = KubernetesResourceUtil.getPodLabelSelector(resources);
    if (selector == null) {
        log.warn("Unable to determine a selector for application pods");
        return null;
    }
    Properties properties = SpringBootUtil.getSpringBootApplicationProperties(getContext().getProject());
    SpringBootConfigurationHelper propertyHelper = new SpringBootConfigurationHelper(SpringBootUtil.getSpringBootVersion(getContext().getProject()));
    PortForwardService portForwardService = getContext().getFabric8ServiceHub().getPortForwardService();
    int port = IoUtil.getFreeRandomPort();
    int containerPort = findSpringBootWebPort(propertyHelper, properties);
    portForwardService.forwardPortAsync(getContext().getLogger(), selector, containerPort, port);
    return createForwardUrl(propertyHelper, properties, port);
}
Also used : SpringBootConfigurationHelper(io.fabric8.maven.core.util.SpringBootConfigurationHelper) LabelSelector(io.fabric8.kubernetes.api.model.LabelSelector) Properties(java.util.Properties) PortForwardService(io.fabric8.maven.core.service.PortForwardService)

Aggregations

ContainerPort (io.fabric8.kubernetes.api.model.ContainerPort)17 Container (io.fabric8.kubernetes.api.model.Container)11 ArrayList (java.util.ArrayList)8 PodSpec (io.fabric8.kubernetes.api.model.PodSpec)6 HashMap (java.util.HashMap)6 ContainerPortBuilder (io.fabric8.kubernetes.api.model.ContainerPortBuilder)5 List (java.util.List)5 Map (java.util.Map)5 EnvVar (io.fabric8.kubernetes.api.model.EnvVar)3 Exchange (org.apache.camel.Exchange)3 Processor (org.apache.camel.Processor)3 Test (org.junit.Test)3 ContainerBuilder (io.fabric8.kubernetes.api.model.ContainerBuilder)2 Deployment (io.fabric8.kubernetes.api.model.extensions.Deployment)2 DeploymentBuilder (io.fabric8.kubernetes.api.model.extensions.DeploymentBuilder)2 Collectors (java.util.stream.Collectors)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Deployment (com.liveperson.ephemerals.deploy.Deployment)1 com.liveperson.ephemerals.deploy.probe (com.liveperson.ephemerals.deploy.probe)1 ContainerizedDeploymentUnit (com.liveperson.ephemerals.deploy.unit.ContainerizedDeploymentUnit)1