io.fabric8.kubernetes.api.model.ServiceList Java Examples
The following examples show how to use
io.fabric8.kubernetes.api.model.ServiceList.
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: ServicesList.java From fabric8-forge with Apache License 2.0 | 6 votes |
private void printServices(ServiceList services, PrintStream out) { TablePrinter table = new TablePrinter(); table.columns("id", "labels", "selector", "port"); List<Service> items = services.getItems(); if (items == null) { items = Collections.EMPTY_LIST; } Filter<Service> filter = KubernetesHelper.createServiceFilter(filterText.getValue()); for (Service service : items) { if (filter.matches(service)) { String labels = KubernetesHelper.toLabelsString(service.getMetadata().getLabels()); String selector = KubernetesHelper.toLabelsString(getSelector(service)); Set<Integer> ports = getPorts(service); List<Integer> portList = new ArrayList<>(ports); String portText; if (portList.size() == 1) { portText = portList.get(0).toString(); } else { portText = Strings.join(portList, ", "); } table.row(KubernetesHelper.getName(service), labels, selector, portText); } } table.print(); }
Example #2
Source File: ServiceDelete.java From fabric8-forge with Apache License 2.0 | 6 votes |
@Override public void initializeUI(UIBuilder builder) throws Exception { super.initializeUI(builder); // populate autocompletion options serviceId.setCompleter(new UICompleter<String>() { @Override public Iterable<String> getCompletionProposals(UIContext context, InputComponent<?, String> input, String value) { List<String> list = new ArrayList<String>(); ServiceList services = getKubernetes().services().inNamespace(getNamespace()).list(); if (services != null) { List<Service> items = services.getItems(); if (items != null) { for (Service item : items) { String id = KubernetesHelper.getName(item); list.add(id); } } } Collections.sort(list); return list; } }); builder.add(serviceId); }
Example #3
Source File: KubernetesDiscoveryClientFilterTest.java From spring-cloud-kubernetes with Apache License 2.0 | 6 votes |
@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); }
Example #4
Source File: KubernetesMockTest.java From vxms with Apache License 2.0 | 6 votes |
@Test public void findServices() { final ObjectMeta build = new ObjectMetaBuilder().addToLabels("test", "test").build(); final ServiceSpec spec = new ServiceSpecBuilder().addNewPort().and().withClusterIP("192.168.1.1").build(); final Service service = new ServiceBuilder().withMetadata(build).withSpec(spec).build(); server .expect() .withPath("/api/v1/namespaces/default/services") .andReturn(200, new ServiceListBuilder().addToItems().addToItems(service).build()) .once(); KubernetesClient client = this.client; final ServiceList list = client.services().inNamespace("default").list(); assertNotNull(list); assertEquals("test", list.getItems().get(0).getMetadata().getLabels().get("test")); System.out.println(list.getItems().get(0).getSpec().getClusterIP()); }
Example #5
Source File: KubernetesDiscoveryClientFilterTest.java From spring-cloud-kubernetes with Apache License 2.0 | 6 votes |
@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); }
Example #6
Source File: KubeDiscovery.java From vxms with Apache License 2.0 | 6 votes |
private static void resolveServiceByLabelOnly( Object bean, JsonObject env, KubernetesClient client, Field serviceNameField, boolean withLabel, boolean withLabels) throws KubernetesClientException { final Map<String, String> labels = getLabelsFromAnnotation(env, serviceNameField, withLabel, withLabels); final ServiceList serviceListResult = getServicesByLabel(labels, client); Optional.ofNullable(serviceListResult) .ifPresent( list -> { if (!list.getItems().isEmpty() && list.getItems().size() == 1) { final Service serviceEntry = list.getItems().get(0); resolveHostAndSetValue(bean, env, serviceNameField, serviceEntry); } else if (!list.getItems().isEmpty() && list.getItems().size() > 1) { handleNonUniqueLabelsError(labels); } }); }
Example #7
Source File: AbstractKubernetesDeployer.java From spring-cloud-deployer-kubernetes with Apache License 2.0 | 5 votes |
protected AppStatus buildAppStatus(String id, PodList podList, ServiceList services) { AppStatus.Builder statusBuilder = AppStatus.of(id); Service service = null; if (podList != null && podList.getItems() != null) { for (Pod pod : podList.getItems()) { String deploymentKey = pod.getMetadata().getLabels().get(SPRING_DEPLOYMENT_KEY); for (Service svc : services.getItems()) { if (svc.getMetadata().getName().equals(deploymentKey)) { service = svc; break; } } //find the container with the correct env var for(Container container : pod.getSpec().getContainers()) { if(container.getEnv().stream().anyMatch(envVar -> "SPRING_CLOUD_APPLICATION_GUID".equals(envVar.getName()))) { //find container status for this container Optional<ContainerStatus> containerStatusOptional = pod.getStatus().getContainerStatuses() .stream().filter(containerStatus -> container.getName().equals(containerStatus.getName())) .findFirst(); statusBuilder.with(new KubernetesAppInstanceStatus(pod, service, properties, containerStatusOptional.orElse(null))); break; } } } } return statusBuilder.build(); }
Example #8
Source File: ServiceCrudTest.java From kubernetes-client with Apache License 2.0 | 5 votes |
@Test public void testCrud() { KubernetesClient client = server.getClient(); 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(); 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() .editMetadata().addToLabels("key1", "value1").endMetadata().done(); assertNotNull(service2); assertEquals("value1", service2.getMetadata().getLabels().get("key1")); }
Example #9
Source File: KubeDiscovery.java From vxms with Apache License 2.0 | 5 votes |
private static Optional<Service> findServiceEntry( JsonObject env, KubernetesClient client, String serviceName) { Objects.requireNonNull(client, "no client available"); final String resolvedServiceName = resolveProperty(env, serviceName); return Optional.ofNullable(client.services().inNamespace(client.getNamespace()).list()) .orElse(new ServiceList()) .getItems() .stream() .filter(item -> item.getMetadata().getName().equalsIgnoreCase(resolvedServiceName)) .findFirst(); }
Example #10
Source File: KubeDiscovery.java From vxms with Apache License 2.0 | 5 votes |
private static FilterWatchListDeletable<Service, ServiceList, Boolean, Watch, Watcher<Service>> createLabelFilterQuery( Map<String, String> labels, MixedOperation<Service, ServiceList, DoneableService, Resource<Service, DoneableService>> services) { FilterWatchListDeletable<Service, ServiceList, Boolean, Watch, Watcher<Service>> listable = null; for (Entry<String, String> entry : labels.entrySet()) { listable = listable == null ? services.withLabel(entry.getKey(), entry.getValue()) : listable.withLabel(entry.getKey(), entry.getValue()); } return listable; }
Example #11
Source File: KubeDiscovery.java From vxms with Apache License 2.0 | 5 votes |
private static ServiceList getServicesByLabel(Map<String, String> labels, KubernetesClient client) throws KubernetesClientException { Objects.requireNonNull(client, "no client available"); final MixedOperation<Service, ServiceList, DoneableService, Resource<Service, DoneableService>> services = client.services(); final FilterWatchListDeletable<Service, ServiceList, Boolean, Watch, Watcher<Service>> listable = createLabelFilterQuery(labels, services); return listable.list(); }
Example #12
Source File: KubernetesAppDeployer.java From spring-cloud-deployer-kubernetes with Apache License 2.0 | 5 votes |
private void deleteService(Map<String, String> labels) { FilterWatchListDeletable<Service, ServiceList, Boolean, Watch, Watcher<Service>> servicesToDelete = client.services().withLabels(labels); if (servicesToDelete != null && servicesToDelete.list().getItems() != null) { boolean servicesDeleted = servicesToDelete.delete(); logger.debug(String.format("Service deleted for: %s - %b", labels, servicesDeleted)); } }
Example #13
Source File: KubernetesDiscoveryClientFilterTest.java From spring-cloud-kubernetes with Apache License 2.0 | 5 votes |
@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); }
Example #14
Source File: ServiceOperationsImpl.java From kubernetes-client with Apache License 2.0 | 4 votes |
public ServiceOperationsImpl(OperationContext context) { super(context.withPlural("services")); this.type = Service.class; this.listType = ServiceList.class; this.doneableType = DoneableService.class; }
Example #15
Source File: AutoAdaptableKubernetesClient.java From kubernetes-client with Apache License 2.0 | 4 votes |
@Override public MixedOperation<Service, ServiceList, DoneableService, ServiceResource<Service, DoneableService>> services() { return delegate.services(); }
Example #16
Source File: ServicesList.java From fabric8-forge with Apache License 2.0 | 4 votes |
@Override public Result execute(UIExecutionContext uiExecutionContext) throws Exception { ServiceList services = getKubernetes().services().list(); printServices(services, System.out); return null; }
Example #17
Source File: DefaultKubernetesClient.java From kubernetes-client with Apache License 2.0 | 4 votes |
@Override public MixedOperation<Service, ServiceList, DoneableService, ServiceResource<Service, DoneableService>> services() { return new ServiceOperationsImpl(httpClient, getConfiguration()); }
Example #18
Source File: ManagedKubernetesClient.java From kubernetes-client with Apache License 2.0 | 4 votes |
public MixedOperation<io.fabric8.kubernetes.api.model.Service, ServiceList, DoneableService, ServiceResource<io.fabric8.kubernetes.api.model.Service, DoneableService>> services() { return delegate.services(); }
Example #19
Source File: ServiceMockBuilder.java From strimzi-kafka-operator with Apache License 2.0 | 4 votes |
public ServiceMockBuilder(Map<String, Service> svcDb, Map<String, Endpoints> endpointsDb) { super(Service.class, ServiceList.class, DoneableService.class, castClass(ServiceResource.class), svcDb); this.endpointsDb = endpointsDb; }
Example #20
Source File: ServiceOperator.java From strimzi-kafka-operator with Apache License 2.0 | 4 votes |
@Override protected MixedOperation<Service, ServiceList, DoneableService, ServiceResource<Service, DoneableService>> operation() { return client.services(); }
Example #21
Source File: KubernetesDiscoveryClientTest.java From spring-cloud-kubernetes with Apache License 2.0 | 4 votes |
@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 #22
Source File: ServicesController.java From rabbitmq-operator with Apache License 2.0 | 4 votes |
@Override protected MixedOperation<Service, ServiceList, DoneableService, ServiceResource<Service, DoneableService>> operation() { return getClient().services(); }
Example #23
Source File: KubernetesClient.java From kubernetes-client with Apache License 2.0 | 2 votes |
/** * API entrypoint for Service related operations. Service (core/v1) * * @return MixedOperation object for Service related operations. */ MixedOperation<Service, ServiceList, DoneableService, ServiceResource<Service, DoneableService>> services();