Class KubernetesClient
-
Method Summary
Modifier and TypeMethodDescriptionvoidvoidcreateAndEdit(Map resource, Consumer<Map> editor) Ensures that a) the resource exists (by reading and possibly creating it) and b) the resource is edited.voidvoidvoidvoidLists the specified resources.Lists the specified resources.voidpatch(String apiVersion, String kind, String namespace, String name, String contentType, Object body) version()watch(String apiVersion, String kind, String namespace, Long resourceVersion, ExecutorService executors, Watcher watcher)
-
Method Details
-
version
- Throws:
HttpExceptionIOException
-
getClient
-
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 listkind- the resource kind to listnamespace- the resource namespace to list, or null if listing resources for all namespacesbatchSize- 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:
IOExceptionKubernetesApiException
-
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 listkind- the resource kind to listnamespace- the resource namespace to list, or null if listing resources for all namespacesbatchSize- size of the batches to return.- Throws:
IOExceptionKubernetesApiException
-
watch
public Closeable watch(String apiVersion, String kind, String namespace, Long resourceVersion, ExecutorService executors, Watcher watcher) throws IOException, KubernetesApiException - Parameters:
apiVersion- the apiVersion to watchkind- the kind to watchnamespace- the namespace to watch (or null to watch all namespaces)- Returns:
- the watch, which can be closed
- Throws:
IOException- an underlaying communication problemKubernetesApiException- on a Kubernetes API problem
-
read
- Parameters:
resource- the resource to read- Throws:
IOException- an underlaying communication problemKubernetesApiException- 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 readkind- the resource to readnamespace- the resource to readname- the resource to read- Throws:
IOException- an underlaying communication problemKubernetesApiException- code 404 reason "NotFound", if resource does not exist
-
create
- Parameters:
resource- the resource to create- Returns:
- the resource created
- Throws:
IOException- an underlaying communication problemKubernetesApiException- with code 409 reason "AlreadyExists", if the resource already exists
-
delete
- Parameters:
resource- the resource to delete- Throws:
IOException- an underlaying communication problemKubernetesApiException- 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 deletekind- the resource to deletenamespace- the resource to deletename- the resource to delete- Throws:
IOException- an underlaying communication problemKubernetesApiException- 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:
IOExceptionKubernetesApiException
-
apply
- Parameters:
resource- the resource to apply- Throws:
IOException- an underlaying communication problemKubernetesApiException- on a Kubernetes API problem
-
edit
- Parameters:
resource- the resource to edit- Throws:
IOException- an underlaying communication problemKubernetesApiException- 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 editkind- the resource to editnamespace- the resource to editname- the resource to edit- Throws:
IOException- an underlaying communication problemKubernetesApiException- 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 problemKubernetesApiException- code 409 reason "Conflict", if someone else modified the object; code 404, if someone deleted the resource while we attempted to edit it.
-
getNamespace
-