]> git.immae.eu Git - github/fretlink/ansible-kong-app.git/commitdiff
Merge pull request #5 from gaetanfl/support_backend v0.4
authorGaëtan <36162164+gaetanfl@users.noreply.github.com>
Thu, 17 May 2018 15:11:14 +0000 (17:11 +0200)
committerGitHub <noreply@github.com>
Thu, 17 May 2018 15:11:14 +0000 (17:11 +0200)
Support ring-balancer

README.md
tasks/kong-app.yml
tasks/upstream.yml [new file with mode: 0644]
tests/test.yml

index bdf665e658cf2d84b43cd381af55973ad989726e..8db9391ee1bc4e176be41a4ea6a29c8b3417d8f7 100644 (file)
--- a/README.md
+++ b/README.md
@@ -15,7 +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
+    * `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 (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`
index bff2f46dc06658d05f71c7a43f5a9a0e0595ba9f..975e2cdffb99458c51f75f5a98fb3accc53b89d1 100644 (file)
@@ -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 (file)
index 0000000..43c1baf
--- /dev/null
@@ -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,404
+  with_items: "{{ kong_app_service_targets_check.json.data }}"
+  when: not kong_app_service_targets_check is skipped
index fb0d9b8ca885149b220f5bfd98e676b054f19e35..490c967d11ac143ccbc6a7960e00f130e892ad1f 100644 (file)
@@ -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