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

Backport fixes for k8s module 2.6 #42525

Merged
merged 3 commits into from Jul 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/ansible/module_utils/k8s/common.py
Expand Up @@ -138,8 +138,11 @@ def get_api_client(self, **auth_params):
elif key in auth_args and value is None:
env_value = os.getenv('K8S_AUTH_{0}'.format(key.upper()), None)
if env_value is not None:
setattr(configuration, key, env_value)
auth[key] = env_value
if key == 'api_key':
setattr(configuration, key, {'authorization': "Bearer {0}".format(env_value)})
else:
setattr(configuration, key, env_value)
auth[key] = env_value

kubernetes.client.Configuration.set_default(configuration)

Expand Down
67 changes: 50 additions & 17 deletions lib/ansible/module_utils/k8s/raw.py
Expand Up @@ -23,7 +23,7 @@


try:
from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError
from openshift.dynamic.exceptions import DynamicApiError, NotFoundError, ConflictError, ForbiddenError
except ImportError:
# Exception handled in common
pass
Expand All @@ -43,10 +43,10 @@ def __init__(self, *args, **kwargs):
supports_check_mode=True,
**kwargs)

kind = self.params.pop('kind')
api_version = self.params.pop('api_version')
name = self.params.pop('name')
namespace = self.params.pop('namespace')
self.kind = self.params.pop('kind')
self.api_version = self.params.pop('api_version')
self.name = self.params.pop('name')
self.namespace = self.params.pop('namespace')
resource_definition = self.params.pop('resource_definition')
if resource_definition:
self.resource_definitions = [resource_definition]
Expand All @@ -56,11 +56,11 @@ def __init__(self, *args, **kwargs):

if not resource_definition and not src:
self.resource_definitions = [{
'kind': kind,
'apiVersion': api_version,
'kind': self.kind,
'apiVersion': self.api_version,
'metadata': {
'name': name,
'namespace': namespace
'name': self.name,
'namespace': self.namespace
}
}]

Expand All @@ -69,14 +69,13 @@ def execute_module(self):
results = []
self.client = self.get_api_client()
for definition in self.resource_definitions:
kind = definition.get('kind')
kind = definition.get('kind', self.kind)
search_kind = kind
if kind.lower().endswith('list'):
search_kind = kind[:-4]
api_version = definition.get('apiVersion')
api_version = definition.get('apiVersion', self.api_version)
resource = self.find_resource(search_kind, api_version, fail=True)
definition['kind'] = resource.kind
definition['apiVersion'] = resource.group_version
definition = self.set_defaults(resource, definition)
result = self.perform_action(resource, definition)
changed = changed or result['changed']
results.append(result)
Expand All @@ -91,17 +90,28 @@ def execute_module(self):
}
})

def set_defaults(self, resource, definition):
definition['kind'] = resource.kind
definition['apiVersion'] = resource.group_version
if not definition.get('metadata'):
definition['metadata'] = {}
if self.name and not definition['metadata'].get('name'):
definition['metadata']['name'] = self.name
if resource.namespaced and self.namespace and not definition['metadata'].get('namespace'):
definition['metadata']['namespace'] = self.namespace
return definition

def perform_action(self, resource, definition):
result = {'changed': False, 'result': {}}
state = self.params.pop('state', None)
force = self.params.pop('force', False)
state = self.params.get('state', None)
force = self.params.get('force', False)
name = definition.get('metadata', {}).get('name')
namespace = definition.get('metadata', {}).get('namespace')
existing = None

self.remove_aliases()

if definition['kind'].endswith('list'):
if definition['kind'].endswith('List'):
result['result'] = resource.get(namespace=namespace).to_dict()
result['changed'] = False
result['method'] = 'get'
Expand All @@ -111,6 +121,11 @@ def perform_action(self, resource, definition):
existing = resource.get(name=name, namespace=namespace)
except NotFoundError:
pass
except ForbiddenError as exc:
if definition['kind'] in ['Project', 'ProjectRequest'] and state != 'absent':
return self.create_project_request(definition)
self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc.body),
error=exc.status, status=exc.status, reason=exc.reason)
except DynamicApiError as exc:
self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc.body),
error=exc.status, status=exc.status, reason=exc.reason)
Expand Down Expand Up @@ -143,7 +158,10 @@ def perform_action(self, resource, definition):
self.warn("{0} was not found, but creating it returned a 409 Conflict error. This can happen \
if the resource you are creating does not directly create a resource of the same kind.".format(name))
return result
result['result'] = k8s_obj.to_dict()
except DynamicApiError as exc:
self.fail_json(msg="Failed to create object: {0}".format(exc.body),
error=exc.status, status=exc.status, reason=exc.reason)
result['result'] = k8s_obj.to_dict()
result['changed'] = True
result['method'] = 'create'
return result
Expand Down Expand Up @@ -178,3 +196,18 @@ def perform_action(self, resource, definition):
result['method'] = 'patch'
result['diff'] = diffs
return result

def create_project_request(self, definition):
definition['kind'] = 'ProjectRequest'
result = {'changed': False, 'result': {}}
resource = self.find_resource('ProjectRequest', definition['apiVersion'], fail=True)
if not self.check_mode:
try:
k8s_obj = resource.create(definition)
result['result'] = k8s_obj.to_dict()
except DynamicApiError as exc:
self.fail_json(msg="Failed to create object: {0}".format(exc.body),
error=exc.status, status=exc.status, reason=exc.reason)
result['changed'] = True
result['method'] = 'create'
return result