krkn-lib 0.0.0b0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (58) hide show
  1. krkn_lib-0.0.0b0/LICENSE +177 -0
  2. krkn_lib-0.0.0b0/PKG-INFO +68 -0
  3. krkn_lib-0.0.0b0/README.md +33 -0
  4. krkn_lib-0.0.0b0/pyproject.toml +61 -0
  5. krkn_lib-0.0.0b0/src/krkn_lib/elastic/__init__.py +1 -0
  6. krkn_lib-0.0.0b0/src/krkn_lib/elastic/krkn_elastic.py +246 -0
  7. krkn_lib-0.0.0b0/src/krkn_lib/k8s/__init__.py +1 -0
  8. krkn_lib-0.0.0b0/src/krkn_lib/k8s/krkn_kubernetes.py +3496 -0
  9. krkn_lib-0.0.0b0/src/krkn_lib/k8s/pods_monitor_pool.py +202 -0
  10. krkn_lib-0.0.0b0/src/krkn_lib/k8s/templates/hog_pod.j2 +55 -0
  11. krkn_lib-0.0.0b0/src/krkn_lib/k8s/templates/node_exec_pod.j2 +30 -0
  12. krkn_lib-0.0.0b0/src/krkn_lib/k8s/templates/service_hijacking_config_map.j2 +8 -0
  13. krkn_lib-0.0.0b0/src/krkn_lib/k8s/templates/service_hijacking_pod.j2 +31 -0
  14. krkn_lib-0.0.0b0/src/krkn_lib/k8s/templates/syn_flood_pod.j2 +41 -0
  15. krkn_lib-0.0.0b0/src/krkn_lib/models/__init__.py +0 -0
  16. krkn_lib-0.0.0b0/src/krkn_lib/models/elastic/__init__.py +1 -0
  17. krkn_lib-0.0.0b0/src/krkn_lib/models/elastic/models.py +234 -0
  18. krkn_lib-0.0.0b0/src/krkn_lib/models/k8s/__init__.py +1 -0
  19. krkn_lib-0.0.0b0/src/krkn_lib/models/k8s/models.py +406 -0
  20. krkn_lib-0.0.0b0/src/krkn_lib/models/krkn/__init__.py +1 -0
  21. krkn_lib-0.0.0b0/src/krkn_lib/models/krkn/models.py +236 -0
  22. krkn_lib-0.0.0b0/src/krkn_lib/models/telemetry/__init__.py +1 -0
  23. krkn_lib-0.0.0b0/src/krkn_lib/models/telemetry/models.py +562 -0
  24. krkn_lib-0.0.0b0/src/krkn_lib/ocp/__init__.py +1 -0
  25. krkn_lib-0.0.0b0/src/krkn_lib/ocp/krkn_openshift.py +482 -0
  26. krkn_lib-0.0.0b0/src/krkn_lib/prometheus/__init__.py +0 -0
  27. krkn_lib-0.0.0b0/src/krkn_lib/prometheus/krkn_prometheus.py +257 -0
  28. krkn_lib-0.0.0b0/src/krkn_lib/telemetry/__init__.py +0 -0
  29. krkn_lib-0.0.0b0/src/krkn_lib/telemetry/k8s/__init__.py +1 -0
  30. krkn_lib-0.0.0b0/src/krkn_lib/telemetry/k8s/krkn_telemetry_kubernetes.py +645 -0
  31. krkn_lib-0.0.0b0/src/krkn_lib/telemetry/ocp/__init__.py +1 -0
  32. krkn_lib-0.0.0b0/src/krkn_lib/telemetry/ocp/krkn_telemetry_openshift.py +264 -0
  33. krkn_lib-0.0.0b0/src/krkn_lib/tests/__init__.py +1 -0
  34. krkn_lib-0.0.0b0/src/krkn_lib/tests/base_test.py +582 -0
  35. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_elastic.py +170 -0
  36. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_elastic_models.py +235 -0
  37. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_check.py +230 -0
  38. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_create.py +70 -0
  39. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_delete.py +124 -0
  40. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_exec.py +198 -0
  41. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_get.py +351 -0
  42. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_list.py +145 -0
  43. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_misc.py +349 -0
  44. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_models.py +169 -0
  45. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_monitor.py +367 -0
  46. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_kubernetes_pods_monitor_pool.py +128 -0
  47. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_openshift.py +123 -0
  48. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_prometheus.py +234 -0
  49. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_telemetry_kubernetes.py +326 -0
  50. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_telemetry_models.py +438 -0
  51. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_krkn_telemetry_openshift.py +56 -0
  52. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_utils.py +412 -0
  53. krkn_lib-0.0.0b0/src/krkn_lib/tests/test_version.py +8 -0
  54. krkn_lib-0.0.0b0/src/krkn_lib/utils/__init__.py +2 -0
  55. krkn_lib-0.0.0b0/src/krkn_lib/utils/functions.py +459 -0
  56. krkn_lib-0.0.0b0/src/krkn_lib/utils/safe_logger.py +121 -0
  57. krkn_lib-0.0.0b0/src/krkn_lib/version/__init__.py +1 -0
  58. krkn_lib-0.0.0b0/src/krkn_lib/version/version.py +3 -0
@@ -0,0 +1,177 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.3
2
+ Name: krkn-lib
3
+ Version: 0.0.0b0
4
+ Summary: Foundation library for Kraken
5
+ License: Apache-2.0
6
+ Author: Red Hat Chaos Team
7
+ Requires-Python: >=3.9,<4.0
8
+ Classifier: License :: OSI Approved :: Apache Software License
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Requires-Dist: PyYAML (==6.0.1)
16
+ Requires-Dist: base64io (>=1.0.3,<2.0.0)
17
+ Requires-Dist: coverage (>=7.6.12,<8.0.0)
18
+ Requires-Dist: cython (==3.0)
19
+ Requires-Dist: deprecation (==2.1.0)
20
+ Requires-Dist: elasticsearch (==7.13.4)
21
+ Requires-Dist: elasticsearch-dsl (==7.4.1)
22
+ Requires-Dist: kubeconfig (>=1.1.1,<2.0.0)
23
+ Requires-Dist: kubernetes (==28.1.0)
24
+ Requires-Dist: numpy (==1.26.4)
25
+ Requires-Dist: prometheus-api-client (>=0.5.4,<0.6.0)
26
+ Requires-Dist: pytz (>=2023.3,<2024.0)
27
+ Requires-Dist: requests (>=2.29.0,<3.0.0)
28
+ Requires-Dist: sphinx-rtd-theme (>=1.2.2,<2.0.0)
29
+ Requires-Dist: sphinxnotes-markdown-builder (>=0.5.6,<0.6.0)
30
+ Requires-Dist: tzlocal (==5.1)
31
+ Requires-Dist: wheel (>=0.42.0,<0.43.0)
32
+ Project-URL: Homepage, https://github.com/redhat-chaos/krkn
33
+ Description-Content-Type: text/markdown
34
+
35
+ ![action](https://github.com/krkn-chaos/krkn-lib/actions/workflows/build.yaml/badge.svg)
36
+ ![coverage](https://krkn-chaos.github.io/krkn-lib-docs/coverage_badge.svg)
37
+ ![PyPI](https://img.shields.io/pypi/v/krkn-lib?label=PyPi)
38
+ ![PyPI - Downloads](https://img.shields.io/pypi/dm/krkn-lib)
39
+ # krkn-lib
40
+ ## Krkn Chaos and resiliency testing tool Foundation Library
41
+
42
+ ### Contents
43
+ The Library contains Classes, Models and helper functions used in [Kraken](https://github.com/krkn-chaos/krkn) to interact with
44
+ Kubernetes, Openshift and other external APIS.
45
+ The goal of this library is to give to developers the building blocks to realize new Chaos
46
+ Scenarios and to increase the testability and the modularity of the Krkn codebase.
47
+
48
+ ### Packages
49
+
50
+ The library is subdivided in several Packages
51
+
52
+ - **ocp:** Openshift Integration
53
+ - **k8s:** Kubernetes Integration
54
+ - **telemetry:**
55
+ - - **k8s:** Kubernetes Telemetry collection and distribution
56
+ - - **ocp:** Openshift Telemetry collection and distribution
57
+ - **models:** Krkn shared data models
58
+ - **utils:** common functions
59
+
60
+ ### Documentation
61
+
62
+ The Library documentation is available [here](https://krkn-chaos.github.io/krkn-lib-docs/).
63
+ The documentation is automatically generated by [Sphinx](https://www.sphinx-doc.org/en/master/) on top
64
+ of the [reStructuredText Docstring Format](https://peps.python.org/pep-0287/) comments present in the code.
65
+
66
+
67
+
68
+
@@ -0,0 +1,33 @@
1
+ ![action](https://github.com/krkn-chaos/krkn-lib/actions/workflows/build.yaml/badge.svg)
2
+ ![coverage](https://krkn-chaos.github.io/krkn-lib-docs/coverage_badge.svg)
3
+ ![PyPI](https://img.shields.io/pypi/v/krkn-lib?label=PyPi)
4
+ ![PyPI - Downloads](https://img.shields.io/pypi/dm/krkn-lib)
5
+ # krkn-lib
6
+ ## Krkn Chaos and resiliency testing tool Foundation Library
7
+
8
+ ### Contents
9
+ The Library contains Classes, Models and helper functions used in [Kraken](https://github.com/krkn-chaos/krkn) to interact with
10
+ Kubernetes, Openshift and other external APIS.
11
+ The goal of this library is to give to developers the building blocks to realize new Chaos
12
+ Scenarios and to increase the testability and the modularity of the Krkn codebase.
13
+
14
+ ### Packages
15
+
16
+ The library is subdivided in several Packages
17
+
18
+ - **ocp:** Openshift Integration
19
+ - **k8s:** Kubernetes Integration
20
+ - **telemetry:**
21
+ - - **k8s:** Kubernetes Telemetry collection and distribution
22
+ - - **ocp:** Openshift Telemetry collection and distribution
23
+ - **models:** Krkn shared data models
24
+ - **utils:** common functions
25
+
26
+ ### Documentation
27
+
28
+ The Library documentation is available [here](https://krkn-chaos.github.io/krkn-lib-docs/).
29
+ The documentation is automatically generated by [Sphinx](https://www.sphinx-doc.org/en/master/) on top
30
+ of the [reStructuredText Docstring Format](https://peps.python.org/pep-0287/) comments present in the code.
31
+
32
+
33
+
@@ -0,0 +1,61 @@
1
+ [tool.poetry]
2
+ name = "krkn-lib"
3
+ version = "0.0.0-beta"
4
+ description = "Foundation library for Kraken"
5
+ authors = ["Red Hat Chaos Team"]
6
+ license = "Apache-2.0"
7
+ readme = "README.md"
8
+ homepage = "https://github.com/redhat-chaos/krkn"
9
+ #packages = [{include= "src/krkn_lib"}]
10
+
11
+ [tool.poetry.dependencies]
12
+ python = "^3.9"
13
+ kubernetes ="28.1.0"
14
+ sphinxnotes-markdown-builder="^0.5.6"
15
+ requests="^2.29.0"
16
+ kubeconfig = "^1.1.1"
17
+ base64io = "^1.0.3"
18
+ sphinx-rtd-theme = "^1.2.2"
19
+ tzlocal = "5.1"
20
+ pytz = "^2023.3"
21
+ PyYAML = "6.0.1"
22
+ prometheus-api-client = "^0.5.4"
23
+ elasticsearch = "7.13.4"
24
+ elasticsearch-dsl = "7.4.1"
25
+ wheel = "^0.42.0"
26
+ cython = "3.0"
27
+ numpy= "1.26.4"
28
+ deprecation="2.1.0"
29
+ coverage="^7.6.12"
30
+
31
+ [tool.poetry.group.test.dependencies]
32
+ jinja2 = "^3.1.2"
33
+ boto3 = "^1.28.12"
34
+
35
+ [tool.black]
36
+ line-length = 79
37
+ target-version = ['py37', 'py38']
38
+ include = '\.pyi?$'
39
+ exclude = '''
40
+ /(
41
+ \.eggs
42
+ | \.git
43
+ | \.hg
44
+ | \.mypy_cache
45
+ | \.tox
46
+ | \.venv
47
+ | _build
48
+ | buck-out
49
+ | build
50
+ | dist
51
+
52
+ # The following are specific to Black, you probably don't want those.
53
+ | blib2to3
54
+ | tests/data
55
+ | profiling
56
+ )/
57
+ '''
58
+
59
+ [build-system]
60
+ requires = ["poetry-core>=1.1.0"]
61
+ build-backend = "poetry.core.masonry.api"
@@ -0,0 +1 @@
1
+ from .krkn_elastic import * # NOQA
@@ -0,0 +1,246 @@
1
+ from __future__ import annotations
2
+
3
+ import logging
4
+ import math
5
+ import time
6
+
7
+ import urllib3
8
+ from elasticsearch import Elasticsearch, NotFoundError
9
+ from elasticsearch_dsl import Search
10
+
11
+ from krkn_lib.models.elastic.models import (
12
+ ElasticAlert,
13
+ ElasticChaosRunTelemetry,
14
+ ElasticMetric,
15
+ )
16
+ from krkn_lib.models.telemetry import ChaosRunTelemetry
17
+ from krkn_lib.utils.safe_logger import SafeLogger
18
+
19
+
20
+ class KrknElastic:
21
+ es = None
22
+
23
+ def __init__(
24
+ self,
25
+ safe_logger: SafeLogger,
26
+ elastic_url: str,
27
+ elastic_port: int = 443,
28
+ verify_certs: bool = False,
29
+ username: str = None,
30
+ password: str = None,
31
+ ):
32
+ es_logger = logging.getLogger("elasticsearch")
33
+ es_logger.setLevel(logging.WARNING)
34
+ urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
35
+ urllib3.disable_warnings(DeprecationWarning)
36
+ es_transport_logger = logging.getLogger("elastic_transport.transport")
37
+ es_transport_logger.setLevel(logging.CRITICAL)
38
+ self.safe_logger = safe_logger
39
+ try:
40
+ if not elastic_url:
41
+ raise Exception("elastic search url is not valid")
42
+ if not elastic_port:
43
+ raise Exception("elastic port is not valid")
44
+ # create Elasticsearch object
45
+
46
+ credentials = (
47
+ (username, password) if username and password else None
48
+ )
49
+ self.es = Elasticsearch(
50
+ f"{elastic_url}:{elastic_port}",
51
+ http_auth=credentials,
52
+ verify_certs=verify_certs,
53
+ ssl_show_warn=False,
54
+ )
55
+ except Exception as e:
56
+ self.safe_logger.error("Failed to initalize elasticsearch: %s" % e)
57
+ raise e
58
+
59
+ def upload_data_to_elasticsearch(self, item: dict, index: str = "") -> int:
60
+ """uploads captured data in item dictionary to Elasticsearch
61
+
62
+
63
+ :param item: the data to post to elastic search
64
+ :param index: the elastic search index pattern to post to
65
+
66
+ :return: the time taken to post the result,
67
+ will be 0 if index and es are blank
68
+ """
69
+
70
+ if self.es and index != "":
71
+ # Attach to elastic search and attempt index creation
72
+ start = time.time()
73
+ self.safe_logger.info(
74
+ f"Uploading item {item} to index {index} in Elasticsearch"
75
+ )
76
+ try:
77
+ response = self.es.index(index=index, body=item)
78
+ self.safe_logger.info(f"Response back was {response}")
79
+ if response["result"] != "created":
80
+ self.safe_logger.error(
81
+ f"Error trying to create new item in {index}"
82
+ )
83
+ return -1
84
+ except Exception as e:
85
+ self.safe_logger.error(f"Error trying to create new item: {e}")
86
+ return -1
87
+ end = time.time()
88
+ elapsed_time = end - start
89
+
90
+ # return elapsed time for upload if no issues
91
+ return math.ceil(elapsed_time)
92
+ return 0
93
+
94
+ def upload_metrics_to_elasticsearch(
95
+ self,
96
+ run_uuid: str,
97
+ raw_data: list[dict[str, str | int | float]],
98
+ index: str,
99
+ ) -> int:
100
+ """
101
+ Saves raw data returned from the Krkn prometheus
102
+ client to elastic search as a ElasticMetric object
103
+ raw data will be a mixed types dictionary in the format
104
+ {"name":"str", "values":[(10, '3.14'), (11,'3.15').....]
105
+
106
+ :param run_uuid: the krkn run id
107
+ :param raw_data: the mixed type dictionary (will be validated
108
+ checking the attributes types )
109
+ :param index: the elasticsearch index where
110
+ the object will be stored
111
+ :return: the time needed to save if succeeded -1 if failed
112
+ """
113
+ if not index:
114
+ raise Exception("index cannot be None or empty")
115
+ if not run_uuid:
116
+ raise Exception("run uuid cannot be None or empty")
117
+ time_start = time.time()
118
+ try:
119
+ for metric in raw_data:
120
+ result = self.push_metric(
121
+ ElasticMetric(run_uuid=run_uuid, **metric),
122
+ index,
123
+ )
124
+ if result == -1:
125
+ self.safe_logger.error(
126
+ f"failed to save metric "
127
+ f"to elasticsearch : {metric}"
128
+ )
129
+
130
+ return int(time.time() - time_start)
131
+ except Exception as e:
132
+ self.safe_logger.error(f"Upload metric exception: {e}")
133
+ return -1
134
+
135
+ def push_alert(self, alert: ElasticAlert, index: str) -> int:
136
+ """
137
+ Pushes an ElasticAlert object to elastic
138
+
139
+ :param alert: the populated ElasticAlert object
140
+ :param index: the index where the ElasticAlert Object
141
+ is pushed
142
+ :return: the time needed to save if succeeded -1 if failed
143
+ """
144
+ if not index:
145
+ raise Exception("index cannot be None or empty")
146
+ try:
147
+ time_start = time.time()
148
+ alert.save(using=self.es, index=index)
149
+ return int(time.time() - time_start)
150
+ except Exception as e:
151
+ self.safe_logger.error(f"Push alert exception: {e}")
152
+ return -1
153
+
154
+ def push_metric(self, metric: ElasticMetric, index: str) -> int:
155
+ """
156
+ Pushes an ElasticMetric object to elastic
157
+
158
+ :param metric: the populated ElasticMetric object
159
+ :param index: the index where the ElasticMetric Object
160
+ is pushed
161
+ :return: the time needed to save if succeeded -1 if failed
162
+ """
163
+ if not index:
164
+ raise Exception("index cannot be None or empty")
165
+ try:
166
+ time_start = time.time()
167
+ metric.save(using=self.es, index=index)
168
+ return int(time.time() - time_start)
169
+ except Exception as e:
170
+ print("error" + str(e))
171
+ self.safe_logger.error(f"Exception pushing metric: {e}")
172
+ return -1
173
+
174
+ def push_telemetry(self, telemetry: ChaosRunTelemetry, index: str):
175
+ if not index:
176
+ raise Exception("index cannot be None or empty")
177
+ try:
178
+ elastic_chaos = ElasticChaosRunTelemetry(telemetry)
179
+ time_start = time.time()
180
+ elastic_chaos.save(using=self.es, index=index)
181
+ return int(time.time() - time_start)
182
+ except Exception as e:
183
+ self.safe_logger.info(f"Elastic push telemetry error: {e}")
184
+ return -1
185
+
186
+ def search_telemetry(self, run_uuid: str, index: str):
187
+ """
188
+ Searches ElasticChaosRunTelemetry by run_uuid
189
+ :param run_uuid: the Krkn run id to search
190
+ :param index: the index where the ElasticChaosRunTelemetry
191
+ should have been saved
192
+ :return: the list of objects retrieved (Empty if nothing
193
+ has been found)
194
+ """
195
+ try:
196
+ search = Search(using=self.es, index=index).filter(
197
+ "match", run_uuid=run_uuid
198
+ )
199
+ result = search.execute()
200
+ documents = [
201
+ ElasticChaosRunTelemetry(**hit.to_dict()) for hit in result
202
+ ]
203
+ except NotFoundError:
204
+ self.safe_logger.error("Search telemetry not found")
205
+ return []
206
+ return documents
207
+
208
+ def search_alert(self, run_uuid: str, index: str) -> list[ElasticAlert]:
209
+ """
210
+ Searches ElasticAlerts by run_uuid
211
+ :param run_uuid: the Krkn run id to search
212
+ :param index: the index where the ElasticAlert
213
+ should have been saved
214
+ :return: the list of objects retrieved (Empty if nothing
215
+ has been found)
216
+ """
217
+ try:
218
+ search = Search(using=self.es, index=index).filter(
219
+ "match", run_uuid=run_uuid
220
+ )
221
+ result = search.execute()
222
+ documents = [ElasticAlert(**hit.to_dict()) for hit in result]
223
+ except NotFoundError:
224
+ self.safe_logger.error("Search alert not found")
225
+ return []
226
+ return documents
227
+
228
+ def search_metric(self, run_uuid: str, index: str) -> list[ElasticMetric]:
229
+ """
230
+ Searches ElasticMetric by run_uuid
231
+ :param run_uuid: the Krkn run id to search
232
+ :param index: the index where the ElasticAlert
233
+ should have been saved
234
+ :return: the list of objects retrieved (Empty if nothing
235
+ has been found)
236
+ """
237
+ try:
238
+ search = Search(using=self.es, index=index).filter(
239
+ "match", run_uuid=run_uuid
240
+ )
241
+ result = search.execute()
242
+ documents = [ElasticMetric(**hit.to_dict()) for hit in result]
243
+ except NotFoundError:
244
+ self.safe_logger.error("Search metric not found")
245
+ return []
246
+ return documents
@@ -0,0 +1 @@
1
+ from .krkn_kubernetes import * # NOQA