Skip to content

Commit

Permalink
salt: add *Ingress* support to renderer/state/module
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasT committed Sep 30, 2019
1 parent a0e016b commit 7d5301d
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 0 deletions.
92 changes: 92 additions & 0 deletions salt/_modules/metalk8s_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2871,3 +2871,95 @@ def replace_podsecuritypolicy(
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)


def show_ingress(name, namespace='default', **kwargs):
'''
Return the kubernetes ingres defined by name and namespace
'''
cfg = _setup_conn(**kwargs)
try:
api_instance = kubernetes.client.ExtensionsV1beta1Api()
api_response = api_instance.read_namespaced_ingress(name, namespace)

return api_response.to_dict()
except (ApiException, HTTPError) as exc:
if isinstance(exc, ApiException) and exc.status == 404:
return None
else:
log.exception(
'Exception when calling '
'ExtensionsV1beta1Api->read_namespaced_ingress'
)
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)


def create_ingress(
name,
namespace,
metadata,
spec,
**kwargs):
if not metadata:
metadata = {}
metadata['name'] = name
metadata['namespace'] = namespace
meta_obj = kubernetes.client.V1ObjectMeta(**metadata)
body = kubernetes.client.V1beta1Ingress(
metadata=meta_obj, spec=spec)

cfg = _setup_conn(**kwargs)

try:
api_instance = kubernetes.client.ExtensionsV1beta1Api()
api_response = api_instance.create_namespaced_ingress(
namespace=namespace, body=body)

return api_response.to_dict()
except (ApiException, HTTPError) as exc:
if isinstance(exc, ApiException) and exc.status == 404:
return None
else:
log.exception(
'Exception when calling '
'ExtensionsV1beta1Api->create_namespaced_ingreess'
)
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)


def replace_ingress(
name,
namespace,
metadata,
spec,
**kwargs):
if not metadata:
metadata = {}
metadata['name'] = name
metadata['namespace'] = namespace
meta_obj = kubernetes.client.V1ObjectMeta(**metadata)
body = kubernetes.client.V1beta1Ingress(
metadata=meta_obj, spec=spec)

cfg = _setup_conn(**kwargs)

try:
api_instance = kubernetes.client.ExtensionsV1beta1Api()
api_response = api_instance.replace_namespaced_ingress(name, namespace, body)

return api_response.to_dict()
except (ApiException, HTTPError) as exc:
if isinstance(exc, ApiException) and exc.status == 404:
return None
else:
log.exception(
'Exception when calling '
'ExtensionsV1beta1Api->replace_namespaced_ingress'
)
raise CommandExecutionError(exc)
finally:
_cleanup(**cfg)
15 changes: 15 additions & 0 deletions salt/_renderers/metalk8s_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,21 @@ def _handle_extensions_v1beta1_podsecuritypolicy(obj, kubeconfig, context,
}


@handle('networking.k8s.io/v1beta1', 'Ingress')
def _handle_ingress(obj, kubeconfig, context, absent):
return {
'metalk8s_kubernetes.ingress_{}'.format(
'absent' if absent else 'present'): [
{'name': obj['metadata']['name']},
{'metadata': obj['metadata']},
{'spec': obj['spec']},
{'kubeconfig': kubeconfig},
{'context': context},
{'namespace': obj['metadata']['namespace']},
]
}


del handle


Expand Down
52 changes: 52 additions & 0 deletions salt/_states/metalk8s_kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2046,3 +2046,55 @@ def podsecuritypolicy_present(
'new': res}

return ret


def ingress_present(
name,
namespace,
metadata,
spec,
**kwargs):
ret = {'name': name,
'changes': {},
'result': False,
'comment': ''}

ingress = __salt__['metalk8s_kubernetes.show_ingress'](name, namespace, **kwargs)

if ingress is None:
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'The ingress is going to be created'
return ret

res = __salt__['metalk8s_kubernetes.create_ingress'](name=name,
namespace=namespace,
metadata=metadata,
spec=spec,
**kwargs)
ret['result'] = True
ret['changes']['{0}.{1}'.format(namespace, name)] = {
'old': {},
'new': res}
else:
if __opts__['test']:
ret['result'] = None
ret['comment'] = 'The ingress is going to be replaced'
return ret

# TODO: improve checks # pylint: disable=fixme
log.info('Forcing the recreation of the ingress')
ret['comment'] = 'The ingress is already present. Forcing recreation'
res = __salt__['metalk8s_kubernetes.replace_ingress'](
name=name,
namespace=namespace,
metadata=metadata,
spec=spec,
**kwargs)

ret['result'] = True
ret['changes']['{0}.{1}'.format(namespace, name)] = {
'old': ingress,
'new': res}

return ret

0 comments on commit 7d5301d

Please sign in to comment.