Java Code Examples for io.fabric8.kubernetes.api.model.Container#getPorts()

The following examples show how to use io.fabric8.kubernetes.api.model.Container#getPorts() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
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);
}
 
Example 2
Source File: Issue421Test.java    From dekorate with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldHavePort() {
  KubernetesList list = Serialization.unmarshalAsList(Issue421Test.class.getClassLoader().getResourceAsStream("META-INF/dekorate/kubernetes.yml"));
  assertNotNull(list);
  Deployment d = findFirst(list, Deployment.class).orElseThrow(() -> new IllegalStateException());
  assertNotNull(d);
  Container c = d.getSpec().getTemplate().getSpec().getContainers().get(0);
  assertNotNull(c);

  List<ContainerPort> ports = c.getPorts();
  assertNotNull(ports);
  assertEquals(1, ports.size());

  ContainerPort port = ports.get(0);
  assertEquals("HTTP", port.getName());
  assertEquals(8080, port.getContainerPort());
}
 
Example 3
Source File: DefaultContainerFactoryTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 6 votes vote down vote up
@Test
public void createWithPorts() {
	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	DefaultContainerFactory defaultContainerFactory = new DefaultContainerFactory(
			kubernetesDeployerProperties);

	AppDefinition definition = new AppDefinition("app-test", null);
	Resource resource = getResource();
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.containerPorts",
			"8081, 8082, 65535");
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition,
			resource, props);

	ContainerConfiguration containerConfiguration = new ContainerConfiguration("app-test", appDeploymentRequest);
	Container container = defaultContainerFactory.create(containerConfiguration);
	assertNotNull(container);
	List<ContainerPort> containerPorts = container.getPorts();
	assertNotNull(containerPorts);
	assertTrue("There should be three ports set", containerPorts.size() == 3);
	assertTrue(8081 == containerPorts.get(0).getContainerPort());
	assertTrue(8082 == containerPorts.get(1).getContainerPort());
	assertTrue(65535 == containerPorts.get(2).getContainerPort());
}
 
Example 4
Source File: K8sNetworkingUtil.java    From onos with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the container port number by given container port name.
 *
 * @param pod           kubernetes POD
 * @param portName      port name
 * @return container port number,
 *         return 0 if there is no port number mapped with the given port name
 */
public static int portNumberByName(Pod pod, String portName) {

    if (pod == null || pod.getSpec() == null) {
        return 0;
    }

    for (Container container : pod.getSpec().getContainers()) {
        for (ContainerPort cp : container.getPorts()) {
            if (cp.getName() != null && cp.getName().equals(portName)) {
                return cp.getContainerPort();
            }
        }
    }

    return 0;
}
 
Example 5
Source File: DebugEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private boolean enableDebugging(HasMetadata entity, PodTemplateSpec template) {
    if (template != null) {
        PodSpec podSpec = template.getSpec();
        if (podSpec != null) {
            List<Container> containers = podSpec.getContainers();
            if (!containers.isEmpty()) {
                Container container = containers.get(0);
                List<EnvVar> env = container.getEnv();
                if (env == null) {
                    env = new ArrayList<>();
                }
                String remoteDebugPort =
                    KubernetesHelper.getEnvVar(env, ENV_VAR_JAVA_DEBUG_PORT, ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                boolean enabled = false;
                if (KubernetesHelper.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 " + KubernetesHelper.getKind(entity) + " " + KubernetesHelper.getName(
                        entity) + " due to the property: " + ENABLE_DEBUG_MAVEN_PROPERTY);
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 6
Source File: DefaultContainerFactoryTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void createWithPortAndHostNetwork() {
	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	DefaultContainerFactory defaultContainerFactory = new DefaultContainerFactory(
			kubernetesDeployerProperties);

	AppDefinition definition = new AppDefinition("app-test", null);
	Resource resource = getResource();
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.containerPorts",
			"8081, 8082, 65535");
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition,
			resource, props);

	ContainerConfiguration containerConfiguration = new ContainerConfiguration("app-test", appDeploymentRequest)
			.withHostNetwork(true);
	Container container = defaultContainerFactory.create(containerConfiguration);
	assertNotNull(container);
	List<ContainerPort> containerPorts = container.getPorts();
	assertNotNull(containerPorts);
	assertTrue("There should be three container ports set", containerPorts.size() == 3);
	assertTrue(8081 == containerPorts.get(0).getContainerPort());
	assertTrue(8081 == containerPorts.get(0).getHostPort());
	assertTrue(8082 == containerPorts.get(1).getContainerPort());
	assertTrue(8082 == containerPorts.get(1).getHostPort());
	assertTrue(65535 == containerPorts.get(2).getContainerPort());
	assertTrue(65535 == containerPorts.get(2).getHostPort());
}
 
Example 7
Source File: DefaultContainerFactoryTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void createCustomLivenessPortFromProperties() {
	int defaultPort = 8080;
	int livenessPort = 8090;

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.setLivenessProbePort(livenessPort);
	DefaultContainerFactory defaultContainerFactory = new DefaultContainerFactory(
			kubernetesDeployerProperties);

	AppDefinition definition = new AppDefinition("app-test", null);
	Resource resource = getResource();
	Map<String, String> props = new HashMap<>();
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition,
			resource, props);

	ContainerConfiguration containerConfiguration = new ContainerConfiguration("app-test", appDeploymentRequest)
			.withExternalPort(defaultPort)
			.withHostNetwork(true);
	Container container = defaultContainerFactory.create(containerConfiguration);
	assertNotNull(container);

	List<ContainerPort> containerPorts = container.getPorts();
	assertNotNull(containerPorts);

	assertTrue("Only two container ports should be set", containerPorts.size() == 2);
	assertTrue(8080 == containerPorts.get(0).getContainerPort());
	assertTrue(8080 == containerPorts.get(0).getHostPort());
	assertTrue(8090 == containerPorts.get(1).getContainerPort());
	assertTrue(8090 == containerPorts.get(1).getHostPort());
	assertTrue(8090 == container.getLivenessProbe().getHttpGet().getPort().getIntVal());
}
 
Example 8
Source File: DefaultContainerFactoryTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void createCustomLivenessPortFromAppRequest() {
	int defaultPort = 8080;
	int livenessPort = 8090;

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	DefaultContainerFactory defaultContainerFactory = new DefaultContainerFactory(
			kubernetesDeployerProperties);

	AppDefinition definition = new AppDefinition("app-test", null);
	Resource resource = getResource();
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.liveness-probe-port", Integer.toString(livenessPort));
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition,
			resource, props);

	ContainerConfiguration containerConfiguration = new ContainerConfiguration("app-test", appDeploymentRequest)
			.withHostNetwork(true)
			.withExternalPort(defaultPort);
	Container container = defaultContainerFactory.create(containerConfiguration);
	assertNotNull(container);

	List<ContainerPort> containerPorts = container.getPorts();
	assertNotNull(containerPorts);

	assertTrue("Only two container ports should be set", containerPorts.size() == 2);
	assertTrue(8080 == containerPorts.get(0).getContainerPort());
	assertTrue(8080 == containerPorts.get(0).getHostPort());
	assertTrue(8090 == containerPorts.get(1).getContainerPort());
	assertTrue(8090 == containerPorts.get(1).getHostPort());
	assertTrue(8090 == container.getLivenessProbe().getHttpGet().getPort().getIntVal());
}
 
Example 9
Source File: DefaultContainerFactoryTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void createCustomReadinessPortFromAppRequest() {
	int defaultPort = 8080;
	int readinessPort = 8090;

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	DefaultContainerFactory defaultContainerFactory = new DefaultContainerFactory(
			kubernetesDeployerProperties);

	AppDefinition definition = new AppDefinition("app-test", null);
	Resource resource = getResource();
	Map<String, String> props = new HashMap<>();
	props.put("spring.cloud.deployer.kubernetes.readinessProbePort", Integer.toString(readinessPort));
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition,
			resource, props);

	ContainerConfiguration containerConfiguration = new ContainerConfiguration("app-test", appDeploymentRequest)
			.withHostNetwork(true)
			.withExternalPort(defaultPort);
	Container container = defaultContainerFactory.create(containerConfiguration);
	assertNotNull(container);

	List<ContainerPort> containerPorts = container.getPorts();
	assertNotNull(containerPorts);

	assertTrue("Only two container ports should be set", containerPorts.size() == 2);
	assertTrue(8080 == containerPorts.get(0).getContainerPort());
	assertTrue(8080 == containerPorts.get(0).getHostPort());
	assertTrue(8090 == containerPorts.get(1).getContainerPort());
	assertTrue(8090 == containerPorts.get(1).getHostPort());
	assertTrue(8090 == container.getReadinessProbe().getHttpGet().getPort().getIntVal());
}
 
Example 10
Source File: DefaultContainerFactoryTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void createCustomReadinessPortFromProperties() {
	int defaultPort = 8080;
	int readinessPort = 8090;

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	kubernetesDeployerProperties.setReadinessProbePort(readinessPort);
	DefaultContainerFactory defaultContainerFactory = new DefaultContainerFactory(
			kubernetesDeployerProperties);

	AppDefinition definition = new AppDefinition("app-test", null);
	Resource resource = getResource();
	Map<String, String> props = new HashMap<>();
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition,
			resource, props);

	ContainerConfiguration containerConfiguration = new ContainerConfiguration("app-test", appDeploymentRequest)
			.withHostNetwork(true)
			.withExternalPort(defaultPort);
	Container container = defaultContainerFactory.create(containerConfiguration);
	assertNotNull(container);

	List<ContainerPort> containerPorts = container.getPorts();
	assertNotNull(containerPorts);

	assertTrue("Only two container ports should be set", containerPorts.size() == 2);
	assertTrue(8080 == containerPorts.get(0).getContainerPort());
	assertTrue(8080 == containerPorts.get(0).getHostPort());
	assertTrue(8090 == containerPorts.get(1).getContainerPort());
	assertTrue(8090 == containerPorts.get(1).getHostPort());
	assertTrue(8090 == container.getReadinessProbe().getHttpGet().getPort().getIntVal());
}
 
Example 11
Source File: DefaultContainerFactoryTests.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void createDefaultProbePorts() {
	int defaultPort = 8080;

	KubernetesDeployerProperties kubernetesDeployerProperties = new KubernetesDeployerProperties();
	DefaultContainerFactory defaultContainerFactory = new DefaultContainerFactory(
			kubernetesDeployerProperties);

	AppDefinition definition = new AppDefinition("app-test", null);
	Resource resource = getResource();
	Map<String, String> props = new HashMap<>();
	AppDeploymentRequest appDeploymentRequest = new AppDeploymentRequest(definition,
			resource, props);

	ContainerConfiguration containerConfiguration = new ContainerConfiguration("app-test", appDeploymentRequest)
			.withHostNetwork(true)
			.withExternalPort(defaultPort);
	Container container = defaultContainerFactory.create(containerConfiguration);
	assertNotNull(container);
	List<ContainerPort> containerPorts = container.getPorts();
	assertNotNull(containerPorts);
	assertTrue("Only the default container port should set", containerPorts.size() == 1);
	assertTrue(8080 == containerPorts.get(0).getContainerPort());
	assertTrue(8080 == containerPorts.get(0).getHostPort());
	assertTrue(8080 == container.getLivenessProbe().getHttpGet().getPort().getIntVal());
	assertTrue(8080 == container.getReadinessProbe().getHttpGet().getPort().getIntVal());
}
 
Example 12
Source File: KubernetesPluginsToolingApplierTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private void verifyPortsExposed(Container container, int... ports) {
  List<ContainerPort> actualPorts = container.getPorts();
  List<ContainerPort> expectedPorts = new ArrayList<>();
  for (int port : ports) {
    expectedPorts.add(
        new ContainerPortBuilder().withContainerPort(port).withProtocol("TCP").build());
  }
  assertEquals(actualPorts, expectedPorts);
}
 
Example 13
Source File: DebugMojo.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
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 = KubernetesHelper.getEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT, DebugConstants.ENV_VAR_JAVA_DEBUG_PORT_DEFAULT);
                if (KubernetesHelper.setEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG, "true")) {
                    container.setEnv(env);
                    enabled = true;
                }
                if (KubernetesHelper.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());
                    KubernetesHelper.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 " + KubernetesHelper.getKind(entity) + " " + getName(entity) + " to allow attaching a remote debugger during suspension");
                        container.setReadinessProbe(null);
                    }
                    enabled = true;
                } else {
                    if (KubernetesHelper.removeEnvVar(env, DebugConstants.ENV_VAR_JAVA_DEBUG_SESSION)) {
                        container.setEnv(env);
                        enabled = true;
                    }
                }
            }
            if (enabled) {
                log.info("Enabling debug on " + KubernetesHelper.getKind(entity) + " " + getName(entity));
                return true;
            }
        }
    }
    return false;
}
 
Example 14
Source File: PodInfo.java    From fabric8-forge with Apache License 2.0 4 votes vote down vote up
@Override
protected void executePod(Pod podInfo, String podId) {
    System.out.println("Created: " + podInfo.getMetadata().getCreationTimestamp());
    System.out.println("Labels: ");
    Map<String, String> labels = podInfo.getMetadata().getLabels();
    for (Map.Entry<String, String> entry : labels.entrySet()) {
        System.out.println(indent + entry.getKey() + " = " + entry.getValue());
    }
    PodStatus currentState = podInfo.getStatus();
    if (currentState != null) {
        printValue("Host", currentState.getHostIP());
        printValue("IP", currentState.getPodIP());
        printValue("Status", getStatusText(currentState));
    }
    PodSpec spec = podInfo.getSpec();
    if (spec != null) {
        List<Container> containers = spec.getContainers();
        if (notEmpty(containers)) {
            System.out.println("Containers:");
            indentCount++;
            for (Container container : containers) {
                printValue("Name", container.getName());
                printValue("Image", container.getImage());
                printValue("Working Dir", container.getWorkingDir());
                printValue("Command", container.getCommand());

                List<ContainerPort> ports = container.getPorts();
                if (notEmpty(ports)) {
                    println("Ports:");
                    indentCount++;
                    for (ContainerPort port : ports) {
                        printValue("Name", port.getName());
                        printValue("Protocol", port.getProtocol());
                        printValue("Host Port", port.getHostPort());
                        printValue("Container Port", port.getContainerPort());
                    }
                    indentCount--;
                }

                List<EnvVar> envList = container.getEnv();
                if (notEmpty(envList)) {
                    println("Environment:");
                    indentCount++;
                    for (EnvVar env : envList) {
                        printValue(env.getName(), env.getValue());
                    }
                    indentCount--;
                }
                List<VolumeMount> volumeMounts = container.getVolumeMounts();
                if (notEmpty(volumeMounts)) {
                    println("Volume Mounts:");
                    indentCount++;
                    for (VolumeMount volumeMount : volumeMounts) {
                        printValue("Name", volumeMount.getName());
                        printValue("Mount Path", volumeMount.getMountPath());
                        printValue("Read Only", volumeMount.getReadOnly());
                    }
                    indentCount--;
                }
            }
        }

        List<Volume> volumes = spec.getVolumes();
        if (volumes != null) {
            System.out.println("Volumes: ");
            for (Volume volume : volumes) {
                System.out.println(indent + volume.getName());
            }
        }
    }
}