Instantiate common cassandra cluster

Issue-ID: OOM-1198

Change-Id: Ie49a34cfbe5bdb2620312e8780dccbc0b86a0d6e
Signed-off-by: Mahendra Raghuwanshi <mahendra.raghuwanshi@amdocs.com>
diff --git a/kubernetes/common/cassandra/templates/configmap.yaml b/kubernetes/common/cassandra/templates/configmap.yaml
new file mode 100644
index 0000000..a9420d7
--- /dev/null
+++ b/kubernetes/common/cassandra/templates/configmap.yaml
@@ -0,0 +1,15 @@
+{{- if .Values.configOverrides }}
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: {{ include "common.fullname" . }}-configOverrides
+  namespace: {{ include "common.namespace" . }}
+  labels:
+    app: {{ include "common.name" . }}
+    chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
+    release: {{ .Release.Name }}
+    heritage: {{ .Release.Service }}
+data:
+{{ toYaml .Values.configOverrides | indent 2 }}
+{{- end }}
+
diff --git a/kubernetes/common/cassandra/templates/volumes.yaml b/kubernetes/common/cassandra/templates/pv.yaml
similarity index 96%
rename from kubernetes/common/cassandra/templates/volumes.yaml
rename to kubernetes/common/cassandra/templates/pv.yaml
index 9640aef..06f41e7 100644
--- a/kubernetes/common/cassandra/templates/volumes.yaml
+++ b/kubernetes/common/cassandra/templates/pv.yaml
@@ -12,7 +12,7 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-#{{ if .Values.persistence.enabled }}
+{{ if .Values.persistence.enabled }}
 {{- $root := . -}}
 {{ range $i, $e := until (int $root.Values.replicaCount) }}
 ---
@@ -36,4 +36,4 @@
     path: {{ $root.Values.persistence.mountPath }}/{{ $root.Release.Name }}/{{ $root.Values.persistence.mountSubPath }}-{{ $i }}
   persistentVolumeReclaimPolicy: {{ $root.Values.persistence.volumeReclaimPolicy }}
 {{ end }}
-#{{ end }}
+{{ end }}
diff --git a/kubernetes/common/cassandra/templates/service.yaml b/kubernetes/common/cassandra/templates/service.yaml
index 467a6a9..cda5191 100644
--- a/kubernetes/common/cassandra/templates/service.yaml
+++ b/kubernetes/common/cassandra/templates/service.yaml
@@ -22,48 +22,28 @@
     chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
     release: {{ .Release.Name }}
     heritage: {{ .Release.Service }}
+  annotations:
+    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
 spec:
   type: {{ .Values.service.type }}
+  publishNotReadyAddresses: true
   ports:
-    {{if eq .Values.service.type "NodePort" -}}
-    - port: {{ .Values.service.externalPort }}
-      targetPort: {{ .Values.service.internalPort }}
-      nodePort: {{ .Values.global.nodePortPrefix | default .Values.nodePortPrefix }}{{ .Values.service.nodePort }}
-      name: {{ .Values.service.portName }}
-    - port: {{ .Values.service.externalPort2 }}
-      targetPort: {{ .Values.service.internalPort2 }}
-      nodePort: {{ .Values.global.nodePortPrefix | default .Values.nodePortPrefix }}{{ .Values.service.nodePort2 }}
-      name: {{ .Values.service.portName }}2
-    - port: {{ .Values.service.externalPort3 }}
-      targetPort: {{ .Values.service.internalPort3 }}
-      nodePort: {{ .Values.global.nodePortPrefix | default .Values.nodePortPrefix }}{{ .Values.service.nodePort3 }}
-      name: {{ .Values.service.portName }}3
-    - port: {{ .Values.service.externalPort4 }}
-      targetPort: {{ .Values.service.internalPort4 }}
-      nodePort: {{ .Values.global.nodePortPrefix | default .Values.nodePortPrefix }}{{ .Values.service.nodePort4 }}
-      name: {{ .Values.service.portName }}4
-    - port: {{ .Values.service.externalPort5 }}
-      targetPort: {{ .Values.service.internalPort5 }}
-      nodePort: {{ .Values.global.nodePortPrefix | default .Values.nodePortPrefix }}{{ .Values.service.nodePort5 }}
-      name: {{ .Values.service.portName }}5
-
-    {{- else -}}
-    - port: {{ .Values.service.externalPort }}
-      targetPort: {{ .Values.service.internalPort }}
-      name: {{ .Values.service.portName }}
-    - port: {{ .Values.service.externalPort2 }}
-      targetPort: {{ .Values.service.internalPort2 }}
-      name: {{ .Values.service.portName }}2
-    - port: {{ .Values.service.externalPort3 }}
-      targetPort: {{ .Values.service.internalPort3 }}
-      name: {{ .Values.service.portName }}3
-    - port: {{ .Values.service.externalPort4 }}
-      targetPort: {{ .Values.service.internalPort4 }}
-      name: {{ .Values.service.portName }}4
-    - port: {{ .Values.service.externalPort5 }}
-      targetPort: {{ .Values.service.internalPort5 }}
-      name: {{ .Values.service.portName }}5
-    {{- end}}
+  {{if eq .Values.service.type "NodePort" -}}
+  {{- $global := . }}
+  {{- range $index, $ports := .Values.service.ports }}
+  - port: {{ $ports.port }}
+    targetPort: {{ $ports.port }}
+    nodePort: {{ $global.Values.global.nodePortPrefix | default $global.Values.nodePortPrefix }}{{ $ports.nodePort }}
+    name: {{ $ports.name }}
+  {{- end }}
+{{- else -}}
+  {{- range $index, $ports := .Values.service.ports }}
+  - port: {{ $ports.port }}
+    targetPort: {{ $ports.port }}
+    name: {{ $ports.name }}
+  {{- end }}
+{{- end}}
   selector:
     app: {{ include "common.name" . }}
     release: {{ .Release.Name }}
+  clusterIP: None
diff --git a/kubernetes/common/cassandra/templates/statefulset.yaml b/kubernetes/common/cassandra/templates/statefulset.yaml
index 960d97e..86cf467 100644
--- a/kubernetes/common/cassandra/templates/statefulset.yaml
+++ b/kubernetes/common/cassandra/templates/statefulset.yaml
@@ -23,6 +23,10 @@
     release: {{ .Release.Name }}
     heritage: {{ .Release.Service }}
 spec:
+  selector:
+    matchLabels:
+      app: {{ include  "common.name" . }}
+      release: {{ .Release.Name }}
   serviceName: {{ include "common.servicename" . }}
   replicas: {{ .Values.replicaCount }}
   podManagementPolicy: {{ .Values.podManagementPolicy }}
@@ -35,24 +39,26 @@
         release: {{ .Release.Name }}
       name: {{ include "common.name" . }}
     spec:
+      hostNetwork: {{ .Values.hostNetwork }}
       containers:
       - name: {{ include "common.name" . }}
         image: {{ .Values.image }}
         imagePullPolicy: {{ .Values.global.pullPolicy | default .Values.pullPolicy }}
         ports:
-        - containerPort: {{ .Values.service.internalPort }}
-        - containerPort: {{ .Values.service.internalPort2 }}
-        - containerPort: {{ .Values.service.internalPort3 }}
-        - containerPort: {{ .Values.service.internalPort4 }}
-        - containerPort: {{ .Values.service.internalPort5 }}
+        {{- range $index, $ports := .Values.service.ports }}
+        - containerPort: {{ $ports.port }}
+        {{- end }}
         volumeMounts:
-          - name: {{ .Values.service.name }}
+          - name: cassandra-data
             mountPath: /var/lib/cassandra
-          {{ if eq .Values.configmapping true }}
-          - name: {{ .Values.service.name }}-docker-entry-initd
-            mountPath: /{{ .Values.service.name }}-docker-entrypoint-initdb.d/cassandra.cql
-            subPath: cassandra.cql
-          {{ end }}
+          - name: localtime
+            mountPath: /etc/localtime
+            readOnly: true
+          {{- range $key, $value := .Values.configOverrides }}
+          - name: cassandra-config-{{ $key | replace "." "-" }}
+            mountPath: /etc/cassandra/{{ $key }}
+            subPath: {{ $key }}
+          {{- end }}
         {{- if eq .Values.liveness.enabled true }}
         livenessProbe:
           exec:
@@ -62,6 +68,9 @@
             - nodetool status | grep $POD_IP | awk '$1!="UN" { exit 1; }'
           initialDelaySeconds: {{ .Values.liveness.initialDelaySeconds }}
           periodSeconds: {{ .Values.liveness.periodSeconds }}
+          timeoutSeconds: {{ .Values.liveness.timeoutSeconds }}
+          successThreshold: {{ .Values.liveness.successThreshold }}
+          failureThreshold: {{ .Values.liveness.failureThreshold }}
         {{ end -}}
         readinessProbe:
           exec:
@@ -70,10 +79,19 @@
             - -c
             - nodetool status | grep $POD_IP | awk '$1!="UN" { exit 1; }'
           initialDelaySeconds: {{ .Values.readiness.initialDelaySeconds }}
-          timeoutSeconds: {{ .Values.liveness.periodSeconds }}
+          periodSeconds: {{ .Values.readiness.periodSeconds }}
+          timeoutSeconds: {{ .Values.readiness.timeoutSeconds }}
+          successThreshold: {{ .Values.readiness.successThreshold }}
+          failureThreshold: {{ .Values.readiness.failureThreshold }}
         env:
-        {{- $seed_size := default 1 .Values.replicaCount | int -}}
-        {{- $global := . }}
+          {{- $seed_size := default 1 .Values.replicaCount | int -}}
+          {{- $global := . }}
+          - name: CASSANDRA_SEEDS
+          {{- if .Values.hostNetwork }}
+            value: {{ required "You must fill \".Values.config.seeds\" with list of Cassandra seeds when hostNetwork is set to true" .Values.config.seeds | quote }}
+          {{- else }}
+            value: "{{- range $i, $e := until $seed_size }}{{ template "common.fullname" $global }}-{{ $i }}.{{ template "common.servicename" $global }}.{{ $global.Release.Namespace }}.svc.{{ $global.Values.config.cluster_domain }}{{- if (lt ( add1 $i ) $seed_size ) }},{{- end }}{{- end }}"
+          {{- end }}
           - name: MAX_HEAP_SIZE
             value: {{ .Values.config.heap.max }}
           - name: HEAP_NEWSIZE
@@ -88,22 +106,22 @@
             value: {{ .Values.config.rackName | quote }}
           - name: CASSANDRA_AUTO_BOOTSTRAP
             value: {{ .Values.config.autoBootstrap | quote }}
+          - name: CASSANDRA_START_RPC
+            value: {{ default "true" .Values.config.start_rpc | quote }}
+          - name: CASSANDRA_ENDPOINT_SNITCH
+            value: {{ default "SimpleSnitch" .Values.config.endpoint_snitch | quote }}
           - name: POD_IP
             valueFrom:
               fieldRef:
                 fieldPath: status.podIP
-        {{ if eq .Values.configmapping true }}
         lifecycle:
-          postStart:
+          preStop:
             exec:
-              command:
-              - /bin/sh
-              - -c
-              - >
-                /bin/sleep {{ .Values.readiness.initialDelaySeconds }};
-                cd /{{ .Values.service.name }}-docker-entrypoint-initdb.d;
-                cqlsh -u root -p root -f cassandra.cql
-        {{ end }}
+        {{- if not .Values.persistence.enabled }}
+              command: ["/bin/sh", "-c", "exec nodetool decommission"]
+        {{- else }}
+              command: ["/bin/sh", "-c", "PID=$(pidof java) && kill $PID && while ps -p $PID > /dev/null; do sleep 1; done"]
+        {{- end }}
         resources:
 {{ toYaml .Values.resources | indent 10 }}
       {{- if .Values.nodeSelector }}
@@ -118,21 +136,20 @@
       - name: localtime
         hostPath:
           path: /etc/localtime
-      {{ if eq .Values.configmapping true }}
-      - name: {{ .Values.service.name }}-docker-entry-initd
+      {{- range $key, $value := .Values.configOverrides }}
+      - name: cassandra-config-{{ $key | replace "." "-" }}
         configMap:
-          name: {{ .Values.service.name }}-docker-entry-initd
-      {{ end }}
+          name: {{ include "common.fullname" . }}-configOverrides
+      {{- end }}
   {{- if not .Values.persistence.enabled }}
-      - name: {{ .Values.service.name }}
+      - name: cassandra-data
         emptyDir: {}
   {{- else }}
   volumeClaimTemplates:
     - metadata:
-        name: {{ .Values.service.name }}
+        name: cassandra-data
         labels:
           app: {{ template "common.fullname" . }}
-          chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
           release: "{{ .Release.Name }}"
           heritage: "{{ .Release.Service }}"
       annotations: