Coverage for ocp_resources/node_health_check.py: 0%

32 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-01-30 10:48 +0200

1from ocp_resources.resource import MissingRequiredArgumentError, Resource 

2 

3 

4class NodeHealthCheck(Resource): 

5 """ 

6 NodeHealthCheck object. 

7 Reference : https://github.com/medik8s/node-healthcheck-operator 

8 """ 

9 

10 api_group = Resource.ApiGroup.REMEDIATION_MEDIK8S_IO 

11 

12 def __init__( 

13 self, 

14 selector_match_expressions=None, 

15 selector_match_labels=None, 

16 min_unhealthy=None, 

17 unhealthy_conditions=None, 

18 escalating_remediation=None, 

19 remediation_template=None, 

20 **kwargs, 

21 ): 

22 """ 

23 Create NodeHealthCheck object. 

24 

25 Args: 

26 selector_match_expressions (dict, Optional if `selector_match_labels` exist): Belongs to selector object. 

27 This helps in matching nodes. 

28 selector_match_labels (dict, Optional if `selector_match_expressions` exist): Belongs to selector object. 

29 This is a map of {key,value} pairs. 

30 min_unhealthy (int, Optional): Remediation is allowed if at least "MinHealthy" nodes selected by "selector" 

31 unhealthy_conditions (list, Optional): UnhealthyConditions contains a list of the conditions that determine 

32 whether a node is considered unhealthy. 

33 remediation_template (dict, mandatory if `escalating_remediation` is not provided) : remediation template 

34 provided by an infrastructure provider. 

35 escalating_remediation (list, mandatory if `remediation_template` is not provided): EscalatingRemediations 

36 contain a list of ordered remediation templates with a timeout. 

37 example: [{'remediationTemplate': {'apiVersion': 'self-node-remediation.medik8s.io/v1alpha1', 'kind': 

38 'SelfNodeRemediationTemplate', 'namespace': 'openshift-operators', 

39 'name': 'self-node-remediation-resource-deletion-template'}, 'order': 1, 'timeout': '70s'}] 

40 """ 

41 super().__init__( 

42 **kwargs, 

43 ) 

44 self.selector_match_expressions = selector_match_expressions 

45 self.selector_match_labels = selector_match_labels 

46 self.min_unhealthy = min_unhealthy 

47 self.unhealthy_conditions = unhealthy_conditions 

48 self.escalating_remediation = escalating_remediation 

49 self.remediation_template = remediation_template 

50 

51 def to_dict(self) -> None: 

52 super().to_dict() 

53 if not self.kind_dict and not self.yaml_file: 

54 if not (self.selector_match_expressions or self.selector_match_labels): 

55 raise MissingRequiredArgumentError(argument="'selector_match_expressions' or 'selector_match_labels'") 

56 

57 if self.escalating_remediation and self.remediation_template: 

58 raise ValueError("remediation_template and escalating_remediation usage is mutual exclusive") 

59 

60 spec = self.res.setdefault("spec", {}) 

61 

62 if self.escalating_remediation: 

63 spec["escalatingRemediations"] = self.escalating_remediation 

64 

65 if self.remediation_template: 

66 spec["remediationTemplate"] = self.remediation_template 

67 

68 if self.min_unhealthy: 

69 spec["minHealthy"] = self.min_unhealthy 

70 

71 if self.unhealthy_conditions: 

72 spec["unhealthyConditions"] = self.unhealthy_conditions 

73 

74 spec.setdefault("selector", {}) 

75 

76 if self.selector_match_expressions: 

77 spec["selector"]["matchExpressions"] = self.selector_match_expressions 

78 

79 if self.selector_match_labels: 

80 spec["selector"]["matchLabels"] = self.selector_match_labels