From ccc3f89060b18b3532d6b054b15a7de10255671a Mon Sep 17 00:00:00 2001
From: Egor <iam@aylium.net>
Date: Sun, 21 Oct 2018 10:35:52 +0300
Subject: [PATCH] Add kube-router annotations (#3533)

---
 docs/kube-router.md                           | 17 +++++++++++++++
 .../k8s-cluster/k8s-net-kube-router.yml       |  9 ++++++++
 .../kube-router/defaults/main.yml             | 12 +++++++++++
 .../kube-router/tasks/annotate.yml            | 21 +++++++++++++++++++
 .../network_plugin/kube-router/tasks/main.yml |  5 ++++-
 5 files changed, 63 insertions(+), 1 deletion(-)
 create mode 100644 roles/network_plugin/kube-router/tasks/annotate.yml

diff --git a/docs/kube-router.md b/docs/kube-router.md
index dca749023..5d6598746 100644
--- a/docs/kube-router.md
+++ b/docs/kube-router.md
@@ -89,3 +89,20 @@ kubeadm_enabled: true
 kube_router_run_service_proxy: true
 kube_proxy_mode: ipvs
 ```
+
+## Advanced BGP Capabilities
+https://github.com/cloudnativelabs/kube-router#advanced-bgp-capabilities
+
+If you have other networking devices or SDN systems that talk BGP, kube-router will fit in perfectly.
+From a simple full node-to-node mesh to per-node peering configurations, most routing needs can be attained.
+The configuration is Kubernetes native (annotations) just like the rest of kube-router.
+
+For more details please refer to the https://github.com/cloudnativelabs/kube-router/blob/master/docs/bgp.md.
+
+Next options will set up annotations for kube-router, using `kubectl annotate` command.
+
+```
+kube_router_annotations_master: []
+kube_router_annotations_node: []
+kube_router_annotations_all: []
+```
\ No newline at end of file
diff --git a/inventory/sample/group_vars/k8s-cluster/k8s-net-kube-router.yml b/inventory/sample/group_vars/k8s-cluster/k8s-net-kube-router.yml
index 3d5cb7256..38066edec 100644
--- a/inventory/sample/group_vars/k8s-cluster/k8s-net-kube-router.yml
+++ b/inventory/sample/group_vars/k8s-cluster/k8s-net-kube-router.yml
@@ -35,3 +35,12 @@
 # Setups node CNI to allow hairpin mode, requires node reboots, see
 # https://github.com/cloudnativelabs/kube-router/blob/master/docs/user-guide.md#hairpin-mode
 # kube_router_support_hairpin_mode: false
+
+# Array of annotations for master
+# kube_router_annotations_master: []
+
+# Array of annotations for every node
+# kube_router_annotations_node: []
+
+# Array of common annotations for every node
+# kube_router_annotations_all: []
diff --git a/roles/network_plugin/kube-router/defaults/main.yml b/roles/network_plugin/kube-router/defaults/main.yml
index 5aacbc2b0..a4c6046aa 100644
--- a/roles/network_plugin/kube-router/defaults/main.yml
+++ b/roles/network_plugin/kube-router/defaults/main.yml
@@ -34,3 +34,15 @@ kube_router_peer_router_ports: ~
 # Setups node CNI to allow hairpin mode, requires node reboots, see
 # https://github.com/cloudnativelabs/kube-router/blob/master/docs/user-guide.md#hairpin-mode
 kube_router_support_hairpin_mode: false
+
+# Adds annotations to kubernetes nodes for advanced configuration of BGP Peers.
+# https://github.com/cloudnativelabs/kube-router/blob/master/docs/bgp.md
+
+# Array of annotations for master
+kube_router_annotations_master: []
+
+# Array of annotations for every node
+kube_router_annotations_node: []
+
+# Array of common annotations for every node
+kube_router_annotations_all: []
diff --git a/roles/network_plugin/kube-router/tasks/annotate.yml b/roles/network_plugin/kube-router/tasks/annotate.yml
new file mode 100644
index 000000000..18880e123
--- /dev/null
+++ b/roles/network_plugin/kube-router/tasks/annotate.yml
@@ -0,0 +1,21 @@
+---
+- name: kube-router | Add annotations on kube-master
+  command: "{{bin_dir}}/kubectl annotate --overwrite node {{ ansible_hostname }} {{ item }}"
+  with_items:
+  - "{{ kube_router_annotations_master }}"
+  delegate_to: "{{groups['kube-master'][0]}}"
+  when: kube_router_annotations_master is defined
+
+- name: kube-router | Add annotations on kube-node
+  command: "{{bin_dir}}/kubectl annotate --overwrite node {{ ansible_hostname }} {{ item }}"
+  with_items:
+  - "{{ kube_router_annotations_node }}"
+  delegate_to: "{{groups['kube-master'][0]}}"
+  when: kube_router_annotations_node is defined
+
+- name: kube-router | Add common annotations on all servers
+  command: "{{bin_dir}}/kubectl annotate --overwrite node {{ ansible_hostname }} {{ item }}"
+  with_items:
+  - "{{ kube_router_annotations_all }}"
+  delegate_to: "{{groups['kube-master'][0]}}"
+  when: kube_router_annotations_all is defined
\ No newline at end of file
diff --git a/roles/network_plugin/kube-router/tasks/main.yml b/roles/network_plugin/kube-router/tasks/main.yml
index 9fc5b0a7d..f1996313d 100644
--- a/roles/network_plugin/kube-router/tasks/main.yml
+++ b/roles/network_plugin/kube-router/tasks/main.yml
@@ -1,4 +1,7 @@
 ---
+- name: kube-router | Create annotations
+  include: annotate.yml
+  tags: annotate
 
 - name: kube-router | Copy cni plugins from hyperkube
   command: "{{ docker_bin_dir }}/docker run --rm -v /opt/cni/bin:/cnibindir {{ hyperkube_image_repo }}:{{ hyperkube_image_tag }} /bin/cp -prf /opt/cni/bin/. /cnibindir/"
@@ -14,4 +17,4 @@
 - name: kube-router | Create manifest
   template:
     src: kube-router.yml.j2
-    dest: "{{ kube_config_dir }}/kube-router.yml"
+    dest: "{{ kube_config_dir }}/kube-router.yml"
\ No newline at end of file
-- 
GitLab