io.fabric8.kubernetes.api.model.ContainerPort Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.ContainerPort. 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: KafkaCluster.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
private List<ContainerPort> getContainerPortList() {
    List<ContainerPort> portList = new ArrayList<>(5);
    portList.add(createContainerPort(REPLICATION_PORT_NAME, REPLICATION_PORT, "TCP"));

    if (listeners != null && listeners.getPlain() != null) {
        portList.add(createContainerPort(CLIENT_PORT_NAME, CLIENT_PORT, "TCP"));
    }

    if (listeners != null && listeners.getTls() != null) {
        portList.add(createContainerPort(CLIENT_TLS_PORT_NAME, CLIENT_TLS_PORT, "TCP"));
    }

    if (isExposed()) {
        portList.add(createContainerPort(EXTERNAL_PORT_NAME, EXTERNAL_PORT, "TCP"));
    }

    if (isMetricsEnabled) {
        portList.add(createContainerPort(METRICS_PORT_NAME, METRICS_PORT, "TCP"));
    }

    return portList;
}
 
Example #2
Source File: KubernetesIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
private PodSpec createPodSpec() throws IOException {
    PodSpec podSpec = new PodSpec();
    podSpec.setHostname("localhost");

    Container container = new Container();
    container.setImage("docker.io/wildflyext/wildfly-camel:latest");
    container.setName("wildfly-camel-test");

    ContainerPort port = new ContainerPort();
    port.setHostIP("0.0.0.0");
    port.setContainerPort(8080);

    List<ContainerPort> ports = new ArrayList<>();
    ports.add(port);
    container.setPorts(ports);

    List<Container> containers = new ArrayList<>();
    containers.add(container);

    podSpec.setContainers(containers);

    return podSpec;
}
 
Example #3
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 #4
Source File: InitJobManagerDecoratorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testMainContainerPorts() {
	final List<ContainerPort> expectedContainerPorts = Arrays.asList(
		new ContainerPortBuilder()
			.withName(Constants.REST_PORT_NAME)
			.withContainerPort(REST_PORT)
		.build(),
		new ContainerPortBuilder()
			.withName(Constants.JOB_MANAGER_RPC_PORT_NAME)
			.withContainerPort(RPC_PORT)
		.build(),
		new ContainerPortBuilder()
			.withName(Constants.BLOB_SERVER_PORT_NAME)
			.withContainerPort(BLOB_SERVER_PORT)
		.build());

	assertEquals(expectedContainerPorts, this.resultMainContainer.getPorts());
}
 
Example #5
Source File: KubernetesServerExposer.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private void exposeInContainerIfNeeded(ServicePort servicePort) {
  if (container
      .getPorts()
      .stream()
      .noneMatch(
          p ->
              p.getContainerPort().equals(servicePort.getPort())
                  && servicePort.getProtocol().equals(p.getProtocol()))) {
    ContainerPort containerPort =
        new ContainerPortBuilder()
            .withContainerPort(servicePort.getPort())
            .withProtocol(servicePort.getProtocol())
            .build();
    container.getPorts().add(containerPort);
  }
}
 
Example #6
Source File: KafkaBridgeCluster.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
protected List<ContainerPort> getContainerPortList() {
    List<ContainerPort> portList = new ArrayList<>(3);

    int port = DEFAULT_REST_API_PORT;
    if (http != null) {
        port = http.getPort();
    }

    portList.add(createContainerPort(REST_API_PORT_NAME, port, "TCP"));

    if (isMetricsEnabled) {
        portList.add(createContainerPort(METRICS_PORT_NAME, METRICS_PORT, "TCP"));
    }

    return portList;
}
 
Example #7
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 #8
Source File: Ports.java    From dekorate with Apache License 2.0 6 votes vote down vote up
public static Optional<ContainerPort> getHttpPort(ContainerFluent<?> container) {
  //If we have a single port, return that no matter what.
  if (container.getPorts().size() == 1) {
    return Optional.of(container.getPorts().get(0));
  }

  //Check the service name
  Optional<ContainerPort> port = container.getPorts().stream().filter(p -> HTTP_PORT_NAMES.contains(p.getName())).findFirst();
  if (port.isPresent()) {
    return port;
  }

  port = container.getPorts().stream().filter(p -> HTTP_PORT_NUMBERS.contains(p.getHostPort())).findFirst();
  if (port.isPresent()) {
    return port;
  }
  return Optional.empty();
}
 
Example #9
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 #10
Source File: KubernetesResourceUtil.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean addPort(List<ContainerPort> ports, String portNumberText, String portName, KitLogger log) {
    if (StringUtils.isBlank(portNumberText)) {
        return false;
    }
    int portValue;
    try {
        portValue = Integer.parseInt(portNumberText);
    } catch (NumberFormatException e) {
        log.warn("Could not parse remote debugging port %s as an integer: %s", portNumberText, e);
        return false;
    }
    for (ContainerPort port : ports) {
        String name = port.getName();
        Integer containerPort = port.getContainerPort();
        if (containerPort != null && containerPort.intValue() == portValue) {
            return false;
        }
    }
    ports.add(new ContainerPortBuilder().withName(portName).withContainerPort(portValue).build());
    return true;
}
 
Example #11
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 #12
Source File: ContainerHandler.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private List<ContainerPort> getContainerPorts(ImageConfiguration imageConfig) {
    BuildConfiguration buildConfig = imageConfig.getBuildConfiguration();
    List<String> ports = buildConfig.getPorts();
    if (!ports.isEmpty()) {
        List<ContainerPort> ret = new ArrayList<>();
        PortMapping portMapping = new PortMapping(ports, configurationProperties);
        JsonArray portSpecs = portMapping.toJson();
        for (int i = 0; i < portSpecs.size(); i ++) {
            JsonObject portSpec = portSpecs.get(i).getAsJsonObject();
            ret.add(extractContainerPort(portSpec));
        }
        return ret;
    } else {
        return null;
    }
}
 
Example #13
Source File: KnativeServiceHandler.java    From module-ballerina-kubernetes with Apache License 2.0 6 votes vote down vote up
private Container generateContainer(ServiceModel serviceModel, List<ContainerPort> containerPorts) {
    String dockerRegistry = serviceModel.getRegistry();
    String deploymentImageName = serviceModel.getImage();
    if (null != dockerRegistry && !"".equals(dockerRegistry)) {
        deploymentImageName = dockerRegistry + REGISTRY_SEPARATOR + deploymentImageName;
    }
    return new ContainerBuilder()
            .withName(serviceModel.getName())
            .withImage(deploymentImageName)
            .withPorts(containerPorts)
            .withEnv(populateEnvVar(serviceModel.getEnv()))
            .withVolumeMounts(populateVolumeMounts(serviceModel))
            .withLivenessProbe(generateProbe(serviceModel.getLivenessProbe()))
            .withReadinessProbe(generateProbe(serviceModel.getReadinessProbe()))
            .build();
}
 
Example #14
Source File: AbstractModel.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
protected ContainerPort createContainerPort(String name, int port, String protocol) {
    ContainerPort containerPort = new ContainerPortBuilder()
            .withName(name)
            .withProtocol(protocol)
            .withContainerPort(port)
            .build();
    log.trace("Created container port {}", containerPort);
    return containerPort;
}
 
Example #15
Source File: KafkaConnectCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
protected List<ContainerPort> getContainerPortList() {
    List<ContainerPort> portList = new ArrayList<>(2);
    portList.add(createContainerPort(REST_API_PORT_NAME, REST_API_PORT, "TCP"));
    if (isMetricsEnabled) {
        portList.add(createContainerPort(METRICS_PORT_NAME, METRICS_PORT, "TCP"));
    }

    return portList;
}
 
Example #16
Source File: KnativeServiceHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
/**
 * Generate kubernetes deployment definition from annotation.
 *
 * @param serviceModel @{@link ServiceModel} definition
 * @throws KubernetesPluginException If an error occurs while generating artifact.
 */
private void generate(ServiceModel serviceModel) throws KubernetesPluginException {
    List<ContainerPort> containerPorts = null;
    if (serviceModel.getPorts() != null) {
        containerPorts = populatePorts(serviceModel.getPorts());
    }
    Container container = generateContainer(serviceModel, containerPorts);
    Service knativeSvc = new ServiceBuilder()
            .withNewMetadata()
            .withName(serviceModel.getName())
            .withNamespace(knativeDataHolder.getNamespace())
            .withAnnotations(serviceModel.getAnnotations())
            .withLabels(serviceModel.getLabels())
            .endMetadata()
            .withNewSpec()
            .withNewTemplate()
            .withNewSpec()
            .withContainerConcurrency((long) serviceModel.getContainerConcurrency())
            .withTimeoutSeconds((long) serviceModel.getTimeoutSeconds())
            .withContainers(container)
            .withInitContainers(generateInitContainer(serviceModel))
            .withVolumes(populateVolume(serviceModel))
            .endSpec()
            .endTemplate()
            .endSpec()
            .build();

    try {
        String knativeSvcContent = SerializationUtils.dumpWithoutRuntimeStateAsYaml(knativeSvc);
        KnativeUtils.writeToFile(knativeSvcContent, KNATIVE_SVC_FILE_POSTFIX + YAML);
    } catch (IOException e) {
        String errorMessage = "error while generating yaml file for deployment: " + serviceModel.getName();
        throw new KubernetesPluginException(errorMessage, e);
    }
}
 
Example #17
Source File: CruiseControl.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
protected List<ContainerPort> getContainerPortList() {
    List<ContainerPort> portList = new ArrayList<>(1);

    portList.add(createContainerPort(REST_API_PORT_NAME, REST_API_PORT, "TCP"));

    if (isMetricsEnabled) {
        portList.add(createContainerPort(METRICS_PORT_NAME, METRICS_PORT, "TCP"));
    }

    return portList;
}
 
Example #18
Source File: KafkaMirrorMakerCluster.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
protected List<ContainerPort> getContainerPortList() {
    List<ContainerPort> portList = new ArrayList<>(1);
    if (isMetricsEnabled) {
        portList.add(createContainerPort(METRICS_PORT_NAME, METRICS_PORT, "TCP"));
    }

    return portList;
}
 
Example #19
Source File: OpenShiftInternalRuntimeTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static Container mockContainer(String name, int... ports) {
  final Container container = mock(Container.class);
  when(container.getName()).thenReturn(name);
  final List<ContainerPort> containerPorts = new ArrayList<>(ports.length);
  for (int port : ports) {
    containerPorts.add(new ContainerPortBuilder().withContainerPort(port).build());
  }
  when(container.getPorts()).thenReturn(containerPorts);
  return container;
}
 
Example #20
Source File: ContainerHandler.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private ContainerPort extractContainerPort(JsonObject portSpec) {
    ContainerPortBuilder portBuilder = new ContainerPortBuilder()
        .withContainerPort(portSpec.get("containerPort").getAsInt());
    if (portSpec.has("hostPort")) {
        portBuilder.withHostPort(portSpec.get("hostPort").getAsInt());
    }
    if (portSpec.has("protocol")) {
        portBuilder.withProtocol(portSpec.get("protocol").getAsString().toUpperCase());
    }
    if (portSpec.has("hostIP")) {
        portBuilder.withHostIP(portSpec.get("hostIP").getAsString());
    }
    return portBuilder.build();
}
 
Example #21
Source File: K8sContainerResolver.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private List<ContainerPort> getContainerPorts() {
  return containerEndpoints
      .stream()
      .map(
          endpoint ->
              new ContainerPortBuilder()
                  .withContainerPort(endpoint.getTargetPort())
                  .withProtocol("TCP")
                  .build())
      .collect(Collectors.toList());
}
 
Example #22
Source File: KubernetesInternalRuntimeTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private static Container mockContainer(String name, int... ports) {
  final Container container = mock(Container.class);
  when(container.getName()).thenReturn(name);
  final List<ContainerPort> containerPorts = new ArrayList<>(ports.length);
  for (int port : ports) {
    containerPorts.add(new ContainerPortBuilder().withContainerPort(port).build());
  }
  when(container.getPorts()).thenReturn(containerPorts);
  return container;
}
 
Example #23
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 #24
Source File: PortMapping.java    From kubernetes-plugin with Apache License 2.0 5 votes vote down vote up
public ContainerPort toPort() {
    ContainerPort p = new ContainerPort();
    p.setName(name);
    p.setContainerPort(containerPort);
    if(hostPort != null) {
        p.setHostPort(hostPort);
    }
    return p;
}
 
Example #25
Source File: InitJobManagerDecorator.java    From flink with Apache License 2.0 5 votes vote down vote up
private List<ContainerPort> getContainerPorts() {
	return Arrays.asList(
		new ContainerPortBuilder()
			.withName(Constants.REST_PORT_NAME)
			.withContainerPort(kubernetesJobManagerParameters.getRestPort())
			.build(),
		new ContainerPortBuilder()
			.withName(Constants.JOB_MANAGER_RPC_PORT_NAME)
			.withContainerPort(kubernetesJobManagerParameters.getRPCPort())
			.build(),
		new ContainerPortBuilder()
			.withName(Constants.BLOB_SERVER_PORT_NAME)
			.withContainerPort(kubernetesJobManagerParameters.getBlobServerPort())
			.build());
}
 
Example #26
Source File: InitTaskManagerDecoratorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testMainContainerPorts() {
	final List<ContainerPort> expectedContainerPorts = Collections.singletonList(
		new ContainerPortBuilder()
			.withName(Constants.TASK_MANAGER_RPC_PORT_NAME)
			.withContainerPort(RPC_PORT)
		.build());

	assertEquals(expectedContainerPorts, this.resultMainContainer.getPorts());
}
 
Example #27
Source File: KubernetesIaasUtil.java    From attic-stratos with Apache License 2.0 5 votes vote down vote up
/**
 * Convert stratos port mappings to kubernetes container ports
 *
 * @param portMappings
 * @return
 */
public static List<ContainerPort> convertPortMappings(List<PortMapping> portMappings) {
    List<ContainerPort> ports = new ArrayList<ContainerPort>();
    for (PortMapping portMapping : portMappings) {
        ContainerPort containerPort = new ContainerPort();
        containerPort.setName(preparePortNameFromPortMapping(portMapping));
        containerPort.setContainerPort(portMapping.getPort());
        ports.add(containerPort);
    }
    return ports;
}
 
Example #28
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 #29
Source File: DeploymentHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
private List<ContainerPort> populatePorts(Set<Integer> ports) {
    List<ContainerPort> containerPorts = new ArrayList<>();
    for (int port : ports) {
        ContainerPort containerPort = new ContainerPortBuilder()
                .withContainerPort(port)
                .withProtocol(KubernetesConstants.KUBERNETES_SVC_PROTOCOL)
                .build();
        containerPorts.add(containerPort);
    }
    return containerPorts;
}
 
Example #30
Source File: KnativeServiceHandler.java    From module-ballerina-kubernetes with Apache License 2.0 5 votes vote down vote up
private List<ContainerPort> populatePorts(Set<Integer> ports) {
    List<ContainerPort> containerPorts = new ArrayList<>();
    for (int port : ports) {
        ContainerPort containerPort = new ContainerPortBuilder()
                .withContainerPort(port)
                .withProtocol(KubernetesConstants.KUBERNETES_SVC_PROTOCOL)
                .build();
        containerPorts.add(containerPort);
    }
    return containerPorts;
}