From fb5b452707a004967d7b27abc5d515a03278d446 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Ga=C3=ABtan=20Duchaussois?= Date: Mon, 14 May 2018 16:22:00 +0200 Subject: [PATCH] add support for upstream with multiple targets --- README.md | 5 +++++ tasks/kong-app.yml | 4 ++++ tasks/upstream.yml | 55 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 tasks/upstream.yml diff --git a/README.md b/README.md index bdf665e..5853226 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,11 @@ Role Variables * `services` an array of services to setup (default to []) * `name` the name of the service to create for this app, mandatory * `url` the url of the backend of the app, mandatory + * `upstream` if the url reference an upstream a dict with the configuration + * `conf` the configuration as expected by kong for an upstream creataion + * `targets` an array of dict defining a target for kong + * `target` the host:port to reach the target + * `weight` the weight of the target * `plugins` An array of plugins to activate with their name and config in a dict * `name` * `config` diff --git a/tasks/kong-app.yml b/tasks/kong-app.yml index bff2f46..975e2cd 100644 --- a/tasks/kong-app.yml +++ b/tasks/kong-app.yml @@ -1,3 +1,7 @@ +- name: Upstream Creation + import_tasks: upstream.yml + when: service.upstream is defined + - name: Check if service exists uri: url: "{{ server.kong_app_admin_url }}/services/{{ service.name }}" diff --git a/tasks/upstream.yml b/tasks/upstream.yml new file mode 100644 index 0000000..e944c82 --- /dev/null +++ b/tasks/upstream.yml @@ -0,0 +1,55 @@ +- name: Set upstream variable + set_fact: + upstream: "{{ service.upstream }}" + +- name: Check if upstream exists + uri: + url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}" + method: GET + headers: + apikey: "{{ server.kong_app_admin_apikey }}" + status_code: 200,404 + register: kong_app_check_upstream + +- name: Update or Create upstream + uri: + url: "{{ server.kong_app_admin_url }}/upstreams/{{ exists_upstream | ternary('', upstream.conf.name) }}" + method: "{{ exists_upstream | ternary('POST', 'PATCH') }}" + body: "{{ upstream.conf | to_json }}" + status_code: 200,201 + headers: + apikey: "{{ server.kong_app_admin_apikey }}" + Content-Type: application/json + vars: + exists_upstream: "{{ kong_app_check_upstream.status|default(404) == 404 }}" + +- name: Get upstream targets + uri: + url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}/targets" + method: GET + headers: + apikey: "{{ server.kong_app_admin_apikey }}" + register: kong_app_service_targets_check + +- name: Create targets + uri: + url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}/targets" + method: POST + body: "{{ target | to_json }}" + headers: + apikey: "{{ server.kong_app_admin_apikey }}" + Content-Type: application/json + status_code: 201 + with_items: "{{ upstream.targets | default([]) }}" + loop_control: + loop_var: target + +- name: Delete old targets + uri: + url: "{{ server.kong_app_admin_url }}/upstreams/{{ upstream.conf.name }}/targets/{{ item.id }}" + method: DELETE + headers: + apikey: "{{ server.kong_app_admin_apikey }}" + status_code: 204 + with_items: "{{ kong_app_service_targets_check.json.data }}" + when: not kong_app_service_targets_check is skipped -- 2.41.0