Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scope kubernetes discovery to a single namespace #223

Merged
merged 3 commits into from
Oct 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#
aiocache==0.8.0
aiofiles==0.3.2 # via sanic
bison==0.0.6
bison==0.1.0
cachetools==2.1.0 # via google-auth
certifi==2018.4.16 # via kubernetes, requests
chardet==3.0.4 # via requests
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
python_requires='>=3.6',
install_requires=[
'aiocache',
'bison>=0.0.6',
'bison>=0.1.0',
'grpcio',
'kubernetes',
'pyyaml',
Expand Down
2 changes: 2 additions & 0 deletions synse/config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Synse Server configuration and scheme definition."""
# pylint: disable=line-too-long

from bison import Bison, DictOption, ListOption, Option, Scheme

Expand All @@ -12,6 +13,7 @@
ListOption('unix', default=[], member_type=str, bind_env=True),
DictOption('discover', required=False, bind_env=True, scheme=Scheme(
DictOption('kubernetes', required=False, bind_env=True, scheme=Scheme(
Option('namespace', required=False, bind_env=True, field_type=str),
DictOption('endpoints', required=False, bind_env=True, scheme=Scheme(
DictOption('labels', bind_env=True, scheme=None)
)),
Expand Down
33 changes: 27 additions & 6 deletions synse/discovery/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,50 @@ def discover():
if not cfg:
return addresses

# Currently, everything we want to be able to discover (namely, endpoints)
# should all be in the same namespace, so we define it globally. If other
# methods of lookup are added later, we could also have a namespace per
# resource, e.g. so we can look for endpoints in namespace X and pods in
# namespace Y, etc.
#
# If no namespace is provided via user configuration, if will default to
# the 'default' namespace.
ns = config.options.get('plugin.discover.kubernetes.namespace', 'default')
logger.debug(_('Using namespace "{}" for k8s discovery').format(ns))

# Currently, we only support plugin discovery via kubernetes service
# endpoints, under the `plugin.discover.kubernetes.endpoints` config
# field.
#
# We can support other means later.
from_endpoints = _register_from_endpoints(cfg.get('endpoints'))
addresses.extend(from_endpoints)
endpoints_cfg = cfg.get('endpoints')
if endpoints_cfg:
addresses.extend(_register_from_endpoints(ns=ns, cfg=endpoints_cfg))

return addresses


def _register_from_endpoints(cfg):
def _register_from_endpoints(ns, cfg):
"""Register plugins with Synse Server discovered via kubernetes
service endpoints.

Args:
cfg (dict): The configuration for service discovery via kubernetes
endpoints.
ns (str): The namespace to get the endpoints from.
cfg (dict): The configuration for service discovery via
kubernetes endpoints.

Returns:
list[str]: A list of host:port addresses for the plugin endpoints
that matched the config.

Raises:
ValueError: The given namespace is empty.
"""
if not ns:
raise ValueError(
'A namespace must be provided for discovery via k8s service endpoints.'
)

found = []

# Gather the filters/options needed to find the endpoints we are interested
Expand All @@ -69,7 +90,7 @@ def _register_from_endpoints(cfg):
kubernetes.config.load_incluster_config()
v1 = kubernetes.client.CoreV1Api()

endpoints = v1.list_endpoints_for_all_namespaces(label_selector=label_selector)
endpoints = v1.list_namespaced_endpoints(namespace=ns, label_selector=label_selector)

# Now we parse out the endpoints to get the routing info to a plugin.
# There are some assumptions here:
Expand Down
28 changes: 16 additions & 12 deletions synse/locale/en_US/LC_MESSAGES/synse.po
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Synse Server VERSION\n"
"Report-Msgid-Bugs-To: EMAIL@ADDRESS\n"
"POT-Creation-Date: 2018-10-24 11:18-0400\n"
"POT-Creation-Date: 2018-10-26 09:17-0400\n"
"PO-Revision-Date: 2018-04-23 14:33-0400\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language: en_US\n"
Expand Down Expand Up @@ -317,47 +317,51 @@ msgstr ""
msgid "Failed to add transaction {} to the cache"
msgstr ""

#: synse/discovery/kubernetes.py:55
#: synse/discovery/kubernetes.py:36
msgid "Using namespace \"{}\" for k8s discovery"
msgstr ""

#: synse/discovery/kubernetes.py:78
msgid "No labels found for kubernetes service discovery via endpoints"
msgstr ""

#: synse/discovery/kubernetes.py:65
#: synse/discovery/kubernetes.py:88
msgid "Using endpoint label selector: {}"
msgstr ""

#: synse/discovery/kubernetes.py:79
#: synse/discovery/kubernetes.py:102
msgid "Found endpoint with name: {}"
msgstr ""

#: synse/discovery/kubernetes.py:87
#: synse/discovery/kubernetes.py:110
msgid "No addresses for subset of endpoint - skipping ({})"
msgstr ""

#: synse/discovery/kubernetes.py:96
#: synse/discovery/kubernetes.py:119
msgid "Address has no target_ref - skipping ({})"
msgstr ""

#: synse/discovery/kubernetes.py:101
#: synse/discovery/kubernetes.py:124
msgid "Address is not a pod address - skipping ({})"
msgstr ""

#: synse/discovery/kubernetes.py:109
#: synse/discovery/kubernetes.py:132
msgid "No ips found for endpoint, will not search for port"
msgstr ""

#: synse/discovery/kubernetes.py:116
#: synse/discovery/kubernetes.py:139
msgid "No ports for subset of endpoint - skipping ({})"
msgstr ""

#: synse/discovery/kubernetes.py:126
#: synse/discovery/kubernetes.py:149
msgid "skipping port (want name:http, but found name:{})"
msgstr ""

#: synse/discovery/kubernetes.py:138
#: synse/discovery/kubernetes.py:161
msgid "found plugin: endpoint.name={}, ip={}, port={}"
msgstr ""

#: synse/discovery/kubernetes.py:145
#: synse/discovery/kubernetes.py:168
msgid "Did not find any plugins via kubernetes endpoints (labels={})"
msgstr ""

Expand Down
Loading