Search in sources :

Example 1 with ServiceList

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);
}
Also used : ServiceList(io.fabric8.kubernetes.api.model.ServiceList) Service(io.fabric8.kubernetes.api.model.Service) Test(org.junit.Test)

Example 2 with ServiceList

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);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) Service(io.fabric8.kubernetes.api.model.Service) Test(org.junit.Test)

Example 3 with ServiceList

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);
}
Also used : ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) Service(io.fabric8.kubernetes.api.model.Service) Test(org.junit.Test)

Example 4 with ServiceList

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();
}
Also used : EndpointsList(io.fabric8.kubernetes.api.model.EndpointsList) ServiceListBuilder(io.fabric8.kubernetes.api.model.ServiceListBuilder) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) HashMap(java.util.HashMap) ServiceList(io.fabric8.kubernetes.api.model.ServiceList) ArrayList(java.util.ArrayList) ServiceInstance(org.springframework.cloud.client.ServiceInstance) EndpointsBuilder(io.fabric8.kubernetes.api.model.EndpointsBuilder) Endpoints(io.fabric8.kubernetes.api.model.Endpoints) KubernetesDiscoveryProperties(org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties) ReactiveDiscoveryClient(org.springframework.cloud.client.discovery.ReactiveDiscoveryClient) Test(org.junit.jupiter.api.Test)

Example 5 with ServiceList

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"));
}
Also used : KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.junit.jupiter.api.Test) HttpURLConnection(java.net.HttpURLConnection) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) ServiceResource(io.fabric8.kubernetes.client.dsl.ServiceResource) List(java.util.List) EnableKubernetesMockClient(io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) io.fabric8.kubernetes.api.model(io.fabric8.kubernetes.api.model) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collections(java.util.Collections) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) Test(org.junit.jupiter.api.Test)

Aggregations

Service (io.fabric8.kubernetes.api.model.Service)18 ServiceList (io.fabric8.kubernetes.api.model.ServiceList)15 ArrayList (java.util.ArrayList)8 Test (org.junit.jupiter.api.Test)7 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)5 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)5 MalformedURLException (java.net.MalformedURLException)4 Test (org.junit.Test)4 ServiceDiscoveryException (org.wso2.carbon.apimgt.core.exception.ServiceDiscoveryException)4 ServiceList (io.fabric8.knative.serving.v1.ServiceList)3 Pod (io.fabric8.kubernetes.api.model.Pod)3 PodList (io.fabric8.kubernetes.api.model.PodList)3 ServicePort (io.fabric8.kubernetes.api.model.ServicePort)3 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)3 HashMap (java.util.HashMap)3 Service (io.fabric8.knative.serving.v1.Service)2 ServiceBuilder (io.fabric8.knative.serving.v1.ServiceBuilder)2 DoneableService (io.fabric8.kubernetes.api.model.DoneableService)2 Endpoints (io.fabric8.kubernetes.api.model.Endpoints)2 EndpointsBuilder (io.fabric8.kubernetes.api.model.EndpointsBuilder)2