From dad95c873bcae975e3e5addb162e83f83c0bc34b Mon Sep 17 00:00:00 2001
From: Wei Tie <nuaafe@gmail.com>
Date: Thu, 7 Dec 2017 23:33:29 -0800
Subject: [PATCH] Remove templating for etcd members

Use a etcd-initer init container to generate etcd args, it determines
etcd name by comparing its ip and etcd cluster ips. This way will
make etcd configuration independent to the ansible templating so
that could be easier on adding master nodes.
---
 roles/network_plugin/contiv/defaults/main.yml | 12 +-------
 roles/network_plugin/contiv/tasks/main.yml    |  6 ----
 .../contiv/templates/contiv-etcd.env.j2       |  8 ------
 .../contiv/templates/contiv-etcd.yml.j2       | 28 +++++++++++++++++--
 4 files changed, 26 insertions(+), 28 deletions(-)
 delete mode 100644 roles/network_plugin/contiv/templates/contiv-etcd.env.j2

diff --git a/roles/network_plugin/contiv/defaults/main.yml b/roles/network_plugin/contiv/defaults/main.yml
index 83d940173..b6e237df5 100644
--- a/roles/network_plugin/contiv/defaults/main.yml
+++ b/roles/network_plugin/contiv/defaults/main.yml
@@ -1,29 +1,19 @@
 ---
 
 contiv_config_dir: "{{ kube_config_dir }}/contiv"
-contiv_etcd_conf_dir: "/etc/contiv/etcd/"
+contiv_etcd_conf_dir: "/etc/contiv/etcd"
 contiv_etcd_data_dir: "/var/lib/etcd/contiv-data"
 contiv_netmaster_port: 9999
 contiv_cni_version: 0.1.0
 
 contiv_etcd_image_repo: "{{ etcd_image_repo }}"
 contiv_etcd_image_tag: "{{ etcd_image_tag }}"
-contiv_etcd_listen_ip: "{{ ip | default(ansible_default_ipv4['address']) }}"
 contiv_etcd_listen_port: 6666
 contiv_etcd_peer_port: 6667
-contiv_etcd_ad_urls: http://{{ contiv_etcd_listen_ip }}:{{ contiv_etcd_listen_port }}
-contiv_etcd_peer_urls: http://{{ contiv_etcd_listen_ip }}:{{ contiv_etcd_peer_port }}
-contiv_etcd_listen_urls:
-  - http://{{ contiv_etcd_listen_ip }}:{{ contiv_etcd_listen_port }}
-  - http://127.0.0.1:{{ contiv_etcd_listen_port }}
 contiv_etcd_endpoints: |-
   {% for host in groups['kube-master'] -%}
     contiv_etcd{{ loop.index }}=http://{{ hostvars[host]['ip'] | default(hostvars[host].ansible_default_ipv4['address']) }}:{{ contiv_etcd_peer_port }}{% if not loop.last %},{% endif %}
   {%- endfor %}
-contiv_etcd_name: |-
-  {% for host in groups['kube-master'] %}
-  {% if host == inventory_hostname -%}contiv_etcd{{ loop.index }}{%- endif %}
-  {% endfor %}
 
 # Parameters for Contiv api-proxy
 contiv_enable_api_proxy: true
diff --git a/roles/network_plugin/contiv/tasks/main.yml b/roles/network_plugin/contiv/tasks/main.yml
index f05090ffb..d9b372480 100644
--- a/roles/network_plugin/contiv/tasks/main.yml
+++ b/roles/network_plugin/contiv/tasks/main.yml
@@ -17,12 +17,6 @@
     - "{{ contiv_etcd_conf_dir }}"
     - "{{ contiv_etcd_data_dir }}"
 
-- name: Contiv | Create contiv etcd config env
-  template:
-    src: contiv-etcd.env.j2
-    dest: "{{ contiv_etcd_conf_dir }}/contiv-etcd.env"
-  when: inventory_hostname in groups['kube-master']
-
 - set_fact:
     contiv_config_dir: "{{ contiv_config_dir }}"
     contiv_enable_api_proxy: "{{ contiv_enable_api_proxy }}"
diff --git a/roles/network_plugin/contiv/templates/contiv-etcd.env.j2 b/roles/network_plugin/contiv/templates/contiv-etcd.env.j2
deleted file mode 100644
index e5f920af1..000000000
--- a/roles/network_plugin/contiv/templates/contiv-etcd.env.j2
+++ /dev/null
@@ -1,8 +0,0 @@
-# contiv etcd config
-export ETCD_DATA_DIR=/var/lib/etcd/contiv-data
-export ETCD_ADVERTISE_CLIENT_URLS={{ contiv_etcd_ad_urls }}
-export ETCD_INITIAL_ADVERTISE_PEER_URLS={{ contiv_etcd_peer_urls }}
-export ETCD_LISTEN_PEER_URLS={{ contiv_etcd_peer_urls }}
-export ETCD_LISTEN_CLIENT_URLS={{ contiv_etcd_listen_urls | join(",") }}
-export ETCD_NAME={{ contiv_etcd_name }}
-export ETCD_INITIAL_CLUSTER={{ contiv_etcd_endpoints }}
diff --git a/roles/network_plugin/contiv/templates/contiv-etcd.yml.j2 b/roles/network_plugin/contiv/templates/contiv-etcd.yml.j2
index 5e2327a3c..8060f4c01 100644
--- a/roles/network_plugin/contiv/templates/contiv-etcd.yml.j2
+++ b/roles/network_plugin/contiv/templates/contiv-etcd.yml.j2
@@ -24,12 +24,34 @@ spec:
       tolerations:
         - key: node-role.kubernetes.io/master
           effect: NoSchedule
+      initContainers:
+        - name: contiv-etcd-init
+          image: ferest/etcd-initer:latest
+          imagePullPolicy: Always
+          env:
+            - name: ETCD_INIT_ARGSFILE
+              value: '{{ contiv_etcd_conf_dir }}/contiv-etcd-args'
+            - name: ETCD_INIT_LISTEN_PORT
+              value: '{{ contiv_etcd_listen_port }}'
+            - name: ETCD_INIT_PEER_PORT
+              value: '{{ contiv_etcd_peer_port }}'
+            - name: ETCD_INIT_CLUSTER
+              value: '{{ contiv_etcd_endpoints }}'
+            - name: ETCD_INIT_DATA_DIR
+              value: '{{ contiv_etcd_data_dir }}'
+          volumeMounts:
+            - name: contiv-etcd-conf-dir
+              mountPath: {{ contiv_etcd_conf_dir }}
       containers:
         - name: contiv-etcd
           image: {{ contiv_etcd_image_repo }}:{{ contiv_etcd_image_tag }}
-          command: ["sh","-c"]
-          args:
-            - '. {{ contiv_etcd_conf_dir }}/contiv-etcd.env && /usr/local/bin/etcd'
+          command:
+            - sh
+            - -c
+            - "/usr/local/bin/etcd $(cat $ETCD_INIT_ARGSFILE)"
+          env:
+            - name: ETCD_INIT_ARGSFILE
+              value: {{ contiv_etcd_conf_dir }}/contiv-etcd-args
           volumeMounts:
             - name: contiv-etcd-conf-dir
               mountPath: {{ contiv_etcd_conf_dir }}
-- 
GitLab