First round of fixes

Apply fixes requested inside of PR.

Signed-off-by: Flavio Castelli <fcastelli@suse.com>
This commit is contained in:
Flavio Castelli 2017-02-23 10:17:03 +01:00
parent 1834f8695e
commit 925cab88a0
No known key found for this signature in database
GPG Key ID: F1020D69DC004F48
2 changed files with 40 additions and 59 deletions

View File

@ -11,7 +11,7 @@ Module for handling kubernetes calls.
''' '''
# Import Python Futures # Import Python Futures
from __future__ import absolute_import from __future__ import absolute_import
from six import iteritems from salt.ext.six import iteritems
from salt.exceptions import CommandExecutionError from salt.exceptions import CommandExecutionError
import salt.utils import salt.utils
import salt.utils.templates import salt.utils.templates
@ -54,7 +54,7 @@ def ping():
status = True status = True
try: try:
nodes() nodes()
except: except Exception:
status = False status = False
return status return status
@ -113,9 +113,7 @@ def show_deployment(name, namespace):
if exc.status == 404: if exc.status == 404:
return None return None
else: else:
print( log.exception(exc)
"Exception when calling "
"ExtensionsV1beta1Api->read_namespaced_deployment: %s\n" % exc)
raise exc raise exc
@ -138,9 +136,7 @@ def show_service(name, namespace):
if exc.status == 404: if exc.status == 404:
return None return None
else: else:
print( log.exception(exc)
"Exception when calling CoreV1Api->read_namespaced_service: "
"%s\n" % exc)
raise exc raise exc
@ -168,10 +164,7 @@ def delete_deployment(name, namespace):
if exc.status == 404: if exc.status == 404:
return None return None
else: else:
print( log.exception(exc)
"Exception when calling "
"ExtensionsV1beta1Api->delete_namespaced_deployment: "
"%s\n" % exc)
raise exc raise exc
@ -197,9 +190,7 @@ def delete_service(name, namespace):
if exc.status == 404: if exc.status == 404:
return None return None
else: else:
print( log.exception(exc)
"Exception when calling CoreV1Api->delete_namespaced_service: "
"%s\n" % exc)
raise exc raise exc
@ -238,10 +229,7 @@ def create_deployment(
if exc.status == 404: if exc.status == 404:
return None return None
else: else:
print( log.exception(exc)
"Exception when calling "
"ExtensionsV1beta1Api->create_namespaced_deployment: "
"%s\n" % exc)
raise exc raise exc
@ -282,9 +270,7 @@ def create_service(
if exc.status == 404: if exc.status == 404:
return None return None
else: else:
print( log.exception(exc)
"Exception when calling CoreV1Api->create_namespaced_service: "
"%s\n" % exc)
raise exc raise exc
@ -323,10 +309,7 @@ def replace_deployment(name,
if exc.status == 404: if exc.status == 404:
return None return None
else: else:
print( log.exception(exc)
"Exception when calling "
"ExtensionsV1beta1Api->replace_namespaced_deployment: "
"%s\n" % exc)
raise exc raise exc
@ -356,8 +339,8 @@ def replace_service(name,
# Some attributes have to be preserved # Some attributes have to be preserved
# otherwise exceptions will be thrown # otherwise exceptions will be thrown
body.spec.cluster_ip = old_service["spec"]["cluster_ip"] body.spec.cluster_ip = old_service['spec']['cluster_ip']
body.metadata.resource_version = old_service["metadata"]["resource_version"] body.metadata.resource_version = old_service['metadata']['resource_version']
_setup_conn() _setup_conn()
@ -371,9 +354,7 @@ def replace_service(name,
if exc.status == 404: if exc.status == 404:
return None return None
else: else:
print( log.exception(exc)
"Exception when calling "
"CoreV1Api->replace_namespaced_service: %s\n" % exc)
raise exc raise exc
@ -394,7 +375,7 @@ def __create_object_body(kind,
sfn = __salt__['cp.cache_file'](source, saltenv) sfn = __salt__['cp.cache_file'](source, saltenv)
if not sfn: if not sfn:
raise CommandExecutionError( raise CommandExecutionError(
"Source file \'{0}\' not found".format(source)) 'Source file \'{0}\' not found'.format(source))
with salt.utils.fopen(sfn, 'r') as src: with salt.utils.fopen(sfn, 'r') as src:
contents = src.read() contents = src.read()
@ -417,8 +398,8 @@ def __create_object_body(kind,
if not data['result']: if not data['result']:
# Failed to render the template # Failed to render the template
raise CommandExecutionError( raise CommandExecutionError(
"Failed to render file path with error: " 'Failed to render file path with error: '
"%s" % data['data']) '%s' % data['data'])
contents = data['data'].encode('utf-8') contents = data['data'].encode('utf-8')
else: else:
@ -433,8 +414,8 @@ def __create_object_body(kind,
src_obj['kind'] != kind src_obj['kind'] != kind
): ):
raise CommandExecutionError( raise CommandExecutionError(
"The source file should define only " 'The source file should define only '
"a {0} object".format(kind)) 'a {0} object'.format(kind))
if 'metadata' in src_obj: if 'metadata' in src_obj:
metadata = src_obj['metadata'] metadata = src_obj['metadata']
@ -447,9 +428,9 @@ def __create_object_body(kind,
def __dict_to_object_meta(name, namespace, metadata): def __dict_to_object_meta(name, namespace, metadata):
""" '''
Converts a dictionary into kubernetes ObjectMetaV1 instance. Converts a dictionary into kubernetes ObjectMetaV1 instance.
""" '''
meta_obj = kubernetes.client.V1ObjectMeta() meta_obj = kubernetes.client.V1ObjectMeta()
meta_obj.namespace = namespace meta_obj.namespace = namespace
for key, value in iteritems(metadata): for key, value in iteritems(metadata):
@ -458,17 +439,17 @@ def __dict_to_object_meta(name, namespace, metadata):
if meta_obj.name != name: if meta_obj.name != name:
log.warning( log.warning(
"The object already has a name attribute, overwriting it with " 'The object already has a name attribute, overwriting it with '
"the one defined inside of salt") 'the one defined inside of salt')
meta_obj.name = name meta_obj.name = name
return meta_obj return meta_obj
def __dict_to_deployment_spec(spec): def __dict_to_deployment_spec(spec):
""" '''
Converts a dictionary into kubernetes V1beta1DeploymentSpec instance. Converts a dictionary into kubernetes V1beta1DeploymentSpec instance.
""" '''
spec_obj = kubernetes.client.V1beta1DeploymentSpec() spec_obj = kubernetes.client.V1beta1DeploymentSpec()
for key, value in iteritems(spec): for key, value in iteritems(spec):
if hasattr(spec_obj, key): if hasattr(spec_obj, key):
@ -478,12 +459,12 @@ def __dict_to_deployment_spec(spec):
def __dict_to_service_spec(spec): def __dict_to_service_spec(spec):
""" '''
Converts a dictionary into kubernetes V1ServiceSpec instance. Converts a dictionary into kubernetes V1ServiceSpec instance.
""" '''
spec_obj = kubernetes.client.V1ServiceSpec() spec_obj = kubernetes.client.V1ServiceSpec()
for key, value in iteritems(spec): for key, value in iteritems(spec):
if key == "ports": if key == 'ports':
spec_obj.ports = [] spec_obj.ports = []
for port in value: for port in value:
kube_port = kubernetes.client.V1ServicePort() kube_port = kubernetes.client.V1ServicePort()

View File

@ -87,7 +87,7 @@ def _error(ret, err_msg):
return ret return ret
def deployment_absent(name, namespace="default"): def deployment_absent(name, namespace='default'):
''' '''
Ensures that the named deployment is absent from the given namespace. Ensures that the named deployment is absent from the given namespace.
@ -128,11 +128,11 @@ def deployment_absent(name, namespace="default"):
def deployment_present( def deployment_present(
name, name,
namespace="default", namespace='default',
metadata=None, metadata=None,
spec=None, spec=None,
source="", source='',
template=""): template=''):
''' '''
Ensures that the named deployment is present inside of the specified Ensures that the named deployment is present inside of the specified
namespace with the given metadata and spec. namespace with the given metadata and spec.
@ -142,7 +142,7 @@ def deployment_present(
The name of the deployment. The name of the deployment.
namespace namespace
The namespace holding the deployment. The "default" one is going to be The namespace holding the deployment. The 'default' one is going to be
used unless a different one is specified. used unless a different one is specified.
metadata metadata
@ -190,7 +190,7 @@ def deployment_present(
source=source, source=source,
template=template, template=template,
saltenv=__env__) saltenv=__env__)
ret['changes']["{}.{}".format(namespace, name)] = { ret['changes']['{}.{}'.format(namespace, name)] = {
'old': {}, 'old': {},
'new': res} 'new': res}
else: else:
@ -202,7 +202,7 @@ def deployment_present(
ret['result'] = None ret['result'] = None
return ret return ret
# TODO: improve checks # TODO: improve checks
log.info("Forcing the recreation of the deploymentv") log.info('Forcing the recreation of the deploymentv')
res = __salt__['kubernetes.replace_deployment']( res = __salt__['kubernetes.replace_deployment'](
name=name, name=name,
namespace=namespace, namespace=namespace,
@ -219,11 +219,11 @@ def deployment_present(
def service_present( def service_present(
name, name,
namespace="default", namespace='default',
metadata=None, metadata=None,
spec=None, spec=None,
source="", source='',
template=""): template=''):
''' '''
Ensures that the named service is present inside of the specified namespace Ensures that the named service is present inside of the specified namespace
with the given metadata and spec. with the given metadata and spec.
@ -233,7 +233,7 @@ def service_present(
The name of the service. The name of the service.
namespace namespace
The namespace holding the service. The "default" one is going to be The namespace holding the service. The 'default' one is going to be
used unless a different one is specified. used unless a different one is specified.
metadata metadata
@ -281,7 +281,7 @@ def service_present(
source=source, source=source,
template=template, template=template,
saltenv=__env__) saltenv=__env__)
ret['changes']["{}.{}".format(namespace, name)] = { ret['changes']['{}.{}'.format(namespace, name)] = {
'old': {}, 'old': {},
'new': res} 'new': res}
else: else:
@ -293,7 +293,7 @@ def service_present(
ret['result'] = None ret['result'] = None
return ret return ret
# TODO: improve checks # TODO: improve checks
log.info("Forcing the recreation of the service") log.info('Forcing the recreation of the service')
res = __salt__['kubernetes.replace_service']( res = __salt__['kubernetes.replace_service'](
name=name, name=name,
namespace=namespace, namespace=namespace,
@ -309,7 +309,7 @@ def service_present(
return ret return ret
def service_absent(name, namespace="default"): def service_absent(name, namespace='default'):
''' '''
Ensures that the named service is absent from the given namespace. Ensures that the named service is absent from the given namespace.