Clean imports in code

In some part in the code we import objects. In the Openstack style
guidelines they recommend to import only modules.

http://docs.openstack.org/developer/hacking/#imports

TrivialFix

Change-Id: I8bd10114b9771637dcca9af04d6970afc9c36ad0
This commit is contained in:
Nguyen Hung Phuong 2016-09-12 14:11:02 +07:00
parent a488ca210a
commit 3d80a2fee8
5 changed files with 49 additions and 42 deletions

View File

@ -16,7 +16,7 @@ import sys
from cliff import app from cliff import app
from cliff import commandmanager from cliff import commandmanager
from cliff.help import HelpAction from cliff import help
from kolla_kubernetes.version import version_info from kolla_kubernetes.version import version_info
@ -47,7 +47,7 @@ class KollaKubernetesApp(app.App):
def _print_help(self): def _print_help(self):
"""Generate the help string using cliff.help.HelpAction.""" """Generate the help string using cliff.help.HelpAction."""
action = HelpAction(None, None, default=self) action = help.HelpAction(None, None, default=self)
action(self.parser, self.options, None, None) action(self.parser, self.options, None, None)
def initialize_app(self, argv): def initialize_app(self, argv):

View File

@ -12,11 +12,11 @@
from cliff import command from cliff import command
from kolla_kubernetes.app import KollaKubernetesApp from kolla_kubernetes import app
class KollaKubernetesBaseCommand(command.Command): class KollaKubernetesBaseCommand(command.Command):
def get_global_args(self): def get_global_args(self):
"""Provides a method to access global parsed options""" """Provides a method to access global parsed options"""
return KollaKubernetesApp.Get().get_parsed_options() return app.KollaKubernetesApp.Get().get_parsed_options()

View File

@ -19,19 +19,15 @@ import sys
import tempfile import tempfile
import yaml import yaml
from kolla_kubernetes.commands.base_command import KollaKubernetesBaseCommand from kolla_kubernetes.commands import base_command
from kolla_kubernetes.pathfinder import PathFinder from kolla_kubernetes import pathfinder
from kolla_kubernetes.service_resources import KollaKubernetesResources from kolla_kubernetes import service_resources
from kolla_kubernetes.service_resources import Service from kolla_kubernetes import utils
from kolla_kubernetes.utils import ExecUtils
from kolla_kubernetes.utils import FileUtils
from kolla_kubernetes.utils import JinjaUtils
from kolla_kubernetes.utils import YamlUtils
KKR = KollaKubernetesResources.Get() KKR = service_resources.KollaKubernetesResources.Get()
class ResourceBase(KollaKubernetesBaseCommand): class ResourceBase(base_command.KollaKubernetesBaseCommand):
"""Create, delete, or query status for kolla-kubernetes resources""" """Create, delete, or query status for kolla-kubernetes resources"""
def get_parser(self, prog_name, skip_action=False): def get_parser(self, prog_name, skip_action=False):
@ -40,25 +36,30 @@ class ResourceBase(KollaKubernetesBaseCommand):
parser.add_argument( parser.add_argument(
"action", "action",
metavar="<action>", metavar="<action>",
help=("One of [%s]" % ("|".join(Service.VALID_ACTIONS))) help=("One of [%s]" % ("|".join(service_resources.Service.
VALID_ACTIONS)))
) )
parser.add_argument( parser.add_argument(
"resource_type", "resource_type",
metavar="<resource-type>", metavar="<resource-type>",
help=("One of [%s]" % ("|".join(Service.VALID_RESOURCE_TYPES))) help=("One of [%s]" % ("|".join(service_resources.Service.
VALID_RESOURCE_TYPES)))
) )
return parser return parser
def validate_args(self, args, skip_action=False): def validate_args(self, args, skip_action=False):
if not skip_action and args.action not in Service.VALID_ACTIONS: if not skip_action and args.action not in service_resources.Service.\
VALID_ACTIONS:
msg = ("action [{}] not in valid actions [{}]".format( msg = ("action [{}] not in valid actions [{}]".format(
args.action, args.action,
"|".join(Service.VALID_ACTIONS))) "|".join(service_resources.Service.VALID_ACTIONS)))
raise Exception(msg) raise Exception(msg)
if args.resource_type not in Service.VALID_RESOURCE_TYPES: if args.resource_type not in service_resources.Service.\
VALID_RESOURCE_TYPES:
msg = ("resource_type [{}] not in valid resource_types [{}]" msg = ("resource_type [{}] not in valid resource_types [{}]"
.format(args.resource_type, .format(args.resource_type,
"|".join(Service.VALID_RESOURCE_TYPES))) "|".join(service_resources.Service.
VALID_RESOURCE_TYPES)))
raise Exception(msg) raise Exception(msg)
@ -122,6 +123,7 @@ class ResourceTemplate(ResourceBase):
def take_action(self, args, skip_and_return=False): def take_action(self, args, skip_and_return=False):
# Validate input arguments # Validate input arguments
self.validate_args(args) self.validate_args(args)
multi = len(args.resource_name) != 1 multi = len(args.resource_name) != 1
multidoc = { multidoc = {
'apiVersion': 'v1', 'apiVersion': 'v1',
@ -147,7 +149,7 @@ class ResourceTemplate(ResourceBase):
# handle the debug option --print-jinja-vars # handle the debug option --print-jinja-vars
if args.print_jinja_vars is True: if args.print_jinja_vars is True:
print(YamlUtils.yaml_dict_to_string(variables), print(utils.YamlUtils.yaml_dict_to_string(variables),
file=sys.stderr) file=sys.stderr)
if args.resource_type == 'configmap' and \ if args.resource_type == 'configmap' and \
@ -156,21 +158,23 @@ class ResourceTemplate(ResourceBase):
cmd = "kubectl create configmap {} -o yaml --dry-run" cmd = "kubectl create configmap {} -o yaml --dry-run"
cmd = cmd.format(resource_name) cmd = cmd.format(resource_name)
for f in PathFinder.find_config_files(resource_name): for f in pathfinder.PathFinder.find_config_files(
resource_name):
cmd += ' --from-file={}={}'.format( cmd += ' --from-file={}={}'.format(
os.path.basename(f).replace("_", "-"), f) os.path.basename(f).replace("_", "-"), f)
# Execute the command # Execute the command
out, err = ExecUtils.exec_command(cmd) out, err = utils.ExecUtils.exec_command(cmd)
y = yaml.load(out) y = yaml.load(out)
y['metadata']['namespace'] = variables[nsname] y['metadata']['namespace'] = variables[nsname]
res = y res = y
else: else:
# process the template # process the template
raw_doc = JinjaUtils.render_jinja( raw_doc = utils.JinjaUtils.render_jinja(
variables, variables,
FileUtils.read_string_from_file(rt.getTemplatePath())) utils.FileUtils.read_string_from_file(
rt.getTemplatePath()))
res = yaml.load(raw_doc) res = yaml.load(raw_doc)
if args.debug_container is not None: if args.debug_container is not None:
@ -348,7 +352,7 @@ class Resource(ResourceTemplate):
args.action) args.action)
class ResourceMap(KollaKubernetesBaseCommand): class ResourceMap(base_command.KollaKubernetesBaseCommand):
"""List available kolla-kubernetes resources to be created or deleted""" """List available kolla-kubernetes resources to be created or deleted"""
# If the operator has any question on what Services have what resources, # If the operator has any question on what Services have what resources,
@ -362,7 +366,7 @@ class ResourceMap(KollaKubernetesBaseCommand):
"--resource-type", "--resource-type",
metavar="<resource-type>", metavar="<resource-type>",
help=("Filter by one of [%s]" % ( help=("Filter by one of [%s]" % (
"|".join(Service.VALID_RESOURCE_TYPES))) "|".join(service_resources.Service.VALID_RESOURCE_TYPES)))
) )
parser.add_argument( parser.add_argument(
"--service-name", "--service-name",
@ -392,7 +396,7 @@ class ResourceMap(KollaKubernetesBaseCommand):
if args.output == 'text': if args.output == 'text':
print('service[{}]'.format(s.getName())) print('service[{}]'.format(s.getName()))
for t in Service.VALID_RESOURCE_TYPES: for t in service_resources.Service.VALID_RESOURCE_TYPES:
# Skip specific resource_types if the user has defined a filter # Skip specific resource_types if the user has defined a filter
if args.resource_type is not None and args.resource_type != t: if args.resource_type is not None and args.resource_type != t:
continue continue

View File

@ -12,14 +12,14 @@
from __future__ import print_function from __future__ import print_function
from kolla_kubernetes.commands.base_command import KollaKubernetesBaseCommand from kolla_kubernetes.commands import base_command
from kolla_kubernetes.service_resources import KollaKubernetesResources from kolla_kubernetes import service_resources
from kolla_kubernetes.service_resources import Service
KKR = KollaKubernetesResources.Get()
class _ServiceCommand(KollaKubernetesBaseCommand): KKR = service_resources.KollaKubernetesResources.Get()
class _ServiceCommand(base_command.KollaKubernetesBaseCommand):
_action = None # must be set in derived classes _action = None # must be set in derived classes
@ -44,11 +44,14 @@ class _ServiceCommand(KollaKubernetesBaseCommand):
service = KKR.getServiceByName(args.service_name) service = KKR.getServiceByName(args.service_name)
if (self._action == 'bootstrap'): if (self._action == 'bootstrap'):
service.do_apply('create', Service.LEGACY_BOOTSTRAP_RESOURCES) service.do_apply(
'create', service_resources.Service.LEGACY_BOOTSTRAP_RESOURCES)
elif (self._action == 'run'): elif (self._action == 'run'):
service.do_apply('create', Service.LEGACY_RUN_RESOURCES) service.do_apply('create',
service_resources.Service.LEGACY_RUN_RESOURCES)
elif (self._action == 'kill'): elif (self._action == 'kill'):
service.do_apply('delete', Service.VALID_RESOURCE_TYPES) service.do_apply('delete',
service_resources.Service.VALID_RESOURCE_TYPES)
else: else:
raise Exception("Code Error") raise Exception("Code Error")

View File

@ -14,8 +14,7 @@ import re
from oslo_log import log from oslo_log import log
from kolla_kubernetes.utils import ExecUtils from kolla_kubernetes import utils
from kolla_kubernetes.utils import YamlUtils
LOG = log.getLogger(__name__) LOG = log.getLogger(__name__)
@ -145,7 +144,7 @@ class KubeResourceTemplateStatus(object):
self.resource_template_obj.getName()) self.resource_template_obj.getName())
# Execute the command to get the processed template output # Execute the command to get the processed template output
template_out, err = ExecUtils.exec_command(cmd) template_out, err = utils.ExecUtils.exec_command(cmd)
# Skip templates which which produce no-ops (100% whitespace) # Skip templates which which produce no-ops (100% whitespace)
# (e.g. pv templates for AWS should not use persistent # (e.g. pv templates for AWS should not use persistent
@ -184,7 +183,8 @@ class KubeResourceYamlStatus(object):
assert len(kube_resource_definition_yaml) > 0 assert len(kube_resource_definition_yaml) > 0
# Initialize internal vars # Initialize internal vars
self.y = YamlUtils.yaml_dict_from_string(kube_resource_definition_yaml) self.y = utils.YamlUtils.yaml_dict_from_string(
kube_resource_definition_yaml)
self.definition = kube_resource_definition_yaml self.definition = kube_resource_definition_yaml
self.errors = [] self.errors = []
@ -235,7 +235,7 @@ class KubeResourceYamlStatus(object):
cmd = ('echo \'{}\' | kubectl describe -f -'.format( cmd = ('echo \'{}\' | kubectl describe -f -'.format(
self.definition.replace("'", "'\\''"))) # escape for bash self.definition.replace("'", "'\\''"))) # escape for bash
out, err = ExecUtils.exec_command(cmd) out, err = utils.ExecUtils.exec_command(cmd)
# Check if kubectl returns non-zero exit status # Check if kubectl returns non-zero exit status
if err is not None: if err is not None: