Class KubernetesClient

java.lang.Object
com.predic8.membrane.core.kubernetes.client.KubernetesClient

public class KubernetesClient extends Object
  • Method Details

    • version

      public Map version() throws HttpException, IOException
      Throws:
      HttpException
      IOException
    • getClient

      public ExceptionThrowingConsumer<Exchange> getClient()
    • getBaseURL

      public String getBaseURL()
    • listItems

      public Stream<Map> listItems(String apiVersion, String kind, String namespace, int batchSize) throws IOException, KubernetesApiException
      Lists the specified resources.

      Use this method only, if you do need not any information from the list structure. (e.g. no subsequent call to watch(String, String, String, Long, ExecutorService, Watcher)).

      Parameters:
      apiVersion - the resource apiVersion to list
      kind - the resource kind to list
      namespace - the resource namespace to list, or null if listing resources for all namespaces
      batchSize - size of the batches to fetch. (Note that this method always returns all items. The batchSize only affects the size of the internal HTTP responses used by this method to fetch the items.)
      Throws:
      IOException
      KubernetesApiException
    • list

      public Stream<Map> list(String apiVersion, String kind, String namespace, int batchSize) throws IOException, KubernetesApiException
      Lists the specified resources.

      Use this method only, if you need some information from the list structure (e.g. the resourceVersion to initialize a subsequent call to watch(String, String, String, Long, ExecutorService, Watcher)).

      To only list the items, use listItems(String, String, String, int) instead.

      To get the items of the list batches returned by this method, call list(...).flatMap(map -> ((List)map.get("items")).stream());

      Parameters:
      apiVersion - the resource apiVersion to list
      kind - the resource kind to list
      namespace - the resource namespace to list, or null if listing resources for all namespaces
      batchSize - size of the batches to return.
      Throws:
      IOException
      KubernetesApiException
    • watch

      public Closeable watch(String apiVersion, String kind, String namespace, Long resourceVersion, ExecutorService executors, Watcher watcher) throws IOException, KubernetesApiException
      Parameters:
      apiVersion - the apiVersion to watch
      kind - the kind to watch
      namespace - the namespace to watch (or null to watch all namespaces)
      Returns:
      the watch, which can be closed
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - on a Kubernetes API problem
    • read

      public Map read(Map resource) throws IOException, KubernetesApiException
      Parameters:
      resource - the resource to read
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - code 404 reason "NotFound", if resource does not exist
    • read

      public Map read(String apiVersion, String kind, String namespace, String name) throws IOException, KubernetesApiException
      Parameters:
      apiVersion - the resource to read
      kind - the resource to read
      namespace - the resource to read
      name - the resource to read
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - code 404 reason "NotFound", if resource does not exist
    • create

      public Map create(Map resource) throws IOException, KubernetesApiException
      Parameters:
      resource - the resource to create
      Returns:
      the resource created
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - with code 409 reason "AlreadyExists", if the resource already exists
    • delete

      public void delete(Map resource) throws IOException, KubernetesApiException
      Parameters:
      resource - the resource to delete
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - with code 404 and reason NotFound if it does not exist
    • delete

      public void delete(String apiVersion, String kind, String namespace, String name) throws IOException, KubernetesApiException
      Parameters:
      apiVersion - the resource to delete
      kind - the resource to delete
      namespace - the resource to delete
      name - the resource to delete
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - with code 404 and reason NotFound if it does not exist
    • patch

      public void patch(String apiVersion, String kind, String namespace, String name, String contentType, Object body) throws IOException, KubernetesApiException
      Throws:
      IOException
      KubernetesApiException
    • apply

      public void apply(Map resource) throws IOException, KubernetesApiException
      Parameters:
      resource - the resource to apply
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - on a Kubernetes API problem
    • edit

      public void edit(Map resource, Consumer<Map> editor) throws IOException, KubernetesApiException
      Parameters:
      resource - the resource to edit
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - code 409 reason "Conflict", if someone else modified the resource; code 404 reason "NotFound", if resource does not exist.
    • edit

      public void edit(String apiVersion, String kind, String namespace, String name, Consumer<Map> editor) throws IOException, KubernetesApiException
      Parameters:
      apiVersion - the resource to edit
      kind - the resource to edit
      namespace - the resource to edit
      name - the resource to edit
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - code 409 reason "Conflict", if someone else modified the resource; code 404 reason "NotFound", if resource does not exist.
    • createAndEdit

      public void createAndEdit(Map resource, Consumer<Map> editor) throws IOException, KubernetesApiException
      Ensures that a) the resource exists (by reading and possibly creating it) and b) the resource is edited.

      This method helps with compare-and-swap "CAS" semantics in the Kubernetes API.

      The create-and-edit sequence might seem unnecessary, but it looks this way (tested on 1.23.6): If you 'create' a resource with values (e.g. entries in a Secret (below .data) or Lease (below .spec)), you own these values with the operation 'Update' tracked in the managedFields structure. These values cannot (out of the box, that is without re-owning the fields or manipulating the managedFields) be modified by a subsequent 'apply'.

      If you were to only use 'apply' (for creating and updating the resource), the fieldmanager is OK (operation 'Apply' is tracked in managedFields). But this breaks the CAS semantics, as two concurrent creators have different views of how they want the resource to look like. The first creator (=applier) succeeds (returning HTTP 201), but the second creator (=applier) also succeeds (returning HTTP 200), effectively overriding the first.

      Therefore, to archive correct CAS semantics, using this method, a) create an empty resource b) edit it to the state you want.

      Throws an error, if resource creation fails for any reason other than "AlreadyExists". Throws an error, if editing the resource fails.

      Parameters:
      resource - the resource to create and edit
      Throws:
      IOException - an underlaying communication problem
      KubernetesApiException - code 409 reason "Conflict", if someone else modified the object; code 404, if someone deleted the resource while we attempted to edit it.
    • getNamespace

      public String getNamespace()