KubernetesServiceImporter implements ServiceImporter { static Record createRecord(JsonObject service) { JsonObject metadata = service.getJsonObject("metadata"); Record record = new Record() .setName(metadata.getString("name")); JsonObject labels = metadata.getJsonObject("labels"); if (labels != null) { labels.forEach(entry -> record.getMetadata().put(entry.getKey(), entry.getValue().toString())); } record.getMetadata().put("kubernetes.namespace", metadata.getString("namespace")); record.getMetadata().put("kubernetes.name", metadata.getString("name")); record.getMetadata().put(KUBERNETES_UUID, metadata.getString("uid")); String type = record.getMetadata().getString("service-type"); if (type == null) { type = discoveryType(service, record); } switch (type) { case HttpEndpoint.TYPE: manageHttpService(record, service); break; default: manageUnknownService(record, service, type); break; } return record; }  @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 testRecordCreation() throws Exception { ObjectMeta metadata = new ObjectMeta(); metadata.setName("my-service"); metadata.setUid("uuid"); metadata.setNamespace("my-project"); ServiceSpec spec = new ServiceSpec(); ServicePort port = new ServicePort(); port.setTargetPort(new IntOrString(8080)); port.setPort(1524); spec.setPorts(Collections.singletonList(port)); Service service = new Service(); service.setMetadata(metadata); service.setSpec(spec); Record record = KubernetesServiceImporter.createRecord(new JsonObject(mapper.writeValueAsString(service))); assertThat(record).isNotNull(); assertThat(record.getName()).isEqualTo("my-service"); assertThat(record.getMetadata().getString("kubernetes.name")).isEqualTo("my-service"); assertThat(record.getMetadata().getString("kubernetes.namespace")).isEqualTo("my-project"); assertThat(record.getMetadata().getString("kubernetes.uuid")).isEqualTo("uuid"); assertThat(record.getType()).isEqualTo(ServiceType.UNKNOWN); assertThat(record.getLocation().getInteger("port")).isEqualTo(1524); }
@Test public void testHttpRecordCreation() throws Exception { Service service = getHttpService(); Record record = KubernetesServiceImporter.createRecord(new JsonObject(mapper.writeValueAsString(service))); assertThat(record).isNotNull(); assertThat(record.getName()).isEqualTo("my-service"); assertThat(record.getMetadata().getString("kubernetes.name")).isEqualTo("my-service"); assertThat(record.getMetadata().getString("kubernetes.namespace")).isEqualTo("my-project"); assertThat(record.getMetadata().getString("kubernetes.uuid")).isEqualTo("uuid"); assertThat(record.getType()).isEqualTo(HttpEndpoint.TYPE); assertThat(record.getLocation().getInteger("port")).isEqualTo(8080); assertThat(record.getLocation().getBoolean("ssl")).isFalse(); }
@Test public void testHttpExternalServiceRecordCreation() throws Exception { int servicePort = 8080; Service service = getExternalService(servicePort); Record record = KubernetesServiceImporter.createRecord(new JsonObject(mapper.writeValueAsString(service))); assertThat(record).isNotNull(); assertThat(record.getName()).isEqualTo("my-service"); assertThat(record.getMetadata().getString("kubernetes.name")).isEqualTo("my-service"); assertThat(record.getMetadata().getString("kubernetes.namespace")).isEqualTo("my-project"); assertThat(record.getMetadata().getString("kubernetes.uuid")).isEqualTo("uuid"); assertThat(record.getType()).isEqualTo(HttpEndpoint.TYPE); assertThat(record.getLocation().getString("host")).isEqualTo("my-external-service"); assertThat(record.getLocation().getInteger("port")).isEqualTo(servicePort); assertThat(record.getLocation().getBoolean("ssl")).isFalse(); assertThat(record.getLocation().getString("endpoint")).isEqualTo("http: }
@Test public void testUnknownExternalServiceRecordCreation() throws Exception { int servicePort = 5432; Service service = getExternalService(servicePort); Record record = KubernetesServiceImporter.createRecord(new JsonObject(mapper.writeValueAsString(service))); assertThat(record).isNotNull(); assertThat(record.getName()).isEqualTo("my-service"); assertThat(record.getMetadata().getString("kubernetes.name")).isEqualTo("my-service"); assertThat(record.getMetadata().getString("kubernetes.namespace")).isEqualTo("my-project"); assertThat(record.getMetadata().getString("kubernetes.uuid")).isEqualTo("uuid"); assertThat(record.getType()).isEqualTo(JDBCDataSource.TYPE); assertThat(record.getLocation().getString("host")).isEqualTo("my-external-service"); assertThat(record.getLocation().getInteger("port")).isEqualTo(servicePort); }