1
0
Fork 0

Add annotations to Kubernetes

This commit is contained in:
Joao Gilberto Magalhaes 2022-08-25 20:58:43 -05:00
parent f007f857df
commit 60bf71f6fb
3 changed files with 28 additions and 3 deletions

View file

@ -27,7 +27,7 @@ class DockerLabelHandler:
def get_bool(self, label, default_value = False):
if self.has_label(label):
return self.__data[label].lower() in ["True", "true", "1", "yes"]
return self.__data[label].lower() in ["true", "1", "yes"]
return default_value
def get_json(self, label, default_value = {}):

View file

@ -34,7 +34,7 @@ class ContainerEnv:
class ProcessorInterface:
static_file = "/etc/haproxy/easyconfig.yml"
def __init__(self, filename = None):
self.filename = filename
self.refresh()
@ -134,7 +134,12 @@ class Kubernetes(ProcessorInterface):
self.api_instance = client.CoreV1Api()
self.v1 = client.NetworkingV1Api()
super().__init__()
def _check_annotation(self, annotations, key):
if key not in annotations:
return None
return annotations[key]
def inspect_network(self):
ret = self.v1.list_ingress_for_all_namespaces(watch=False)
@ -144,6 +149,10 @@ class Kubernetes(ProcessorInterface):
if i.metadata.annotations['kubernetes.io/ingress.class'] != "easyhaproxy-ingress":
continue
letsencrypt = self._check_annotation(i.metadata.annotations, "easyhaproxy.letsencrypt")
redirect_ssl = self._check_annotation(i.metadata.annotations, "easyhaproxy.redirect_ssl")
redirect = self._check_annotation(i.metadata.annotations, "easyhaproxy.redirect")
data = {}
#ingress_name = i.metadata.name
data["creation_timestamp"] = i.metadata.creation_timestamp.strftime("%x %X")
@ -156,6 +165,13 @@ class Kubernetes(ProcessorInterface):
rule_data["easyhaproxy.%s_%s.host" % (definition, port_number)] = rule.host
rule_data["easyhaproxy.%s_%s.port" % (definition, port_number)] = "80"
rule_data["easyhaproxy.%s_%s.localport" % (definition, port_number)] = port_number
if redirect_ssl is not None:
rule_data["easyhaproxy.%s_%s.redirect_ssl" % (definition, port_number)] = 'true'
if letsencrypt is not None:
rule_data["easyhaproxy.%s_%s.letsencrypt" % (definition, port_number)] = 'true'
if redirect is not None:
rule_data["easyhaproxy.%s_%s.redirect" % (definition, port_number)] = redirect
service_name = rule.http.paths[0].backend.service.name
try:
api_response = self.api_instance.read_namespaced_service(service_name, i.metadata.namespace)