Java Code Examples for io.fabric8.kubernetes.api.model.ServicePort#setTargetPort()

The following examples show how to use io.fabric8.kubernetes.api.model.ServicePort#setTargetPort() . 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: TillerInstaller.java    From microbean-helm with Apache License 2.0 5 votes vote down vote up
protected ServiceSpec createServiceSpec(final Map<String, String> labels) {
  final ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setType("ClusterIP");

  final ServicePort servicePort = new ServicePort();
  servicePort.setName(DEFAULT_NAME);
  servicePort.setPort(Integer.valueOf(44134));
  servicePort.setTargetPort(new IntOrString(DEFAULT_NAME));
  serviceSpec.setPorts(Arrays.asList(servicePort));

  serviceSpec.setSelector(normalizeLabels(labels));
  return serviceSpec;
}
 
Example 2
Source File: JwtProxyProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test(
    expectedExceptions = InfrastructureException.class,
    expectedExceptionsMessageRegExp =
        "Secure servers which expose the same port should have "
            + "the same `cookiesAuthEnabled` value\\.")
public void shouldThrowAnExceptionIfServersHaveDifferentValueForCookiesAuthEnabled()
    throws Exception {
  // given
  ServerConfigImpl server1 =
      new ServerConfigImpl(
          "4401/tcp",
          "ws",
          "/",
          ImmutableMap.of(SECURE_SERVER_COOKIES_AUTH_ENABLED_ATTRIBUTE, "true"));
  ServerConfigImpl server2 =
      new ServerConfigImpl(
          "4401/tcp",
          "http",
          "/",
          ImmutableMap.of(SECURE_SERVER_COOKIES_AUTH_ENABLED_ATTRIBUTE, "false"));
  ServerConfigImpl server3 = new ServerConfigImpl("4401/tcp", "ws", "/", emptyMap());

  ServicePort port = new ServicePort();
  port.setTargetPort(new IntOrString(4401));

  // when
  jwtProxyProvisioner.expose(
      k8sEnv,
      podWithName(),
      "machine",
      "terminal",
      port,
      "TCP",
      ImmutableMap.of("server1", server1, "server2", server2, "server3", server3));
}
 
Example 3
Source File: JwtProxyProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldFalseValueAsDefaultForCookiesAuthEnabledAttribute() throws Exception {
  // given
  JwtProxyConfigBuilder configBuilder = mock(JwtProxyConfigBuilder.class);
  when(configBuilderFactory.create(any())).thenReturn(configBuilder);

  jwtProxyProvisioner =
      new JwtProxyProvisioner(
          signatureKeyManager,
          configBuilderFactory,
          externalServiceExposureStrategy,
          cookiePathStrategy,
          "eclipse/che-jwtproxy",
          "128mb",
          "0.5",
          "Always",
          runtimeId);

  ServerConfigImpl server1 = new ServerConfigImpl("4401/tcp", "http", "/", emptyMap());

  ServicePort port = new ServicePort();
  port.setTargetPort(new IntOrString(4401));

  // when
  jwtProxyProvisioner.expose(
      k8sEnv,
      podWithName(),
      "machine",
      "terminal",
      port,
      "TCP",
      ImmutableMap.of("server1", server1));

  // then
  verify(configBuilder)
      .addVerifierProxy(
          eq(4400), eq("http://terminal:4401"), eq(emptySet()), eq(false), eq("/"), isNull());
}
 
Example 4
Source File: JwtProxyProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldBindToLocalhostWhenNoServiceForServerExists() throws Exception {
  // given
  JwtProxyConfigBuilder configBuilder = mock(JwtProxyConfigBuilder.class);
  when(configBuilderFactory.create(any())).thenReturn(configBuilder);

  jwtProxyProvisioner =
      new JwtProxyProvisioner(
          signatureKeyManager,
          configBuilderFactory,
          externalServiceExposureStrategy,
          cookiePathStrategy,
          "eclipse/che-jwtproxy",
          "128mb",
          "0.5",
          "Always",
          runtimeId);

  ServerConfigImpl server1 = new ServerConfigImpl("4401/tcp", "http", "/", emptyMap());

  ServicePort port = new ServicePort();
  port.setTargetPort(new IntOrString(4401));

  // when
  jwtProxyProvisioner.expose(
      k8sEnv, podWithName(), "machine", null, port, "TCP", ImmutableMap.of("server1", server1));

  // then
  verify(configBuilder)
      .addVerifierProxy(
          eq(4400), eq("http://127.0.0.1:4401"), eq(emptySet()), eq(false), eq("/"), isNull());
}
 
Example 5
Source File: PassThroughProxyProvisionerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldConfigureProxyWithExcludes() throws Exception {
  // given
  KubernetesEnvironment k8sEnv = KubernetesEnvironment.builder().build();
  JwtProxyConfigBuilderFactory configBuilderFactory = mock(JwtProxyConfigBuilderFactory.class);
  JwtProxyConfigBuilder configBuilder = mock(JwtProxyConfigBuilder.class);
  when(configBuilderFactory.create(any())).thenReturn(configBuilder);

  PassThroughProxyProvisioner passThroughProxyProvisioner =
      new PassThroughProxyProvisioner(
          configBuilderFactory,
          mock(ExternalServiceExposureStrategy.class),
          new CookiePathStrategy(MULTI_HOST_STRATEGY),
          "eclipse/che-jwtproxy",
          "128mb",
          "0.5",
          "Always",
          runtimeId);

  Map<String, String> attrs = new HashMap<>();
  ServerConfig.setCookiesAuthEnabled(attrs, true);
  ServerConfig.setSecure(attrs, true);
  ServerConfigImpl server1 = new ServerConfigImpl("4401/tcp", "http", "/", attrs);

  ServicePort port = new ServicePort();
  port.setTargetPort(new IntOrString(8080));

  // when
  passThroughProxyProvisioner.expose(
      k8sEnv,
      podWithName(),
      "machine",
      "terminal",
      port,
      "TCP",
      ImmutableMap.of("server1", server1));

  // then
  verify(configBuilder)
      .addVerifierProxy(
          eq(4400), eq("http://terminal:8080"), eq(singleton("/")), eq(false), eq("/"), isNull());
}
 
Example 6
Source File: JwtProxyProvisionerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldProvisionJwtProxyRelatedObjectsIntoKubernetesEnvironment() throws Exception {
  // given
  ServerConfigImpl secureServer = new ServerConfigImpl("4401/tcp", "ws", "/", emptyMap());

  ServicePort port = new ServicePort();
  port.setTargetPort(new IntOrString(4401));

  // when
  jwtProxyProvisioner.expose(
      k8sEnv,
      podWithName(),
      "machine",
      "terminal",
      port,
      "TCP",
      ImmutableMap.of("server", secureServer));

  // then
  InternalMachineConfig jwtProxyMachine =
      k8sEnv.getMachines().get(JwtProxyProvisioner.JWT_PROXY_MACHINE_NAME);
  assertNotNull(jwtProxyMachine);

  ConfigMap configMap = k8sEnv.getConfigMaps().get(jwtProxyProvisioner.getConfigMapName());
  assertNotNull(configMap);
  assertEquals(
      configMap.getData().get(JWT_PROXY_PUBLIC_KEY_FILE),
      PUBLIC_KEY_HEADER
          + Base64.getEncoder().encodeToString("publickey".getBytes())
          + PUBLIC_KEY_FOOTER);
  assertNotNull(configMap.getData().get(JWT_PROXY_CONFIG_FILE));

  Pod jwtProxyPod =
      k8sEnv.getInjectablePodsCopy().getOrDefault("machine", emptyMap()).get("che-jwtproxy");
  assertNotNull(jwtProxyPod);

  assertEquals(1, jwtProxyPod.getSpec().getContainers().size());
  Container jwtProxyContainer = jwtProxyPod.getSpec().getContainers().get(0);

  assertEquals(jwtProxyContainer.getArgs().size(), 2);
  assertEquals(jwtProxyContainer.getArgs().get(0), "-config");
  assertEquals(
      jwtProxyContainer.getArgs().get(1), JWT_PROXY_CONFIG_FOLDER + "/" + JWT_PROXY_CONFIG_FILE);

  assertEquals(jwtProxyContainer.getEnv().size(), 1);
  EnvVar xdgHome = jwtProxyContainer.getEnv().get(0);
  assertEquals(xdgHome.getName(), "XDG_CONFIG_HOME");
  assertEquals(xdgHome.getValue(), JWT_PROXY_CONFIG_FOLDER);

  Service jwtProxyService = k8sEnv.getServices().get(jwtProxyProvisioner.getServiceName());
  assertNotNull(jwtProxyService);
}
 
Example 7
Source File: JwtProxyProvisionerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldUseCookiesAuthEnabledFromServersConfigs() throws Exception {
  // given
  JwtProxyConfigBuilder configBuilder = mock(JwtProxyConfigBuilder.class);
  when(configBuilderFactory.create(any())).thenReturn(configBuilder);

  jwtProxyProvisioner =
      new JwtProxyProvisioner(
          signatureKeyManager,
          configBuilderFactory,
          externalServiceExposureStrategy,
          cookiePathStrategy,
          "eclipse/che-jwtproxy",
          "128mb",
          "500m",
          "Always",
          runtimeId);

  ServerConfigImpl server1 =
      new ServerConfigImpl(
          "4401/tcp",
          "http",
          "/",
          ImmutableMap.of(SECURE_SERVER_COOKIES_AUTH_ENABLED_ATTRIBUTE, "true"));
  ServerConfigImpl server2 =
      new ServerConfigImpl(
          "4401/tcp",
          "ws",
          "/",
          ImmutableMap.of(SECURE_SERVER_COOKIES_AUTH_ENABLED_ATTRIBUTE, "true"));

  ServicePort port = new ServicePort();
  port.setTargetPort(new IntOrString(4401));

  // when
  jwtProxyProvisioner.expose(
      k8sEnv,
      podWithName(),
      "machine",
      "terminal",
      port,
      "TCP",
      ImmutableMap.of("server1", server1));

  // then
  verify(configBuilder).addVerifierProxy(any(), any(), any(), eq(true), any(), any());
}
 
Example 8
Source File: JwtProxySecureServerExposerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldExposeSecureServersWithNewJwtProxyServicePort() throws Exception {
  // given
  ServicePort machineServicePort = new ServicePort();
  machineServicePort.setTargetPort(new IntOrString(8080));
  machineServicePort.setProtocol("TCP");
  Map<String, ServerConfig> servers =
      ImmutableMap.of(
          "server1",
          new ServerConfigImpl("8080/tcp", "http", "/api", ImmutableMap.of("secure", "true")),
          "server2",
          new ServerConfigImpl("8080/tcp", "ws", "/connect", ImmutableMap.of("secure", "true")));

  ServicePort jwtProxyServicePort = new ServicePort();
  doReturn(jwtProxyServicePort)
      .when(jwtProxyProvisioner)
      .expose(any(), any(), anyString(), anyString(), any(), anyString(), any());

  when(jwtProxyProvisioner.getServiceName()).thenReturn(JWT_PROXY_SERVICE_NAME);

  // when
  secureServerExposer.expose(
      k8sEnv, null, MACHINE_NAME, MACHINE_SERVICE_NAME, null, machineServicePort, servers);

  // then
  verify(jwtProxyProvisioner)
      .expose(
          eq(k8sEnv),
          any(),
          anyString(),
          eq(MACHINE_SERVICE_NAME),
          eq(machineServicePort),
          eq("TCP"),
          any());
  verify(externalServerExposer)
      .expose(
          eq(k8sEnv),
          eq(MACHINE_NAME),
          eq(JWT_PROXY_SERVICE_NAME),
          isNull(),
          eq(jwtProxyServicePort),
          eq(servers));
}