io.fabric8.kubernetes.client.Watch Java Examples

The following examples show how to use io.fabric8.kubernetes.client.Watch. 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: WatchTest.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Test
public void testHttpErrorWithOutdated() {
  Assertions.assertThrows(KubernetesClientException.class, () -> {
    logger.info("testHttpErrorWithOutdated");
    KubernetesClient client = server.getClient().inNamespace("test");
    // http error: history outdated
    server.expect()
      .withPath("/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&watch=true")
      .andReturn(410, outdatedEvent).once();
    final boolean[] onCloseCalled = {false};
    try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new Watcher<Pod>() {
      @Override
      public void eventReceived(Action action, Pod resource) {
        throw new AssertionFailedError();
      }

      @Override
      public void onClose(KubernetesClientException cause) {
        onCloseCalled[0] =true;
      }
    })) {

    }
    assertTrue(onCloseCalled[0]);
  });
}
 
Example #2
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testDeleteDeploymentThrowingKubernetesClientExceptionShouldCloseWatch()
    throws Exception {
  final String DEPLOYMENT_NAME = "nonExistingPod";
  doReturn(DEPLOYMENT_NAME).when(deploymentMetadata).getName();

  doThrow(KubernetesClientException.class).when(deploymentResource).delete();
  doReturn(deploymentResource).when(deploymentResource).withPropagationPolicy(eq("Background"));
  Watch watch = mock(Watch.class);
  doReturn(watch).when(podResource).watch(any());

  try {
    new KubernetesDeployments("", "", clientFactory, executor)
        .doDeleteDeployment(DEPLOYMENT_NAME)
        .get(5, TimeUnit.SECONDS);
  } catch (KubernetesInfrastructureException e) {
    assertTrue(e.getCause() instanceof KubernetesClientException);
    verify(watch).close();
    return;
  }
  fail("The exception should have been rethrown");
}
 
Example #3
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testDeleteNonExistingDeploymentBeforeWatch() throws Exception {
  final String DEPLOYMENT_NAME = "nonExistingPod";
  doReturn(DEPLOYMENT_NAME).when(deploymentMetadata).getName();
  doReturn(podResource).when(podResource).withPropagationPolicy(eq("Background"));
  doReturn(deploymentResource).when(deploymentResource).withPropagationPolicy(eq("Background"));
  doReturn(Boolean.FALSE).when(deploymentResource).delete();
  Watch watch = mock(Watch.class);
  doReturn(watch).when(podResource).watch(any());

  new KubernetesDeployments("", "", clientFactory, executor)
      .doDeleteDeployment(DEPLOYMENT_NAME)
      .get(5, TimeUnit.SECONDS);

  verify(watch).close();
}
 
Example #4
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testDeleteDeploymentThrowingAnyExceptionShouldCloseWatch() throws Exception {
  final String DEPLOYMENT_NAME = "nonExistingPod";

  doThrow(RuntimeException.class).when(deploymentResource).delete();
  Watch watch = mock(Watch.class);
  doReturn(watch).when(podResource).watch(any());

  try {
    new KubernetesDeployments("", "", clientFactory, executor)
        .doDeleteDeployment(DEPLOYMENT_NAME)
        .get(5, TimeUnit.SECONDS);
  } catch (RuntimeException e) {
    verify(watch).close();
    return;
  }
  fail("The exception should have been rethrown");
}
 
Example #5
Source File: KubernetesNamespaceTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test(expectedExceptions = InfrastructureException.class)
public void testThrowsInfrastructureExceptionWhenWatcherClosed() throws Exception {
  prepareCreateNamespaceRequest();
  final Resource resource = prepareNamespaceResource(NAMESPACE);
  doThrow(new KubernetesClientException("error", 403, null)).when(resource).get();
  when(serviceAccountResource.get()).thenReturn(null);
  doAnswer(
          (Answer<Watch>)
              invocation -> {
                final Watcher<ServiceAccount> watcher = invocation.getArgument(0);
                watcher.onClose(mock(KubernetesClientException.class));
                return mock(Watch.class);
              })
      .when(serviceAccountResource)
      .watch(any());

  new KubernetesNamespace(clientFactory, executor, NAMESPACE, WORKSPACE_ID).prepare(false);
}
 
Example #6
Source File: KubernetesDeploymentsTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testDeleteNonExistingPodBeforeWatch() throws Exception {
  final String POD_NAME = "nonExistingPod";
  doReturn(POD_NAME).when(metadata).getName();

  doReturn(Boolean.FALSE).when(podResource).delete();
  doReturn(podResource).when(podResource).withPropagationPolicy(eq("Background"));
  Watch watch = mock(Watch.class);
  doReturn(watch).when(podResource).watch(any());

  new KubernetesDeployments("", "", clientFactory, executor)
      .doDeletePod(POD_NAME)
      .get(5, TimeUnit.SECONDS);

  verify(watch).close();
}
 
Example #7
Source File: DeploymentRollingUpdater.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
protected PodList listSelectedPods(Deployment obj) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = pods().inNamespace(namespace);
  if (obj.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(obj.getSpec().getSelector().getMatchLabels());
  }
  if (obj.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : obj.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
        case "In":
          podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "NotIn":
          podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "DoesNotExist":
          podLister.withoutLabel(req.getKey());
          break;
        case "Exists":
          podLister.withLabel(req.getKey());
          break;
      }
    }
  }
  return podLister.list();
}
 
Example #8
Source File: KubernetesPersistentVolumeClaims.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
private Watch pvcIsBoundWatcher(
    CompletableFuture<PersistentVolumeClaim> future,
    Resource<PersistentVolumeClaim, DoneablePersistentVolumeClaim> pvcResource) {
  return pvcResource.watch(
      new Watcher<PersistentVolumeClaim>() {
        @Override
        public void eventReceived(Action action, PersistentVolumeClaim pvc) {
          if (pvc.getStatus().getPhase().equals(PVC_BOUND_PHASE)) {
            LOG.debug("pvc '" + pvc.getMetadata().getName() + "' is bound");
            future.complete(pvc);
          }
        }

        @Override
        public void onClose(KubernetesClientException cause) {
          safelyFinishFutureOnClose(cause, future, pvcResource.get().getMetadata().getName());
        }
      });
}
 
Example #9
Source File: AbstractResourceOperator.java    From strimzi-kafka-operator with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public Future<List<T>> listAsync(String namespace, Optional<LabelSelector> selector) {
    Promise<List<T>> result = Promise.promise();
    vertx.createSharedWorkerExecutor("kubernetes-ops-tool").executeBlocking(
        future -> {
            FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> operation;
            if (AbstractWatchableResourceOperator.ANY_NAMESPACE.equals(namespace))  {
                operation = operation().inAnyNamespace();
            } else {
                operation = operation().inNamespace(namespace);
            }
            if (selector.isPresent()) {
                operation = operation.withLabelSelector(selector.get());
            }
            future.complete(operation.list().getItems());
        }, true, result
    );
    return result.future();
}
 
Example #10
Source File: StatefulSetRollingUpdater.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
protected PodList listSelectedPods(StatefulSet obj) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = pods().inNamespace(namespace);
  if (obj.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(obj.getSpec().getSelector().getMatchLabels());
  }
  if (obj.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : obj.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
        case "In":
          podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "NotIn":
          podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "DoesNotExist":
          podLister.withoutLabel(req.getKey());
          break;
        case "Exists":
          podLister.withLabel(req.getKey());
          break;
      }
    }
  }
  return podLister.list();
}
 
Example #11
Source File: ReplicaSetRollingUpdater.java    From kubernetes-client with Apache License 2.0 6 votes vote down vote up
@Override
protected PodList listSelectedPods(ReplicaSet obj) {
  FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podLister = pods().inNamespace(namespace);
  if (obj.getSpec().getSelector().getMatchLabels() != null) {
    podLister.withLabels(obj.getSpec().getSelector().getMatchLabels());
  }
  if (obj.getSpec().getSelector().getMatchExpressions() != null) {
    for (LabelSelectorRequirement req : obj.getSpec().getSelector().getMatchExpressions()) {
      switch (req.getOperator()) {
        case "In":
          podLister.withLabelIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "NotIn":
          podLister.withLabelNotIn(req.getKey(), req.getValues().toArray(new String[]{}));
          break;
        case "DoesNotExist":
          podLister.withoutLabel(req.getKey());
          break;
        case "Exists":
          podLister.withLabel(req.getKey());
          break;
      }
    }
  }
  return podLister.list();
}
 
Example #12
Source File: AddressUtils.java    From enmasse with Apache License 2.0 6 votes vote down vote up
private static void waitForAddressesMatched(TimeoutBudget timeoutBudget, int totalDestinations, FilterWatchListMultiDeletable<Address, AddressList, Boolean, Watch, Watcher<Address>> addressClient, AddressListMatcher addressListMatcher) {
    TestUtils.waitUntilCondition(totalDestinations + " match", phase -> {
        try {
            List<Address> addressList = addressClient.list().getItems();
            Map<String, Address> notMatched = addressListMatcher.matchAddresses(addressList);
            if (verboseLogs) {
                notMatched.values().forEach(address ->
                    log.info("Waiting until address {} ready, message {}", address.getMetadata().getName(), address.getStatus().getMessages()));
            }
            if (!notMatched.isEmpty() && phase == WaitPhase.LAST_TRY) {
                log.info(notMatched.size() + " out of " + totalDestinations + " addresses are not matched: " + notMatched.values());
            }
            return notMatched.isEmpty();
        } catch (KubernetesClientException e) {
            if (phase == WaitPhase.LAST_TRY) {
                log.error("Client can't read address resources", e);
            } else {
                log.warn("Client can't read address resources");
            }
            return false;
        }
    }, timeoutBudget);
}
 
Example #13
Source File: AbstractNonNamespacedResourceOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testReconcileThrowsWhenDeletionTimesOut(VertxTestContext context) {
    T resource = resource();
    AtomicBoolean watchWasClosed = new AtomicBoolean(false);
    Resource mockResource = mock(resourceType());
    when(mockResource.get()).thenReturn(resource);
    when(mockResource.withGracePeriod(anyLong())).thenReturn(mockResource);
    when(mockResource.delete()).thenReturn(true);
    when(mockResource.watch(any())).thenAnswer(invocation -> {
        Watcher<T> watcher = invocation.getArgument(0);
        return (Watch) () -> {
            watchWasClosed.set(true);
        };
    });

    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);

    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);

    C mockClient = mock(clientType());
    mocker(mockClient, mockCms);

    AbstractNonNamespacedResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient);

    Checkpoint async = context.checkpoint();
    op.reconcile(resource.getMetadata().getName(), null).onComplete(context.failing(e -> context.verify(() -> {
        assertThat(e, instanceOf(TimeoutException.class));
        verify(mockResource).delete();
        assertThat("Watch was not closed", watchWasClosed.get(), is(true));
        async.flag();
    })));
}
 
Example #14
Source File: CustomResourceTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Should be able to watch a single resource with some name")
public void testWatchSingleResource() throws IOException, InterruptedException {
  // Given
  server.expect().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos"+ "?fieldSelector=" + Utils.toUrlEncoded("metadata.name=example-hello")+"&watch=true")
    .andUpgradeToWebSocket()
    .open()
    .waitFor(WATCH_EVENT_PERIOD)
    .andEmit( new WatchEvent(null, "ADDED"))
    .done().always();

  KubernetesClient client = server.getClient();

  CountDownLatch anyEventReceieved = new CountDownLatch(1);
  // When
  Watch watch = client.customResource(customResourceDefinitionContext)
    .watch("ns1", "example-hello", null, (ListOptions)null,
      new Watcher<String>() {
        @Override
        public void eventReceived(Action action, String resource) { anyEventReceieved.countDown(); }
        @Override
        public void onClose(KubernetesClientException cause) { }
      });

  // Then
  assertTrue(anyEventReceieved.await(1, TimeUnit.SECONDS));
  watch.close();
}
 
Example #15
Source File: PortForwardService.java    From jkube with Eclipse Public License 2.0 5 votes vote down vote up
private Pod getNewestPod(LabelSelector selector) {
    FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> pods =
            KubernetesHelper.withSelector(kubernetes.pods(), selector, log);

    PodList list = pods.list();
    if (list != null) {
        List<Pod> items = list.getItems();
        return getNewestPod(items);
    }
    return null;
}
 
Example #16
Source File: CustomResourceTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
@DisplayName("Should be able to watch some resource in a namespace with null name, labelSelector and ListOptions")
public void testWatchAllResource() throws IOException, InterruptedException {
  // Given
  server.expect().withPath("/apis/test.fabric8.io/v1alpha1/namespaces/ns1/hellos?watch=true")
    .andUpgradeToWebSocket()
    .open()
    .waitFor(WATCH_EVENT_PERIOD)
    .andEmit(new WatchEvent(null, "ADDED"))
    .done().always();

  KubernetesClient client = server.getClient();

  CountDownLatch anyEventReceived = new CountDownLatch(1);
  // When
  Watch watch = client.customResource(customResourceDefinitionContext)
    .watch("ns1", null, null, (ListOptions)null,
      new Watcher<String>() {
        @Override
        public void eventReceived(Action action, String resource) { anyEventReceived.countDown(); }
        @Override
        public void onClose(KubernetesClientException cause) { }
      });

  // Then
  assertTrue(anyEventReceived.await(1, TimeUnit.SECONDS));
  watch.close();
}
 
Example #17
Source File: KubernetesAppDeployer.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
private void deleteStatefulSet(Map<String, String> labels) {
	FilterWatchListDeletable<StatefulSet, StatefulSetList, Boolean, Watch, Watcher<StatefulSet>> ssToDelete =
			client.apps().statefulSets().withLabels(labels);

	if (ssToDelete != null && ssToDelete.list().getItems() != null) {
		boolean ssDeleted = ssToDelete.delete();
		logger.debug(String.format("StatefulSet deleted for: %s - %b", labels, ssDeleted));
	}
}
 
Example #18
Source File: MockBuilder.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
private Watch addWatcher(PredicatedWatcher<T> predicatedWatcher) {
    watchers.add(predicatedWatcher);
    return () -> {
        watchers.remove(predicatedWatcher);
        LOGGER.debug("Watcher {} removed", predicatedWatcher);
    };
}
 
Example #19
Source File: AbstractNonNamespacedResourceOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
public void testReconcileDeletionThrowsWhenDeleteMethodThrows(VertxTestContext context) {
    T resource = resource();
    AtomicBoolean watchWasClosed = new AtomicBoolean(false);
    RuntimeException ex = new RuntimeException("Testing this exception is handled correctly");

    Resource mockResource = mock(resourceType());
    when(mockResource.get()).thenReturn(resource);
    when(mockResource.withGracePeriod(anyLong())).thenReturn(mockResource);
    when(mockResource.delete()).thenThrow(ex);
    when(mockResource.watch(any())).thenAnswer(invocation -> {
        Watcher<T> watcher = invocation.getArgument(0);
        watcher.eventReceived(Watcher.Action.DELETED, null);
        return (Watch) () -> {
            watchWasClosed.set(true);
        };
    });

    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);

    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);

    C mockClient = mock(clientType());
    mocker(mockClient, mockCms);

    AbstractNonNamespacedResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient);

    Checkpoint async = context.checkpoint();
    op.reconcile(resource.getMetadata().getName(), null).onComplete(context.failing(e -> context.verify(() -> {
        assertThat(e, is(ex));
        assertThat("Watch was not closed", watchWasClosed.get(), is(true));
        async.flag();
    })));
}
 
Example #20
Source File: WatchTest.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Test
public void testHttpErrorReconnect() throws InterruptedException {
  logger.info("testHttpErrorReconnect");
  String path = "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&watch=true";
  KubernetesClient client = server.getClient().inNamespace("test");
  // accept watch and disconnect
  server.expect().withPath(path).andUpgradeToWebSocket().open().done().once();
  // refuse reconnect attempts 6 times
  server.expect().withPath(path).andReturn(503, new StatusBuilder().withCode(503).build()).times(6);
  // accept next reconnect and send outdated event to stop the watch
  server.expect().withPath(path).andUpgradeToWebSocket().open(outdatedEvent).done().once();

  final CountDownLatch closeLatch = new CountDownLatch(1);
  try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new Watcher<Pod>() {
    @Override
    public void eventReceived(Action action, Pod resource) {
      throw new AssertionFailedError();
    }

    @Override
    public void onClose(KubernetesClientException cause) {
      logger.debug("onClose", cause);
      closeLatch.countDown();
    }
  })) /* autoclose */ {
    assertTrue(closeLatch.await(3, TimeUnit.MINUTES));
  }
}
 
Example #21
Source File: AbstractNonNamespacedResourceOperatorTest.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testReconcileDeletionSuccessful(VertxTestContext context) {
    T resource = resource();
    AtomicBoolean watchWasClosed = new AtomicBoolean(false);
    Resource mockResource = mock(resourceType());
    when(mockResource.get()).thenReturn(resource);
    when(mockResource.withGracePeriod(anyLong())).thenReturn(mockResource);
    when(mockResource.delete()).thenReturn(Boolean.TRUE);
    when(mockResource.watch(any())).thenAnswer(invocation -> {
        Watcher<T> watcher = invocation.getArgument(0);
        watcher.eventReceived(Watcher.Action.DELETED, null);
        return (Watch) () -> {
            watchWasClosed.set(true);
        };
    });

    NonNamespaceOperation mockNameable = mock(NonNamespaceOperation.class);
    when(mockNameable.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);

    MixedOperation mockCms = mock(MixedOperation.class);
    when(mockCms.withName(matches(RESOURCE_NAME))).thenReturn(mockResource);

    C mockClient = mock(clientType());
    mocker(mockClient, mockCms);

    AbstractNonNamespacedResourceOperator<C, T, L, D, R> op = createResourceOperations(vertx, mockClient);

    Checkpoint async = context.checkpoint();
    op.reconcile(resource.getMetadata().getName(), null).onComplete(context.succeeding(rrDeleted -> {
        verify(mockResource).delete();
        context.verify(() -> assertThat("Watch was not closed", watchWasClosed.get(), is(true)));
        async.flag();
    }));
}
 
Example #22
Source File: KubernetesTaskLauncher.java    From spring-cloud-deployer-kubernetes with Apache License 2.0 5 votes vote down vote up
private void deletePod(String id) {
	FilterWatchListDeletable<Pod, PodList, Boolean, Watch, Watcher<Pod>> podsToDelete = client.pods()
			.withLabel(SPRING_APP_KEY, id);

	if (podsToDelete != null && podsToDelete.list().getItems() != null) {
		logger.debug(String.format("Deleting Pod for task: %s", id));
		boolean podsDeleted = podsToDelete.delete();
		logger.debug(String.format("Pod deleted for: %s - %b", id, podsDeleted));
	}
}
 
Example #23
Source File: AbstractWatchableResourceOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
public Watch watch(String namespace, Watcher<T> watcher) {
    if (ANY_NAMESPACE.equals(namespace))    {
        return watchInAnyNamespace(watcher);
    } else {
        return watchInNamespace(namespace, watcher);
    }
}
 
Example #24
Source File: KubernetesPersistentVolumeClaims.java    From che with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Creates and returns {@link Watch} that watches for 'WaitForFirstConsumer' events on given PVC.
 */
private Watch pvcIsWaitingForConsumerWatcher(
    CompletableFuture<PersistentVolumeClaim> future, PersistentVolumeClaim actualPvc)
    throws InfrastructureException {
  return clientFactory
      .create(workspaceId)
      .events()
      .inNamespace(namespace)
      .withField(PVC_EVENT_REASON_FIELD_KEY, PVC_EVENT_WAIT_CONSUMER_REASON)
      .withField(PVC_EVENT_UID_FIELD_KEY, actualPvc.getMetadata().getUid())
      .watch(
          new Watcher<Event>() {
            @Override
            public void eventReceived(Action action, Event resource) {
              LOG.debug(
                  "PVC '"
                      + actualPvc.getMetadata().getName()
                      + "' is waiting for first consumer. Don't wait to bound to avoid deadlock.");
              future.complete(actualPvc);
            }

            @Override
            public void onClose(KubernetesClientException cause) {
              safelyFinishFutureOnClose(cause, future, actualPvc.getMetadata().getName());
            }
          });
}
 
Example #25
Source File: AbstractResourceOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked") // due to L extends KubernetesResourceList/*<T>*/
protected List<T> listInAnyNamespace(Labels selector) {
    FilterWatchListMultiDeletable<T, L, Boolean, Watch, Watcher<T>> operation = operation().inAnyNamespace();

    if (selector != null) {
        Map<String, String> labels = selector.toMap();
        return operation.withLabels(labels)
                .list()
                .getItems();
    } else {
        return operation
                .list()
                .getItems();
    }
}
 
Example #26
Source File: AbstractWatchableResourceOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
public Watch watch(String namespace, Optional<LabelSelector> selector, Watcher<T> watcher) {
    FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> operation
            = ANY_NAMESPACE.equals(namespace) ? operation().inAnyNamespace() : operation().inNamespace(namespace);
    if (selector.isPresent()) {
        operation = operation.withLabelSelector(selector.get());
    }
    return operation.watch(watcher);
}
 
Example #27
Source File: AbstractNonNamespacedResourceOperator.java    From strimzi-kafka-operator with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked") // due to L extends KubernetesResourceList/*<T>*/
protected List<T> listInAnyNamespace(Labels selector) {
    FilterWatchListMultiDeletable<T, L, Boolean, Watch, Watcher<T>> operation = operation();

    if (selector != null) {
        Map<String, String> labels = selector.toMap();
        return operation.withLabels(labels)
                .list()
                .getItems();
    } else {
        return operation
                .list()
                .getItems();
    }
}
 
Example #28
Source File: BaseOperation.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
@Deprecated
public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withoutFields(Map<String, String> fields) throws
  KubernetesClientException {
  // Re-use "withoutField" to convert values from String to String[]
  labels.forEach(this::withoutField);
  return this;
}
 
Example #29
Source File: KubeDiscovery.java    From vxms with Apache License 2.0 5 votes vote down vote up
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 #30
Source File: BaseOperation.java    From kubernetes-client with Apache License 2.0 5 votes vote down vote up
@Override
public FilterWatchListDeletable<T, L, Boolean, Watch, Watcher<T>> withoutField(String key, String value) {
  fieldsNot.merge(key, new String[]{value}, (oldList, newList) -> {
    if (Utils.isNotNullOrEmpty(newList[0])) { // Only add new values when not null
      final String[] concatList = (String[]) Array.newInstance(String.class, oldList.length + newList.length);
      System.arraycopy(oldList, 0, concatList, 0, oldList.length);
      System.arraycopy(newList, 0, concatList, oldList.length, newList.length);
      return concatList;
    } else {
      return oldList;
    }
  });
  return this;
}