Skip to content

Commit

Permalink
GKEPodHook needs to have all methods KPO calls (#31266)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dstandish committed May 15, 2023
1 parent bb4a0b3 commit d3a2dbe
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 6 deletions.
21 changes: 16 additions & 5 deletions airflow/providers/google/cloud/hooks/kubernetes_engine.py
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
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
@@ -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
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

0 comments on commit d3a2dbe

Please sign in to comment.