Use with_options to customize options on any existing task or flow¶
fromprefect_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 called
For more tips on how to use tasks and flows in a Collection, check out Using Collections!
Specify and run a Kubernetes Job from a yaml file¶
fromprefect_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 from KubernetesClusterConfig¶
# 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)