Skip to content

prefect_kubernetes.credentials

Module for defining Kubernetes credential handling and client generation.

KubernetesClusterConfig

Bases: Block

Stores configuration for interaction with Kubernetes clusters.

See from_file for creation.

Attributes:

Name Type Description
config Dict

The entire loaded YAML contents of a kubectl config file

context_name str

The name of the kubectl context to use

Example

Load a saved Kubernetes cluster config:

from prefect_kubernetes.credentials import import KubernetesClusterConfig

cluster_config_block = KubernetesClusterConfig.load("BLOCK_NAME")
Source code in prefect_kubernetes/credentials.py
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class KubernetesClusterConfig(Block):
    """
    Stores configuration for interaction with Kubernetes clusters.

    See `from_file` for creation.

    Attributes:
        config: The entire loaded YAML contents of a kubectl config file
        context_name: The name of the kubectl context to use

    Example:
        Load a saved Kubernetes cluster config:
        ```python
        from prefect_kubernetes.credentials import import KubernetesClusterConfig

        cluster_config_block = KubernetesClusterConfig.load("BLOCK_NAME")
        ```
    """

    _block_type_name = "Kubernetes Cluster Config"
    _logo_url = "https://cdn.sanity.io/images/3ugk85nk/production/2d0b896006ad463b49c28aaac14f31e00e32cfab-250x250.png"
    _documentation_url = "https://prefecthq.github.io/prefect-kubernetes/credentials/#prefect_kubernetes.credentials.KubernetesClusterConfig"  # noqa
    config: Dict = Field(
        default=..., description="The entire contents of a kubectl config file."
    )
    context_name: str = Field(
        default=..., description="The name of the kubectl context to use."
    )

    @field_validator("config", mode="before")
    @classmethod
    def parse_yaml_config(cls, value):
        if isinstance(value, str):
            return yaml.safe_load(value)
        return value

    @classmethod
    def from_file(
        cls: Type[Self], path: Optional[Path] = None, context_name: Optional[str] = None
    ) -> Self:
        """
        Create a cluster config from the a Kubernetes config file.

        By default, the current context in the default Kubernetes config file will be
        used.

        An alternative file or context may be specified.

        The entire config file will be loaded and stored.
        """

        path = Path(path or config.kube_config.KUBE_CONFIG_DEFAULT_LOCATION)
        path = path.expanduser().resolve()

        # Determine the context
        (
            existing_contexts,
            current_context,
        ) = config.kube_config.list_kube_config_contexts(config_file=str(path))
        context_names = {ctx["name"] for ctx in existing_contexts}
        if context_name:
            if context_name not in context_names:
                raise ValueError(
                    f"Context {context_name!r} not found. "
                    f"Specify one of: {listrepr(context_names, sep=', ')}."
                )
        else:
            context_name = current_context["name"]

        # Load the entire config file
        config_file_contents = path.read_text()
        config_dict = yaml.safe_load(config_file_contents)
        return cls(config=config_dict, context_name=context_name)

    async def get_api_client(self) -> "ApiClient":
        """
        Returns a Kubernetes API client for this cluster config.
        """
        return await config.kube_config.new_client_from_config_dict(
            config_dict=self.config, context=self.context_name
        )

    async def configure_client(self) -> None:
        """
        Activates this cluster configuration by loading the configuration into the
        Kubernetes Python client. After calling this, Kubernetes API clients can use
        this config's context.
        """
        await config.kube_config.load_kube_config_from_dict(
            config_dict=self.config, context=self.context_name
        )

configure_client() async

Activates this cluster configuration by loading the configuration into the Kubernetes Python client. After calling this, Kubernetes API clients can use this config's context.

Source code in prefect_kubernetes/credentials.py
116
117
118
119
120
121
122
123
124
async def configure_client(self) -> None:
    """
    Activates this cluster configuration by loading the configuration into the
    Kubernetes Python client. After calling this, Kubernetes API clients can use
    this config's context.
    """
    await config.kube_config.load_kube_config_from_dict(
        config_dict=self.config, context=self.context_name
    )

from_file(path=None, context_name=None) classmethod

Create a cluster config from the a Kubernetes config file.

By default, the current context in the default Kubernetes config file will be used.

An alternative file or context may be specified.

The entire config file will be loaded and stored.

Source code in prefect_kubernetes/credentials.py
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@classmethod
def from_file(
    cls: Type[Self], path: Optional[Path] = None, context_name: Optional[str] = None
) -> Self:
    """
    Create a cluster config from the a Kubernetes config file.

    By default, the current context in the default Kubernetes config file will be
    used.

    An alternative file or context may be specified.

    The entire config file will be loaded and stored.
    """

    path = Path(path or config.kube_config.KUBE_CONFIG_DEFAULT_LOCATION)
    path = path.expanduser().resolve()

    # Determine the context
    (
        existing_contexts,
        current_context,
    ) = config.kube_config.list_kube_config_contexts(config_file=str(path))
    context_names = {ctx["name"] for ctx in existing_contexts}
    if context_name:
        if context_name not in context_names:
            raise ValueError(
                f"Context {context_name!r} not found. "
                f"Specify one of: {listrepr(context_names, sep=', ')}."
            )
    else:
        context_name = current_context["name"]

    # Load the entire config file
    config_file_contents = path.read_text()
    config_dict = yaml.safe_load(config_file_contents)
    return cls(config=config_dict, context_name=context_name)

get_api_client() async

Returns a Kubernetes API client for this cluster config.

Source code in prefect_kubernetes/credentials.py
108
109
110
111
112
113
114
async def get_api_client(self) -> "ApiClient":
    """
    Returns a Kubernetes API client for this cluster config.
    """
    return await config.kube_config.new_client_from_config_dict(
        config_dict=self.config, context=self.context_name
    )

KubernetesCredentials

Bases: Block

Credentials block for generating configured Kubernetes API clients.

Attributes:

Name Type Description
cluster_config Optional[KubernetesClusterConfig]

A KubernetesClusterConfig block holding a JSON kube config for a specific kubernetes context.

Example

Load stored Kubernetes credentials:

from prefect_kubernetes.credentials import KubernetesCredentials

kubernetes_credentials = KubernetesCredentials.load("BLOCK_NAME")
Source code in prefect_kubernetes/credentials.py
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
class KubernetesCredentials(Block):
    """Credentials block for generating configured Kubernetes API clients.

    Attributes:
        cluster_config: A `KubernetesClusterConfig` block holding a JSON kube
            config for a specific kubernetes context.

    Example:
        Load stored Kubernetes credentials:
        ```python
        from prefect_kubernetes.credentials import KubernetesCredentials

        kubernetes_credentials = KubernetesCredentials.load("BLOCK_NAME")
        ```
    """

    _block_type_name = "Kubernetes Credentials"
    _logo_url = "https://cdn.sanity.io/images/3ugk85nk/production/2d0b896006ad463b49c28aaac14f31e00e32cfab-250x250.png"  # noqa
    _documentation_url = "https://prefecthq.github.io/prefect-kubernetes/credentials/#prefect_kubernetes.credentials.KubernetesCredentials"  # noqa

    cluster_config: Optional[KubernetesClusterConfig] = None

    @asynccontextmanager
    async def get_client(
        self,
        client_type: Literal["apps", "batch", "core", "custom_objects"],
        configuration: Optional[Configuration] = None,
    ) -> AsyncGenerator[KubernetesClient, None]:
        """Convenience method for retrieving a Kubernetes API client for deployment resources.

        Args:
            client_type: The resource-specific type of Kubernetes client to retrieve.

        Yields:
            An authenticated, resource-specific Kubernetes API client.

        Example:
            ```python
            from prefect_kubernetes.credentials import KubernetesCredentials

            async with KubernetesCredentials.get_client("core") as core_v1_client:
                pods = await core_v1_client.list_namespaced_pod()
                for pod in pods.items:
                    print(pod.metadata.name)
            ```
        """
        client_configuration = configuration or Configuration()
        if self.cluster_config:
            config_dict = self.cluster_config.config
            context = self.cluster_config.context_name

            # Use Configuration to load configuration from a dictionary
            await config.load_kube_config_from_dict(
                config_dict=config_dict,
                context=context,
                client_configuration=client_configuration,
            )
        else:
            try:
                config.load_incluster_config(client_configuration=client_configuration)
            except config.ConfigException:
                # If in-cluster config fails, load the local kubeconfig
                await config.load_kube_config(
                    client_configuration=client_configuration,
                )
        async with ApiClient(configuration=client_configuration) as api_client:
            try:
                yield await self.get_resource_specific_client(
                    client_type, api_client=api_client
                )
            finally:
                await api_client.close()

    async def get_resource_specific_client(
        self,
        client_type: str,
        api_client: ApiClient,
    ) -> Union[AppsV1Api, BatchV1Api, CoreV1Api]:
        """
        Utility function for configuring a generic Kubernetes client.
        It will attempt to connect to a Kubernetes cluster in three steps with
        the first successful connection attempt becoming the mode of communication with
        a cluster:

        1. It will first attempt to use a `KubernetesCredentials` block's
        `cluster_config` to configure a client using
        `KubernetesClusterConfig.configure_client`.

        2. Attempt in-cluster connection (will only work when running on a pod).

        3. Attempt out-of-cluster connection using the default location for a
        kube config file.

        Args:
            client_type: The Kubernetes API client type for interacting with specific
                Kubernetes resources.

        Returns:
            KubernetesClient: An authenticated, resource-specific Kubernetes Client.

        Raises:
            ValueError: If `client_type` is not a valid Kubernetes API client type.
        """

        if self.cluster_config:
            await self.cluster_config.configure_client()
        else:
            try:
                config.load_incluster_config()
            except ConfigException:
                await config.load_kube_config()

        try:
            return K8S_CLIENT_TYPES[client_type](api_client)
        except KeyError:
            raise ValueError(
                f"Invalid client type provided '{client_type}'."
                f" Must be one of {listrepr(K8S_CLIENT_TYPES.keys())}."
            )

get_client(client_type, configuration=None) async

Convenience method for retrieving a Kubernetes API client for deployment resources.

Parameters:

Name Type Description Default
client_type Literal['apps', 'batch', 'core', 'custom_objects']

The resource-specific type of Kubernetes client to retrieve.

required

Yields:

Type Description
AsyncGenerator[KubernetesClient, None]

An authenticated, resource-specific Kubernetes API client.

Example
from prefect_kubernetes.credentials import KubernetesCredentials

async with KubernetesCredentials.get_client("core") as core_v1_client:
    pods = await core_v1_client.list_namespaced_pod()
    for pod in pods.items:
        print(pod.metadata.name)
Source code in prefect_kubernetes/credentials.py
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
@asynccontextmanager
async def get_client(
    self,
    client_type: Literal["apps", "batch", "core", "custom_objects"],
    configuration: Optional[Configuration] = None,
) -> AsyncGenerator[KubernetesClient, None]:
    """Convenience method for retrieving a Kubernetes API client for deployment resources.

    Args:
        client_type: The resource-specific type of Kubernetes client to retrieve.

    Yields:
        An authenticated, resource-specific Kubernetes API client.

    Example:
        ```python
        from prefect_kubernetes.credentials import KubernetesCredentials

        async with KubernetesCredentials.get_client("core") as core_v1_client:
            pods = await core_v1_client.list_namespaced_pod()
            for pod in pods.items:
                print(pod.metadata.name)
        ```
    """
    client_configuration = configuration or Configuration()
    if self.cluster_config:
        config_dict = self.cluster_config.config
        context = self.cluster_config.context_name

        # Use Configuration to load configuration from a dictionary
        await config.load_kube_config_from_dict(
            config_dict=config_dict,
            context=context,
            client_configuration=client_configuration,
        )
    else:
        try:
            config.load_incluster_config(client_configuration=client_configuration)
        except config.ConfigException:
            # If in-cluster config fails, load the local kubeconfig
            await config.load_kube_config(
                client_configuration=client_configuration,
            )
    async with ApiClient(configuration=client_configuration) as api_client:
        try:
            yield await self.get_resource_specific_client(
                client_type, api_client=api_client
            )
        finally:
            await api_client.close()

get_resource_specific_client(client_type, api_client) async

Utility function for configuring a generic Kubernetes client. It will attempt to connect to a Kubernetes cluster in three steps with the first successful connection attempt becoming the mode of communication with a cluster:

  1. It will first attempt to use a KubernetesCredentials block's cluster_config to configure a client using KubernetesClusterConfig.configure_client.

  2. Attempt in-cluster connection (will only work when running on a pod).

  3. Attempt out-of-cluster connection using the default location for a kube config file.

Parameters:

Name Type Description Default
client_type str

The Kubernetes API client type for interacting with specific Kubernetes resources.

required

Returns:

Name Type Description
KubernetesClient Union[AppsV1Api, BatchV1Api, CoreV1Api]

An authenticated, resource-specific Kubernetes Client.

Raises:

Type Description
ValueError

If client_type is not a valid Kubernetes API client type.

Source code in prefect_kubernetes/credentials.py
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
async def get_resource_specific_client(
    self,
    client_type: str,
    api_client: ApiClient,
) -> Union[AppsV1Api, BatchV1Api, CoreV1Api]:
    """
    Utility function for configuring a generic Kubernetes client.
    It will attempt to connect to a Kubernetes cluster in three steps with
    the first successful connection attempt becoming the mode of communication with
    a cluster:

    1. It will first attempt to use a `KubernetesCredentials` block's
    `cluster_config` to configure a client using
    `KubernetesClusterConfig.configure_client`.

    2. Attempt in-cluster connection (will only work when running on a pod).

    3. Attempt out-of-cluster connection using the default location for a
    kube config file.

    Args:
        client_type: The Kubernetes API client type for interacting with specific
            Kubernetes resources.

    Returns:
        KubernetesClient: An authenticated, resource-specific Kubernetes Client.

    Raises:
        ValueError: If `client_type` is not a valid Kubernetes API client type.
    """

    if self.cluster_config:
        await self.cluster_config.configure_client()
    else:
        try:
            config.load_incluster_config()
        except ConfigException:
            await config.load_kube_config()

    try:
        return K8S_CLIENT_TYPES[client_type](api_client)
    except KeyError:
        raise ValueError(
            f"Invalid client type provided '{client_type}'."
            f" Must be one of {listrepr(K8S_CLIENT_TYPES.keys())}."
        )