Pooyan
02/12/2024, 5:18 PMfrom prefect import flow
from prefect_kubernetes.credentials import KubernetesCredentials
from prefect_kubernetes.custom_objects import get_namespaced_custom_object
@flow
def kubernetes_orchestrator():
custom_object_metadata = get_namespaced_custom_object(
kubernetes_credentials=KubernetesCredentials.load("k8s-creds"),
group="crd-group",
version="crd-version",
plural="crd-plural",
name="my-custom-object",
)
Marvin
02/12/2024, 5:19 PMkind
of the Kubernetes Custom Resource Definition (CRD). For example, if the kind
is Pod
, the plural
might be pods
.
`group`: This is the name of the API group that the Custom Resource Definition (CRD) belongs to. Kubernetes supports multiple API groups to extend its functionality, and CRDs can be a part of custom groups.
As for an example, suppose you have a Kubernetes cluster with a custom resource of kind MyCustomResource
and you have defined a CRD for it. Let's also assume your CRD is within the group <http://example.com|example.com>
and the version is v1alpha1
. The plural
might be defined as mycustomresources
because you typically access multiple instances of the resource at a time.
Your Python code would look something like this:
python
from prefect import flow
from prefect_kubernetes.credentials import KubernetesCredentials
from prefect_kubernetes.custom_objects import get_namespaced_custom_object
@flow
def kubernetes_orchestrator():
custom_object_metadata = get_namespaced_custom_object(
kubernetes_credentials=KubernetesCredentials.load("k8s-creds"),
group="<http://example.com|example.com>",
version="v1alpha1",
plural="mycustomresources",
name="my-custom-object-name",
)
Here, mycustomresources
is the plural
indicating how you refer to the resources in API calls, and <http://example.com|example.com>
is the group
specifying the grouping under the Kubernetes API groups. my-custom-object-name
is the specific name of the custom object instance you want to retrieve.