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

GKEPodHook needs to have all methods KPO calls #31266

Merged
merged 2 commits into from
May 15, 2023
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
21 changes: 16 additions & 5 deletions airflow/providers/google/cloud/hooks/kubernetes_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't need this because returning None gets you to the same place. (and we shouldn't be using pod_generator_deprecated anyway)

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()
Expand Down
36 changes: 36 additions & 0 deletions tests/ast_helpers.py
Original file line number Diff line number Diff line change
@@ -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
39 changes: 38 additions & 1 deletion tests/providers/google/cloud/hooks/test_kubernetes_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# under the License.
from __future__ import annotations

import ast
import sys
from asyncio import Future

Expand All @@ -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):
Expand Down Expand Up @@ -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