prefect-kubernetes


Nameprefect-kubernetes JSON
Version 0.3.8 PyPI version JSON
download
home_pageNone
SummaryPrefect integrations for interacting with Kubernetes.
upload_time2024-04-25 19:21:31
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseApache License 2.0
keywords prefect
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # prefect-kubernetes

<p align="center">
    <a href="https://pypi.python.org/pypi/prefect-kubernetes/" alt="PyPI version">
        <img alt="PyPI" src="https://img.shields.io/pypi/v/prefect-kubernetes?color=26272B&labelColor=090422"></a>
    <a href="https://pypistats.org/packages/prefect-kubernetes/" alt="Downloads">
        <img src="https://img.shields.io/pypi/dm/prefect-kubernetes?color=26272B&labelColor=090422" /></a>
</p>


## Welcome!

`prefect-kubernetes` is a collection of Prefect tasks, flows, and blocks enabling orchestration, observation and management of Kubernetes resources.

Jump to [examples](#example-usage).

## Resources

For more tips on how to use tasks and flows in a Collection, check out [Using Collections](https://docs.prefect.io/collections/usage/)!

### Installation
Install `prefect-kubernetes` with `pip`:
```bash
 pip install prefect-kubernetes
 ```

Requires an installation of Python 3.8+.

We recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.

These tasks are designed to work with Prefect 2. For more information about how to use Prefect, please refer to the [Prefect documentation](https://docs.prefect.io/).

Then, to register [blocks](https://docs.prefect.io/ui/blocks/) on Prefect Cloud:

```bash
prefect block register -m prefect_kubernetes
```

Note, to use the `load` method on Blocks, you must already have a block document [saved through code](https://docs.prefect.io/concepts/blocks/#saving-blocks) or saved through the UI.


### Example Usage

#### Use `with_options` to customize options on any existing task or flow

```python
from prefect_kubernetes.flows import run_namespaced_job

customized_run_namespaced_job = run_namespaced_job.with_options(
    name="My flow running a Kubernetes Job",
    retries=2,
    retry_delay_seconds=10,
) # this is now a new flow object that can be called
```

For more tips on how to use tasks and flows in a Collection, check out [Using Collections](https://docs.prefect.io/collections/usage/)!


#### Specify and run a Kubernetes Job from a yaml file

```python
from prefect_kubernetes.credentials import KubernetesCredentials
from prefect_kubernetes.flows import run_namespaced_job # this is a flow
from prefect_kubernetes.jobs import KubernetesJob

k8s_creds = KubernetesCredentials.load("k8s-creds")

job = KubernetesJob.from_yaml_file( # or create in the UI with a dict manifest
    credentials=k8s_creds,
    manifest_path="path/to/job.yaml",
)

job.save("my-k8s-job", overwrite=True)

if __name__ == "__main__":
    # run the flow
    run_namespaced_job(job)
```

#### Generate a resource-specific client from `KubernetesClusterConfig`

```python
# with minikube / docker desktop & a valid ~/.kube/config this should ~just work~
from prefect.blocks.kubernetes import KubernetesClusterConfig
from prefect_kubernetes.credentials import KubernetesCredentials

k8s_config = KubernetesClusterConfig.from_file('~/.kube/config')

k8s_credentials = KubernetesCredentials(cluster_config=k8s_config)

with k8s_credentials.get_client("core") as v1_core_client:
    for namespace in v1_core_client.list_namespace().items:
        print(namespace.metadata.name)
```


#### List jobs in a specific namespace

```python
from prefect import flow
from prefect_kubernetes.credentials import KubernetesCredentials
from prefect_kubernetes.jobs import list_namespaced_job

@flow
def kubernetes_orchestrator():
    v1_job_list = list_namespaced_job(
        kubernetes_credentials=KubernetesCredentials.load("k8s-creds"),
        namespace="my-namespace",
    )
```

#### Patch an existing deployment

```python
from kubernetes.client.models import V1Deployment

from prefect import flow
from prefect_kubernetes.credentials import KubernetesCredentials
from prefect_kubernetes.deployments import patch_namespaced_deployment
from prefect_kubernetes.utilities import convert_manifest_to_model

@flow
def kubernetes_orchestrator():

    v1_deployment_updates = convert_manifest_to_model(
        manifest="path/to/manifest.yaml",
        v1_model_name="V1Deployment",
    )

    v1_deployment = patch_namespaced_deployment(
        kubernetes_credentials=KubernetesCredentials.load("k8s-creds"),
        deployment_name="my-deployment",
        deployment_updates=v1_deployment_updates,
        namespace="my-namespace"
    )
```

## Feedback

If you encounter any bugs while using `prefect-kubernetes`, feel free to open an issue in the [prefect](https://github.com/PrefectHQ/prefect) repository.

If you have any questions or issues while using `prefect-kubernetes`, you can find help in either the [Prefect Discourse forum](https://discourse.prefect.io/) or the [Prefect Slack community](https://prefect.io/slack).

## Contributing

If you'd like to help contribute to fix an issue or add a feature to `prefect-kubernetes`, please [propose changes through a pull request from a fork of the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork).
 
Here are the steps:
 
1. [Fork the repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#forking-a-repository)
2. [Clone the forked repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#cloning-your-forked-repository)
3. Install the repository and its dependencies:
```
 pip install -e ".[dev]"
```
4. Make desired changes
5. Add tests
6. Install `pre-commit` to perform quality checks prior to commit:
```
 pre-commit install
 ```
8. `git commit`, `git push`, and create a pull request

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "prefect-kubernetes",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "prefect",
    "author": null,
    "author_email": "\"Prefect Technologies, Inc.\" <help@prefect.io>",
    "download_url": "https://files.pythonhosted.org/packages/f6/4b/4da3a9084805f9cea42087c8f541fefbf93de237e3c3a30ceceea743eaa5/prefect_kubernetes-0.3.8.tar.gz",
    "platform": null,
    "description": "# prefect-kubernetes\n\n<p align=\"center\">\n    <a href=\"https://pypi.python.org/pypi/prefect-kubernetes/\" alt=\"PyPI version\">\n        <img alt=\"PyPI\" src=\"https://img.shields.io/pypi/v/prefect-kubernetes?color=26272B&labelColor=090422\"></a>\n    <a href=\"https://pypistats.org/packages/prefect-kubernetes/\" alt=\"Downloads\">\n        <img src=\"https://img.shields.io/pypi/dm/prefect-kubernetes?color=26272B&labelColor=090422\" /></a>\n</p>\n\n\n## Welcome!\n\n`prefect-kubernetes` is a collection of Prefect tasks, flows, and blocks enabling orchestration, observation and management of Kubernetes resources.\n\nJump to [examples](#example-usage).\n\n## Resources\n\nFor more tips on how to use tasks and flows in a Collection, check out [Using Collections](https://docs.prefect.io/collections/usage/)!\n\n### Installation\nInstall `prefect-kubernetes` with `pip`:\n```bash\n pip install prefect-kubernetes\n ```\n\nRequires an installation of Python 3.8+.\n\nWe recommend using a Python virtual environment manager such as pipenv, conda or virtualenv.\n\nThese tasks are designed to work with Prefect 2. For more information about how to use Prefect, please refer to the [Prefect documentation](https://docs.prefect.io/).\n\nThen, to register [blocks](https://docs.prefect.io/ui/blocks/) on Prefect Cloud:\n\n```bash\nprefect block register -m prefect_kubernetes\n```\n\nNote, to use the `load` method on Blocks, you must already have a block document [saved through code](https://docs.prefect.io/concepts/blocks/#saving-blocks) or saved through the UI.\n\n\n### Example Usage\n\n#### Use `with_options` to customize options on any existing task or flow\n\n```python\nfrom prefect_kubernetes.flows import run_namespaced_job\n\ncustomized_run_namespaced_job = run_namespaced_job.with_options(\n    name=\"My flow running a Kubernetes Job\",\n    retries=2,\n    retry_delay_seconds=10,\n) # this is now a new flow object that can be called\n```\n\nFor more tips on how to use tasks and flows in a Collection, check out [Using Collections](https://docs.prefect.io/collections/usage/)!\n\n\n#### Specify and run a Kubernetes Job from a yaml file\n\n```python\nfrom prefect_kubernetes.credentials import KubernetesCredentials\nfrom prefect_kubernetes.flows import run_namespaced_job # this is a flow\nfrom prefect_kubernetes.jobs import KubernetesJob\n\nk8s_creds = KubernetesCredentials.load(\"k8s-creds\")\n\njob = KubernetesJob.from_yaml_file( # or create in the UI with a dict manifest\n    credentials=k8s_creds,\n    manifest_path=\"path/to/job.yaml\",\n)\n\njob.save(\"my-k8s-job\", overwrite=True)\n\nif __name__ == \"__main__\":\n    # run the flow\n    run_namespaced_job(job)\n```\n\n#### Generate a resource-specific client from `KubernetesClusterConfig`\n\n```python\n# with minikube / docker desktop & a valid ~/.kube/config this should ~just work~\nfrom prefect.blocks.kubernetes import KubernetesClusterConfig\nfrom prefect_kubernetes.credentials import KubernetesCredentials\n\nk8s_config = KubernetesClusterConfig.from_file('~/.kube/config')\n\nk8s_credentials = KubernetesCredentials(cluster_config=k8s_config)\n\nwith k8s_credentials.get_client(\"core\") as v1_core_client:\n    for namespace in v1_core_client.list_namespace().items:\n        print(namespace.metadata.name)\n```\n\n\n#### List jobs in a specific namespace\n\n```python\nfrom prefect import flow\nfrom prefect_kubernetes.credentials import KubernetesCredentials\nfrom prefect_kubernetes.jobs import list_namespaced_job\n\n@flow\ndef kubernetes_orchestrator():\n    v1_job_list = list_namespaced_job(\n        kubernetes_credentials=KubernetesCredentials.load(\"k8s-creds\"),\n        namespace=\"my-namespace\",\n    )\n```\n\n#### Patch an existing deployment\n\n```python\nfrom kubernetes.client.models import V1Deployment\n\nfrom prefect import flow\nfrom prefect_kubernetes.credentials import KubernetesCredentials\nfrom prefect_kubernetes.deployments import patch_namespaced_deployment\nfrom prefect_kubernetes.utilities import convert_manifest_to_model\n\n@flow\ndef kubernetes_orchestrator():\n\n    v1_deployment_updates = convert_manifest_to_model(\n        manifest=\"path/to/manifest.yaml\",\n        v1_model_name=\"V1Deployment\",\n    )\n\n    v1_deployment = patch_namespaced_deployment(\n        kubernetes_credentials=KubernetesCredentials.load(\"k8s-creds\"),\n        deployment_name=\"my-deployment\",\n        deployment_updates=v1_deployment_updates,\n        namespace=\"my-namespace\"\n    )\n```\n\n## Feedback\n\nIf you encounter any bugs while using `prefect-kubernetes`, feel free to open an issue in the [prefect](https://github.com/PrefectHQ/prefect) repository.\n\nIf you have any questions or issues while using `prefect-kubernetes`, you can find help in either the [Prefect Discourse forum](https://discourse.prefect.io/) or the [Prefect Slack community](https://prefect.io/slack).\n\n## Contributing\n\nIf you'd like to help contribute to fix an issue or add a feature to `prefect-kubernetes`, please [propose changes through a pull request from a fork of the repository](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request-from-a-fork).\n \nHere are the steps:\n \n1. [Fork the repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#forking-a-repository)\n2. [Clone the forked repository](https://docs.github.com/en/get-started/quickstart/fork-a-repo#cloning-your-forked-repository)\n3. Install the repository and its dependencies:\n```\n pip install -e \".[dev]\"\n```\n4. Make desired changes\n5. Add tests\n6. Install `pre-commit` to perform quality checks prior to commit:\n```\n pre-commit install\n ```\n8. `git commit`, `git push`, and create a pull request\n",
    "bugtrack_url": null,
    "license": "Apache License 2.0",
    "summary": "Prefect integrations for interacting with Kubernetes.",
    "version": "0.3.8",
    "project_urls": {
        "Homepage": "https://github.com/PrefectHQ/prefect/tree/main/src/integrations/prefect-kubernetes"
    },
    "split_keywords": [
        "prefect"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "12c70e68099de0d939fd4cf803d2961b719db4e2efe5d746d7a3308827f41579",
                "md5": "561f456f267de7d84e0bb45a878dc3fe",
                "sha256": "3638974ba5784aefa67bd6ab1e48cd0094b147133400cfcc4a491fbc258c190c"
            },
            "downloads": -1,
            "filename": "prefect_kubernetes-0.3.8-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "561f456f267de7d84e0bb45a878dc3fe",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 33964,
            "upload_time": "2024-04-25T19:21:27",
            "upload_time_iso_8601": "2024-04-25T19:21:27.204743Z",
            "url": "https://files.pythonhosted.org/packages/12/c7/0e68099de0d939fd4cf803d2961b719db4e2efe5d746d7a3308827f41579/prefect_kubernetes-0.3.8-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "f64b4da3a9084805f9cea42087c8f541fefbf93de237e3c3a30ceceea743eaa5",
                "md5": "0f0357c4169eb465ce2fde311f427a95",
                "sha256": "0af41abc1e5c1dbf22a7ac98fc7f1e033739d266b6b91e4f187bc1547faf6249"
            },
            "downloads": -1,
            "filename": "prefect_kubernetes-0.3.8.tar.gz",
            "has_sig": false,
            "md5_digest": "0f0357c4169eb465ce2fde311f427a95",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 52887,
            "upload_time": "2024-04-25T19:21:31",
            "upload_time_iso_8601": "2024-04-25T19:21:31.602351Z",
            "url": "https://files.pythonhosted.org/packages/f6/4b/4da3a9084805f9cea42087c8f541fefbf93de237e3c3a30ceceea743eaa5/prefect_kubernetes-0.3.8.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-25 19:21:31",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "PrefectHQ",
    "github_project": "prefect",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "circle": true,
    "requirements": [],
    "lcname": "prefect-kubernetes"
}
        
Elapsed time: 0.24009s