Coverage for /pythoncovmergedfiles/medio/medio/src/airflow/build/lib/airflow/serialization/serializers/kubernetes.py: 44%

32 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

1# 

2# Licensed to the Apache Software Foundation (ASF) under one 

3# or more contributor license agreements. See the NOTICE file 

4# distributed with this work for additional information 

5# regarding copyright ownership. The ASF licenses this file 

6# to you under the Apache License, Version 2.0 (the 

7# "License"); you may not use this file except in compliance 

8# with the License. You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, 

13# software distributed under the License is distributed on an 

14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 

15# KIND, either express or implied. See the License for the 

16# specific language governing permissions and limitations 

17# under the License. 

18from __future__ import annotations 

19 

20import logging 

21from typing import TYPE_CHECKING 

22 

23from airflow.utils.module_loading import qualname 

24 

25serializers = [] 

26 

27try: 

28 from kubernetes.client import models as k8s 

29 

30 serializers = [k8s.v1_pod.V1Pod, k8s.V1ResourceRequirements] 

31except ImportError: 

32 k8s = None 

33 

34if TYPE_CHECKING: 

35 from airflow.serialization.serde import U 

36 

37 

38__version__ = 1 

39 

40deserializers: list[type[object]] = [] 

41log = logging.getLogger(__name__) 

42 

43 

44def serialize(o: object) -> tuple[U, str, int, bool]: 

45 if not k8s: 

46 return "", "", 0, False 

47 

48 if isinstance(o, (k8s.V1Pod, k8s.V1ResourceRequirements)): 

49 from airflow.kubernetes.pod_generator import PodGenerator 

50 

51 def safe_get_name(pod): 

52 """ 

53 We're running this in an except block, so we don't want it to 

54 fail under any circumstances, e.g. by accessing an attribute that isn't there 

55 """ 

56 try: 

57 return pod.metadata.name 

58 except Exception: 

59 return None 

60 

61 try: 

62 return PodGenerator.serialize_pod(o), qualname(o), __version__, True 

63 except Exception: 

64 log.warning("Serialization failed for pod %s", safe_get_name(o)) 

65 log.debug("traceback for serialization error", exc_info=True) 

66 return "", "", 0, False 

67 

68 return "", "", 0, False