From 5c448b6896a5584d231a9b94664dd0881d773597 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Marco=20Mart=C3=ADnez?= <marco.martinez.bautista@gmail.com>
Date: Thu, 24 Sep 2020 12:24:05 +0200
Subject: [PATCH] Add retries to update calico-rr data in etcd through
 calicoctl (#6505)

* Add retries to update calico-rr data in etcd through calicoctl

* Update update-node yaml syntax

* Add comment to clarify ansible block loop

* Remove trailing space
---
 roles/network_plugin/calico/rr/tasks/main.yml | 20 +---------
 .../calico/rr/tasks/update-node.yml           | 39 +++++++++++++++++++
 2 files changed, 41 insertions(+), 18 deletions(-)
 create mode 100644 roles/network_plugin/calico/rr/tasks/update-node.yml

diff --git a/roles/network_plugin/calico/rr/tasks/main.yml b/roles/network_plugin/calico/rr/tasks/main.yml
index 6b19e31b5..42b6144bd 100644
--- a/roles/network_plugin/calico/rr/tasks/main.yml
+++ b/roles/network_plugin/calico/rr/tasks/main.yml
@@ -2,24 +2,8 @@
 - name: Calico-rr | Pre-upgrade tasks
   include_tasks: pre.yml
 
-- name: Calico-rr | Fetch current node object
-  command: "{{ bin_dir }}/calicoctl.sh get node {{ inventory_hostname }} -ojson"
-  changed_when: false
-  register: calico_rr_node
-  until: calico_rr_node is succeeded
-  delay: "{{ retry_stagger | random + 3 }}"
-  retries: 10
-
-- name: Calico-rr | Set route reflector cluster ID
-  set_fact:
-    calico_rr_node_patched: >-
-      {{ calico_rr_node.stdout | from_json | combine({ 'spec': { 'bgp':
-      { 'routeReflectorClusterID': cluster_id }}}, recursive=True) }}
-
-- name: Calico-rr | Configure route reflector  # noqa 301 305
-  shell: "{{ bin_dir }}/calicoctl.sh replace -f-"
-  args:
-    stdin: "{{ calico_rr_node_patched | to_json }}"
+- name: Calico-rr | Configuring node tasks
+  include_tasks: update-node.yml
 
 - name: Calico-rr | Set label for route reflector  # noqa 301
   command: >-
diff --git a/roles/network_plugin/calico/rr/tasks/update-node.yml b/roles/network_plugin/calico/rr/tasks/update-node.yml
new file mode 100644
index 000000000..21c96a596
--- /dev/null
+++ b/roles/network_plugin/calico/rr/tasks/update-node.yml
@@ -0,0 +1,39 @@
+---
+# Workaround to retry a block of tasks, ansible doesn't have a direct way to do it,
+# you can follow the block loop request in: https://github.com/ansible/ansible/issues/46203
+- block:
+  - name: Set the retry count
+    set_fact:
+      retry_count: "{{ 0 if retry_count is undefined else retry_count|int + 1 }}"
+
+  - name: Calico-rr | Fetch current node object
+    command: "{{ bin_dir }}/calicoctl.sh get node {{ inventory_hostname }} -ojson"
+    changed_when: false
+    register: calico_rr_node
+    until: calico_rr_node is succeeded
+    delay: "{{ retry_stagger | random + 3 }}"
+    retries: 10
+
+  - name: Calico-rr | Set route reflector cluster ID
+    set_fact:
+      calico_rr_node_patched: >-
+        {{ calico_rr_node.stdout | from_json | combine({ 'spec': { 'bgp':
+        { 'routeReflectorClusterID': cluster_id }}}, recursive=True) }}
+
+  - name: Calico-rr | Configure route reflector  # noqa 301 305
+    shell: "{{ bin_dir }}/calicoctl.sh replace -f-"
+    args:
+      stdin: "{{ calico_rr_node_patched | to_json }}"
+
+  rescue:
+  - name: Fail if retry limit is reached
+    fail:
+      msg: Ended after 10 retries
+    when: retry_count|int == 10
+
+  - name: Retrying node configuration
+    debug:
+      msg: "Failed to configure route reflector - Retrying..."
+
+  - name: Retry node configuration
+    include_tasks: update-node.yml
-- 
GitLab