Rohan Agrawal | c5bdbbc | 2019-11-14 12:39:39 +0000 | [diff] [blame] | 1 | # |
| 2 | # Copyright 2019 the original author or authors. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
Matt Jeanneret | 2e3cb8d | 2019-11-16 09:22:41 -0500 | [diff] [blame] | 16 | from http.server import SimpleHTTPRequestHandler |
Rohan Agrawal | c5bdbbc | 2019-11-14 12:39:39 +0000 | [diff] [blame] | 17 | from structlog import get_logger |
| 18 | |
| 19 | log = get_logger() |
| 20 | |
| 21 | class Probe(SimpleHTTPRequestHandler): |
Neha Sharma | 61446d3 | 2020-03-23 14:32:31 +0000 | [diff] [blame] | 22 | # Checks for Onu Adapter Readiness; all should be true |
Rohan Agrawal | c5bdbbc | 2019-11-14 12:39:39 +0000 | [diff] [blame] | 23 | kafka_cluster_proxy_running = False |
Neha Sharma | 61446d3 | 2020-03-23 14:32:31 +0000 | [diff] [blame] | 24 | kafka_adapter_proxy_running = False |
| 25 | adapter_registered_with_core = False |
| 26 | |
| 27 | # Only Kafka connectivity check defines Liveness |
| 28 | kafka_proxy_faulty = True |
Rohan Agrawal | c5bdbbc | 2019-11-14 12:39:39 +0000 | [diff] [blame] | 29 | |
| 30 | def readiness_probe(self): |
Neha Sharma | 61446d3 | 2020-03-23 14:32:31 +0000 | [diff] [blame] | 31 | return Probe.kafka_adapter_proxy_running and Probe.kafka_cluster_proxy_running and Probe.adapter_registered_with_core |
Rohan Agrawal | c5bdbbc | 2019-11-14 12:39:39 +0000 | [diff] [blame] | 32 | |
| 33 | def liveness_probe(self): |
Neha Sharma | 61446d3 | 2020-03-23 14:32:31 +0000 | [diff] [blame] | 34 | return not Probe.kafka_proxy_faulty |
Rohan Agrawal | c5bdbbc | 2019-11-14 12:39:39 +0000 | [diff] [blame] | 35 | |
| 36 | def do_GET(self): |
| 37 | if self.path == '/readz': |
| 38 | self.ready_probe() |
| 39 | elif self.path == '/healthz': |
| 40 | self.health_probe() |
| 41 | |
| 42 | def ready_probe(self): |
| 43 | if self.readiness_probe(): |
| 44 | self.send_response(200) |
| 45 | self.end_headers() |
| 46 | else : |
| 47 | self.send_response(418) |
| 48 | self.end_headers() |
| 49 | |
| 50 | def health_probe(self): |
| 51 | if self.liveness_probe(): |
| 52 | self.send_response(200) |
| 53 | self.end_headers() |
| 54 | else : |
| 55 | self.send_response(418) |
| 56 | self.end_headers() |