Skip to content

Commit a6b7121

Browse files
authored
Merge pull request #9 from Balasys/develop
TOSCA-related fixes
2 parents 3cadc84 + de83062 commit a6b7121

File tree

2 files changed

+14
-38
lines changed

2 files changed

+14
-38
lines changed

zorp_ic/controller.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ def write_secret(self, name, secret):
9797
self._write_and_set_perms(keyfilename, secret["tls.key"])
9898

9999
def write_config_debug(self):
100-
f = open("/tmp/k8s-config", "w")
100+
f = open("/tmp/k8s-config2", "w")
101101
f.write(str(self.config)+"\n")
102102
f.close()
103103

@@ -133,7 +133,7 @@ def load_k8s_config(self):
133133
if annotation is not None:
134134
self.config["conf"] = json.loads(annotation)
135135
self.config["services"] = self.k8s.get_services_from_annotation(self.config["conf"])
136-
self.config["endpoints"] = self.k8s.get_endpoints_from_annotation(self.config["conf"])
136+
self.config["endpoints"] = self.k8s.get_relevant_endpoints(self.config["services"])
137137
self.secrets = self.k8s.get_secrets_from_annotation(self.config["conf"])
138138
self.write_config_debug()
139139
if oldconfig != self.config:

zorp_ic/kubernetes_backend.py

+12-36
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ def __init__(self, namespace='default', ignore_namespaces=["kube-system"], ingre
2424
self.namespace = namespace
2525
self.ignore_namespaces = ignore_namespaces
2626
self.ingress_class = ingress_class
27+
self._logger = logging.getLogger('flask.app')
28+
self._logger.setLevel(logging.getLevelName('INFO'))
29+
30+
self._logger.info("Initializing Kubernetes Backend; namespace='%s', ignore_namespaces='%s', ingress_class='%s'" % (self.namespace, self.ignore_namespaces, self.ingress_class))
2731

2832
if not self._api or not self._ext_api:
29-
self._logger = logging.getLogger('flask.app')
30-
self._logger.setLevel(logging.getLevelName('INFO'))
3133
self._logger.info('Initializing Kubernetes Client.')
32-
3334
config.load_incluster_config()
3435

3536
self._api = client.CoreV1Api()
@@ -135,7 +136,7 @@ def _get_services(self):
135136
def get_relevant_services(self, ingress):
136137
services = {}
137138
for service in self._get_services().items:
138-
if service.metadata.name in ingress["services"] and service.metadata.namespace not in self.ignore_namespaces:
139+
if service.metadata.name in ingress["services"] and service.metadata.namespace not in self.ignore_namespaces and service.metadata.name != "kubernetes":
139140
ports = {}
140141
for port in service.spec.ports:
141142
ports[port.protocol] = { port.port: port.target_port }
@@ -151,9 +152,12 @@ def get_services_from_annotation(self, annotation):
151152
for service in self._get_services().items:
152153
ports = {}
153154
for port in service.spec.ports:
154-
if port.port in relevant_ports and service.metadata.namespace not in self.ignore_namespaces:
155-
ports[port.protocol] = { port.port: port.target_port }
156-
services[service.metadata.name] = ports
155+
if port.port in relevant_ports:
156+
if service.metadata.namespace not in self.ignore_namespaces and service.metadata.name != "kubernetes":
157+
ports[port.protocol] = { port.port: port.target_port }
158+
services[service.metadata.name] = ports
159+
else:
160+
self._logger.info("Ignoring service from namespace; name='%s', namespace='%s'" % (service.metadata.name, service.metadata.namespace))
157161
return services
158162

159163
def _get_endpoints(self):
@@ -174,7 +178,7 @@ def get_relevant_endpoints(self, services):
174178
tcp_endpoints = {}
175179
udp_endpoints = {}
176180
for endpoint in self._get_endpoints().items:
177-
if endpoint.metadata.name in services.keys() and endpoint.metadata.namespace not in self.ignore_namespaces:
181+
if endpoint.metadata.name in services.keys():
178182
for subset in endpoint.subsets:
179183
for address in subset.addresses:
180184
for port in subset.ports:
@@ -192,34 +196,6 @@ def get_relevant_endpoints(self, services):
192196
endpoints[port.port] = { name : [address.ip, ]}
193197
return {"TCP": tcp_endpoints, "UDP": udp_endpoints}
194198

195-
def get_endpoints_from_annotation(self, annotation):
196-
relevant_ports = []
197-
for rule in annotation:
198-
if "target_ports" in rule:
199-
relevant_ports.extend(rule["target_ports"])
200-
201-
tcp_endpoints = {}
202-
udp_endpoints = {}
203-
for endpoint in self._get_endpoints().items:
204-
if endpoint.subsets is not None and endpoint.metadata.namespace not in self.ignore_namespaces:
205-
for subset in endpoint.subsets:
206-
for port in subset.ports:
207-
if port.port in relevant_ports and subset.addresses is not None:
208-
for address in subset.addresses:
209-
name = endpoint.metadata.name
210-
if port.protocol == "TCP":
211-
endpoints = tcp_endpoints
212-
else:
213-
endpoints = udp_endpoints
214-
if port.port in endpoints:
215-
if name in endpoints[port.port]:
216-
endpoints[port.port][name].append(address.ip)
217-
else:
218-
endpoints[port.port][name] = [address.ip, ]
219-
else:
220-
endpoints[port.port] = { name : [address.ip, ]}
221-
return {"TCP": tcp_endpoints, "UDP": udp_endpoints}
222-
223199
def _get_secret(self, namespace=None, name='tls-secret'):
224200
if namespace is None:
225201
namespace = self.namespace

0 commit comments

Comments
 (0)