From a90763a1edafb145960a2400704d938a9bd43e52 Mon Sep 17 00:00:00 2001 From: balchua Date: Mon, 13 Jul 2020 18:03:37 +0800 Subject: [PATCH] fix enabling of dns when running HA. (#1403) --- scripts/cluster/common/utils.py | 25 +++++++++++++++++++++++++ scripts/cluster/distributed_op.py | 15 +++++++++------ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/scripts/cluster/common/utils.py b/scripts/cluster/common/utils.py index a34bba5355..c1463e7ca3 100644 --- a/scripts/cluster/common/utils.py +++ b/scripts/cluster/common/utils.py @@ -4,6 +4,7 @@ import string import random import yaml +import socket def try_set_file_permissions(file): @@ -146,3 +147,27 @@ def get_cluster_agent_port(): if len(port_parse) > 1: cluster_agent_port = port_parse[1].rstrip() return cluster_agent_port + + +def get_internal_ip_from_get_node(node_info): + """ + Retrieves the InternalIp returned by kubectl get no -o json + """ + for status_addresses in node_info['status']['addresses']: + if status_addresses["type"] == "InternalIP": + return status_addresses["address"] + + +def is_same_server(hostname, ip): + """ + Check if the hostname is the same as the current node's hostname + """ + try: + hname, _, _ = socket.gethostbyaddr(ip) + if hname == hostname: + return True + except: + # Ignore any unresolvable IP by host, surely this is not from the same node. + pass + + return False diff --git a/scripts/cluster/distributed_op.py b/scripts/cluster/distributed_op.py index cd7bfc4f70..2577e9c983 100644 --- a/scripts/cluster/distributed_op.py +++ b/scripts/cluster/distributed_op.py @@ -9,7 +9,11 @@ import json import socket -from common.utils import is_node_running_dqlite +from common.utils import ( + is_node_running_dqlite, + get_internal_ip_from_get_node, + is_same_server, +) urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) CLUSTER_API = "cluster/api/v1.0" @@ -39,13 +43,12 @@ def do_op(remote_op): ) info = json.loads(nodes_info.decode()) for node_info in info["items"]: - node = node_info['metadata']['name'] - # TODO: What if the user has set a hostname override in the kubelet args? - if node == hostname: + node_ip = get_internal_ip_from_get_node(node_info) + if is_same_server(hostname, node_ip): continue - print("Configuring node {}".format(node)) + print("Configuring node {}".format(node_ip)) # TODO: make port configurable - node_ep = "{}:{}".format(node, '25000') + node_ep = "{}:{}".format(node_ip, '25000') remote_op["callback"] = token.rstrip() # TODO: handle ssl verification res = requests.post(