Java Code Examples for io.fabric8.kubernetes.api.model.ServiceSpec#setPorts()

The following examples show how to use io.fabric8.kubernetes.api.model.ServiceSpec#setPorts() . 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: CustomServiceController.java    From java-operator-sdk with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<CustomService> createOrUpdateResource(CustomService resource) {
    log.info("Execution createOrUpdateResource for: {}", resource.getMetadata().getName());

    ServicePort servicePort = new ServicePort();
    servicePort.setPort(8080);
    ServiceSpec serviceSpec = new ServiceSpec();
    serviceSpec.setPorts(Arrays.asList(servicePort));

    kubernetesClient.services().inNamespace(resource.getMetadata().getNamespace()).createOrReplaceWithNew()
            .withNewMetadata()
            .withName(resource.getSpec().getName())
            .addToLabels("testLabel", resource.getSpec().getLabel())
            .endMetadata()
            .withSpec(serviceSpec)
            .done();
    return Optional.of(resource);
}
 
Example 2
Source File: MockKubernetesServerSupport.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
protected void createMockService(final String serviceName, final String ip, final Map<String, String> labels, final String namespace) {
    final ServiceSpec serviceSpec = new ServiceSpec();
    serviceSpec.setPorts(Collections.singletonList(new ServicePort("http", 0, 8080, "http", new IntOrString(8080))));
    serviceSpec.setClusterIP(ip);
    serviceSpec.setType("ClusterIP");
    serviceSpec.setSessionAffinity("ClientIP");

    final ObjectMeta metadata = new ObjectMeta();
    metadata.setName(serviceName);
    metadata.setNamespace(MOCK_NAMESPACE);
    metadata.setLabels(labels);

    final Service service = new Service("v1", "Service", metadata, serviceSpec, new ServiceStatus(new LoadBalancerStatus()));
    if (namespace != null) {
        this.server.getClient().inNamespace(namespace).services().create(service);
    } else {
        this.server.getClient().services().create(service);
    }

}
 
Example 3
Source File: KubernetesDiscoveredServiceWorkItemHandlerTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testGivenServiceExists() {
    final ServiceSpec serviceSpec = new ServiceSpec();
    serviceSpec.setPorts(Collections.singletonList(new ServicePort("http", 0, 8080, "http", new IntOrString(8080))));
    serviceSpec.setClusterIP("172.30.158.31");
    serviceSpec.setType("ClusterIP");
    serviceSpec.setSessionAffinity("ClientIP");

    final ObjectMeta metadata = new ObjectMeta();
    metadata.setName("test-kieserver");
    metadata.setNamespace(MOCK_NAMESPACE);
    metadata.setLabels(Collections.singletonMap("test-kieserver", "service"));

    final Service service = new Service("v1", "Service", metadata, serviceSpec, new ServiceStatus(new LoadBalancerStatus()));
    getClient().services().create(service);

    final DiscoveredServiceWorkItemHandler handler = new TestDiscoveredServiceWorkItemHandler(this);
    final ServiceInfo serviceInfo = handler.findEndpoint(MOCK_NAMESPACE, "test-kieserver");
    assertThat(serviceInfo, notNullValue());
    assertThat(serviceInfo.getUrl(), is("http://172.30.158.31:8080/test-kieserver"));
}
 
Example 4
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 5
Source File: OpenShiftPreviewUrlCommandProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldDoNothingWhenCantFindRouteForPreviewUrl() throws InfrastructureException {
  int port = 8080;
  List<CommandImpl> commands =
      Collections.singletonList(
          new CommandImpl("a", "a", "a", new PreviewUrlImpl(port, null), Collections.emptyMap()));
  OpenShiftEnvironment env =
      OpenShiftEnvironment.builder().setCommands(new ArrayList<>(commands)).build();

  Mockito.when(mockProject.services()).thenReturn(mockServices);
  Service service = new Service();
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(new ServicePort("a", null, port, "TCP", new IntOrString(port))));
  service.setSpec(spec);
  Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));

  Mockito.when(mockProject.routes()).thenReturn(mockRoutes);
  Mockito.when(mockRoutes.get()).thenReturn(Collections.emptyList());

  previewUrlCommandProvisioner.provision(env, mockProject);

  assertTrue(commands.containsAll(env.getCommands()));
  assertTrue(env.getCommands().containsAll(commands));
  assertEquals(
      env.getWarnings().get(0).getCode(), Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL);
}
 
Example 6
Source File: OpenShiftPreviewUrlCommandProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldUpdateCommandWhenServiceAndIngressFound() throws InfrastructureException {
  int port = 8080;
  List<CommandImpl> commands =
      Collections.singletonList(
          new CommandImpl("a", "a", "a", new PreviewUrlImpl(port, null), Collections.emptyMap()));
  OpenShiftEnvironment env =
      OpenShiftEnvironment.builder().setCommands(new ArrayList<>(commands)).build();

  Mockito.when(mockProject.services()).thenReturn(mockServices);
  Service service = new Service();
  ObjectMeta metadata = new ObjectMeta();
  metadata.setName("servicename");
  service.setMetadata(metadata);
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(
          new ServicePort("8080", null, port, "TCP", new IntOrString(port))));
  service.setSpec(spec);
  Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));

  Route route = new Route();
  RouteSpec routeSpec = new RouteSpec();
  routeSpec.setPort(new RoutePort(new IntOrString("8080")));
  routeSpec.setTo(new RouteTargetReference("a", "servicename", 1));
  routeSpec.setHost("testhost");
  route.setSpec(routeSpec);

  Mockito.when(mockProject.routes()).thenReturn(mockRoutes);
  Mockito.when(mockRoutes.get()).thenReturn(Collections.singletonList(route));

  previewUrlCommandProvisioner.provision(env, mockProject);

  assertTrue(env.getCommands().get(0).getAttributes().containsKey("previewUrl"));
  assertEquals(env.getCommands().get(0).getAttributes().get("previewUrl"), "testhost");
  assertTrue(env.getWarnings().isEmpty());
}
 
Example 7
Source File: OpenShiftPreviewUrlExposerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldNotProvisionWhenServiceAndRouteFound() throws InternalInfrastructureException {
  final int PORT = 8080;
  final String SERVER_PORT_NAME = "server-" + PORT;

  CommandImpl command =
      new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());

  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName("servicename");
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(serviceSpec);

  Route route = new Route();
  RouteSpec routeSpec = new RouteSpec();
  routeSpec.setPort(new RoutePort(new IntOrString(SERVER_PORT_NAME)));
  routeSpec.setTo(new RouteTargetReference("routekind", "servicename", 1));
  route.setSpec(routeSpec);

  Map<String, Service> services = new HashMap<>();
  services.put("servicename", service);
  Map<String, Route> routes = new HashMap<>();
  routes.put("routename", route);

  OpenShiftEnvironment env =
      OpenShiftEnvironment.builder()
          .setCommands(singletonList(new CommandImpl(command)))
          .setServices(services)
          .setRoutes(routes)
          .build();

  assertEquals(env.getRoutes().size(), 1);
  previewUrlEndpointsProvisioner.expose(env);
  assertEquals(env.getRoutes().size(), 1);
}
 
Example 8
Source File: OpenShiftPreviewUrlExposerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldProvisionRouteWhenNotFound() throws InternalInfrastructureException {
  final int PORT = 8080;
  final String SERVER_PORT_NAME = "server-" + PORT;
  final String SERVICE_NAME = "servicename";

  CommandImpl command =
      new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());

  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName(SERVICE_NAME);
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(serviceSpec);

  Map<String, Service> services = new HashMap<>();
  services.put(SERVICE_NAME, service);

  OpenShiftEnvironment env =
      OpenShiftEnvironment.builder()
          .setCommands(singletonList(new CommandImpl(command)))
          .setServices(services)
          .setRoutes(new HashMap<>())
          .build();

  previewUrlEndpointsProvisioner.expose(env);
  assertEquals(env.getRoutes().size(), 1);
  Route provisionedRoute = env.getRoutes().values().iterator().next();
  assertEquals(provisionedRoute.getSpec().getTo().getName(), SERVICE_NAME);
  assertEquals(
      provisionedRoute.getSpec().getPort().getTargetPort().getStrVal(), SERVER_PORT_NAME);
}
 
Example 9
Source File: RoutesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Service createService(String portString, int portInt) {
  Service service = new Service();
  ObjectMeta metadata = new ObjectMeta();
  metadata.setName("servicename");
  service.setMetadata(metadata);
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(new ServicePort(portString, null, portInt, "TCP", null)));
  service.setSpec(spec);
  return service;
}
 
Example 10
Source File: KubernetesPreviewUrlCommandProvisionerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldDoNothingWhenCantFindIngressForPreviewUrl() throws InfrastructureException {
  int port = 8080;
  List<CommandImpl> commands =
      Collections.singletonList(
          new CommandImpl("a", "a", "a", new PreviewUrlImpl(port, null), Collections.emptyMap()));
  KubernetesEnvironment env =
      KubernetesEnvironment.builder().setCommands(new ArrayList<>(commands)).build();

  Mockito.when(mockNamespace.services()).thenReturn(mockServices);
  Service service = new Service();
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(new ServicePort("a", null, port, "TCP", new IntOrString(port))));
  service.setSpec(spec);
  Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));

  Mockito.when(mockNamespace.ingresses()).thenReturn(mockIngresses);
  Mockito.when(mockIngresses.get()).thenReturn(Collections.emptyList());

  previewUrlCommandProvisioner.provision(env, mockNamespace);

  assertTrue(commands.containsAll(env.getCommands()));
  assertTrue(env.getCommands().containsAll(commands));
  assertEquals(
      env.getWarnings().get(0).getCode(), Warnings.NOT_ABLE_TO_PROVISION_OBJECTS_FOR_PREVIEW_URL);
}
 
Example 11
Source File: PreviewUrlExposerTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void shouldProvisionIngressWhenNotFound() throws InternalInfrastructureException {
  Mockito.when(
          externalServiceExposureStrategy.getExternalPath(Mockito.anyString(), Mockito.any()))
      .thenReturn("some-server-path");

  final int PORT = 8080;
  final String SERVER_PORT_NAME = "server-" + PORT;
  final String SERVICE_NAME = "servicename";

  CommandImpl command =
      new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());

  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName(SERVICE_NAME);
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(serviceSpec);

  Map<String, Service> services = new HashMap<>();
  services.put(SERVICE_NAME, service);

  KubernetesEnvironment env =
      KubernetesEnvironment.builder()
          .setCommands(singletonList(new CommandImpl(command)))
          .setServices(services)
          .setIngresses(new HashMap<>())
          .build();

  previewUrlExposer.expose(env);
  assertEquals(env.getIngresses().size(), 1);
  Ingress provisionedIngress = env.getIngresses().values().iterator().next();
  IngressBackend provisionedIngressBackend =
      provisionedIngress.getSpec().getRules().get(0).getHttp().getPaths().get(0).getBackend();
  assertEquals(provisionedIngressBackend.getServicePort().getStrVal(), SERVER_PORT_NAME);
  assertEquals(provisionedIngressBackend.getServiceName(), SERVICE_NAME);
}
 
Example 12
Source File: IngressesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
private Service createService(String serverPortName, int port) {
  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName("servicename");
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(serverPortName, null, port, "TCP", new IntOrString(port))));
  service.setSpec(serviceSpec);
  return service;
}
 
Example 13
Source File: ServicesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindPortWhenExists() {
  final int PORT = 1234;

  Service service = new Service();
  ServiceSpec spec = new ServiceSpec();
  ServicePort port = new ServicePort();
  port.setPort(PORT);
  spec.setPorts(Arrays.asList(port, new ServicePort()));
  service.setSpec(spec);

  assertEquals(Services.findPort(service, PORT).get(), port);
}
 
Example 14
Source File: ServicesTest.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testFindServiceWhenExists() {
  final int PORT = 1234;

  Service service = new Service();
  ServiceSpec spec = new ServiceSpec();
  ServicePort port = new ServicePort();
  port.setPort(PORT);
  spec.setPorts(Collections.singletonList(port));
  service.setSpec(spec);

  assertEquals(
      Services.findServiceWithPort(Arrays.asList(service, new Service()), PORT).get(), service);
}
 
Example 15
Source File: KubernetesPreviewUrlCommandProvisionerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldUpdateCommandWhenServiceAndIngressFound() throws InfrastructureException {
  final int PORT = 8080;
  final String SERVICE_PORT_NAME = "service-" + PORT;
  List<CommandImpl> commands =
      Collections.singletonList(
          new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap()));
  KubernetesEnvironment env =
      KubernetesEnvironment.builder().setCommands(new ArrayList<>(commands)).build();

  Mockito.when(mockNamespace.services()).thenReturn(mockServices);
  Service service = new Service();
  ObjectMeta metadata = new ObjectMeta();
  metadata.setName("servicename");
  service.setMetadata(metadata);
  ServiceSpec spec = new ServiceSpec();
  spec.setPorts(
      Collections.singletonList(
          new ServicePort(SERVICE_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(spec);
  Mockito.when(mockServices.get()).thenReturn(Collections.singletonList(service));

  Ingress ingress = new Ingress();
  IngressSpec ingressSpec = new IngressSpec();
  IngressRule rule =
      new IngressRule(
          "testhost",
          new HTTPIngressRuleValue(
              Collections.singletonList(
                  new HTTPIngressPath(
                      new IngressBackend("servicename", new IntOrString(SERVICE_PORT_NAME)),
                      null))));
  ingressSpec.setRules(Collections.singletonList(rule));
  ingress.setSpec(ingressSpec);
  Mockito.when(mockNamespace.ingresses()).thenReturn(mockIngresses);
  Mockito.when(mockIngresses.get()).thenReturn(Collections.singletonList(ingress));

  previewUrlCommandProvisioner.provision(env, mockNamespace);

  assertTrue(env.getCommands().get(0).getAttributes().containsKey("previewUrl"));
  assertEquals(env.getCommands().get(0).getAttributes().get("previewUrl"), "testhost");
  assertTrue(env.getWarnings().isEmpty());
}
 
Example 16
Source File: PreviewUrlExposerTest.java    From che with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void shouldNotProvisionWhenServiceAndIngressFound()
    throws InternalInfrastructureException {
  final int PORT = 8080;
  final String SERVER_PORT_NAME = "server-" + PORT;

  CommandImpl command =
      new CommandImpl("a", "a", "a", new PreviewUrlImpl(PORT, null), Collections.emptyMap());

  Service service = new Service();
  ObjectMeta serviceMeta = new ObjectMeta();
  serviceMeta.setName("servicename");
  service.setMetadata(serviceMeta);
  ServiceSpec serviceSpec = new ServiceSpec();
  serviceSpec.setPorts(
      singletonList(new ServicePort(SERVER_PORT_NAME, null, PORT, "TCP", new IntOrString(PORT))));
  service.setSpec(serviceSpec);

  Ingress ingress = new Ingress();
  ObjectMeta ingressMeta = new ObjectMeta();
  ingressMeta.setName("ingressname");
  ingress.setMetadata(ingressMeta);
  IngressSpec ingressSpec = new IngressSpec();
  IngressRule ingressRule = new IngressRule();
  ingressRule.setHost("ingresshost");
  IngressBackend ingressBackend =
      new IngressBackend("servicename", new IntOrString(SERVER_PORT_NAME));
  ingressRule.setHttp(
      new HTTPIngressRuleValue(singletonList(new HTTPIngressPath(ingressBackend, null))));
  ingressSpec.setRules(singletonList(ingressRule));
  ingress.setSpec(ingressSpec);

  Map<String, Service> services = new HashMap<>();
  services.put("servicename", service);
  Map<String, Ingress> ingresses = new HashMap<>();
  ingresses.put("ingressname", ingress);

  KubernetesEnvironment env =
      KubernetesEnvironment.builder()
          .setCommands(singletonList(new CommandImpl(command)))
          .setServices(services)
          .setIngresses(ingresses)
          .build();

  assertEquals(env.getIngresses().size(), 1);
  previewUrlExposer.expose(env);
  assertEquals(env.getIngresses().size(), 1);
}