Fix helm hooks doesn't work in oaisim with Helm 2.15

Also use helper for reducing duplicate code lines for service account
and resource labels.

Change-Id: I2987d1f5fc9ac961da9adf858b36fb9828154210
diff --git a/oaisim/Chart.yaml b/oaisim/Chart.yaml
index 5968db2..1ed2e71 100644
--- a/oaisim/Chart.yaml
+++ b/oaisim/Chart.yaml
@@ -16,4 +16,4 @@
 appVersion: "1.0"
 description: openairinterface5g eNB and UE
 name: oaisim
-version: 0.1.2
+version: 0.1.3
diff --git a/oaisim/templates/_helper.tpl b/oaisim/templates/_helper.tpl
index 48c53dd..20c344e 100644
--- a/oaisim/templates/_helper.tpl
+++ b/oaisim/templates/_helper.tpl
@@ -49,3 +49,70 @@
 {{- $mnc := index . 1 -}}
 {{- printf "%s%s" $mcc $mnc -}}
 {{- end -}}
+
+{{/*
+Renders a set of standardised labels
+*/}}
+{{- define "oaisim.metadata_labels" -}}
+{{- $application := index . 0 -}}
+{{- $context := index . 1 -}}
+release: {{ $context.Release.Name }}
+app: {{ $application }}
+{{- end -}}
+
+{{/*
+Render ServiceAccount, Role, and RoleBinding required for kubernetes-entrypoint.
+*/}}
+{{- define "oaisim.service_account" -}}
+{{- $context := index . 1 -}}
+{{- $saName := index . 0 -}}
+{{- $saNamespace := $context.Release.Namespace }}
+---
+apiVersion: v1
+kind: ServiceAccount
+metadata:
+  name: {{ $saName }}
+  namespace: {{ $saNamespace }}
+  labels:
+{{ tuple $saName $context | include "oaisim.metadata_labels" | indent 4 }}
+---
+apiVersion: rbac.authorization.k8s.io/v1beta1
+kind: RoleBinding
+metadata:
+  name: {{ $saName }}
+  namespace: {{ $saNamespace }}
+  labels:
+{{ tuple $saName $context | include "oaisim.metadata_labels" | indent 4 }}
+roleRef:
+  apiGroup: rbac.authorization.k8s.io
+  kind: Role
+  name: {{ $saName }}
+subjects:
+  - kind: ServiceAccount
+    name: {{ $saName }}
+    namespace: {{ $saNamespace }}
+---
+apiVersion: rbac.authorization.k8s.io/v1beta1
+kind: Role
+metadata:
+  name: {{ $saName }}
+  namespace: {{ $saNamespace }}
+  labels:
+{{ tuple $saName $context | include "oaisim.metadata_labels" | indent 4 }}
+rules:
+  - apiGroups:
+      - ""
+      - extensions
+      - batch
+      - apps
+    verbs:
+      - get
+      - list
+    resources:
+      - statefulsets
+      - jobs
+      - pods
+      - services
+      - endpoints
+      - configmaps
+{{- end -}}
diff --git a/oaisim/templates/configmap-enb.yaml b/oaisim/templates/configmap-enb.yaml
index be11d02..35ca2df 100644
--- a/oaisim/templates/configmap-enb.yaml
+++ b/oaisim/templates/configmap-enb.yaml
@@ -18,7 +18,9 @@
 apiVersion: v1
 kind: ConfigMap
 metadata:
-  name: {{ .Values.config.enb.name }}
+  name: enb
+  labels:
+{{ tuple "enb" . | include "oaisim.metadata_labels" | indent 4 }}
 data:
   nfapi.conf: |
     Active_eNBs = ("eNB-OAISIM-LTEBox");
diff --git a/oaisim/templates/configmap-ue.yaml b/oaisim/templates/configmap-ue.yaml
index 6476f7d..60b6b09 100644
--- a/oaisim/templates/configmap-ue.yaml
+++ b/oaisim/templates/configmap-ue.yaml
@@ -19,7 +19,9 @@
 apiVersion: v1
 kind: ConfigMap
 metadata:
-  name: {{ .Values.config.ue.name }}
+  name: ue
+  labels:
+{{ tuple "ue" . | include "oaisim.metadata_labels" | indent 4 }}
 data:
   ue_comac_test.conf: |
     PLMN: {
diff --git a/oaisim/templates/job-ue-setup-if.yaml b/oaisim/templates/job-ue-setup-if.yaml
index 9117578..bb78ccf 100644
--- a/oaisim/templates/job-ue-setup-if.yaml
+++ b/oaisim/templates/job-ue-setup-if.yaml
@@ -14,24 +14,21 @@
 limitations under the License.
 */}}
 
-{{- $releaseName := .Release.Name }}
-{{- $namespace := .Release.Namespace }}
-{{- $name := .Values.config.ue.name }}
 ---
 apiVersion: batch/v1
 kind: Job
 metadata:
-  name: {{ $name }}-setup-if
+  name: ue-setup-if
   labels:
-    app: {{ $name }}
+{{ tuple "ue" . | include "oaisim.metadata_labels" | indent 4 }}
   annotations:
-    "helm.sh/hook": pre-install
-    "helm.sh/hook-delete-policy": hook-succeeded
+    "helm.sh/hook": "pre-install"
+    "helm.sh/hook-delete-policy": "before-hook-creation"
 spec:
   template:
     metadata:
       labels:
-        app: {{ $name }}
+{{ tuple "ue" . | include "oaisim.metadata_labels" | indent 8 }}
     spec:
     {{- if .Values.nodeSelectors.enabled }}
       nodeSelector:
diff --git a/oaisim/templates/job-ue-teardown-if.yaml b/oaisim/templates/job-ue-teardown-if.yaml
index 0d5ce47..41c2e15 100644
--- a/oaisim/templates/job-ue-teardown-if.yaml
+++ b/oaisim/templates/job-ue-teardown-if.yaml
@@ -14,22 +14,21 @@
 limitations under the License.
 */}}
 
-{{- $name := .Values.config.ue.name }}
 ---
 apiVersion: batch/v1
 kind: Job
 metadata:
-  name: {{ $name }}-teardown-if
+  name: ue-teardown-if
   labels:
-    app: {{ $name }}
+{{ tuple "ue" . | include "oaisim.metadata_labels" | indent 4 }}
   annotations:
-    "helm.sh/hook": post-delete
-    "helm.sh/hook-delete-policy": hook-succeeded
+    "helm.sh/hook": "post-delete"
+    "helm.sh/hook-delete-policy": "before-hook-creation"
 spec:
   template:
     metadata:
       labels:
-        app: {{ $name }}
+{{ tuple "ue" . | include "oaisim.metadata_labels" | indent 8 }}
     spec:
     {{- if .Values.nodeSelectors.enabled }}
       nodeSelector:
diff --git a/oaisim/templates/statefulset-enb.yaml b/oaisim/templates/statefulset-enb.yaml
index 173b868..b96a809 100644
--- a/oaisim/templates/statefulset-enb.yaml
+++ b/oaisim/templates/statefulset-enb.yaml
@@ -14,73 +14,29 @@
 limitations under the License.
 */}}
 
-{{- $releaseName := .Release.Name }}
-{{- $namespace := .Release.Namespace }}
-{{- $name := .Values.config.enb.name }}
----
-apiVersion: v1
-kind: ServiceAccount
-metadata:
-  name: {{ $releaseName }}-{{ $name }}
-  namespace: {{ $namespace }}
----
-kind: Role
-apiVersion: rbac.authorization.k8s.io/v1
-metadata:
-  name: {{ $releaseName }}-{{ $name }}
-  namespace: {{ $namespace }}
-rules:
-  - apiGroups:
-      - ""
-      - extensions
-      - batch
-      - apps
-    verbs:
-      - get
-      - list
-    resources:
-      - daemonsets
-      - jobs
-      - pods
-      - services
-      - endpoints
----
-kind: RoleBinding
-apiVersion: rbac.authorization.k8s.io/v1
-metadata:
-  name: {{ $releaseName }}-{{ $name }}
-  namespace: {{ $namespace }}
-roleRef:
-  apiGroup: rbac.authorization.k8s.io
-  kind: Role
-  name: {{ $releaseName }}-{{ $name }}
-subjects:
-  - kind: ServiceAccount
-    name: {{ $releaseName }}-{{ $name }}
+{{ tuple "enb" . | include "oaisim.service_account" }}
 ---
 apiVersion: apps/v1
 kind: StatefulSet
 metadata:
-  name: {{ $name }}
-  namespace: {{ $namespace }}
+  name: enb
   labels:
-    app: oai-enb
+{{ tuple "enb" . | include "oaisim.metadata_labels" | indent 4 }}
 spec:
   replicas: 1
   selector:
     matchLabels:
-      app: oai-enb
-  serviceName: {{ $name | quote }}
+{{ tuple "enb" . | include "oaisim.metadata_labels" | indent 6 }}
   template:
     metadata:
       labels:
-        app: oai-enb
+{{ tuple "enb" . | include "oaisim.metadata_labels" | indent 8 }}
     spec:
     {{- if .Values.nodeSelectors.enabled }}
       nodeSelector:
         {{ .Values.nodeSelectors.enb.label }}: {{ .Values.nodeSelectors.enb.value }}
     {{- end }}
-      serviceAccountName: {{ $releaseName }}-{{ $name }}
+      serviceAccountName: enb
       hostNetwork: true
       initContainers:
         - name: enb-dep-check
@@ -134,7 +90,7 @@
             - name: pod-share
               mountPath: /etc/oaisim/enb
       containers:
-        - name: {{ $name }}
+        - name: enb
           image: {{ .Values.images.tags.enb | quote }}
           imagePullPolicy: {{ .Values.images.pullPolicy }}
           securityContext:
@@ -162,11 +118,11 @@
       volumes:
         - name: config
           configMap:
-            name: {{ $name }}
+            name: enb
             defaultMode: 420
         - name: script
           configMap:
-            name: {{ $name }}
+            name: enb
             defaultMode: 493
         - name: pod-share
           emptyDir: {}
diff --git a/oaisim/templates/statefulset-ue.yaml b/oaisim/templates/statefulset-ue.yaml
index 1d412b7..289dad3 100644
--- a/oaisim/templates/statefulset-ue.yaml
+++ b/oaisim/templates/statefulset-ue.yaml
@@ -14,73 +14,29 @@
 limitations under the License.
 */}}
 
-{{- $releaseName := .Release.Name }}
-{{- $namespace := .Release.Namespace }}
-{{- $name := .Values.config.ue.name }}
----
-apiVersion: v1
-kind: ServiceAccount
-metadata:
-  name: {{ $releaseName }}-{{ $name }}
-  namespace: {{ $namespace }}
----
-kind: Role
-apiVersion: rbac.authorization.k8s.io/v1
-metadata:
-  name: {{ $releaseName }}-{{ $name }}
-  namespace: {{ $namespace }}
-rules:
-  - apiGroups:
-      - ""
-      - extensions
-      - batch
-      - apps
-    verbs:
-      - get
-      - list
-    resources:
-      - daemonsets
-      - jobs
-      - pods
-      - services
-      - endpoints
----
-kind: RoleBinding
-apiVersion: rbac.authorization.k8s.io/v1
-metadata:
-  name: {{ $releaseName }}-{{ $name }}
-  namespace: {{ $namespace }}
-roleRef:
-  apiGroup: rbac.authorization.k8s.io
-  kind: Role
-  name: {{ $releaseName }}-{{ $name }}
-subjects:
-  - kind: ServiceAccount
-    name: {{ $releaseName }}-{{ $name }}
+{{ tuple "ue" . | include "oaisim.service_account" }}
 ---
 apiVersion: apps/v1
 kind: StatefulSet
 metadata:
-  name: {{ $name }}
-  namespace: {{ $namespace }}
+  name: ue
   labels:
-    app: oai-ue
+{{ tuple "ue" . | include "oaisim.metadata_labels" | indent 4 }}
 spec:
   replicas: 1
   selector:
     matchLabels:
-      app: oai-ue
-  serviceName: {{ $name | quote }}
+{{ tuple "ue" . | include "oaisim.metadata_labels" | indent 6 }}
   template:
     metadata:
       labels:
-        app: oai-ue
+{{ tuple "ue" . | include "oaisim.metadata_labels" | indent 8 }}
     spec:
     {{- if .Values.nodeSelectors.enabled }}
       nodeSelector:
         {{ .Values.nodeSelectors.ue.label }}: {{ .Values.nodeSelectors.ue.value }}
     {{- end }}
-      serviceAccountName: {{ $releaseName }}-{{ $name }}
+      serviceAccountName: ue
       hostNetwork: true
       initContainers:
         - name: ue-dep-check
@@ -106,7 +62,9 @@
             - name: COMMAND
               value: "echo done"
             - name: DEPENDENCY_POD_JSON
-              value: '[{"labels": {"app": "oai-enb"}, "requireSameNode": true}]'
+              value: '[{"labels": {"app": "enb"}, "requireSameNode": true}]'
+            - name: DEPENDENCY_JOBS
+              value: ue-setup-if
           command:
             - kubernetes-entrypoint
           volumeMounts:
@@ -136,7 +94,7 @@
               mountPath: /opt/oaisim/ue/scripts/ue-init.sh
               subPath: ue-init.sh
       containers:
-        - name: {{ $name }}
+        - name: ue
           image: {{ .Values.images.tags.ue | quote }}
           imagePullPolicy: {{ .Values.images.pullPolicy }}
           securityContext:
@@ -166,11 +124,11 @@
       volumes:
         - name: config
           configMap:
-            name: {{ $name }}
+            name: ue
             defaultMode: 420
         - name: script
           configMap:
-            name: {{ $name }}
+            name: ue
             defaultMode: 493
         - name: pod-share
           emptyDir: {}
diff --git a/oaisim/values.yaml b/oaisim/values.yaml
index b9d87f0..3ea6ba2 100644
--- a/oaisim/values.yaml
+++ b/oaisim/values.yaml
@@ -37,7 +37,6 @@
     mnc: "01"
     mnc_length: 2
   enb:
-    name: enb
     mme:
       # Provide the IP address of MME.
       # MME provies nodePort for eNB access, so 127.0.0.1 should work
@@ -53,7 +52,6 @@
         port: 2152
   ue:
     # 1 UE is supported for now
-    name: ue
     sim:
       msin: "4567891201"
       api_key: "465b5ce8b199b49faa5f0a2ee238a6bc"