Package: prefect-kubernetes
prefect-kubernetesWelcome!prefect-kubernetesis a collection of Prefect tasks, flows, and blocks enabling orchestration, observation and management of Kubernetes resources.Jump toexamples.ResourcesFor more tips on how to use tasks and flows in a Collection, check outUsing Collections!InstallationInstallprefect-kuberneteswithpip:pipinstallprefect-kubernetesRequires 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 thePrefect documentation.Then, to registerblockson Prefect Cloud:prefectblockregister-mprefect_kubernetesNote, to use theloadmethod on Blocks, you must already have a block documentsaved through codeor saved through the UI.Example UsageUsewith_optionsto customize options on any existing task or flowfromprefect_kubernetes.flowsimportrun_namespaced_jobcustomized_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 calledFor more tips on how to use tasks and flows in a Collection, check outUsing Collections!Specify and run a Kubernetes Job from a yaml filefromprefect_kubernetes.credentialsimportKubernetesCredentialsfromprefect_kubernetes.flowsimportrun_namespaced_job# this is a flowfromprefect_kubernetes.jobsimportKubernetesJobk8s_creds=KubernetesCredentials.load("k8s-creds")job=KubernetesJob.from_yaml_file(# or create in the UI with a dict manifestcredentials=k8s_creds,manifest_path="path/to/job.yaml",)job.save("my-k8s-job",overwrite=True)if__name__=="__main__":# run the flowrun_namespaced_job(job)Generate a resource-specific client fromKubernetesClusterConfig# with minikube / docker desktop & a valid ~/.kube/config this should ~just work~fromprefect.blocks.kubernetesimportKubernetesClusterConfigfromprefect_kubernetes.credentialsimportKubernetesCredentialsk8s_config=KubernetesClusterConfig.from_file('~/.kube/config')k8s_credentials=KubernetesCredentials(cluster_config=k8s_config)withk8s_credentials.get_client("core")asv1_core_client:fornamespaceinv1_core_client.list_namespace().items:print(namespace.metadata.name)List jobs in a specific namespacefromprefectimportflowfromprefect_kubernetes.credentialsimportKubernetesCredentialsfromprefect_kubernetes.jobsimportlist_namespaced_job@flowdefkubernetes_orchestrator():v1_job_list=list_namespaced_job(kubernetes_credentials=KubernetesCredentials.load("k8s-creds"),namespace="my-namespace",)Patch an existing deploymentfromkubernetes.client.modelsimportV1Deploymentfromprefectimportflowfromprefect_kubernetes.credentialsimportKubernetesCredentialsfromprefect_kubernetes.deploymentsimportpatch_namespaced_deploymentfromprefect_kubernetes.utilitiesimportconvert_manifest_to_model@flowdefkubernetes_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")FeedbackIf you encounter any bugs while usingprefect-kubernetes, feel free to open an issue in theprefect-kubernetesrepository.If you have any questions or issues while usingprefect-kubernetes, you can find help in either thePrefect Discourse forumor thePrefect Slack community.Feel free to star or watchprefect-kubernetesfor updates too!ContributingIf you'd like to help contribute to fix an issue or add a feature toprefect-kubernetes, pleasepropose changes through a pull request from a fork of the repository.Here are the steps:Fork the repositoryClone the forked repositoryInstall the repository and its dependencies:pip install -e ".[dev]"Make desired changesAdd testsInsert an entry toCHANGELOG.mdInstallpre-committo perform quality checks prior to commit:pre-commit installgit commit,git push, and create a pull request