From d3a2dbe028e4a8c2da9fcb2b3257e2117c413a73 Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Mon, 15 May 2023 13:34:56 -0700 Subject: [PATCH] GKEPodHook needs to have all methods KPO calls (#31266) From time to time methods are added to KubernetesHook for use with KPO and we neglect to add them to GKEPodHook, thus breaking GKEStartPodOperator. Here I add missing methods and a test to ensure that the two classes stay consistent in the ways necessary. It might be nice to make a common interface such as KubernetesPodOperatorHookInterface which would define the necessary methods, but that is deferred for another day. For now we simply update GKEPodHook and at an easy test to verify consistency. --- .../google/cloud/hooks/kubernetes_engine.py | 21 +++++++--- tests/ast_helpers.py | 36 +++++++++++++++++ .../cloud/hooks/test_kubernetes_engine.py | 39 ++++++++++++++++++- 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 tests/ast_helpers.py diff --git a/airflow/providers/google/cloud/hooks/kubernetes_engine.py b/airflow/providers/google/cloud/hooks/kubernetes_engine.py index f5be0f5d189a2..e617f6f0d01e3 100644 --- a/airflow/providers/google/cloud/hooks/kubernetes_engine.py +++ b/airflow/providers/google/cloud/hooks/kubernetes_engine.py @@ -51,7 +51,6 @@ from airflow import version from airflow.compat.functools import cached_property from airflow.exceptions import AirflowException, AirflowProviderDeprecationWarning -from airflow.kubernetes.pod_generator_deprecated import PodDefaults from airflow.providers.google.common.consts import CLIENT_INFO from airflow.providers.google.common.hooks.base_google import ( PROVIDE_PROJECT_ID, @@ -373,10 +372,22 @@ def core_v1_client(self) -> client.CoreV1Api: def is_in_cluster(self) -> bool: return False - @staticmethod - def get_xcom_sidecar_container_image(): - """Returns the xcom sidecar image that defined in the connection""" - return PodDefaults.SIDECAR_CONTAINER.image + def _get_namespace(self): + """Implemented for compatibility with KubernetesHook. Deprecated; do not use.""" + + def get_xcom_sidecar_container_image(self): + """ + Returns the xcom sidecar image defined in the connection. + + Implemented for compatibility with KubernetesHook. + """ + + def get_xcom_sidecar_container_resources(self): + """ + Returns the xcom sidecar resources defined in the connection. + + Implemented for compatibility with KubernetesHook. + """ def get_conn(self) -> client.ApiClient: configuration = self._get_config() diff --git a/tests/ast_helpers.py b/tests/ast_helpers.py new file mode 100644 index 0000000000000..8e20d6ca5b32e --- /dev/null +++ b/tests/ast_helpers.py @@ -0,0 +1,36 @@ +# 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. +from __future__ import annotations + +import ast + + +def extract_ast_class_def_by_name(content, class_name): + """ + Extracts class definition by name + + :param content: python file content + :param class_name: name of the class. + :return: class node found + """ + + ast_tree = ast.parse(content) + for node in ast.walk(ast_tree): + if isinstance(node, ast.ClassDef) and node.name == class_name: + return node + + return None diff --git a/tests/providers/google/cloud/hooks/test_kubernetes_engine.py b/tests/providers/google/cloud/hooks/test_kubernetes_engine.py index a9724cbf5418d..8ec2120b1806a 100644 --- a/tests/providers/google/cloud/hooks/test_kubernetes_engine.py +++ b/tests/providers/google/cloud/hooks/test_kubernetes_engine.py @@ -17,6 +17,7 @@ # under the License. from __future__ import annotations +import ast import sys from asyncio import Future @@ -26,8 +27,15 @@ from google.cloud.container_v1.types import Cluster from airflow.exceptions import AirflowException -from airflow.providers.google.cloud.hooks.kubernetes_engine import GKEAsyncHook, GKEHook, GKEPodAsyncHook +from airflow.providers.google.cloud.hooks.kubernetes_engine import ( + GKEAsyncHook, + GKEHook, + GKEPodAsyncHook, + GKEPodHook, +) from airflow.providers.google.common.consts import CLIENT_INFO +from tests import REPO_ROOT +from tests.ast_helpers import extract_ast_class_def_by_name from tests.providers.google.cloud.utils.base_gcp_mock import mock_base_gcp_hook_default_project_id if sys.version_info < (3, 8): @@ -408,3 +416,32 @@ async def test_get_operation(self, mock_get_client, async_gke_hook, mock_async_g mock_async_gke_cluster_client.get_operation.assert_called_once_with( name=operation_path, ) + + +def test_hook_has_methods_required_by_kpo(): + kpo_file = REPO_ROOT / "airflow/providers/cncf/kubernetes/operators/pod.py" + class_def = extract_ast_class_def_by_name(kpo_file.read_text(), "KubernetesPodOperator") + + def get_hook_attr_refs(tree, attr): + for node in ast.walk(tree): + if isinstance(node, ast.Attribute): + if isinstance(node.value, ast.Attribute) and node.value.attr == attr: + yield node.attr + + # use AST to find all hook attr references in KPO + methods = set(get_hook_attr_refs(class_def, "hook")) + # actually verify that GKE has all attrs referenced by KPO + assert methods.intersection(GKEPodHook.__dict__) == methods + + # sanity check below + # the list here is not strictly required but it's helpful to verify that the test is working + # will need to be updated when new hook method / attr references added to KPO + expected = { + "core_v1_client", + "get_pod", + "_get_namespace", + "is_in_cluster", + "get_xcom_sidecar_container_image", + "get_xcom_sidecar_container_resources", + } + assert methods == expected