io.fabric8.kubernetes.api.model.EndpointsBuilder Java Examples

The following examples show how to use io.fabric8.kubernetes.api.model.EndpointsBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KubernetesDiscoveryClientFilterMetadataTest.java    From spring-cloud-kubernetes with Apache License 2.0 6 votes vote down vote up
private void setupServiceWithLabelsAndAnnotationsAndPorts(String serviceId,
		String namespace, Map<String, String> labels, Map<String, String> annotations,
		Map<Integer, String> ports) {
	final Service service = new ServiceBuilder().withNewMetadata()
			.withNamespace(namespace).withLabels(labels).withAnnotations(annotations)
			.endMetadata().withNewSpec().withPorts(getServicePorts(ports)).endSpec()
			.build();
	when(this.serviceOperation.withName(serviceId)).thenReturn(this.serviceResource);
	when(this.serviceResource.get()).thenReturn(service);
	when(this.kubernetesClient.services()).thenReturn(this.serviceOperation);
	when(this.kubernetesClient.services().inNamespace(anyString()))
			.thenReturn(this.serviceOperation);

	ObjectMeta objectMeta = new ObjectMeta();
	objectMeta.setNamespace(namespace);

	final Endpoints endpoints = new EndpointsBuilder().withMetadata(objectMeta)
			.addNewSubset().addAllToPorts(getEndpointPorts(ports)).addNewAddress()
			.endAddress().endSubset().build();

	when(this.endpointsResource.get()).thenReturn(endpoints);
	when(this.endpointsOperation.withName(serviceId))
			.thenReturn(this.endpointsResource);
	when(this.kubernetesClient.endpoints()).thenReturn(this.endpointsOperation);
}
 
Example #2
Source File: EndpointsTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteMulti() {
  Endpoints endpoint1 = new EndpointsBuilder().withNewMetadata().withName("endpoint1").withNamespace("test").endMetadata().build();
  Endpoints endpoint2 = new EndpointsBuilder().withNewMetadata().withName("endpoint2").withNamespace("ns1").endMetadata().build();
  Endpoints endpoint3 = new EndpointsBuilder().withNewMetadata().withName("endpoint3").withNamespace("any").endMetadata().build();

  server.expect().withPath("/api/v1/namespaces/test/endpoints/endpoint1").andReturn(200, endpoint1).once();
  server.expect().withPath("/api/v1/namespaces/ns1/endpoints/endpoint2").andReturn(200, endpoint2).once();

  KubernetesClient client = server.getClient();
  Boolean deleted = client.endpoints().inAnyNamespace().delete(endpoint1, endpoint2);
  assertTrue(deleted);

  deleted = client.endpoints().inAnyNamespace().delete(endpoint3);
  assertFalse(deleted);
}
 
Example #3
Source File: EndpointsTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testBuild() {
  Endpoints endpoints = new EndpointsBuilder()
    .withNewMetadata().withName("endpoint").withNamespace("test").endMetadata()
    .withSubsets().addNewSubset().addNewAddress().withIp("10.10.50.53").endAddress()
    .addNewPort().withPort(80).withName("apache").endPort()
    .endSubset()
    .build();

  server.expect().withPath("/api/v1/namespaces/test/endpoints/endpoint").andReturn(200, endpoints).once();

  KubernetesClient client = server.getClient();
  endpoints = client.endpoints().inNamespace("test").withName("endpoint").get();
  assertNotNull(endpoints);
  assertEquals("apache", endpoints.getSubsets().get(0).getPorts().get(0).getName());
}
 
Example #4
Source File: KubernetesDiscoveryClientTest.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void getInstancesShouldBeAbleToHandleEndpointsSingleAddress() {
	mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint")
			.andReturn(200, new EndpointsBuilder().withNewMetadata()
					.withName("endpoint").endMetadata().addNewSubset().addNewAddress()
					.withIp("ip1").withNewTargetRef().withUid("uid1").endTargetRef()
					.endAddress().addNewPort("http", 80, "TCP").endSubset().build())
			.once();

	mockServer.expect().get().withPath("/api/v1/services/endpoint")
			.andReturn(200, new ServiceBuilder().withNewMetadata()
					.withName("endpoint").withLabels(new HashMap<String, String>() {
						{
							put("l", "v");
						}
					}).endMetadata().build())
			.always();

	final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();

	final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient,
			properties, KubernetesClient::services,
			new DefaultIsServicePortSecureResolver(properties));

	final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint");

	assertThat(instances).hasSize(1)
			.filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1)
			.filteredOn(s -> s.getInstanceId().equals("uid1")).hasSize(1);
}
 
Example #5
Source File: KubernetesDiscoveryClientTest.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void getInstancesShouldBeAbleToHandleEndpointsSingleAddressAndMultiplePorts() {
	mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint")
			.andReturn(200, new EndpointsBuilder().withNewMetadata()
					.withName("endpoint").endMetadata().addNewSubset().addNewAddress()
					.withIp("ip1").withNewTargetRef().withUid("uid").endTargetRef()
					.endAddress().addNewPort("mgmt", 9000, "TCP")
					.addNewPort("http", 80, "TCP").endSubset().build())
			.once();

	mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint")
			.andReturn(200, new ServiceBuilder().withNewMetadata()
					.withName("endpoint").withLabels(new HashMap<String, String>() {
						{
							put("l", "v");
						}
					}).endMetadata().build())
			.always();

	final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	properties.setPrimaryPortName("http");
	final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient,
			properties, KubernetesClient::services,
			new DefaultIsServicePortSecureResolver(properties));

	final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint");

	assertThat(instances).hasSize(1)
			.filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure()).hasSize(1)
			.filteredOn(s -> s.getInstanceId().equals("uid")).hasSize(1)
			.filteredOn(s -> 80 == s.getPort()).hasSize(1);
}
 
Example #6
Source File: KubernetesDiscoveryClientTest.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
@Test
public void getInstancesShouldBeAbleToHandleEndpointsMultipleAddresses() {
	mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint")
			.andReturn(200, new EndpointsBuilder().withNewMetadata()
					.withName("endpoint").endMetadata().addNewSubset().addNewAddress()
					.withIp("ip1").endAddress().addNewAddress().withIp("ip2")
					.endAddress().addNewPort("https", 443, "TCP").endSubset().build())
			.once();

	mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint")
			.andReturn(200, new ServiceBuilder().withNewMetadata()
					.withName("endpoint").withLabels(new HashMap<String, String>() {
						{
							put("l", "v");
						}
					}).endMetadata().build())
			.always();

	final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient,
			properties, KubernetesClient::services,
			new DefaultIsServicePortSecureResolver(properties));

	final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint");

	assertThat(instances).hasSize(2).filteredOn(ServiceInstance::isSecure)
			.extracting(ServiceInstance::getHost).containsOnly("ip1", "ip2");
}
 
Example #7
Source File: EndpointsTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testGet() {
  server.expect().withPath("/api/v1/namespaces/test/endpoints/endpoint1").andReturn(200, new EndpointsBuilder().build()).once();
  server.expect().withPath("/api/v1/namespaces/ns1/endpoints/endpoint2").andReturn(200, new EndpointsBuilder().build()).once();

  KubernetesClient client = server.getClient();
  Endpoints endpoints = client.endpoints().inNamespace("test").withName("endpoint1").get();
  assertNotNull(endpoints);

  endpoints = client.endpoints().withName("endpoint2").get();
  assertNull(endpoints);

  endpoints = client.endpoints().inNamespace("ns1").withName("endpoint2").get();
  assertNotNull(endpoints);
}
 
Example #8
Source File: EndpointsTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testDelete() {
  server.expect().withPath("/api/v1/namespaces/test/endpoints/endpoint1").andReturn(200, new EndpointsBuilder().build()).once();
  server.expect().withPath("/api/v1/namespaces/ns1/endpoints/endpoint2").andReturn(200, new EndpointsBuilder().build()).once();

  KubernetesClient client = server.getClient();
  Boolean deleted = client.endpoints().inNamespace("test").withName("endpoint1").delete();
  assertTrue(deleted);

  deleted = client.endpoints().withName("endpoint2").delete();
  assertFalse(deleted);

  deleted = client.endpoints().inNamespace("ns1").withName("endpoint2").delete();
  assertTrue(deleted);
}
 
Example #9
Source File: EndpointsTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateWithNameMismatch() {
  Assertions.assertThrows(KubernetesClientException.class, () -> {
    Endpoints endpoint1 = new EndpointsBuilder().withNewMetadata().withName("endpoint1").withNamespace("test").endMetadata().build();
    Endpoints endpoint2 = new EndpointsBuilder().withNewMetadata().withName("endpoint2").withNamespace("ns1").endMetadata().build();

    KubernetesClient client = server.getClient();
    client.endpoints().inNamespace("test1").withName("myendpoint1").create(endpoint1);
  });
}
 
Example #10
Source File: EndpointsExample.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  String master = "https://localhost:8443";

  Config config = new ConfigBuilder().withMasterUrl(master).build();
  try (final KubernetesClient client = new DefaultKubernetesClient(config)) {
    try {
      String namespace = "default";
      log("namespace", namespace);
      Deployment deployment = client.apps().deployments().inNamespace(namespace).load(EndpointsExample.class.getResourceAsStream("/endpoints-deployment.yml")).get();
      log("Deployment created");
      client.apps().deployments().inNamespace(namespace).create(deployment);

      Service service = client.services().inNamespace(namespace).load(EndpointsExample.class.getResourceAsStream("/endpoints-service.yml")).get();
      log("Service created");
      client.services().inNamespace(namespace).create(service);

      Endpoints endpoints = new EndpointsBuilder()
        .withNewMetadata().withName("external-web").withNamespace(namespace).endMetadata()
        .withSubsets().addNewSubset().addNewAddress().withIp("10.10.50.53").endAddress()
        .addNewPort().withPort(80).withName("apache").endPort()
        .endSubset()
        .build();
      log("Endpoint created");
      client.endpoints().inNamespace(namespace).create(endpoints);
      log("Endpoint url");
      endpoints = client.endpoints().inNamespace(namespace).withName("external-web").get();
      log("Endpoint Port", endpoints.getSubsets().get(0).getPorts().get(0).getName());
    } finally {
      // clear resources
      client.apps().deployments().inNamespace("default").withName("endpoints-deployment").delete();
      client.services().inNamespace("default").withName("endpoints-nginx").delete();
      client.endpoints().inNamespace("default").withName("external-web").delete();
    }
  } catch (Exception e) {
    log("Exception occurred: ", e.getMessage());
    e.printStackTrace();
  }
}
 
Example #11
Source File: KubernetesDiscoveryClientTest.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void getInstancesShouldBeAbleToHandleEndpointsFromMultipleNamespaces() {
	Endpoints endPoints1 = new EndpointsBuilder().withNewMetadata()
			.withName("endpoint").withNamespace("test").endMetadata().addNewSubset()
			.addNewAddress().withIp("ip1").withNewTargetRef().withUid("uid1")
			.endTargetRef().endAddress().addNewPort("http", 80, "TCP").endSubset()
			.build();

	Endpoints endpoints2 = new EndpointsBuilder().withNewMetadata()
			.withName("endpoint").withNamespace("test2").endMetadata().addNewSubset()
			.addNewAddress().withIp("ip2").withNewTargetRef().withUid("uid2")
			.endTargetRef().endAddress().addNewPort("http", 80, "TCP").endSubset()
			.build();

	List<Endpoints> endpointsList = new ArrayList<>();
	endpointsList.add(endPoints1);
	endpointsList.add(endpoints2);

	EndpointsList endpoints = new EndpointsList();
	endpoints.setItems(endpointsList);

	mockServer.expect().get()
			.withPath("/api/v1/endpoints?fieldSelector=metadata.name%3Dendpoint")
			.andReturn(200, endpoints).once();

	mockServer.expect().get().withPath("/api/v1/namespaces/test/endpoints/endpoint")
			.andReturn(200, endPoints1).once();

	mockServer.expect().get().withPath("/api/v1/namespaces/test2/endpoints/endpoint")
			.andReturn(200, endpoints2).once();

	Service service1 = new ServiceBuilder().withNewMetadata().withName("endpoint")
			.withNamespace("test").withLabels(new HashMap<String, String>() {
				{
					put("l", "v");
				}
			}).endMetadata().build();

	Service service2 = new ServiceBuilder().withNewMetadata().withName("endpoint")
			.withNamespace("test2").withLabels(new HashMap<String, String>() {
				{
					put("l", "v");
				}
			}).endMetadata().build();

	List<Service> servicesList = new ArrayList<>();
	servicesList.add(service1);
	servicesList.add(service2);

	ServiceList services = new ServiceList();
	services.setItems(servicesList);

	mockServer.expect().get()
			.withPath("/api/v1/services?fieldSelector=metadata.name%3Dendpoint")
			.andReturn(200, services).once();

	mockServer.expect().get().withPath("/api/v1/namespaces/test/services/endpoint")
			.andReturn(200, service1).always();

	mockServer.expect().get().withPath("/api/v1/namespaces/test2/services/endpoint")
			.andReturn(200, service2).always();

	final KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	properties.setAllNamespaces(true);

	final DiscoveryClient discoveryClient = new KubernetesDiscoveryClient(mockClient,
			properties, KubernetesClient::services,
			new DefaultIsServicePortSecureResolver(properties));

	final List<ServiceInstance> instances = discoveryClient.getInstances("endpoint");

	assertThat(instances).hasSize(2);
	assertThat(instances).filteredOn(s -> s.getHost().equals("ip1") && !s.isSecure())
			.hasSize(1);
	assertThat(instances).filteredOn(s -> s.getHost().equals("ip2") && !s.isSecure())
			.hasSize(1);
	assertThat(instances).filteredOn(s -> s.getInstanceId().equals("uid1"))
			.hasSize(1);
	assertThat(instances).filteredOn(s -> s.getInstanceId().equals("uid2"))
			.hasSize(1);
}
 
Example #12
Source File: KubernetesReactiveDiscoveryClientTests.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldReturnFlux(@Client KubernetesClient kubernetesClient,
		@Server KubernetesServer kubernetesServer) {
	kubernetesServer.expect().get().withPath("/api/v1/namespaces/test/services")
			.andReturn(200,
					new ServiceListBuilder().addNewItem().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().endItem().build())
			.once();

	Endpoints endPoints = new EndpointsBuilder().withNewMetadata()
			.withName("endpoint").withNamespace("test").endMetadata().addNewSubset()
			.addNewAddress().withIp("ip1").withNewTargetRef().withUid("uid1")
			.endTargetRef().endAddress().addNewPort("http", 80, "TCP").endSubset()
			.build();

	kubernetesServer.expect().get()
			.withPath("/api/v1/namespaces/test/endpoints/existing-service")
			.andReturn(200, endPoints).once();

	kubernetesServer.expect().get()
			.withPath("/api/v1/namespaces/test/services/existing-service")
			.andReturn(200,
					new ServiceBuilder().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().build())
			.once();

	KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(
			kubernetesClient, properties, KubernetesClient::services);
	Flux<ServiceInstance> instances = client.getInstances("existing-service");
	StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
}
 
Example #13
Source File: KubernetesReactiveDiscoveryClientTests.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldReturnFluxWithPrefixedMetadata(
		@Client KubernetesClient kubernetesClient,
		@Server KubernetesServer kubernetesServer) {
	kubernetesServer.expect().get().withPath("/api/v1/namespaces/test/services")
			.andReturn(200,
					new ServiceListBuilder().addNewItem().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().endItem().build())
			.once();

	Endpoints endPoints = new EndpointsBuilder().withNewMetadata()
			.withName("endpoint").withNamespace("test").endMetadata().addNewSubset()
			.addNewAddress().withIp("ip1").withNewTargetRef().withUid("uid1")
			.endTargetRef().endAddress().addNewPort("http", 80, "TCP").endSubset()
			.build();

	kubernetesServer.expect().get()
			.withPath("/api/v1/namespaces/test/endpoints/existing-service")
			.andReturn(200, endPoints).once();

	kubernetesServer.expect().get()
			.withPath("/api/v1/namespaces/test/services/existing-service")
			.andReturn(200,
					new ServiceBuilder().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().build())
			.once();

	KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	properties.getMetadata().setAnnotationsPrefix("annotation.");
	properties.getMetadata().setLabelsPrefix("label.");
	properties.getMetadata().setPortsPrefix("port.");
	ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(
			kubernetesClient, properties, KubernetesClient::services);
	Flux<ServiceInstance> instances = client.getInstances("existing-service");
	StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
}
 
Example #14
Source File: KubernetesReactiveDiscoveryClientTests.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldReturnFluxWhenServiceHasMultiplePortsAndPrimaryPortNameIsSet(
		@Client KubernetesClient kubernetesClient,
		@Server KubernetesServer kubernetesServer) {
	kubernetesServer.expect().get().withPath("/api/v1/namespaces/test/services")
			.andReturn(200,
					new ServiceListBuilder().addNewItem().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().endItem().build())
			.once();

	Endpoints endPoints = new EndpointsBuilder().withNewMetadata()
			.withName("endpoint").withNamespace("test").endMetadata().addNewSubset()
			.addNewAddress().withIp("ip1").withNewTargetRef().withUid("uid1")
			.endTargetRef().endAddress().addNewPort("http", 80, "TCP")
			.addNewPort("https", 443, "TCP").endSubset().build();

	kubernetesServer.expect().get()
			.withPath("/api/v1/namespaces/test/endpoints/existing-service")
			.andReturn(200, endPoints).once();

	kubernetesServer.expect().get()
			.withPath("/api/v1/namespaces/test/services/existing-service")
			.andReturn(200,
					new ServiceBuilder().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().build())
			.once();

	KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	properties.setPrimaryPortName("https");
	ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(
			kubernetesClient, properties, KubernetesClient::services);
	Flux<ServiceInstance> instances = client.getInstances("existing-service");
	StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
}
 
Example #15
Source File: KubernetesReactiveDiscoveryClientTests.java    From spring-cloud-kubernetes with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldReturnFluxOfServicesAcrossAllNamespaces(
		@Client KubernetesClient kubernetesClient,
		@Server KubernetesServer kubernetesServer) {
	kubernetesServer.expect().get().withPath("/api/v1/namespaces/test/services")
			.andReturn(200,
					new ServiceListBuilder().addNewItem().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().endItem().build())
			.once();

	Endpoints endpoints = new EndpointsBuilder().withNewMetadata()
			.withName("endpoint").withNamespace("test").endMetadata().addNewSubset()
			.addNewAddress().withIp("ip1").withNewTargetRef().withUid("uid1")
			.endTargetRef().endAddress().addNewPort("http", 80, "TCP")
			.addNewPort("https", 443, "TCP").endSubset().build();

	EndpointsList endpointsList = new EndpointsList();
	endpointsList.setItems(singletonList(endpoints));

	kubernetesServer.expect().get().withPath(
			"/api/v1/endpoints?fieldSelector=metadata.name%3Dexisting-service")
			.andReturn(200, endpointsList).once();

	kubernetesServer.expect().get()
			.withPath("/api/v1/namespaces/test/services/existing-service")
			.andReturn(200,
					new ServiceBuilder().withNewMetadata()
							.withName("existing-service")
							.withLabels(new HashMap<String, String>() {
								{
									put("label", "value");
								}
							}).endMetadata().build())
			.once();

	KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties();
	properties.setAllNamespaces(true);
	ReactiveDiscoveryClient client = new KubernetesReactiveDiscoveryClient(
			kubernetesClient, properties, KubernetesClient::services);
	Flux<ServiceInstance> instances = client.getInstances("existing-service");
	StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
}
 
Example #16
Source File: EndpointOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
@Override
protected Endpoints resource() {
    return new EndpointsBuilder().withNewMetadata().withNamespace(NAMESPACE).withName(RESOURCE_NAME).endMetadata().build();
}