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

The following examples show how to use io.fabric8.kubernetes.api.model.ContainerPortBuilder. 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: 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 #2
Source File: InitTaskManagerDecorator.java    From flink with Apache License 2.0 6 votes vote down vote up
private Container decorateMainContainer(Container container) {
	final ResourceRequirements resourceRequirements = KubernetesUtils.getResourceRequirements(
			kubernetesTaskManagerParameters.getTaskManagerMemoryMB(),
			kubernetesTaskManagerParameters.getTaskManagerCPU(),
			kubernetesTaskManagerParameters.getTaskManagerExternalResources());

	return new ContainerBuilder(container)
			.withName(kubernetesTaskManagerParameters.getTaskManagerMainContainerName())
			.withImage(kubernetesTaskManagerParameters.getImage())
			.withImagePullPolicy(kubernetesTaskManagerParameters.getImagePullPolicy().name())
			.withResources(resourceRequirements)
			.withPorts(new ContainerPortBuilder()
				.withName(Constants.TASK_MANAGER_RPC_PORT_NAME)
				.withContainerPort(kubernetesTaskManagerParameters.getRPCPort())
				.build())
			.withEnv(getCustomizedEnvs())
			.build();
}
 
Example #3
Source File: PortNameEnricherTest.java    From jkube with Eclipse Public License 2.0 6 votes vote down vote up
private KubernetesListBuilder getPodTemplateList() {
    Container container = new ContainerBuilder()
            .withName("test-port-enricher")
            .withImage("test-image")
            .withPorts(new ContainerPortBuilder().withContainerPort(80).withProtocol("TCP").build())
            .build();
    PodTemplateBuilder ptb = new PodTemplateBuilder()
            .withNewMetadata().withName("test-pod")
            .endMetadata()
            .withNewTemplate()
            .withNewSpec()
            .withContainers(container)
            .endSpec()
            .endTemplate();
    return new KubernetesListBuilder().addToPodTemplateItems(ptb.build());
}
 
Example #4
Source File: KubernetesServerExposerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void shouldNotAddAdditionalContainerPortWhenItIsAlreadyExposed() throws Exception {
  // given
  ServerConfigImpl httpServerConfig =
      new ServerConfigImpl("8080/tcp", "http", "/api", ATTRIBUTES_MAP);
  Map<String, ServerConfigImpl> serversToExpose =
      ImmutableMap.of("http-server", httpServerConfig);
  container.setPorts(
      singletonList(
          new ContainerPortBuilder()
              .withName("port-8080")
              .withContainerPort(8080)
              .withProtocol("TCP")
              .build()));

  // when
  serverExposer.expose(serversToExpose);

  // then
  assertThatExternalServerIsExposed(
      MACHINE_NAME,
      "tcp",
      8080,
      "http-server",
      new ServerConfigImpl(httpServerConfig).withAttributes(ATTRIBUTES_MAP));
}
 
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: 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 #7
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 #8
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 #9
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 #10
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 #11
Source File: KubernetesServerExposerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldAddAdditionalContainerPortWhenThereIsTheSameButWithDifferentProtocol()
    throws Exception {
  // given
  ServerConfigImpl udpServerConfig =
      new ServerConfigImpl("8080/udp", "udp", "/api", ATTRIBUTES_MAP);
  Map<String, ServerConfigImpl> serversToExpose = ImmutableMap.of("server", udpServerConfig);
  container.setPorts(
      new ArrayList<>(
          singletonList(
              new ContainerPortBuilder()
                  .withName("port-8080")
                  .withContainerPort(8080)
                  .withProtocol("TCP")
                  .build())));

  // when
  serverExposer.expose(serversToExpose);

  // then
  assertEquals(container.getPorts().size(), 2);
  assertEquals(container.getPorts().get(1).getContainerPort(), new Integer(8080));
  assertEquals(container.getPorts().get(1).getProtocol(), "UDP");
  assertThatExternalServerIsExposed(
      MACHINE_NAME,
      "udp",
      8080,
      "server",
      new ServerConfigImpl(udpServerConfig).withAttributes(ATTRIBUTES_MAP));
}
 
Example #12
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 #13
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 #14
Source File: SystemtestsKubernetesApps.java    From enmasse with Apache License 2.0 5 votes vote down vote up
private static Deployment getProxyApiAppDeploymentResource() {
    return new DeploymentBuilder()
            .withNewMetadata()
            .withName(API_PROXY)
            .addToLabels("app", API_PROXY)
            .endMetadata()
            .withNewSpec()
            .withNewSelector()
            .addToMatchLabels("app", API_PROXY)
            .endSelector()
            .withReplicas(1)
            .withNewTemplate()
            .withNewMetadata()
            .addToLabels("app", API_PROXY)
            .endMetadata()
            .withNewSpec()
            .addNewContainer()
            .withName(API_PROXY)
            .withImage("quay.io/enmasse/api-proxy:latest")
            .withPorts(new ContainerPortBuilder().withContainerPort(8443).withName("https").withProtocol("TCP").build())
            .withVolumeMounts(new VolumeMountBuilder().withMountPath("/etc/tls/private").withName("api-proxy-tls").withReadOnly(true).build())
            .endContainer()
            .withVolumes(Collections.singletonList(new VolumeBuilder().withName("api-proxy-tls").withSecret(new SecretVolumeSourceBuilder().withDefaultMode(420).withSecretName("api-proxy-cert").build()).build()))
            .endSpec()
            .endTemplate()
            .endSpec()
            .build();
}
 
Example #15
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 #16
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 #17
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;
}
 
Example #18
Source File: PortNameEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private void addPortName(ContainerPortBuilder builder, Integer port) {
    String protocol = getProtocol(builder);
    try {
        String serviceName = getDefaultServiceName(port);
        if (serviceName == null) {
            serviceName = extractIANAServiceName(port, protocol);
        }
        if (StringUtils.isNotBlank(serviceName)) {
            builder.withName(serviceName);
        }
    } catch (IOException e) {
        log.error("Internal: Failed to find IANA service names for port %d/%s : %s",
                  port, protocol, e.getMessage());
    }
}
 
Example #19
Source File: PortNameEnricher.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void create(PlatformMode platformMode, KubernetesListBuilder builder) {
    builder.accept(new TypedVisitor<ContainerPortBuilder>() {
        @Override
        public void visit(ContainerPortBuilder portBuilder) {
            Integer port = portBuilder.getContainerPort();

            // If port is given but no name, then try to detect the name
            if (port != null && StringUtils.isBlank(portBuilder.getName())) {
                addPortName(portBuilder, port);
            }
        }
    });
}
 
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: SystemtestsKubernetesApps.java    From enmasse with Apache License 2.0 4 votes vote down vote up
private static Deployment getBrokerDeployment(String name, String user, String password) {
    return new DeploymentBuilder()
            .withNewMetadata()
            .withName(name)
            .addToLabels("app", name)
            .endMetadata()
            .withNewSpec()
            .withNewSelector()
            .addToMatchLabels("app", name)
            .endSelector()
            .withReplicas(1)
            .withNewTemplate()
            .withNewMetadata()
            .addToLabels("app", name)
            .endMetadata()
            .withNewSpec()
            .addToInitContainers(new ContainerBuilder()
                            .withName("artemis-init")
                            .withImage("quay.io/enmasse/artemis-base:2.11.0")
                            .withCommand("/bin/sh")
                            .withArgs("-c",
                                    "/opt/apache-artemis/bin/artemis create /var/run/artemis --allow-anonymous --force --user " + user + " --password " + password + " --role admin")
                            .withVolumeMounts(new VolumeMountBuilder()
                                            .withName("data")
                                            .withMountPath("/var/run/artemis")
                                            .build(),
                                    new VolumeMountBuilder()
                                            .withName(name)
                                            .withMountPath("/etc/amq-secret-volume")
                                            .build())
                            .build(),
                    new ContainerBuilder()
                            .withName("replace-broker-xml")
                            .withImage("quay.io/enmasse/artemis-base:2.11.0")
                            .withCommand("/bin/sh")
                            .withArgs("-c", "cp /etc/amq-secret-volume/broker.xml /var/run/artemis/etc/broker.xml")
                            .withVolumeMounts(new VolumeMountBuilder()
                                            .withName("data")
                                            .withMountPath("/var/run/artemis")
                                            .build(),
                                    new VolumeMountBuilder()
                                            .withName(name)
                                            .withMountPath("/etc/amq-secret-volume")
                                            .build())
                            .build())
            .addNewContainer()
            .withName(name)
            .withImage("quay.io/enmasse/artemis-base:2.11.0")
            .withImagePullPolicy("IfNotPresent")
            .withCommand("/bin/sh")
            .withArgs("-c", "/var/run/artemis/bin/artemis run")
            .addToPorts(new ContainerPortBuilder()
                            .withContainerPort(5672)
                            .withName("amqp")
                            .build(),
                    new ContainerPortBuilder()
                            .withContainerPort(5671)
                            .withName("amqps")
                            .build(),
                    new ContainerPortBuilder()
                            .withContainerPort(55671)
                            .withName("amqpsmutual")
                            .build())
            .withVolumeMounts(new VolumeMountBuilder()
                            .withName("data")
                            .withMountPath("/var/run/artemis")
                            .build(),
                    new VolumeMountBuilder()
                            .withName(name)
                            .withMountPath("/etc/amq-secret-volume")
                            .build())
            .endContainer()
            .addToVolumes(new VolumeBuilder()
                            .withName("data")
                            .withNewEmptyDir()
                            .endEmptyDir()
                            .build(),
                    new VolumeBuilder()
                            .withName(name)
                            .withNewSecret()
                            .withSecretName(name)
                            .endSecret()
                            .build())
            .endSpec()
            .endTemplate()
            .endSpec()
            .build();
}
 
Example #22
Source File: KafkaClientsResource.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
public static DoneableDeployment deployKeycloak() {
    String keycloakName = "keycloak";

    Map<String, String> keycloakLabels = new HashMap<>();
    keycloakLabels.put("app", keycloakName);

    return KubernetesResource.deployNewDeployment(new DeploymentBuilder()
        .withNewMetadata()
            .withNamespace(ResourceManager.kubeClient().getNamespace())
            .withLabels(keycloakLabels)
            .withName(keycloakName)
        .endMetadata()
        .withNewSpec()
            .withNewSelector()
                .withMatchLabels(keycloakLabels)
            .endSelector()
            .withReplicas(1)
            .withNewTemplate()
                .withNewMetadata()
                    .withLabels(keycloakLabels)
                .endMetadata()
                .withNewSpec()
                    .withContainers()
                    .addNewContainer()
                        .withName(keycloakName + "pod")
                        .withImage("jboss/keycloak:8.0.1")
                        .withPorts(
                            new ContainerPortBuilder()
                                .withName("http")
                                .withContainerPort(8080)
                                .build(),
                            new ContainerPortBuilder()
                                .withName("https")
                                .withContainerPort(8443)
                                .build()
                        )
                        .addNewEnv()
                            .withName("KEYCLOAK_USER")
                            .withValue("admin")
                        .endEnv()
                        .addNewEnv()
                            .withName("KEYCLOAK_PASSWORD")
                            .withValue("admin")
                        .endEnv()
                        // for enabling importing authorization script
                        .withArgs("-Dkeycloak.profile.feature.upload_scripts=enabled")
                    .endContainer()
                .endSpec()
            .endTemplate()
        .endSpec()
        .build());
}
 
Example #23
Source File: DeploymentTest.java    From kubernetes-client with Apache License 2.0 4 votes vote down vote up
@Test
public void testRollingUpdate() {
  Deployment deployment = new DeploymentBuilder()
    .withNewMetadata()
    .withName("deployment1")
    .withNamespace("ns1")
    .endMetadata()
    .withNewSpec()
    .withReplicas(1)
    .withNewSelector()
    .withMatchLabels(Collections.singletonMap("service", "http-server"))
    .endSelector()
    .withNewStrategy()
    .withType("RollingUpdate")
    .withNewRollingUpdate()
    .withNewMaxSurge(1)
    .withNewMaxUnavailable(1)
    .endRollingUpdate()
    .endStrategy()
    .withMinReadySeconds(5)
    .withNewTemplate()
    .withNewMetadata().withLabels(Collections.singletonMap("service", "http-server")).endMetadata()
    .withNewSpec()
    .addToContainers(new ContainerBuilder()
      .withName("nginx")
      .withImage("nginx:1.10.2")
      .withImagePullPolicy("IfNotPresent")
      .withPorts(Collections.singletonList(new ContainerPortBuilder().withContainerPort(80).build()))
      .build())
    .endSpec()
    .endTemplate()
    .endSpec()
    .build();

  server.expect().withPath("/apis/apps/v1/namespaces/ns1/deployments/deployment1").andReturn(200, deployment).always();
  server.expect().withPath("/api/v1/namespaces/ns1/pods?labelSelector=service%3Dhttp-server").andReturn(200, new KubernetesListBuilder().build()).once();
  server.expect().post().withPath("/apis/apps/v1/namespaces/ns1/deployments").andReturn(201, deployment).times(2);
  KubernetesClient client = server.getClient();

  client.apps().deployments().inNamespace("ns1")
    .withName("deployment1")
    .rolling()
    .withTimeout(5, TimeUnit.MINUTES)
    .updateImage("");
}
 
Example #24
Source File: PortNameEnricher.java    From jkube with Eclipse Public License 2.0 4 votes vote down vote up
private String getProtocol(ContainerPortBuilder builder) {
    return Optional.ofNullable(builder.getProtocol())
                   .filter(StringUtils::isNotBlank)
                   .map(String::toLowerCase)
                   .orElse("tcp");
}