KubernetesServiceImporter implements ServiceImporter { static String discoveryType(JsonObject service, Record record) { JsonObject spec = service.getJsonObject("spec"); JsonArray ports = spec.getJsonArray("ports"); if (ports == null || ports.isEmpty()) { return ServiceType.UNKNOWN; } if (ports.size() > 1) { LOGGER.warn("More than one ports has been found for " + record.getName() + " - taking the " + "first one to build the record location"); } JsonObject port = ports.getJsonObject(0); int p = port.getInteger("port"); if (p == 80 || p == 443 || p >= 8080 && p <= 9000) { return HttpEndpoint.TYPE; } if (p == 5432 || p == 5433) { return JDBCDataSource.TYPE; } if (p == 3306 || p == 13306) { return JDBCDataSource.TYPE; } if (p == 6379) { return RedisDataSource.TYPE; } if (p == 27017 || p == 27018 || p == 27019) { return MongoDataSource.TYPE; } return ServiceType.UNKNOWN; }  @Override void start(Vertx vertx, ServicePublisher publisher, JsonObject configuration,
                    Promise<Void> completion); @Override void close(Handler<Void> completionHandler); static final String KUBERNETES_UUID; }
@Test public void testServiceTypeDetection() throws Exception { Map<String, String> labels = new LinkedHashMap<>(); ObjectMeta metadata = new ObjectMeta(); metadata.setName("my-service"); metadata.setUid("uuid"); metadata.setNamespace("my-project"); metadata.setLabels(labels); ServiceSpec spec = new ServiceSpec(); ServicePort port = new ServicePort(); port.setTargetPort(new IntOrString(8080)); port.setPort(8080); spec.setPorts(Collections.singletonList(port)); Service service = new Service(); service.setMetadata(metadata); service.setSpec(spec); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(HttpEndpoint.TYPE); port.setPort(443); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(HttpEndpoint.TYPE); port.setPort(433); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(ServiceType.UNKNOWN); port.setPort(8888); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(HttpEndpoint.TYPE); port.setPort(8080); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(HttpEndpoint.TYPE); port.setPort(9000); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(HttpEndpoint.TYPE); port.setPort(80); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(HttpEndpoint.TYPE); port.setPort(6379); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(RedisDataSource.TYPE); port.setPort(3306); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(JDBCDataSource.TYPE); port.setPort(27017); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(MongoDataSource.TYPE); port.setPort(27018); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(MongoDataSource.TYPE); port.setPort(27019); assertThat(KubernetesServiceImporter.discoveryType(new JsonObject(mapper.writeValueAsString(service)), new Record())) .isEqualTo(MongoDataSource.TYPE); }