kubex.core¶
Auto-generated reference for the kubex.core module.
Exceptions¶
kubex.core.exceptions ¶
ConfigurationValueError ¶
Bases: ConfigurationError, ValueError
A configuration failure that used to raise ValueError.
Also a ValueError, so except ValueError keeps working.
KubexApiError ¶
KubexApiError(
content: C,
*args: Any,
status: HTTPStatus | None = None,
retry_after: float | None = None,
)
Bases: Generic[C], KubexClientException
Source code in kubex/core/exceptions.py
retry_after
class-attribute
instance-attribute
¶
Seconds to wait before retrying, from the Retry-After response header
(or the Status details.retryAfterSeconds) when the server sent one —
typically on 429 Too Many Requests and 503 Service Unavailable.
KubexConnectionError ¶
Bases: KubexClientException
The API server could not be reached, or the connection broke mid-response.
KubexTimeoutError ¶
Bases: KubexClientException, TimeoutError
A request exceeded its client-side timeout.
Also a TimeoutError, so except TimeoutError keeps working.
Request and response¶
kubex.core.request ¶
kubex.core.response ¶
API parameters¶
kubex.core.params ¶
AttachOptions ¶
AttachOptions(
*,
container: str | None = None,
stdin: bool = False,
stdout: bool = True,
stderr: bool = True,
tty: bool = False,
)
Options for the Pod attach subresource. Mirrors v1.PodAttachOptions.
Source code in kubex/core/params.py
ExecOptions ¶
ExecOptions(
*,
command: Sequence[str],
container: str | None = None,
stdin: bool = False,
stdout: bool = True,
stderr: bool = True,
tty: bool = False,
)
Options for the Pod exec subresource.
Mirrors the v1.PodExecOptions Kubernetes API. command is required
and must be a non-empty sequence of strings — each element serializes as a
separate command= query parameter. Boolean flags serialize as the
lowercase strings "true" / "false" to match what the Kubernetes API
server expects.
Source code in kubex/core/params.py
InvalidObjects ¶
Bases: str, Enum
What list() and watch() do with an object that does not fit the model.
RAISE (the default) raises pydantic's ValidationError. INCLUDE keeps
the object as an InvalidObject instead: list() returns it in
ListWithInvalid.invalid and watch() yields an InvalidWatchEvent.
PortForwardOptions ¶
Options for the Pod portforward subresource.
Validates that ports are unique, non-empty, and within 1..65535.
to_query_params() returns repeated ("ports", "<n>") pairs in input order.
Source code in kubex/core/params.py
Timeout ¶
Timeout(
total: float | None = None,
*,
connect: float | None = None,
read: float | None = None,
write: float | None = None,
pool: float | None = None,
)
Timeout settings for HTTP requests.
Provides a client-agnostic way to configure timeout settings for the underlying HTTP client. Individual fields may be ignored by a given implementation if the client library does not support them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total
|
float | None
|
Total timeout in seconds for the whole request. Used as the default for unset granular fields. |
None
|
connect
|
float | None
|
Timeout in seconds for establishing a connection. |
None
|
read
|
float | None
|
Timeout in seconds for reading the response. |
None
|
write
|
float | None
|
Timeout in seconds for writing the request body. Only honored by the httpx backend. |
None
|
pool
|
float | None
|
Timeout in seconds for acquiring a connection from the pool. Only honored by the httpx backend. |
None
|
Source code in kubex/core/params.py
coerce
classmethod
¶
Normalize a TimeoutTypes value to Timeout or None.
Source code in kubex/core/params.py
format_rfc3339 ¶
Format a timezone-aware datetime as an RFC 3339 UTC timestamp (...Z).
Naive datetimes are rejected rather than guessed at (local time or UTC).
Source code in kubex/core/params.py
Watch events¶
kubex.core.watch_event ¶
INITIAL_EVENTS_END_ANNOTATION
module-attribute
¶
Annotation on the bookmark that ends the initial events of a watch started
with send_initial_events=True.
Bookmark ¶
Bases: BaseEntity
Bookmark is a pointer to a resource in a stream.
EventType ¶
Bases: str, Enum
EventType is the type of the watch event.
InvalidWatchEvent
dataclass
¶
A watch event whose object does not fit the resource model.
Yielded by Api.watch(invalid=InvalidObjects.INCLUDE) in place of a
WatchEvent for an ADDED, MODIFIED or DELETED event; the stream
goes on. Like a WatchEvent it has type and object.metadata, so code
that only needs an object's name, namespace or resource version works on both.
Attributes:
| Name | Type | Description |
|---|---|---|
type |
EventType
|
|
object |
InvalidObject
|
The object, with its raw data, the validation error and its metadata. |
is_initial_events_end
property
¶
Always False: only a bookmark ends the initial events.
WatchEvent ¶
Bases: Generic[ResourceType]
WatchEvent represents a single event from a watch stream.
Source code in kubex/core/watch_event.py
is_initial_events_end
property
¶
Whether this is the bookmark that ends the initial events.
A watch started with send_initial_events=True first sends an ADDED
event for every existing object, then this bookmark. From here on the
stream carries live changes, and the bookmark's
object.metadata.resource_version is the first one a watch can resume
from. Other bookmarks are periodic checkpoints and return False.
Objects that do not fit the model¶
kubex.core.invalid_object ¶
Objects that do not fit their resource model.
Api.list() and Api.watch() raise pydantic's ValidationError when an
object from the API server does not fit the model, for example a custom resource
stored before its CRD schema changed. With invalid=InvalidObjects.INCLUDE they
keep such an object as an InvalidObject instead, so one bad object neither
fails a whole list nor ends a watch.
InvalidObject
dataclass
¶
An object from the API server that does not fit the resource model.
Attributes:
| Name | Type | Description |
|---|---|---|
raw |
dict[str, Any]
|
The object as the server sent it. |
error |
ValidationError
|
Why it does not fit the model. |
metadata |
ObjectMetadata
|
The object's metadata. It always parsed and always has a
|
ListWithInvalid ¶
Bases: ListEntity[ResourceType], Generic[ResourceType]
A list whose objects that do not fit the model are kept apart.
Returned by Api.list(invalid=InvalidObjects.INCLUDE). items holds the
objects that fit the model, in server order; invalid holds the rest. The
list's metadata (resource_version, continue_) is the server's, so
paging and watching from the list work as usual. invalid is not serialized.
Patch types¶
kubex.core.patch ¶
ApplyPatch ¶
Bases: Patch, Generic[P]
Server-side apply patch (application/apply-patch+yaml).
The body is serialized as JSON (valid YAML, and what kubectl apply
--server-side sends), so timestamps and other non-primitive fields use
their JSON wire format.
apiVersion and kind are always sent, even though exclude_unset would
drop them when they are only model defaults: the server rejects an apply
patch without them. Request builders pass the target resource's values
explicitly (so e.g. a PartialObjectMetadata body applied through
api.metadata.patch() is sent as the target kind); otherwise they come
from the body, falling back to its __RESOURCE_CONFIG__.
metadata.managedFields is always dropped since the server rejects any
non-nil value. metadata.resourceVersion is kept and acts as an
optimistic-concurrency precondition, so when applying an object fetched
earlier, clear it (obj.metadata.resource_version = None) unless a
Conflict on concurrent changes is what you want.
Unlike MergePatch/StrategicMergePatch, exclude_none defaults to True
here: a field management conflict is reported by field ownership, not by
an explicit null, so there is no RFC 7396-style "null deletes the field"
convention to preserve.
JsonPatch ¶
Bases: RootModel[list[JsonPatchOperation]]
RFC 6902 JSON Patch document.
Can be constructed directly or built incrementally via chaining::
patch = JsonPatch().add("/a", 1).remove("/b").replace("/c", "new")
JsonPointer ¶
Bases: str
RFC 6901 JSON Pointer.
A JSON Pointer is either the empty string (referencing the whole document)
or a sequence of reference tokens each prefixed by /.
Construct from a raw string, from individual tokens, or by chaining
the / operator::
ptr = JsonPointer("/foo/0")
ptr = JsonPointer.from_tokens("foo", 0)
ptr = JsonPointer() / "foo" / 0
from_tokens
classmethod
¶
Build a pointer from unescaped reference tokens.
Tokens are escaped per RFC 6901 (~ -> ~0, / -> ~1).
Source code in kubex/core/json_pointer.py
MergePatch ¶
Bases: Patch, Generic[P]
RFC 7396 JSON merge patch (application/merge-patch+json).
exclude_none defaults to False: RFC 7396 uses an explicit null to
delete a key, so a field the caller deliberately set to None must reach
the server as null rather than being stripped before serialization.
exclude_unset still limits the patch to fields the caller actually
touched, so fields left at their default remain distinguishable from
explicitly null ones.
StrategicMergePatch ¶
Bases: Patch, Generic[P]
Kubernetes strategic merge patch (application/strategic-merge-patch+json).
exclude_none defaults to False for the same reason as MergePatch: an
explicit null is meaningful (it deletes the field) and must not be
stripped before serialization.
JSON Patch (RFC 6902)¶
kubex.core.json_patch ¶
JsonPatch ¶
Bases: RootModel[list[JsonPatchOperation]]
RFC 6902 JSON Patch document.
Can be constructed directly or built incrementally via chaining::
patch = JsonPatch().add("/a", 1).remove("/b").replace("/c", "new")
JSON Pointer (RFC 6901)¶
kubex.core.json_pointer ¶
JsonPointer ¶
Bases: str
RFC 6901 JSON Pointer.
A JSON Pointer is either the empty string (referencing the whole document)
or a sequence of reference tokens each prefixed by /.
Construct from a raw string, from individual tokens, or by chaining
the / operator::
ptr = JsonPointer("/foo/0")
ptr = JsonPointer.from_tokens("foo", 0)
ptr = JsonPointer() / "foo" / 0
from_tokens
classmethod
¶
Build a pointer from unescaped reference tokens.
Tokens are escaped per RFC 6901 (~ -> ~0, / -> ~1).
Source code in kubex/core/json_pointer.py
Subresource definitions¶
kubex.core.subresource ¶
WebSocket channel protocol¶
kubex.core.exec_channels ¶
port_prefix_encode ¶
Inverse of port_prefix_decode.
Outbound port-forward writes carry no port prefix (the channel id alone
addresses the kubelet); only the first inbound frame on each channel is
prefixed, which PortForwardSession strips via port_prefix_decode.
Kubex itself has no reason to construct a prefix, so this exists for the
test suite to build fake inbound frames and round-trip against
port_prefix_decode.