EDGEPOD-85 Add ability to enable coredump for OMEC apps

Core files will be generated when config.coreDump.enabled is set to
true. Path of the generated core files can be configured by setting
config.coreDump.path, which is set to /tmp/coredump by default.

Change-Id: I5149751ba091a1f49e2eee91a14889898301a7bf
diff --git a/omec/omec-control-plane/Chart.yaml b/omec/omec-control-plane/Chart.yaml
index 5029523..3a8444e 100644
--- a/omec/omec-control-plane/Chart.yaml
+++ b/omec/omec-control-plane/Chart.yaml
@@ -19,4 +19,4 @@
 name: omec-control-plane
 icon: https://guide.opencord.org/logos/cord.svg
 
-version: 0.1.16
+version: 0.1.17
diff --git a/omec/omec-control-plane/templates/_helpers.tpl b/omec/omec-control-plane/templates/_helpers.tpl
index 7427461..bb98547 100644
--- a/omec/omec-control-plane/templates/_helpers.tpl
+++ b/omec/omec-control-plane/templates/_helpers.tpl
@@ -110,3 +110,23 @@
       - endpoints
       - configmaps
 {{- end -}}
+
+{{/*
+Render init container for coredump.
+*/}}
+{{- define "omec-control-plane.coredump_init" -}}
+{{- $pod := index . 0 -}}
+{{- $context := index . 1 -}}
+- name: {{ $pod }}-coredump-init
+  image: {{ $context.Values.images.tags.init | quote }}
+  imagePullPolicy: {{ $context.Values.images.pullPolicy }}
+  securityContext:
+    privileged: true
+    runAsUser: 0
+  command: ["bash", "-xc"]
+  args:
+    - echo '/tmp/coredump/core.%h.%e.%t' > /mnt/host-rootfs/proc/sys/kernel/core_pattern
+  volumeMounts:
+    - name: host-rootfs
+      mountPath: /mnt/host-rootfs
+{{- end -}}
diff --git a/omec/omec-control-plane/templates/bin/_hss-run.sh.tpl b/omec/omec-control-plane/templates/bin/_hss-run.sh.tpl
index b3d9be1..a391541 100644
--- a/omec/omec-control-plane/templates/bin/_hss-run.sh.tpl
+++ b/omec/omec-control-plane/templates/bin/_hss-run.sh.tpl
@@ -16,6 +16,10 @@
 
 set -ex
 
+{{- if .Values.config.coreDump.enabled }}
+cp /bin/hss /tmp/coredump/
+{{- end }}
+
 CONF_DIR="/opt/c3po/hss/conf"
 LOGS_DIR="/opt/c3po/hss/logs"
 mkdir -p $CONF_DIR $LOGS_DIR
diff --git a/omec/omec-control-plane/templates/bin/_mme-run.sh.tpl b/omec/omec-control-plane/templates/bin/_mme-run.sh.tpl
index 6b7718b..f1b937c 100644
--- a/omec/omec-control-plane/templates/bin/_mme-run.sh.tpl
+++ b/omec/omec-control-plane/templates/bin/_mme-run.sh.tpl
@@ -16,6 +16,10 @@
 
 APPLICATION=$1
 
+{{- if .Values.config.coreDump.enabled }}
+cp /openmme/target/bin/$APPLICATION /tmp/coredump/
+{{- end }}
+
 # copy config files to openmme target directly
 cp /opt/mme/config/shared/* /openmme/target/conf/
 
diff --git a/omec/omec-control-plane/templates/bin/_spgwc-run.sh.tpl b/omec/omec-control-plane/templates/bin/_spgwc-run.sh.tpl
index cd61eac..9dfa29f 100644
--- a/omec/omec-control-plane/templates/bin/_spgwc-run.sh.tpl
+++ b/omec/omec-control-plane/templates/bin/_spgwc-run.sh.tpl
@@ -16,10 +16,14 @@
 
 set -xe
 
+{{- if .Values.config.coreDump.enabled }}
+cp /bin/ngic_controlplane /tmp/coredump/
+{{- end }}
+
 mkdir -p /opt/cp/config
 cd /opt/cp/config
-cp /etc/cp/config/{adc_rules.cfg,cp_config.cfg,interface.cfg,meter_profile.cfg,pcc_rules.cfg,sdf_rules.cfg,app_config.cfg} .
 
+cp /etc/cp/config/{adc_rules.cfg,cp_config.cfg,interface.cfg,meter_profile.cfg,pcc_rules.cfg,sdf_rules.cfg,app_config.cfg} .
 sed -i "s/CP_ADDR/$POD_IP/g" interface.cfg
 
 . cp_config.cfg
diff --git a/omec/omec-control-plane/templates/statefulset-hss.yaml b/omec/omec-control-plane/templates/statefulset-hss.yaml
index 93c8721..dc461b8 100644
--- a/omec/omec-control-plane/templates/statefulset-hss.yaml
+++ b/omec/omec-control-plane/templates/statefulset-hss.yaml
@@ -70,10 +70,18 @@
           - kubernetes-entrypoint
         volumeMounts:
           []
+    {{- if .Values.config.coreDump.enabled }}
+{{ tuple "hss" . | include "omec-control-plane.coredump_init" | indent 6 }}
+    {{- end }}
       containers:
       - name: hss
         image: {{ .Values.images.tags.hss }}
         imagePullPolicy: {{ .Values.images.pullPolicy }}
+      {{- if .Values.config.coreDump.enabled }}
+        securityContext:
+          privileged: true
+          runAsUser: 0
+      {{- end }}
         stdin: true
         tty: true
         command: ["bash", "-c", "/opt/c3po/hss/hss-run.sh; sleep 3600"]
@@ -87,6 +95,10 @@
           subPath: hss-run.sh
         - name: hss-config
           mountPath: /etc/hss/conf
+      {{- if .Values.config.coreDump.enabled }}
+        - name: coredump
+          mountPath: /tmp/coredump
+      {{- end }}
       volumes:
       - name: hss-script
         configMap:
@@ -96,3 +108,11 @@
         configMap:
           name: hss
           defaultMode: 420
+    {{- if .Values.config.coreDump.enabled }}
+      - name: host-rootfs
+        hostPath:
+          path: /
+      - name: coredump
+        hostPath:
+          path: {{ .Values.config.coreDump.path }}
+    {{- end }}
diff --git a/omec/omec-control-plane/templates/statefulset-mme.yaml b/omec/omec-control-plane/templates/statefulset-mme.yaml
index ff64007..a858bab 100644
--- a/omec/omec-control-plane/templates/statefulset-mme.yaml
+++ b/omec/omec-control-plane/templates/statefulset-mme.yaml
@@ -70,7 +70,7 @@
         - >
           if chroot /mnt/host-rootfs modinfo nf_conntrack_proto_sctp > /dev/null 2>&1; then \
               chroot /mnt/host-rootfs modprobe nf_conntrack_proto_sctp; \
-          fi
+          fi;
         volumeMounts:
         - name: host-rootfs
           mountPath: /mnt/host-rootfs
@@ -120,10 +120,18 @@
           mountPath: /opt/mme/config
         - name: shared-data
           mountPath: /opt/mme/config/shared
+    {{- if .Values.config.coreDump.enabled }}
+{{ tuple "mme" . | include "omec-control-plane.coredump_init" | indent 6 }}
+    {{- end }}
       containers:
       - name: mme-app
         image: {{ .Values.images.tags.mme | quote }}
         imagePullPolicy: {{ .Values.images.pullPolicy }}
+      {{- if .Values.config.coreDump.enabled }}
+        securityContext:
+          privileged: true
+          runAsUser: 0
+      {{- end }}
         command: ["bash", "-xc"]
         args:
         - /opt/mme/scripts/mme-run.sh mme-app
@@ -137,13 +145,22 @@
           subPath: mme-run.sh
         - name: shared-data
           mountPath: /opt/mme/config/shared
-        - name: configs
-          mountPath: /opt/mme/config
         - name: shared-app
           mountPath: /tmp
+        - name: configs
+          mountPath: /opt/mme/config
+      {{- if .Values.config.coreDump.enabled }}
+        - name: coredump
+          mountPath: /tmp/coredump
+      {{- end }}
       - name: s1ap-app
         image: {{ .Values.images.tags.mme | quote }}
         imagePullPolicy: {{ .Values.images.pullPolicy }}
+      {{- if .Values.config.coreDump.enabled }}
+        securityContext:
+          privileged: true
+          runAsUser: 0
+      {{- end }}
         command: ["bash", "-xc"]
         args:
         - /opt/mme/scripts/mme-run.sh s1ap-app
@@ -159,9 +176,18 @@
           mountPath: /opt/mme/config/shared
         - name: shared-app
           mountPath: /tmp
+      {{- if .Values.config.coreDump.enabled }}
+        - name: coredump
+          mountPath: /tmp/coredump
+      {{- end }}
       - name: s6a-app
         image: {{ .Values.images.tags.mme | quote }}
         imagePullPolicy: {{ .Values.images.pullPolicy }}
+      {{- if .Values.config.coreDump.enabled }}
+        securityContext:
+          privileged: true
+          runAsUser: 0
+      {{- end }}
         command: ["bash", "-xc"]
         args:
         - /opt/mme/scripts/mme-run.sh s6a-app
@@ -177,9 +203,18 @@
           mountPath: /opt/mme/config/shared
         - name: shared-app
           mountPath: /tmp
+      {{- if .Values.config.coreDump.enabled }}
+        - name: coredump
+          mountPath: /tmp/coredump
+      {{- end }}
       - name: s11-app
         image: {{ .Values.images.tags.mme | quote }}
         imagePullPolicy: {{ .Values.images.pullPolicy }}
+      {{- if .Values.config.coreDump.enabled }}
+        securityContext:
+          privileged: true
+          runAsUser: 0
+      {{- end }}
         command: ["bash", "-xc"]
         args:
         - /opt/mme/scripts/mme-run.sh s11-app
@@ -195,6 +230,10 @@
           mountPath: /opt/mme/config/shared
         - name: shared-app
           mountPath: /tmp
+      {{- if .Values.config.coreDump.enabled }}
+        - name: coredump
+          mountPath: /tmp/coredump
+      {{- end }}
       volumes:
       - name: scripts
         configMap:
@@ -211,3 +250,8 @@
       - name: host-rootfs
         hostPath:
           path: /
+    {{- if .Values.config.coreDump.enabled }}
+      - name: coredump
+        hostPath:
+          path: {{ .Values.config.coreDump.path }}
+    {{- end }}
diff --git a/omec/omec-control-plane/templates/statefulset-spgwc.yaml b/omec/omec-control-plane/templates/statefulset-spgwc.yaml
index ebc0cd0..90775c0 100644
--- a/omec/omec-control-plane/templates/statefulset-spgwc.yaml
+++ b/omec/omec-control-plane/templates/statefulset-spgwc.yaml
@@ -63,10 +63,18 @@
             value: "echo done"
           - name: DEPENDENCY_POD_JSON
             value: '[{"labels": {"app": "mme"}, "requireSameNode": false}]'
+    {{- if .Values.config.coreDump.enabled }}
+{{ tuple "spgwc" . | include "omec-control-plane.coredump_init" | indent 6 }}
+    {{- end }}
       containers:
       - name: spgwc
         image: {{ .Values.images.tags.spgwc }}
         imagePullPolicy: {{ .Values.images.pullPolicy }}
+      {{- if .Values.config.coreDump.enabled }}
+        securityContext:
+          privileged: true
+          runAsUser: 0
+      {{- end }}
         stdin: true
         tty: true
         command: ["/opt/cp/scripts/spgwc-run.sh"]
@@ -96,6 +104,10 @@
           subPath: spgwc-run.sh
         - name: cp-config
           mountPath: /etc/cp/config
+      {{- if .Values.config.coreDump.enabled }}
+        - name: coredump
+          mountPath: /tmp/coredump
+      {{- end }}
       volumes:
       - name: cp-script
         configMap:
@@ -105,3 +117,11 @@
         configMap:
           name: spgwc
           defaultMode: 420
+    {{- if .Values.config.coreDump.enabled }}
+      - name: host-rootfs
+        hostPath:
+          path: /
+      - name: coredump
+        hostPath:
+          path: {{ .Values.config.coreDump.path }}
+    {{- end }}
diff --git a/omec/omec-control-plane/values.yaml b/omec/omec-control-plane/values.yaml
index 9d3263e..c52ce9b 100644
--- a/omec/omec-control-plane/values.yaml
+++ b/omec/omec-control-plane/values.yaml
@@ -87,6 +87,9 @@
 
 config:
   clusterDomain: cluster.local
+  coreDump:
+    enabled: false
+    path: /tmp/coredump
   hss:
     hssdb: cassandra
     s6a: