ServiceHandler { public List<Service> getServices(List<ServiceConfig> services) { ArrayList<Service> ret = new ArrayList<>(); for (ServiceConfig service : services) { ServiceBuilder serviceBuilder = new ServiceBuilder() .withNewMetadata() .withName(service.getName()) .withAnnotations(getAnnotations(service)) .withLabels(getLabels(service)) .endMetadata(); ServiceFluent.SpecNested<ServiceBuilder> serviceSpecBuilder = serviceBuilder.withNewSpec(); List<ServicePort> servicePorts = new ArrayList<>(); for (ServiceConfig.Port port : service.getPorts()) { ServicePort servicePort = new ServicePortBuilder() .withName(port.getName()) .withProtocol(port.getProtocol() != null ? port.getProtocol().name() : "TCP") .withTargetPort(new IntOrString(port.getTargetPort())) .withPort(port.getPort()) .withNodePort(port.getNodePort()) .build(); servicePorts.add(servicePort); } if (!servicePorts.isEmpty()) { serviceSpecBuilder.withPorts(servicePorts); } if (service.isHeadless()) { serviceSpecBuilder.withClusterIP("None"); } if (StringUtils.isNotBlank(service.getType())) { serviceSpecBuilder.withType(service.getType()); } serviceSpecBuilder.endSpec(); if (service.isHeadless() || !servicePorts.isEmpty()) { ret.add(serviceBuilder.build()); } } return ret; }  List<Service> getServices(List<ServiceConfig> services);  }
@Test public void getServicesTest() { port = new ServiceConfig.Port.Builder() .port(8080).protocol("tcp").targetPort(80).nodePort(50).name("port-test").build(); ports.add(port); serviceconfig = new ServiceConfig.Builder() .ports(ports).name("service-test").expose(true).headless(false).type("NodePort").build(); serviceConfigs.add(serviceconfig); services = serviceHandler.getServices(serviceConfigs); assertNotNull(services); assertEquals("TCP", services.get(0).getSpec().getPorts().get(0).getProtocol()); assertEquals("port-test", services.get(0).getSpec().getPorts().get(0).getName()); assertEquals(50, services.get(0).getSpec().getPorts().get(0).getNodePort().intValue()); assertEquals(80, services.get(0).getSpec().getPorts().get(0).getTargetPort().getIntVal().intValue()); assertEquals(8080, services.get(0).getSpec().getPorts().get(0).getPort().intValue()); assertEquals("NodePort", services.get(0).getSpec().getType()); assertTrue(services.get(0).getMetadata().getLabels().get("expose").equalsIgnoreCase("true")); assertTrue(services.get(0).getMetadata().getName().equalsIgnoreCase("service-test")); }
@Test public void getServicesWithoutPortTest() { serviceconfig = new ServiceConfig.Builder() .name("service-test").expose(true).type("NodePort").build(); serviceConfigs.clear(); serviceConfigs.add(serviceconfig); services = serviceHandler.getServices(serviceConfigs); assertTrue(services.isEmpty()); }
@Test public void getServicesWithTCPProtocolTest() { port = new ServiceConfig.Port.Builder() .port(8080).protocol("tcp").targetPort(80).nodePort(50).name("port-test").build(); ports.clear(); ports.add(port); serviceConfigs.clear(); serviceconfig = new ServiceConfig.Builder() .ports(ports).name("service-test").expose(true).headless(false).type("NodePort").build(); serviceConfigs.add(serviceconfig); services = serviceHandler.getServices(serviceConfigs); assertEquals("TCP",services.get(0).getSpec().getPorts().get(0).getProtocol()); }
@Test public void getServicesWithUDPProtocolTest() { port = new ServiceConfig.Port.Builder() .port(8080).protocol("udp").targetPort(80).nodePort(50).name("port-test").build(); ports.clear(); ports.add(port); serviceconfig = new ServiceConfig.Builder() .ports(ports).name("service-test").expose(true).headless(false).type("NodePort").build(); serviceConfigs.clear(); serviceConfigs.add(serviceconfig); services = serviceHandler.getServices(serviceConfigs); assertEquals("UDP",services.get(0).getSpec().getPorts().get(0).getProtocol()); }
@Test public void getServicesWithDefaultProtocolTest() { port = new ServiceConfig.Port.Builder() .port(8080).targetPort(80).nodePort(50).name("port-test").build(); ports.clear(); ports.add(port); serviceconfig = new ServiceConfig.Builder() .ports(ports).name("service-test").expose(true).headless(false).type("NodePort").build(); serviceConfigs.clear(); serviceConfigs.add(serviceconfig); services = serviceHandler.getServices(serviceConfigs); assertEquals("TCP",services.get(0).getSpec().getPorts().get(0).getProtocol()); }
@Test public void getServicesWithNullProtocolTest() { port = new ServiceConfig.Port.Builder() .port(8080).targetPort(80).nodePort(50).name("port-test").build(); ports.clear(); ports.add(port); serviceconfig = new ServiceConfig.Builder() .ports(ports).name("service-test").expose(true).headless(false).build(); serviceConfigs.clear(); serviceConfigs.add(serviceconfig); services = serviceHandler.getServices(serviceConfigs); assertNull(services.get(0).getSpec().getType()); }