From 7d5301d582aca4ab405f6469bb4b3380913620d4 Mon Sep 17 00:00:00 2001 From: Nicolas Trangez Date: Fri, 27 Sep 2019 16:47:42 -0700 Subject: [PATCH] salt: add *Ingress* support to renderer/state/module --- salt/_modules/metalk8s_kubernetes.py | 92 ++++++++++++++++++++++++++ salt/_renderers/metalk8s_kubernetes.py | 15 +++++ salt/_states/metalk8s_kubernetes.py | 52 +++++++++++++++ 3 files changed, 159 insertions(+) diff --git a/salt/_modules/metalk8s_kubernetes.py b/salt/_modules/metalk8s_kubernetes.py index 65e184e130..483ae42ac4 100644 --- a/salt/_modules/metalk8s_kubernetes.py +++ b/salt/_modules/metalk8s_kubernetes.py @@ -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) diff --git a/salt/_renderers/metalk8s_kubernetes.py b/salt/_renderers/metalk8s_kubernetes.py index d4a46678be..5c6f11c91b 100644 --- a/salt/_renderers/metalk8s_kubernetes.py +++ b/salt/_renderers/metalk8s_kubernetes.py @@ -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 diff --git a/salt/_states/metalk8s_kubernetes.py b/salt/_states/metalk8s_kubernetes.py index a3fcd59fe9..d09b178070 100644 --- a/salt/_states/metalk8s_kubernetes.py +++ b/salt/_states/metalk8s_kubernetes.py @@ -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