diff --git a/README.md b/README.md index b1af1c4..ec929d8 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,15 @@ The system will read only `host` and `port.number` There is no necessary to add labels or annotations. +### Kubernetes annotations: + +| annotation | Description | Example | +|-----------------------------|-------------------------------------------------------------------------------------------------|--------------| +| kubernetes.io/ingress.class | (required) Activate EasyHAProxy. | easyhaproxy-ingress +| easyhaproxy.redirect_ssl | (optional) Boolean. Force redirect all endpoints to https. | true/false) +| easyhaproxy.letsencrypt | (optional) Boolean. It will request letsencript certificates for the ingresses domains | true/false +| easyhaproxy.redirect | (optional) Json. Specific a domain and its destination | {"domain":"redirect_url"} + ### Container (Docker or Swarm) labels: diff --git a/src/easymapping/__init__.py b/src/easymapping/__init__.py index 94a893f..f45e1fc 100644 --- a/src/easymapping/__init__.py +++ b/src/easymapping/__init__.py @@ -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 = {}): diff --git a/src/processor/__init__.py b/src/processor/__init__.py index c92bf29..212a316 100644 --- a/src/processor/__init__.py +++ b/src/processor/__init__.py @@ -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)