Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Add __eq__ and __hash__ method for MarathonBackend to fix issue #675
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaowei committed Feb 5, 2021
1 parent 9e669ec commit 7bcd0ea
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions marathon_lb.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ def __hash__(self):
def __repr__(self):
return "MarathonBackend(%r, %r, %r)" % (self.host, self.ip, self.port)

def __hash__(self):
"Return the hash value of a friendly backend server name"
return hash(self.friendly_server_name())

def __eq__(self, oth):
"""
Return true if both backend server name are same, else false.
The friendly server name uses to calculate haproxy server id.
"""
return isinstance(oth, MarathonBackend) and \
self.friendly_server_name() == oth.friendly_server_name()

def friendly_server_name(self):
"""
Return a unique, friendly name for the backend server.
Concat the host, task IP and task port together. If the host and task
IP are actually the same then omit one for clarity.
"""

if self.host != self.ip:
serverHost = self.host + '_' + self.ip
else:
serverHost = self.ip

serverHostName = re.sub(r'[^a-zA-Z0-9\-]', '_', serverHost)

return serverHostName + '_' + str(self.port)


class MarathonService(object):

Expand Down Expand Up @@ -692,20 +721,7 @@ def config(apps, groups, bind_http_https, ssl_certs, templater,
backendServer.port,
backendServer.host)

# Create a unique, friendly name for the backend server. We concat
# the host, task IP and task port together. If the host and task
# IP are actually the same then omit one for clarity.
if backendServer.host != backendServer.ip:
serverName = re.sub(
r'[^a-zA-Z0-9\-]', '_',
(backendServer.host + '_' +
backendServer.ip + '_' +
str(backendServer.port)))
else:
serverName = re.sub(
r'[^a-zA-Z0-9\-]', '_',
(backendServer.ip + '_' +
str(backendServer.port)))
serverName = backendServer.friendly_server_name()
shortHashedServerName = hashlib.sha1(serverName.encode()) \
.hexdigest()[:10]

Expand Down Expand Up @@ -1451,8 +1467,8 @@ def generateAndValidateTempConfig(config, config_file, domain_map_array,
app_map_string = generateMapString(app_map_array)

return writeConfigAndValidate(
config, temp_config_file, domain_map_string, domain_map_file,
app_map_string, app_map_file, haproxy_map)
config, temp_config_file, domain_map_string, domain_map_file,
app_map_string, app_map_file, haproxy_map)


def compareWriteAndReloadConfig(config, config_file, domain_map_array,
Expand Down

0 comments on commit 7bcd0ea

Please sign in to comment.