From fb5b452707a004967d7b27abc5d515a03278d446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Mon, 14 May 2018 16:22:00 +0200 Subject: 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 -- cgit v1.2.3 From d349c5d4f502ba31e33a09c6e8d3af2d14f6196f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Mon, 14 May 2018 16:27:22 +0200 Subject: fix test file --- tests/test.yml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test.yml b/tests/test.yml index fb0d9b8..490c967 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -3,6 +3,9 @@ remote_user: root roles: - role: ansible-kong-app - kong_app_admin_url: http://127.0.0.1:8000 - kong_app_service_name: toto - kong_app_service_url: http://example.com + kong_servers: + - kong_app_admin_url: http://127.0.0.1:8000 + kong_app_admin_apikey: "" + services: + - url: http://example.com + name: test -- cgit v1.2.3 From f9fca1285e7a0e996a5b2db7b807ad7aff63f0d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Tue, 15 May 2018 16:43:03 +0200 Subject: target might be marked as absent if unhealthy --- tasks/upstream.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tasks/upstream.yml b/tasks/upstream.yml index e944c82..43c1baf 100644 --- a/tasks/upstream.yml +++ b/tasks/upstream.yml @@ -50,6 +50,6 @@ method: DELETE headers: apikey: "{{ server.kong_app_admin_apikey }}" - status_code: 204 + status_code: 204,404 with_items: "{{ kong_app_service_targets_check.json.data }}" when: not kong_app_service_targets_check is skipped -- cgit v1.2.3 From e39a878d84f3b3ec5265d332e5e06ee0edd55418 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Duchaussois?= Date: Wed, 16 May 2018 15:29:57 +0200 Subject: add more info on README --- README.md | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5853226..8db9391 100644 --- a/README.md +++ b/README.md @@ -15,12 +15,14 @@ Role Variables * `kong_app_admin_apikey` the apikey to use kong admin api. Default to "" * `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 + * `url` the url of the backend of the app, mandatory. May refer to an upstream by its name (https://upstream\_name/path) + * `upstream` if the url reference an upstream a dict with the configuration, optional + * `conf` the configuration as expected by kong for an upstream creation + * `name` mandatory name for the upstream + * `healthchecks` optional healthchecks configuration as expected by kong api * `targets` an array of dict defining a target for kong - * `target` the host:port to reach the target - * `weight` the weight of the target + * `target` the host:port to reach the target (mandatory) + * `weight` the weight of the target (optional) * `plugins` An array of plugins to activate with their name and config in a dict * `name` * `config` -- cgit v1.2.3