use of io.fabric8.knative.serving.v1.ServiceList in project spring-cloud-kubernetes by spring-cloud.
the class KubernetesDiscoveryClientFilterTest method testNoExpression.
@Test
public void testNoExpression() {
List<String> springBootServiceNames = Arrays.asList("serviceA", "serviceB", "serviceC");
List<Service> services = createSpringBootServiceByName(springBootServiceNames);
ServiceList serviceList = new ServiceList();
serviceList.setItems(services);
when(this.serviceOperation.list()).thenReturn(serviceList);
when(this.kubernetesClient.services()).thenReturn(this.serviceOperation);
when(this.properties.getFilter()).thenReturn("");
List<String> filteredServices = this.underTest.getServices();
System.out.println("Filtered Services: " + filteredServices);
assertThat(filteredServices).isEqualTo(springBootServiceNames);
}
use of io.fabric8.knative.serving.v1.ServiceList in project spring-cloud-kubernetes by spring-cloud.
the class KubernetesDiscoveryClientFilterTest method testFilteredServices.
@Test
public void testFilteredServices() {
List<String> springBootServiceNames = Arrays.asList("serviceA", "serviceB");
List<Service> services = createSpringBootServiceByName(springBootServiceNames);
// Add non spring boot service
Service service = new Service();
ObjectMeta objectMeta = new ObjectMeta();
objectMeta.setName("ServiceNonSpringBoot");
service.setMetadata(objectMeta);
services.add(service);
ServiceList serviceList = new ServiceList();
serviceList.setItems(services);
when(this.serviceOperation.list()).thenReturn(serviceList);
when(this.kubernetesClient.services()).thenReturn(this.serviceOperation);
when(this.properties.getFilter()).thenReturn("metadata.additionalProperties['spring-boot']");
List<String> filteredServices = this.underTest.getServices();
System.out.println("Filtered Services: " + filteredServices);
assertThat(filteredServices).isEqualTo(springBootServiceNames);
}
use of io.fabric8.knative.serving.v1.ServiceList in project spring-cloud-kubernetes by spring-cloud.
the class KubernetesDiscoveryClientFilterTest method testFilteredServicesByPrefix.
@Test
public void testFilteredServicesByPrefix() {
List<String> springBootServiceNames = Arrays.asList("serviceA", "serviceB", "serviceC");
List<Service> services = createSpringBootServiceByName(springBootServiceNames);
// Add non spring boot service
Service service = new Service();
ObjectMeta objectMeta = new ObjectMeta();
objectMeta.setName("anotherService");
service.setMetadata(objectMeta);
services.add(service);
ServiceList serviceList = new ServiceList();
serviceList.setItems(services);
when(this.serviceOperation.list()).thenReturn(serviceList);
when(this.kubernetesClient.services()).thenReturn(this.serviceOperation);
when(this.properties.getFilter()).thenReturn("metadata.name.startsWith('service')");
List<String> filteredServices = this.underTest.getServices();
System.out.println("Filtered Services: " + filteredServices);
assertThat(filteredServices).isEqualTo(springBootServiceNames);
}
use of io.fabric8.knative.serving.v1.ServiceList in project spring-cloud-kubernetes by spring-cloud.
the class KubernetesReactiveDiscoveryClientTests method shouldReturnFlux.
@Test
public void shouldReturnFlux(@KubernetesExtension.Client KubernetesClient kubernetesClient, @KubernetesExtension.Server KubernetesServer kubernetesServer) {
ServiceList services = new ServiceListBuilder().addNewItem().withNewMetadata().withName("existing-service").withNamespace("test").withLabels(new HashMap<String, String>() {
{
put("label", "value");
}
}).endMetadata().endItem().build();
Endpoints endPoint = new EndpointsBuilder().withNewMetadata().withName("existing-service").withNamespace("test").withLabels(new HashMap<String, String>() {
{
put("label", "value");
}
}).endMetadata().addNewSubset().addNewAddress().withIp("ip1").withNewTargetRef().withUid("uid1").endTargetRef().endAddress().addNewPort("http", "http_tcp", 80, "TCP").endSubset().build();
List<Endpoints> endpointsList = new ArrayList<>();
endpointsList.add(endPoint);
EndpointsList endpoints = new EndpointsList();
endpoints.setItems(endpointsList);
kubernetesServer.expect().get().withPath("/api/v1/namespaces/test/endpoints?fieldSelector=metadata.name%3Dexisting-service").andReturn(200, endpoints).once();
kubernetesServer.expect().get().withPath("/api/v1/namespaces/test/services/existing-service").andReturn(200, services.getItems().get(0)).once();
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
properties.getMetadata().setAddAnnotations(false);
properties.getMetadata().setAddLabels(false);
ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(kubernetesClient, properties, KubernetesClient::services);
Flux<ServiceInstance> instances = client.getInstances("existing-service");
StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
}
use of io.fabric8.knative.serving.v1.ServiceList in project kubernetes-client by fabric8io.
the class ServiceCrudTest method testCrud.
@Test
public void testCrud() {
Service service1 = new ServiceBuilder().withNewMetadata().withName("svc1").and().withNewSpec().and().build();
Service service2 = new ServiceBuilder().withNewMetadata().withName("svc2").addToLabels("foo", "bar").and().withNewSpec().and().build();
Service service3 = new ServiceBuilder().withNewMetadata().withName("svc3").addToLabels("foo", "bar").and().withNewSpec().and().build();
// try to patch/replace before the service exists
ServiceResource<Service> serviceOp = client.services().inNamespace("ns2").withName("svc2");
KubernetesClientException result = assertThrows(KubernetesClientException.class, () -> serviceOp.patch(service1));
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.getCode());
result = assertThrows(KubernetesClientException.class, () -> serviceOp.replace(service1));
assertEquals(HttpURLConnection.HTTP_NOT_FOUND, result.getCode());
client.services().inNamespace("ns1").create(service1);
client.services().inNamespace("ns2").create(service2);
client.services().inNamespace("ns1").create(service3);
ServiceList aServiceList = client.services().list();
assertNotNull(aServiceList);
assertEquals(0, aServiceList.getItems().size());
aServiceList = client.services().inAnyNamespace().list();
assertNotNull(aServiceList);
assertEquals(3, aServiceList.getItems().size());
aServiceList = client.services().inNamespace("ns1").list();
assertNotNull(aServiceList);
assertEquals(2, aServiceList.getItems().size());
client.services().inNamespace("ns1").withName("svc1").delete();
aServiceList = client.services().inNamespace("ns1").list();
assertNotNull(aServiceList);
assertEquals(1, aServiceList.getItems().size());
aServiceList = client.services().inAnyNamespace().withLabels(Collections.singletonMap("foo", "bar")).list();
assertNotNull(aServiceList);
assertEquals(2, aServiceList.getItems().size());
service2 = client.services().inNamespace("ns2").withName("svc2").edit(s -> new ServiceBuilder(s).editMetadata().addToLabels("key1", "value1").endMetadata().build());
assertNotNull(service2);
assertEquals("value1", service2.getMetadata().getLabels().get("key1"));
}
Aggregations