Add kuryr-k8s-sanity command

It would be beneficial for the project to have it's own sanity check
command. Where we can verify the configuration used for kuryr is
supported.

Change-Id: I1a11694e938e00be653697e8fd96e071faacc96a
This commit is contained in:
Robin Cernin 2021-08-19 12:06:22 +10:00
parent 990ba608d6
commit 9c33e71ccf
4 changed files with 191 additions and 0 deletions

View File

View File

@ -0,0 +1,85 @@
# Copyright (c) 2021 OpenStack Foundation.
# All Rights Reserved.
#
# 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.
from oslo_config import cfg
from oslo_log import log as logging
from kuryr_kubernetes import config
CONF = config.CONF
LOG = logging.getLogger(__name__)
def _logger():
if cfg.CONF.sanity_check_error:
return LOG.error
else:
return LOG.warning
def ports_pool_min_max():
try:
if not cfg.CONF.vif_pool.ports_pool_max:
return True
pool_max = cfg.CONF.vif_pool.ports_pool_max
pool_min = cfg.CONF.vif_pool.ports_pool_min
if pool_max < pool_min:
_logger()(f'The current configuration of ports_pool_min '
f'"{pool_min}" and ports_pool_max "{pool_max}" '
f'may cause infinite loop of creating '
f'and deleting ports.')
return False
except (OSError, RuntimeError, IndexError, ValueError) as e:
LOG.debug("Exception while checking ports_pool_max. "
"Exception: %s", e)
return False
return True
def ports_pool_min_batch():
try:
pool_min = cfg.CONF.vif_pool.ports_pool_min
pool_batch = cfg.CONF.vif_pool.ports_pool_batch
if pool_min > pool_batch:
_logger()(f'The current configuration of ports_pool_min '
f'"{pool_min}" and ports_pool_batch "{pool_batch}" '
f'may cause kuryr to send multiple unnecessary '
f'bulk ports creation requests. ')
return False
except (OSError, RuntimeError, IndexError, ValueError) as e:
LOG.debug("Exception while checking ports_pool_batch. "
"Exception: %s", e)
return False
return True
def ports_pool_max_batch():
try:
if not cfg.CONF.vif_pool.ports_pool_max:
return True
pool_max = cfg.CONF.vif_pool.ports_pool_max
pool_batch = cfg.CONF.vif_pool.ports_pool_batch
if pool_max < pool_batch:
_logger()(f'The current configuration of ports_pool_max '
f'"{pool_max}" and ports_pool_batch "{pool_batch}" '
f'may cause kuryr to create the '
f'ports and then delete them immediately.')
return False
except (OSError, RuntimeError, IndexError, ValueError) as e:
LOG.debug("Exception while checking ports_pool_batch. "
"Exception: %s", e)
return False
return True

View File

@ -0,0 +1,105 @@
# Copyright (c) 2021 OpenStack Foundation.
# All Rights Reserved.
#
# 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.
"""
CLI interface for kuryr sanity commands.
"""
import sys
from oslo_config import cfg
from oslo_log import log as logging
from kuryr_kubernetes.cmd.sanity import checks
from kuryr_kubernetes import config
from kuryr_kubernetes.controller.drivers import vif_pool # noqa
LOG = logging.getLogger(__name__)
class BoolOptCallback(cfg.BoolOpt):
def __init__(self, name, callback, **kwargs):
if 'default' not in kwargs:
kwargs['default'] = False
self.callback = callback
super(BoolOptCallback, self).__init__(name, **kwargs)
def check_ports_pool_min_max():
result = checks.ports_pool_min_max()
if not result:
LOG.warning("The ports_pool_max is enabled, "
"the ports_pool_min should be smaller than "
"ports_pool_max. Either disable ports_pool_max "
"setting it to 0 or increase it's value.")
return result
def check_ports_pool_min_batch():
result = checks.ports_pool_min_batch()
if not result:
LOG.warning("The ports_pool_min should be lower than "
"ports_pool_batch. Please decrease it's value.")
return result
def check_ports_pool_max_batch():
result = checks.ports_pool_max_batch()
if not result:
LOG.warning("The ports_pool_max is enabled, "
"the ports_pool_max should be higher than "
"ports_pool_batch. Either disable ports_pool_max "
"setting it to 0 or decrease it's value.")
return result
# Define CLI opts to test specific features, with a callback for the test
OPTS = [
BoolOptCallback('vif_pool_min_max', check_ports_pool_min_max,
default=False,
help='Check configuration sanity of ports_pool_min and '
'ports_pool_max.'),
BoolOptCallback('vif_pool_min_batch', check_ports_pool_min_batch,
default=False,
help='Check configuration sanity of ports_pool_min and '
'ports_pool_batch.'),
BoolOptCallback('vif_pool_max_batch', check_ports_pool_max_batch,
default=False,
help='Check configuration sanity of ports_pool_max and '
'ports_pool_batch.'),
]
CLI_OPTS = [
cfg.BoolOpt('sanity_check_error', default=False,
help='If this flag is configured, the sanity command fails '
'if any of the sanity tests fails.'),
]
def all_tests_passed():
results = [opt.callback() for opt in OPTS if cfg.CONF.get(opt.name)]
return all(results)
def main():
cfg.CONF.register_cli_opts(OPTS)
cfg.CONF.register_cli_opts(CLI_OPTS)
config.init(sys.argv[1:], default_config_files=['/etc/kuryr/kuryr.conf'])
config.setup_logging()
return 0 if all_tests_passed() else 1
if __name__ == '__main__':
main()

View File

@ -31,6 +31,7 @@ console_scripts =
kuryr-k8s-controller = kuryr_kubernetes.cmd.eventlet.controller:start kuryr-k8s-controller = kuryr_kubernetes.cmd.eventlet.controller:start
kuryr-daemon = kuryr_kubernetes.cmd.daemon:start kuryr-daemon = kuryr_kubernetes.cmd.daemon:start
kuryr-cni = kuryr_kubernetes.cmd.cni:run kuryr-cni = kuryr_kubernetes.cmd.cni:run
kuryr-k8s-sanity = kuryr_kubernetes.cmd.sanity_checks:main
kuryr-k8s-status = kuryr_kubernetes.cmd.status:main kuryr-k8s-status = kuryr_kubernetes.cmd.status:main
kuryr_kubernetes.vif_translators = kuryr_kubernetes.vif_translators =