Refactor flannel deployment procedure.

Current flannel version requires another deployment procedure.

Change-Id: I107ec843f4aab98c9ed1c34abbb460087cf461b4
diff --git a/kubernetes/_common.sls b/kubernetes/_common.sls
index 0e5b5ca..a4257b7 100644
--- a/kubernetes/_common.sls
+++ b/kubernetes/_common.sls
@@ -4,20 +4,6 @@
   pkg.installed:
   - names: {{ common.pkgs }}
 
-{%- if common.network.get('engine', 'none') == 'flannel' %}
-flannel-tar:
-  archive:
-    - extracted
-    - user: root
-    - name: /usr/local/src
-    - makedirs: True
-    - source: https://storage.googleapis.com/kubernetes-release/flannel/flannel-0.5.5-linux-amd64.tar.gz
-    - tar_options: v
-    - source_hash: md5=972c717254775bef528f040af804f2cc
-    - archive_format: tar
-    - if_missing: /usr/local/src/flannel/flannel-0.5.5/
-{%- endif %}
-
 {%- if common.hyperkube is defined %}
 /tmp/hyperkube:
   file.directory:
diff --git a/kubernetes/files/dockershim/default.pool b/kubernetes/files/dockershim/default.pool
index a9d2839..34ed343 100644
--- a/kubernetes/files/dockershim/default.pool
+++ b/kubernetes/files/dockershim/default.pool
@@ -21,7 +21,7 @@
 {%- else %}
 --node-labels=node-role.kubernetes.io/node=true \
 {%- endif %}
-{%- if pool.network.engine in ['calico', 'opencontrail'] %}
+{%- if pool.network.engine in ['calico', 'opencontrail', 'flannel'] %}
 --network-plugin=cni \
 --cni-bin-dir={{ pool.apiserver.get('cni_bin_dir', '/opt/cni/bin') }} \
 {%- else %}
diff --git a/kubernetes/files/flannel/default.master b/kubernetes/files/flannel/default.master
deleted file mode 100644
index 2a8b881..0000000
--- a/kubernetes/files/flannel/default.master
+++ /dev/null
@@ -1,3 +0,0 @@
-{%- from "kubernetes/map.jinja" import master with context %}
-
-DAEMON_ARGS="--etcd-endpoints={% for member in master.network.etcd.members %}http://{{ member.host }}:4001{% if not loop.last %},{% endif %}{% endfor %} --ip-masq --etcd-prefix=/kubernetes.io/network"
\ No newline at end of file
diff --git a/kubernetes/files/flannel/default.pool b/kubernetes/files/flannel/default.pool
deleted file mode 100644
index 9bf8bfb..0000000
--- a/kubernetes/files/flannel/default.pool
+++ /dev/null
@@ -1,3 +0,0 @@
-{%- from "kubernetes/map.jinja" import pool with context %}
-
-DAEMON_ARGS="--etcd-endpoints={% for member in pool.network.etcd.members %}http://{{ member.host }}:4001{% if not loop.last %},{% endif %}{% endfor %} --ip-masq --etcd-prefix=/kubernetes.io/network"
\ No newline at end of file
diff --git a/kubernetes/files/flannel/flannel.conflist b/kubernetes/files/flannel/flannel.conflist
new file mode 100644
index 0000000..9c17665
--- /dev/null
+++ b/kubernetes/files/flannel/flannel.conflist
@@ -0,0 +1,18 @@
+{
+  "name": "cbr0",
+  "plugins": [
+    {
+      "type": "flannel",
+      "delegate": {
+        "hairpinMode": true,
+        "isDefaultGateway": true
+      }
+    },
+    {
+      "type": "portmap",
+      "capabilities": {
+        "portMappings": true
+      }
+    }
+  ]
+}
diff --git a/kubernetes/files/flannel/network.json b/kubernetes/files/flannel/network.json
deleted file mode 100644
index d45fef2..0000000
--- a/kubernetes/files/flannel/network.json
+++ /dev/null
@@ -1,9 +0,0 @@
-{%- from "kubernetes/map.jinja" import master with context %}
-{
-    "Network": "{{ master.network.private_ip_range }}",
-    "SubnetLen": 24,
-    "Backend": {
-        "Type": "vxlan",
-        "VNI": 1
-     }
-}
\ No newline at end of file
diff --git a/kubernetes/files/kube-addons/flannel/flannel.yml b/kubernetes/files/kube-addons/flannel/flannel.yml
new file mode 100644
index 0000000..095dbe8
--- /dev/null
+++ b/kubernetes/files/kube-addons/flannel/flannel.yml
@@ -0,0 +1,133 @@
+{%- from "kubernetes/map.jinja" import common with context -%}
+{%- from "kubernetes/map.jinja" import master with context -%}
+---
+kind: ClusterRole
+apiVersion: rbac.authorization.k8s.io/v1beta1
+metadata:
+  name: flannel
+  labels:
+    addonmanager.kubernetes.io/mode: Reconcile
+rules:
+  - apiGroups:
+      - ""
+    resources:
+      - pods
+    verbs:
+      - get
+  - apiGroups:
+      - ""
+    resources:
+      - nodes
+    verbs:
+      - list
+      - watch
+  - apiGroups:
+      - ""
+    resources:
+      - nodes/status
+    verbs:
+      - patch
+---
+kind: ClusterRoleBinding
+apiVersion: rbac.authorization.k8s.io/v1beta1
+metadata:
+  name: flannel
+  labels:
+    addonmanager.kubernetes.io/mode: Reconcile
+roleRef:
+  apiGroup: rbac.authorization.k8s.io
+  kind: ClusterRole
+  name: flannel
+subjects:
+- kind: ServiceAccount
+  name: flannel
+  namespace: kube-system
+---
+apiVersion: v1
+kind: ServiceAccount
+metadata:
+  name: flannel
+  namespace: kube-system
+  labels:
+    addonmanager.kubernetes.io/mode: Reconcile
+---
+kind: ConfigMap
+apiVersion: v1
+metadata:
+  name: kube-flannel-cfg
+  namespace: kube-system
+  labels:
+    addonmanager.kubernetes.io/mode: Reconcile
+    tier: node
+    app: flannel
+data:
+  net-conf.json: |
+    {
+      "Network": "{{ master.network.private_ip_range }}",
+      "Backend": {
+        "Type": "vxlan"
+      }
+    }
+---   
+apiVersion: extensions/v1beta1
+kind: DaemonSet
+metadata:
+  name: kube-flannel-ds
+  namespace: kube-system
+  labels:
+    addonmanager.kubernetes.io/mode: Reconcile
+    tier: node
+    app: flannel
+spec: 
+  template:
+    metadata:
+      labels:
+        tier: node
+        app: flannel
+    spec:
+      hostNetwork: true
+      nodeSelector:
+        beta.kubernetes.io/arch: amd64
+      tolerations:
+      - key: node-role.kubernetes.io/master
+        operator: Exists
+        effect: NoSchedule
+      serviceAccountName: flannel
+      containers:
+      - name: kube-flannel
+        image: {{ common.addons.get('flannel', {}).get('image', 'quay.io/coreos/flannel:v0.10.0-amd64') }}
+        command:
+        - /opt/bin/flanneld
+        args:
+        - --ip-masq
+        - --kube-subnet-mgr
+        resources:
+          requests:
+            cpu: "100m"
+            memory: "50Mi"
+          limits:
+            cpu: "100m"
+            memory: "50Mi"
+        securityContext:
+          privileged: true
+        env:
+        - name: POD_NAME
+          valueFrom:
+            fieldRef:
+              fieldPath: metadata.name
+        - name: POD_NAMESPACE
+          valueFrom:
+            fieldRef:
+              fieldPath: metadata.namespace
+        volumeMounts:
+        - name: run
+          mountPath: /run
+        - name: flannel-cfg
+          mountPath: /etc/kube-flannel/
+      volumes:
+        - name: run
+          hostPath:
+            path: /run
+        - name: flannel-cfg
+          configMap:
+            name: kube-flannel-cfg
diff --git a/kubernetes/files/kubelet/default.pool b/kubernetes/files/kubelet/default.pool
index 5d343b8..28d2431 100644
--- a/kubernetes/files/kubelet/default.pool
+++ b/kubernetes/files/kubelet/default.pool
@@ -23,7 +23,7 @@
 {%- else %}
 --node-labels=node-role.kubernetes.io/node=true \
 {%- endif %}
-{%- if pool.network.engine in ['calico', 'opencontrail'] %}
+{%- if pool.network.engine in ['calico', 'opencontrail', 'flannel'] %}
 --network-plugin=cni \
 --cni-bin-dir={{ pool.apiserver.get('cni_bin_dir', '/opt/cni/bin') }} \
 {%- else %}
diff --git a/kubernetes/files/manifest/flannel-server.manifest b/kubernetes/files/manifest/flannel-server.manifest
deleted file mode 100644
index 532a0ba..0000000
--- a/kubernetes/files/manifest/flannel-server.manifest
+++ /dev/null
@@ -1,78 +0,0 @@
-{%- from "kubernetes/map.jinja" import master with context %}
-{
-    "kind": "Pod",
-    "apiVersion": "v1",
-    "metadata": {
-        "name": "flannel-server",
-        "namespace": "kube-system",
-        "labels": {
-            "app": "flannel-server",
-            "version": "v0.1"
-        }
-    },
-    "spec": {
-        "volumes": [
-            {
-                "name": "varlog",
-                "hostPath": {
-                    "path": "/var/log"
-                }
-            },
-            {
-                "name": "etcdstorage",
-                "emptyDir": {}
-            },
-            {
-                "name": "networkconfig",
-                "hostPath": {
-                    "path": "/etc/kubernetes/network.json"
-                }
-            }
-        ],
-        "containers": [
-            {
-                "name": "flannel-server-helper",
-                "image": "gcr.io/google_containers/flannel-server-helper:0.1",
-                "args": [
-                    "--network-config=/etc/kubernetes/network.json",
-                    "--etcd-prefix=/kubernetes.io/network",
-                    "--etcd-server=http://127.0.0.1:4001"
-                ],
-                "volumeMounts": [
-                    {
-                        "name": "networkconfig",
-                        "mountPath": "/etc/kubernetes/network.json"
-                    }
-                ],
-                "imagePullPolicy": "Always"
-            },
-            {
-                "name": "flannel-container",
-                "image": "quay.io/coreos/flannel:0.5.5",
-                "command": [
-                    "/bin/sh",
-                    "-c",
-                    "/opt/bin/flanneld -listen 0.0.0.0:10253 -etcd-endpoints {% for member in master.network.etcd.members %}http://{{ member.host }}:4001{% if not loop.last %},{% endif %}{% endfor %} -etcd-prefix /kubernetes.io/network 2>&1 | tee -a /var/log/flannel-server.log"
-                ],
-                "ports": [
-                    {
-                        "hostPort": 10253,
-                        "containerPort": 10253
-                    }
-                ],
-                "resources": {
-                    "requests": {
-                        "cpu": "100m"
-                    }
-                },
-                "volumeMounts": [
-                    {
-                        "name": "varlog",
-                        "mountPath": "/var/log"
-                    }
-                ]
-            }
-        ],
-        "hostNetwork": true
-    }
-}
\ No newline at end of file
diff --git a/kubernetes/master/controller.sls b/kubernetes/master/controller.sls
index 38b170e..032a198 100644
--- a/kubernetes/master/controller.sls
+++ b/kubernetes/master/controller.sls
@@ -184,6 +184,10 @@
 {%- endif %}
 {%- endif %}
         --v={{ master.get('verbosity', 2) }}
+{%- if master.network.engine == 'flannel' %}
+        --allocate-node-cidrs=true
+        --cluster-cidr={{ master.network.private_ip_range }}
+{%- endif %}
 {%- for key, value in master.get('controller_manager', {}).get('daemon_opts', {}).iteritems() %}
         --{{ key }}={{ value }}
 {% endfor %}"
diff --git a/kubernetes/master/flannel.sls b/kubernetes/master/flannel.sls
index b7bbd65..fa140eb 100644
--- a/kubernetes/master/flannel.sls
+++ b/kubernetes/master/flannel.sls
@@ -1,18 +1,11 @@
 {%- from "kubernetes/map.jinja" import master with context %}
 {%- if master.enabled %}
 
-/etc/kubernetes/network.json:
-  file.managed:
-    - source: salt://kubernetes/files/flannel/network.json
-    - makedirs: True
-    - user: root
-    - group: root
-    - mode: 755
-    - template: jinja
+{%- if not pillar.kubernetes.pool is defined %}
 
-/etc/kubernetes/manifests/flannel-server.manifest:
+/etc/cni/net.d/11-flannel.conflist:
   file.managed:
-    - source: salt://kubernetes/files/manifest/flannel-server.manifest
+    - source: salt://kubernetes/files/flannel/flannel.conflist
     - user: root
     - group: root
     - mode: 644
@@ -20,47 +13,6 @@
     - dir_mode: 755
     - template: jinja
 
-/var/log/etcd-flannel.log:
-  file.managed:
-    - user: root
-    - group: root
-    - mode: 644
-
-/var/log/flannel.log:
-  file.managed:
-    - user: root
-    - group: root
-    - mode: 644
-
-{%- if not pillar.kubernetes.pool is defined %}
-
-flannel-tar:
-  archive:
-    - extracted
-    - user: root
-    - name: /opt/flannel
-    - source: https://storage.googleapis.com/kubernetes-release/flannel/flannel-0.5.5-linux-amd64.tar.gz
-    - tar_options: v
-    - source_hash: md5=972c717254775bef528f040af804f2cc
-    - archive_format: tar
-    - if_missing: /usr/local/src/flannel/flannel-0.5.5/
-
-flannel-symlink:
-  file.symlink:
-    - name: /usr/local/bin/flanneld
-    - target: /usr/local/src/flannel-0.5.5/flanneld
-    - force: true
-    - watch:
-        - archive: flannel-tar
-
-/etc/default/flannel:
-  file.managed:
-    - source: salt://kubernetes/files/flannel/default.master
-    - template: jinja
-    - user: root
-    - group: root
-    - mode: 644
-
 {%- endif %}
 
-{%- endif %}
\ No newline at end of file
+{%- endif %}
diff --git a/kubernetes/master/kube-addons.sls b/kubernetes/master/kube-addons.sls
index 3682b43..66d23bd 100644
--- a/kubernetes/master/kube-addons.sls
+++ b/kubernetes/master/kube-addons.sls
@@ -9,6 +9,16 @@
     - group: root
     - mode: 0755
 
+{%- if master.network.engine == "flannel" %}
+/etc/kubernetes/addons/flannel/flannel.yml:
+  file.managed:
+    - source: salt://kubernetes/files/kube-addons/flannel/flannel.yml
+    - template: jinja
+    - group: root
+    - dir_mode: 755
+    - makedirs: True
+{% endif %}
+
 {%- if master.network.engine == "opencontrail" and master.network.get('version', 3.0) < 4.0 %}
 /etc/kubernetes/addons/contrail-network-controller/contrail-network-controller-configmap.yml:
   file.managed:
diff --git a/kubernetes/pool/flannel.sls b/kubernetes/pool/flannel.sls
index 6cc2513..4f00759 100644
--- a/kubernetes/pool/flannel.sls
+++ b/kubernetes/pool/flannel.sls
@@ -1,31 +1,14 @@
 {%- from "kubernetes/map.jinja" import pool with context %}
 {%- if pool.enabled %}
 
-flannel-tar:
-  archive:
-    - extracted
-    - user: root
-    - name: /opt/flannel
-    - source: https://storage.googleapis.com/kubernetes-release/flannel/flannel-0.5.5-linux-amd64.tar.gz
-    - tar_options: v
-    - source_hash: md5=972c717254775bef528f040af804f2cc
-    - archive_format: tar
-    - if_missing: /usr/local/src/flannel/flannel-0.5.5/
-
-flannel-symlink:
-  file.symlink:
-    - name: /usr/local/bin/flanneld
-    - target: /usr/local/src/flannel-0.5.5/flanneld
-    - force: true
-    - watch:
-        - archive: flannel-tar
-
-/etc/default/flannel:
+/etc/cni/net.d/11-flannel.conflist:
   file.managed:
-    - source: salt://kubernetes/files/flannel/default.pool
-    - template: jinja
+    - source: salt://kubernetes/files/flannel/flannel.conflist
     - user: root
     - group: root
     - mode: 644
+    - makedirs: true
+    - dir_mode: 755
+    - template: jinja
 
-{%- endif %}
\ No newline at end of file
+{%- endif %}