-
Notifications
You must be signed in to change notification settings - Fork 14.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add links for Google Kubernetes Engine operators (#24786)
* Add links for Google Kubernetes Engine operators * Update unit tests for GKE operators
- Loading branch information
1 parent
46bbfda
commit fb71624
Showing
4 changed files
with
106 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import json | ||
from typing import TYPE_CHECKING, Dict, Union | ||
|
||
from google.cloud.container_v1.types import Cluster | ||
|
||
from airflow.providers.google.cloud.links.base import BaseGoogleLink | ||
|
||
if TYPE_CHECKING: | ||
from airflow.utils.context import Context | ||
|
||
KUBERNETES_BASE_LINK = "https://console.cloud.google.com/kubernetes" | ||
KUBERNETES_CLUSTER_LINK = ( | ||
KUBERNETES_BASE_LINK + "/clusters/details/{location}/{cluster_name}/details?project={project_id}" | ||
) | ||
KUBERNETES_POD_LINK = ( | ||
KUBERNETES_BASE_LINK | ||
+ "/pod/{location}/{cluster_name}/{namespace}/{pod_name}/details?project={project_id}" | ||
) | ||
|
||
|
||
class KubernetesEngineClusterLink(BaseGoogleLink): | ||
"""Helper class for constructing Kubernetes Engine Cluster Link""" | ||
|
||
name = "Kubernetes Cluster" | ||
key = "kubernetes_cluster_conf" | ||
format_str = KUBERNETES_CLUSTER_LINK | ||
|
||
@staticmethod | ||
def persist(context: "Context", task_instance, cluster: Union[Dict, Cluster, None]): | ||
if isinstance(cluster, dict): | ||
cluster = Cluster.from_json(json.dumps(cluster)) | ||
|
||
task_instance.xcom_push( | ||
context=context, | ||
key=KubernetesEngineClusterLink.key, | ||
value={ | ||
"location": task_instance.location, | ||
"cluster_name": cluster.name, # type: ignore | ||
"project_id": task_instance.project_id, | ||
}, | ||
) | ||
|
||
|
||
class KubernetesEnginePodLink(BaseGoogleLink): | ||
"""Helper class for constructing Kubernetes Engine Pod Link""" | ||
|
||
name = "Kubernetes Pod" | ||
key = "kubernetes_pod_conf" | ||
format_str = KUBERNETES_POD_LINK | ||
|
||
@staticmethod | ||
def persist( | ||
context: "Context", | ||
task_instance, | ||
): | ||
task_instance.xcom_push( | ||
context=context, | ||
key=KubernetesEnginePodLink.key, | ||
value={ | ||
"location": task_instance.location, | ||
"cluster_name": task_instance.cluster_name, | ||
"namespace": task_instance.pod.metadata.namespace, | ||
"pod_name": task_instance.pod.metadata.name, | ||
"project_id": task_instance.project_id, | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,7 +77,7 @@ def test_create_execute(self, body, mock_hook): | |
project_id=TEST_GCP_PROJECT_ID, location=PROJECT_LOCATION, body=body, task_id=PROJECT_TASK_ID | ||
) | ||
|
||
operator.execute(None) | ||
operator.execute(context=mock.MagicMock()) | ||
mock_hook.return_value.create_cluster.assert_called_once_with( | ||
cluster=body, project_id=TEST_GCP_PROJECT_ID | ||
) | ||
|
@@ -191,6 +191,10 @@ def setUp(self): | |
namespace=NAMESPACE, | ||
image=IMAGE, | ||
) | ||
self.gke_op.pod = mock.MagicMock( | ||
name=TASK_NAME, | ||
namespace=NAMESPACE, | ||
) | ||
|
||
def test_template_fields(self): | ||
assert set(KubernetesPodOperator.template_fields).issubset(GKEStartPodOperator.template_fields) | ||
|
@@ -215,7 +219,7 @@ def test_execute(self, file_mock, mock_execute_in_subprocess, mock_gcp_hook, exe | |
side_effect=[FILE_NAME, '/path/to/new-file'] | ||
) | ||
|
||
self.gke_op.execute(None) | ||
self.gke_op.execute(context=mock.MagicMock()) | ||
|
||
mock_gcp_hook.return_value.provide_authorized_gcloud.assert_called_once() | ||
|
||
|
@@ -258,7 +262,7 @@ def test_execute_regional( | |
side_effect=[FILE_NAME, '/path/to/new-file'] | ||
) | ||
|
||
self.gke_op.execute(None) | ||
self.gke_op.execute(context=mock.MagicMock()) | ||
|
||
mock_gcp_hook.return_value.provide_authorized_gcloud.assert_called_once() | ||
|
||
|
@@ -314,7 +318,7 @@ def test_execute_with_internal_ip( | |
side_effect=[FILE_NAME, '/path/to/new-file'] | ||
) | ||
|
||
self.gke_op.execute(None) | ||
self.gke_op.execute(context=mock.MagicMock()) | ||
|
||
mock_gcp_hook.return_value.provide_authorized_gcloud.assert_called_once() | ||
|
||
|
@@ -357,7 +361,7 @@ def test_execute_with_impersonation_service_account( | |
side_effect=[FILE_NAME, '/path/to/new-file'] | ||
) | ||
self.gke_op.impersonation_chain = "[email protected]" | ||
self.gke_op.execute(None) | ||
self.gke_op.execute(context=mock.MagicMock()) | ||
|
||
mock_gcp_hook.return_value.provide_authorized_gcloud.assert_called_once() | ||
|
||
|
@@ -401,7 +405,7 @@ def test_execute_with_impersonation_service_chain_one_element( | |
side_effect=[FILE_NAME, '/path/to/new-file'] | ||
) | ||
self.gke_op.impersonation_chain = ["[email protected]"] | ||
self.gke_op.execute(None) | ||
self.gke_op.execute(context=mock.MagicMock()) | ||
|
||
mock_gcp_hook.return_value.provide_authorized_gcloud.assert_called_once() | ||
|
||
|