From 86e35666c4d523352d6b55412a2536738b4f867b Mon Sep 17 00:00:00 2001 From: Jaume Devesa Date: Wed, 24 Aug 2016 13:27:04 +0200 Subject: [PATCH] Introduce `kuryr-k8s` service This commit introduces the `kuryr-k8s` service by adding the service binary and focusing on loading configuration options. The configuration options are inherited from kuryr-lib project (http://github.com/openstack/kuryr) and loaded at runtime, together with the project ones. These configuration options can be also generated using the `oslo-config-generator` utility by using: tox -e genconfig The service runs as any other OpenStack-based service: kuryr-k8s [--debug] [--config-file foo] ... Partial-Implements: blueprint kuryr-k8s-integration Change-Id: I7e52aef8fb2767dcc46317f2212f4285a17b11da Signed-off-by: Jaume Devesa Co-Authored-By: Taku Fukushima --- .gitignore | 1 - etc/README-config.txt | 6 +++ etc/kuryr-k8s-config-generator.conf | 5 +++ kuryr_kubernetes/config.py | 65 +++++++++++++++++++++++++++++ kuryr_kubernetes/opts.py | 39 +++++++++++++++++ kuryr_kubernetes/server.py | 59 ++++++++++++++++++++++++++ requirements.txt | 14 +------ setup.cfg | 13 ++++-- tox.ini | 2 +- 9 files changed, 185 insertions(+), 19 deletions(-) create mode 100644 etc/README-config.txt create mode 100644 etc/kuryr-k8s-config-generator.conf create mode 100644 kuryr_kubernetes/config.py create mode 100644 kuryr_kubernetes/opts.py create mode 100644 kuryr_kubernetes/server.py diff --git a/.gitignore b/.gitignore index 59a7be2fd..91e92b605 100644 --- a/.gitignore +++ b/.gitignore @@ -53,7 +53,6 @@ ChangeLog contrib/vagrant/.vagrant # Configuration files -etc/kuryr.conf etc/kuryr.conf.sample # Ignore user specific local.conf settings for vagrant diff --git a/etc/README-config.txt b/etc/README-config.txt new file mode 100644 index 000000000..73a557a6d --- /dev/null +++ b/etc/README-config.txt @@ -0,0 +1,6 @@ +To generate the sample kuryr.conf file, run the following +command from the top level of the kuryr directory: + +tox -e genconfig + +The sample file will be generated at etc/kuryr-k8s.conf.sample diff --git a/etc/kuryr-k8s-config-generator.conf b/etc/kuryr-k8s-config-generator.conf new file mode 100644 index 000000000..29b112e5a --- /dev/null +++ b/etc/kuryr-k8s-config-generator.conf @@ -0,0 +1,5 @@ +[DEFAULT] +output_file = etc/kuryr.conf.sample +wrap_width = 79 +namespace = kuryr_kubernetes +namespace = kuryr_lib diff --git a/kuryr_kubernetes/config.py b/kuryr_kubernetes/config.py new file mode 100644 index 000000000..ad165f070 --- /dev/null +++ b/kuryr_kubernetes/config.py @@ -0,0 +1,65 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import os +import sys + +from kuryr.lib._i18n import _, _LI +from kuryr.lib import config as lib_config +from oslo_config import cfg +from oslo_log import log as logging + +from pbr import version as pbr_version + +LOG = logging.getLogger(__name__) + +kuryr_k8s_opts = [ + cfg.StrOpt('pybasedir', + help=_('Directory where Kuryr-kubernetes python module is ' + 'installed.'), + default=os.path.abspath( + os.path.join(os.path.dirname(__file__), + '../../'))), +] + +k8s_opts = [ + cfg.StrOpt('api_root', + help=_("The root URL of the Kubernetes API"), + default=os.environ.get('K8S_API', 'http://localhost:8080')), +] + + +CONF = cfg.CONF + +CONF.register_opts(lib_config.core_opts) +CONF.register_opts(lib_config.neutron_opts, group='neutron_client') +CONF.register_opts(lib_config.keystone_opts, group='keystone_client') +CONF.register_opts(lib_config.binding_opts, 'binding') + +CONF.register_opts(kuryr_k8s_opts) +CONF.register_opts(k8s_opts, group='kubernetes') +logging.register_options(CONF) + + +def init(args, **kwargs): + version_k8s = pbr_version.VersionInfo('kuryr-kubernetes').version_string() + CONF(args=args, project='kuryr-k8s', version=version_k8s, **kwargs) + + +def setup_logging(): + + logging.setup(CONF, 'kuryr-kubernetes') + logging.set_defaults(default_log_levels=logging.get_default_log_levels()) + version_k8s = pbr_version.VersionInfo('kuryr-kubernetes').version_string() + LOG.info(_LI("Logging enabled!")) + LOG.info(_LI("%(prog)s version %(version)s"), + {'prog': sys.argv[0], + 'version': version_k8s}) diff --git a/kuryr_kubernetes/opts.py b/kuryr_kubernetes/opts.py new file mode 100644 index 000000000..c62aee842 --- /dev/null +++ b/kuryr_kubernetes/opts.py @@ -0,0 +1,39 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +import copy + +from kuryr_kubernetes import config + +_kuryr_k8s_opts = [ + ('kubernetes', config.k8s_opts), + ('kuryr-kubernetes', config.kuryr_k8s_opts), +] + + +def list_kuryr_opts(): + """Return a list of oslo_config options available in Kuryr service. + + Each element of the list is a tuple. The first element is the name of the + group under which the list of elements in the second element will be + registered. A group name of None corresponds to the [DEFAULT] group in + config files. + + This function is also discoverable via the 'kuryr' entry point under + the 'oslo_config.opts' namespace. + + The purpose of this is to allow tools like the Oslo sample config file + generator to discover the options exposed to users by Kuryr. + + :returns: a list of (group_name, opts) tuples + """ + + return [(k, copy.deepcopy(o)) for k, o in _kuryr_k8s_opts] diff --git a/kuryr_kubernetes/server.py b/kuryr_kubernetes/server.py new file mode 100644 index 000000000..954fe849b --- /dev/null +++ b/kuryr_kubernetes/server.py @@ -0,0 +1,59 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +import sys +import time + +from kuryr.lib._i18n import _LI +from oslo_log import log as logging +from oslo_service import service + +from kuryr_kubernetes import config + + +LOG = logging.getLogger(__name__) + + +class KuryrK8sService(service.Service): + + def __init__(self): + super(KuryrK8sService, self).__init__() + + def start(self): + # TODO(devvesa): Remove this line as soon as it does anything + LOG.info(_LI("I am doing nothing")) + try: + while(True): + time.sleep(5) + # TODO(devvesa): Remove this line as soon as does anything + LOG.info(_LI("Keep doing nothing")) + finally: + sys.exit(1) + + def wait(self): + """Waits for K8sController to complete.""" + super(KuryrK8sService, self).wait() + + def stop(self, graceful=False): + """Stops the event loop if it's not stopped already.""" + super(KuryrK8sService, self).stop(graceful) + + +def start(): + config.init(sys.argv[1:]) + config.setup_logging() + kuryrk8s_launcher = service.launch(config.CONF, KuryrK8sService()) + kuryrk8s_launcher.wait() + + +if __name__ == '__main__': + start() diff --git a/requirements.txt b/requirements.txt index e5a2676d2..7f0e84c1a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,16 +2,4 @@ # of appearance. Changing the order has an impact on the overall integration # process, which may cause wedges in the gate later. -pbr>=1.6 # Apache-2.0 -Babel>=2.3.4 # BSD -Flask<1.0,>=0.10 # BSD -jsonschema!=2.5.0,<3.0.0,>=2.0.0 # MIT -netaddr!=0.7.16,>=0.7.12 # BSD -oslo.concurrency>=3.8.0 # Apache-2.0 -oslo.log>=1.14.0 # Apache-2.0 -oslo.serialization>=1.10.0 # Apache-2.0 -oslo.utils>=3.5.0 # Apache-2.0 -python-neutronclient>=4.2.0 # Apache-2.0 -pyroute2>=0.3.10 # Apache-2.0 (+ dual licensed GPL2) -os-client-config>=1.13.1 # Apache-2.0 -neutron-lib>=0.1.0 # Apache-2.0 +-e git://github.com/openstack/kuryr.git@master#egg=kuryr_lib diff --git a/setup.cfg b/setup.cfg index 7522fecd7..0ff6f5b1a 100644 --- a/setup.cfg +++ b/setup.cfg @@ -13,15 +13,20 @@ classifier = License :: OSI Approved :: Apache Software License Operating System :: POSIX :: Linux Programming Language :: Python - Programming Language :: Python :: 2 - Programming Language :: Python :: 2.7 Programming Language :: Python :: 3 - Programming Language :: Python :: 3.3 Programming Language :: Python :: 3.4 +[entry_points] +oslo.config.opts = + kuryr_kubernetes = kuryr_kubernetes.opts:list_kuryr_opts + kuryr_lib = kuryr.lib.opts:list_kuryr_opts + +console_scripts = + kuryr-k8s = kuryr_kubernetes.server:start + [files] packages = - kuryr-kubernetes + kuryr_kubernetes [build_sphinx] source-dir = doc/source diff --git a/tox.ini b/tox.ini index e7412b12d..e391e5040 100644 --- a/tox.ini +++ b/tox.ini @@ -73,4 +73,4 @@ import_exceptions = neutron.i18n local-check-factory = neutron_lib.hacking.checks.factory [testenv:genconfig] -commands = oslo-config-generator --config-file=etc/kuryr-config-generator.conf +commands = oslo-config-generator --config-file=etc/kuryr-k8s-config-generator.conf