Affinity router pre-pairing & votha rw/ro core DNS.

This requires changes to afrouterd to work correctly, please consider/merge both patches together.

- Added headless services for all RW cores.
- Changed RO cores to use a StatefulSet.
- Added a service for the RO cores.
- Modified afrouter config to use pre-configure core connectivity.

Change-Id: I6a29097e446f46fa2a02e1f9213a840af283a18f
diff --git a/voltha/Chart.yaml b/voltha/Chart.yaml
index 59df64f..62d2875 100644
--- a/voltha/Chart.yaml
+++ b/voltha/Chart.yaml
@@ -17,5 +17,5 @@
 description: A Helm chart for Voltha based on K8S resources in Voltha project
 icon: https://guide.opencord.org/logos/cord.svg
 
-version: 2.0.10
+version: 2.0.11
 appVersion: 2.0.0
diff --git a/voltha/templates/afrouter-config.yaml b/voltha/templates/afrouter-config.yaml
index a29c6e3..230eafe 100644
--- a/voltha/templates/afrouter-config.yaml
+++ b/voltha/templates/afrouter-config.yaml
@@ -13,8 +13,7 @@
 # limitations under the License.
 
 ---
-{{- $rw_count := mul .Values.replicas.rw_core 2 | int }}
-{{- $ro_count := .Values.replicas.ro_core | int }}
+{{- $root := . }}
 apiVersion: v1
 data:
   arouter.voltha.json: |
@@ -141,10 +140,10 @@
           {
             "name": "vcore",
             "backends": [
-{{- range $i, $e := until $rw_count }}
-{{- $core_idx := add $i 1 }}
+{{- range $i, $e := until (int .Values.replicas.rw_core) }}
+{{- $cluster := add1 $i }}
               {
-                "name": "vcore{{ $core_idx }}",
+                "name": "vcore{{ $cluster }}",
                 "type": "active_active",
                 "association": {
                   "strategy": "serial_number",
@@ -154,36 +153,35 @@
                 },
                 "connections": [
                   {
-                    "name": "vcore{{ $core_idx }}1",
-                    "addr": "",
-                    "port": ""
+                    "name": "vcore{{ $cluster }}1",
+                    "addr": "voltha-rw-core-{{ $cluster }}1.{{ $root.Release.Namespace }}.svc.cluster.local",
+                    "port": "50057"
                   },
                   {
-                    "name": "vcore{{ $core_idx }}2",
-                    "addr": "",
-                    "port": ""
+                    "name": "vcore{{ $cluster }}2",
+                    "addr": "voltha-rw-core-{{ $cluster }}2.{{ $root.Release.Namespace }}.svc.cluster.local",
+                    "port": "50057"
                   }
                 ]
-              }{{ if lt $core_idx $rw_count }},{{ end }}
+              }{{ if lt $cluster (int $root.Values.replicas.rw_core) }},{{ end }}
 {{- end}}
             ]
           },
           {
             "name": "ro_vcore",
             "backends": [
-{{- range $i, $e := until $ro_count }}
-{{- $core_idx := add $i 1 }}
+{{- range $core_idx, $e := until (int .Values.replicas.rw_core) }}
               {
                 "name": "ro_vcore{{ $core_idx }}",
                 "type": "server",
                 "connections": [
                   {
                     "name": "ro_vcore{{ $core_idx }}1",
-                    "addr": "",
-                    "port": ""
+                    "addr": "voltha-ro-core-{{ $core_idx }}.voltha-ro-core.{{ $root.Release.Namespace }}.svc.cluster.local",
+                    "port": "50057"
                   }
                 ]
-              }{{ if lt $core_idx $ro_count }},{{ end }}
+              }{{ if lt (add1 $core_idx) (int $root.Values.replicas.rw_core) }},{{ end }}
 {{- end}}
             ]
           }
diff --git a/voltha/templates/api-server.yaml b/voltha/templates/api-server.yaml
index 5b690e6..2168d9a 100644
--- a/voltha/templates/api-server.yaml
+++ b/voltha/templates/api-server.yaml
@@ -67,10 +67,6 @@
         env:
         - name: POD_NAMESPACE
           value: {{ .Release.Namespace }}
-        - name: NUM_RW_PODS
-          value: {{ mul .Values.replicas.rw_core 2 | quote}}
-        - name: NUM_RO_PODS
-          value: {{ quote .Values.replicas.ro_core }}
         image: {{ tpl .Values.images.afrouterd.registry . }}{{ tpl .Values.images.afrouterd.repository . }}:{{ tpl ( tpl .Values.images.afrouterd.tag . ) . }}
         imagePullPolicy: {{ tpl .Values.images.afrouterd.pullPolicy . }}
         command: ["/app/arouterd"]
diff --git a/voltha/templates/ro-core.yaml b/voltha/templates/ro-core.yaml
index 7a60699..de2e2c6 100644
--- a/voltha/templates/ro-core.yaml
+++ b/voltha/templates/ro-core.yaml
@@ -30,16 +30,31 @@
 {{- end }}
 
 ---
-apiVersion: apps/v1
-kind: Deployment
+apiVersion: v1
+kind: Service
 metadata:
-  name: ro-core
+  name: voltha-ro-core
+spec:
+  ports:
+    - name: grpc
+      port: 50057
+  clusterIP: None
+  selector:
+    app: ro-core
+
+---
+apiVersion: apps/v1
+kind: StatefulSet
+metadata:
+  name: voltha-ro-core
   serviceAccountName: {{ .Values.serviceaccount }}
 spec:
   replicas: {{ .Values.replicas.ro_core }}
   selector:
     matchLabels:
       app: ro-core
+  serviceName: voltha-ro-core
+  podManagementPolicy: Parallel
   template:
     metadata:
       labels:
@@ -55,16 +70,14 @@
     spec:
       affinity:
         podAntiAffinity:
-          preferredDuringSchedulingIgnoredDuringExecution:
-          - weight: 100
-            podAffinityTerm:
+          requiredDuringSchedulingIgnoredDuringExecution:
+            - topologyKey: "kubernetes.io/hostname"
               labelSelector:
                 matchExpressions:
-                - key: app
-                  operator: In
-                  values:
-                  - ro-core
-              topologyKey: "kubernetes.io/hostname"
+                  - key: app
+                    operator: In
+                    values:
+                    - ro-core
       serviceAccountName: {{ .Values.serviceaccount }}
       containers:
         - name: voltha
diff --git a/voltha/templates/rw-core.yaml b/voltha/templates/rw-core.yaml
index beed9fd..f3328e3 100644
--- a/voltha/templates/rw-core.yaml
+++ b/voltha/templates/rw-core.yaml
@@ -13,7 +13,6 @@
 # limitations under the License.
 
 {{- $root := . -}}
-{{- $count := mul .Values.replicas.rw_core 2 | int }}
 {{- $tag := tpl ( tpl .Values.images.rw_core.tag . ) . }}
 {{- $repository := tpl .Values.images.rw_core.repository . }}
 {{- $registry := tpl .Values.images.rw_core.registry . }}
@@ -38,25 +37,47 @@
 {{- else }}
 {{- $log_level = 3 }}
 {{- end }}
-{{- range $i, $e := until $count }}
-{{- $core_idx := add $i 1 }}
-{{- $affinity_group := add1 (div $i 2) }}
+{{- range $i, $e := until (int .Values.replicas.rw_core) }}
+{{- $cluster := add1 $i }}
+{{- range $i, $e := until 2 }}
+{{- $cluster_core_id := add1 $i }}
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: voltha-rw-core-{{ $cluster }}{{ $cluster_core_id }}
+  serviceAccountName: {{ $root.Values.serviceaccount }}
+spec:
+  serviceAccountName: {{ $root.Values.serviceaccount }}
+  clusterIP: None
+  ports:
+    - name: grpc
+      port: 50057
+      targetPort: 50057
+  selector:
+    app: rw-core
+    affinity-group: {{ quote $cluster }}
+    affinity-group-core-id: {{ quote $cluster_core_id }}
+
 ---
 apiVersion: apps/v1
 kind: Deployment
 metadata:
-  name: rw-core{{ $core_idx }}
+  name: voltha-rw-core-{{ $cluster }}{{ $cluster_core_id }}
   serviceAccountName: {{ $root.Values.serviceaccount }}
 spec:
   replicas: 1
   selector:
     matchLabels:
       app: rw-core
+      affinity-group: {{ quote $cluster }}
+      affinity-group-core-id: {{ quote $cluster_core_id }}
   template:
     metadata:
       labels:
         app: rw-core
-        affinity-group: affinity-group-{{ $affinity_group }}
+        affinity-group: {{ quote $cluster }}
+        affinity-group-core-id: {{ quote $cluster_core_id }}
         app.kubernetes.io/name: "read-write-core"
         app.kubernetes.io/version: {{ quote $root.Chart.AppVersion }}
         app.kubernetes.io/component: "core"
@@ -68,24 +89,18 @@
     spec:
       affinity:
         podAntiAffinity:
-          preferredDuringSchedulingIgnoredDuringExecution:
-          - weight: 100
-            podAffinityTerm:
+          requiredDuringSchedulingIgnoredDuringExecution:
+            - topologyKey: "kubernetes.io/hostname"
               labelSelector:
                 matchExpressions:
                 - key: app
                   operator: In
                   values:
-                  - rw-core
-              topologyKey: "kubernetes.io/hostname"
-          requiredDuringSchedulingIgnoredDuringExecution:
-          - labelSelector:
-              matchExpressions:
-              - key: affinity-group
-                operator: In
-                values:
-                - affinity-group-{{ $affinity_group }}
-            topologyKey: "kubernetes.io/hostname"
+                    - rw-core
+                - key: affinity-group
+                  operator: In
+                  values:
+                    - {{ quote $cluster }}
       serviceAccountName: {{ $root.Values.serviceaccount }}
       containers:
         - name: voltha
@@ -121,3 +136,4 @@
             - containerPort: 50057
               name: grpc-port
 {{end}}
+{{end}}