|
| 1 | +# Copyright 2020 Cortex Labs, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import boto3 |
| 16 | +import os |
| 17 | +import json |
| 18 | + |
| 19 | +from helpers import get_operator_load_balancer |
| 20 | + |
| 21 | + |
| 22 | +def get_operator_target_group_status(): |
| 23 | + cluster_name = os.environ["CORTEX_CLUSTER_NAME"] |
| 24 | + region = os.environ["CORTEX_REGION"] |
| 25 | + |
| 26 | + client_elbv2 = boto3.client("elbv2", region_name=region) |
| 27 | + |
| 28 | + load_balancer_arn = get_operator_load_balancer(cluster_name, client_elbv2)["LoadBalancerArn"] |
| 29 | + target_group_arn = get_load_balancer_https_target_group_arn(load_balancer_arn, client_elbv2) |
| 30 | + return get_target_health(target_group_arn, client_elbv2) |
| 31 | + |
| 32 | + |
| 33 | +def get_load_balancer_https_target_group_arn(load_balancer_arn, client_elbv2): |
| 34 | + paginator = client_elbv2.get_paginator("describe_listeners") |
| 35 | + for listener_page in paginator.paginate(LoadBalancerArn=load_balancer_arn): |
| 36 | + for listener in listener_page["Listeners"]: |
| 37 | + if listener["Port"] == 443: |
| 38 | + return listener["DefaultActions"][0]["TargetGroupArn"] |
| 39 | + |
| 40 | + raise Exception( |
| 41 | + f"unable to find https target group for operator load balancer ({load_balancer_arn})" |
| 42 | + ) |
| 43 | + |
| 44 | + |
| 45 | +def get_target_health(target_group_arn, client_elbv2): |
| 46 | + response = client_elbv2.describe_target_health(TargetGroupArn=target_group_arn) |
| 47 | + for health_description in response["TargetHealthDescriptions"]: |
| 48 | + if health_description["TargetHealth"]["State"] == "healthy": |
| 49 | + return "healthy" |
| 50 | + |
| 51 | + return json.dumps(response["TargetHealthDescriptions"]) |
| 52 | + |
| 53 | + |
| 54 | +if __name__ == "__main__": |
| 55 | + print(get_operator_target_group_status(), end="") |
0 commit comments