Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create GKESuspendJobOperator and GKEResumeJobOperator operators #38677

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 210 additions & 1 deletion airflow/providers/google/cloud/operators/kubernetes_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from deprecated import deprecated
from google.api_core.exceptions import AlreadyExists
from google.cloud.container_v1.types import Cluster
from kubernetes.client import V1JobList
from kubernetes.client import V1JobList, models as k8s
from kubernetes.utils.create_from_yaml import FailToCreateError
from packaging.version import parse as parse_version

Expand All @@ -47,6 +47,7 @@
GKEDeploymentHook,
GKEHook,
GKEJobHook,
GKEKubernetesHook,
GKEPodHook,
)
from airflow.providers.google.cloud.links.kubernetes_engine import (
Expand Down Expand Up @@ -1494,3 +1495,211 @@ def execute(self, context: Context):
).fetch_cluster_info()

return super().execute(context)


class GKESuspendJobOperator(GoogleCloudBaseOperator):
"""
Suspend Job by given name.

.. seealso::
For more information on how to use this operator, take a look at the guide:
:ref:`howto/operator:GKESuspendJobOperator`

:param name: The name of the Job to suspend
:param project_id: The Google Developers Console project id.
:param location: The name of the Google Kubernetes Engine zone or region in which the cluster
resides.
:param cluster_name: The name of the Google Kubernetes Engine cluster.
:param namespace: The name of the Google Kubernetes Engine namespace.
:param use_internal_ip: Use the internal IP address as the endpoint.
:param gcp_conn_id: The connection ID to use connecting to Google Cloud.
:param impersonation_chain: Optional service account to impersonate using short-term
credentials, or chained list of accounts required to get the access_token
of the last account in the list, which will be impersonated in the request.
If set as a string, the account must grant the originating account
the Service Account Token Creator IAM role.
If set as a sequence, the identities from the list must grant
Service Account Token Creator IAM role to the directly preceding identity, with first
account from the list granting this role to the originating account (templated).
"""

template_fields: Sequence[str] = (
"project_id",
"gcp_conn_id",
"name",
"namespace",
"cluster_name",
"location",
"impersonation_chain",
)
operator_extra_links = (KubernetesEngineJobLink(),)

def __init__(
self,
*,
name: str,
location: str,
namespace: str,
cluster_name: str,
project_id: str | None = None,
use_internal_ip: bool = False,
gcp_conn_id: str = "google_cloud_default",
impersonation_chain: str | Sequence[str] | None = None,
**kwargs,
) -> None:
super().__init__(**kwargs)

self.project_id = project_id
self.gcp_conn_id = gcp_conn_id
self.location = location
self.name = name
self.namespace = namespace
self.cluster_name = cluster_name
self.use_internal_ip = use_internal_ip
self.impersonation_chain = impersonation_chain

self.job: V1Job | None = None
self._ssl_ca_cert: str
self._cluster_url: str

@cached_property
def cluster_hook(self) -> GKEHook:
return GKEHook(
gcp_conn_id=self.gcp_conn_id,
location=self.location,
impersonation_chain=self.impersonation_chain,
)

@cached_property
def hook(self) -> GKEKubernetesHook:
self._cluster_url, self._ssl_ca_cert = GKEClusterAuthDetails(
cluster_name=self.cluster_name,
project_id=self.project_id,
use_internal_ip=self.use_internal_ip,
cluster_hook=self.cluster_hook,
).fetch_cluster_info()

return GKEKubernetesHook(
gcp_conn_id=self.gcp_conn_id,
cluster_url=self._cluster_url,
ssl_ca_cert=self._ssl_ca_cert,
)

def execute(self, context: Context) -> None:
self.job = self.hook.patch_namespaced_job(
job_name=self.name,
namespace=self.namespace,
body={"spec": {"suspend": True}},
)
self.log.info(
"Job %s from cluster %s was suspended.",
self.name,
self.cluster_name,
)
KubernetesEngineJobLink.persist(context=context, task_instance=self)

return k8s.V1Job.to_dict(self.job)


class GKEResumeJobOperator(GoogleCloudBaseOperator):
"""
Resume Job by given name.

.. seealso::
For more information on how to use this operator, take a look at the guide:
:ref:`howto/operator:GKEResumeJobOperator`

:param name: The name of the Job to resume
:param project_id: The Google Developers Console project id.
:param location: The name of the Google Kubernetes Engine zone or region in which the cluster
resides.
:param cluster_name: The name of the Google Kubernetes Engine cluster.
:param namespace: The name of the Google Kubernetes Engine namespace.
:param use_internal_ip: Use the internal IP address as the endpoint.
:param gcp_conn_id: The connection ID to use connecting to Google Cloud.
:param impersonation_chain: Optional service account to impersonate using short-term
credentials, or chained list of accounts required to get the access_token
of the last account in the list, which will be impersonated in the request.
If set as a string, the account must grant the originating account
the Service Account Token Creator IAM role.
If set as a sequence, the identities from the list must grant
Service Account Token Creator IAM role to the directly preceding identity, with first
account from the list granting this role to the originating account (templated).
"""

template_fields: Sequence[str] = (
"project_id",
"gcp_conn_id",
"name",
"namespace",
"cluster_name",
"location",
"impersonation_chain",
)
operator_extra_links = (KubernetesEngineJobLink(),)

def __init__(
self,
*,
name: str,
location: str,
namespace: str,
cluster_name: str,
project_id: str | None = None,
use_internal_ip: bool = False,
gcp_conn_id: str = "google_cloud_default",
impersonation_chain: str | Sequence[str] | None = None,
**kwargs,
) -> None:
super().__init__(**kwargs)

self.project_id = project_id
self.gcp_conn_id = gcp_conn_id
self.location = location
self.name = name
self.namespace = namespace
self.cluster_name = cluster_name
self.use_internal_ip = use_internal_ip
self.impersonation_chain = impersonation_chain

self.job: V1Job | None = None
self._ssl_ca_cert: str
self._cluster_url: str

@cached_property
def cluster_hook(self) -> GKEHook:
return GKEHook(
gcp_conn_id=self.gcp_conn_id,
location=self.location,
impersonation_chain=self.impersonation_chain,
)

@cached_property
def hook(self) -> GKEKubernetesHook:
self._cluster_url, self._ssl_ca_cert = GKEClusterAuthDetails(
cluster_name=self.cluster_name,
project_id=self.project_id,
use_internal_ip=self.use_internal_ip,
cluster_hook=self.cluster_hook,
).fetch_cluster_info()

return GKEKubernetesHook(
gcp_conn_id=self.gcp_conn_id,
cluster_url=self._cluster_url,
ssl_ca_cert=self._ssl_ca_cert,
)

def execute(self, context: Context) -> None:
self.job = self.hook.patch_namespaced_job(
job_name=self.name,
namespace=self.namespace,
body={"spec": {"suspend": False}},
)
self.log.info(
"Job %s from cluster %s was resumed.",
self.name,
self.cluster_name,
)
KubernetesEngineJobLink.persist(context=context, task_instance=self)

return k8s.V1Job.to_dict(self.job)
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,36 @@ delete resource in the specified Google Kubernetes Engine cluster.
:start-after: [START howto_operator_gke_delete_resource]
:end-before: [END howto_operator_gke_delete_resource]


.. _howto/operator:GKESuspendJobOperator:

Suspend a Job on a GKE cluster
""""""""""""""""""""""""""""""

You can use :class:`~airflow.providers.google.cloud.operators.kubernetes_engine.GKESuspendJobOperator` to
suspend Job in the specified Google Kubernetes Engine cluster.

.. exampleinclude:: /../../tests/system/providers/google/cloud/kubernetes_engine/example_kubernetes_engine_job.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gke_suspend_job]
:end-before: [END howto_operator_gke_suspend_job]


.. _howto/operator:GKEResumeJobOperator:

Resume a Job on a GKE cluster
"""""""""""""""""""""""""""""

You can use :class:`~airflow.providers.google.cloud.operators.kubernetes_engine.GKEResumeJobOperator` to
resume Job in the specified Google Kubernetes Engine cluster.

.. exampleinclude:: /../../tests/system/providers/google/cloud/kubernetes_engine/example_kubernetes_engine_job.py
:language: python
:dedent: 4
:start-after: [START howto_operator_gke_resume_job]
:end-before: [END howto_operator_gke_resume_job]

Reference
^^^^^^^^^

Expand Down
Loading