split gcs and roscore in ros-core chart

Change-Id: I9453d5bea8de8f04d71663aff77f108c2f386b5c
diff --git a/apps/ros-core/templates/_helpers.tpl b/apps/ros-core/templates/_helpers.tpl
index 706bdf7..c7d4731 100644
--- a/apps/ros-core/templates/_helpers.tpl
+++ b/apps/ros-core/templates/_helpers.tpl
@@ -26,3 +26,70 @@
 {{- $wtf := $context.Template.Name | replace $last $name -}}
 {{ include $wtf $context }}
 {{- end -}}
+
+{{/*
+Render ServiceAccount, Role, and RoleBinding required for kubernetes-entrypoint.
+*/}}
+{{- define "ros-core.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 "ros-core.metadata_labels" | indent 4 }}
+---
+{{- if semverCompare ">=1.16-0" $context.Capabilities.KubeVersion.GitVersion }}
+apiVersion: rbac.authorization.k8s.io/v1
+{{- else }}
+apiVersion: rbac.authorization.k8s.io/v1beta1
+{{- end }}
+kind: RoleBinding
+metadata:
+  name: {{ $saName }}
+  namespace: {{ $saNamespace }}
+  labels:
+{{ tuple $saName $context | include "ros-core.metadata_labels" | indent 4 }}
+roleRef:
+  apiGroup: rbac.authorization.k8s.io
+  kind: Role
+  name: {{ $saName }}
+subjects:
+  - kind: ServiceAccount
+    name: {{ $saName }}
+    namespace: {{ $saNamespace }}
+---
+{{- if semverCompare ">=1.16-0" $context.Capabilities.KubeVersion.GitVersion }}
+apiVersion: rbac.authorization.k8s.io/v1
+{{- else }}
+apiVersion: rbac.authorization.k8s.io/v1beta1
+{{- end }}
+kind: Role
+metadata:
+  name: {{ $saName }}
+  namespace: {{ $saNamespace }}
+  labels:
+{{ tuple $saName $context | include "ros-core.metadata_labels" | indent 4 }}
+rules:
+  - apiGroups:
+      - ""
+      - extensions
+      - batch
+      - apps
+    verbs:
+      - get
+      - list
+      - patch
+    resources:
+      - statefulsets
+      - daemonsets
+      - jobs
+      - pods
+      - services
+      - endpoints
+      - configmaps
+{{- end -}}
\ No newline at end of file
diff --git a/apps/ros-core/templates/bin/_net_listener.tpl b/apps/ros-core/templates/bin/_net_listener.py.tpl
similarity index 100%
rename from apps/ros-core/templates/bin/_net_listener.tpl
rename to apps/ros-core/templates/bin/_net_listener.py.tpl
diff --git a/apps/ros-core/templates/bin/_run_gcs.sh.tpl b/apps/ros-core/templates/bin/_run_gcs.sh.tpl
new file mode 100644
index 0000000..a85b059
--- /dev/null
+++ b/apps/ros-core/templates/bin/_run_gcs.sh.tpl
@@ -0,0 +1,13 @@
+#!/bin/bash
+# Copyright 2021-present Open Networking Foundation
+#
+# SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
+
+set -ex
+
+{{ if .Values.config.rosCore.debug }}
+while true; do sleep 3600; done;
+{{ else }}
+cd /root/TrajBridge-PX4;
+source devel/setup.bash; roslaunch bridge_px4 gcs.launch
+{{ end }}
diff --git a/apps/ros-core/templates/bin/_run_roscore.sh.tpl b/apps/ros-core/templates/bin/_run_roscore.sh.tpl
index 54eff77..b937b4a 100644
--- a/apps/ros-core/templates/bin/_run_roscore.sh.tpl
+++ b/apps/ros-core/templates/bin/_run_roscore.sh.tpl
@@ -8,5 +8,6 @@
 {{ if .Values.config.rosCore.debug }}
 while true; do sleep 3600; done;
 {{ else }}
-source /opt/ros/melodic/setup.bash && roscore;
+cd /root/TrajBridge-PX4;
+source devel/setup.bash; roslaunch bridge_px4 core.launch
 {{ end }}
diff --git a/apps/ros-core/templates/configmap-gcs.yaml b/apps/ros-core/templates/configmap-gcs.yaml
new file mode 100644
index 0000000..594e1b2
--- /dev/null
+++ b/apps/ros-core/templates/configmap-gcs.yaml
@@ -0,0 +1,40 @@
+# Copyright 2021-present Open Networking Foundation
+#
+# SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
+
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: gcs
+  labels:
+{{ tuple "gcs" . | include "ros-core.metadata_labels" | indent 4 }}
+data:
+  run_gcs.sh: |
+{{ tuple "bin/_run_gcs.sh.tpl" . | include "ros-core.template" | indent 4 }}
+
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: gcs-launch
+  labels:
+{{ tuple "gcs-launch" . | include "ros-core.metadata_labels" | indent 4 }}
+data:
+{{- range $key, $value := .Values.config.gcs.launchFiles }}
+  {{ $key }}: |-
+{{ $value | indent 4 }}
+{{- end }}
+
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: gcs-trajectory
+  labels:
+{{ tuple "gcs-trajectory" . | include "ros-core.metadata_labels" | indent 4 }}
+data:
+{{- range $key, $value := .Values.config.gcs.trajectoryFiles }}
+  {{ $key }}: |-
+{{ $value | indent 4 }}
+{{- end }}
\ No newline at end of file
diff --git a/apps/ros-core/templates/configmap-roscore.yaml b/apps/ros-core/templates/configmap-roscore.yaml
index 5a7680f..ea74bcf 100644
--- a/apps/ros-core/templates/configmap-roscore.yaml
+++ b/apps/ros-core/templates/configmap-roscore.yaml
@@ -13,4 +13,17 @@
   run_roscore.sh: |
 {{ tuple "bin/_run_roscore.sh.tpl" . | include "ros-core.template" | indent 4 }}
   net_listener.py: |
-{{ tuple "bin/_net_listener.tpl" . | include "ros-core.template" | indent 4 }}
+{{ tuple "bin/_net_listener.py.tpl" . | include "ros-core.template" | indent 4 }}
+
+---
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  name: ros-core-launch
+  labels:
+{{ tuple "ros-core-launch" . | include "ros-core.metadata_labels" | indent 4 }}
+data:
+{{- range $key, $value := .Values.config.rosCore.launchFiles }}
+  {{ $key }}: |-
+{{ $value | indent 4 }}
+{{- end }}
\ No newline at end of file
diff --git a/apps/ros-core/templates/statefulset-gcs.yaml b/apps/ros-core/templates/statefulset-gcs.yaml
new file mode 100644
index 0000000..ff80453
--- /dev/null
+++ b/apps/ros-core/templates/statefulset-gcs.yaml
@@ -0,0 +1,96 @@
+# Copyright 2021-present Open Networking Foundation
+#
+# SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
+
+{{ tuple "gcs" . | include "ros-core.service_account" }}
+---
+apiVersion: apps/v1
+kind: StatefulSet
+metadata:
+  name: gcs
+  labels:
+{{ tuple "gcs" . | include "ros-core.metadata_labels" | indent 4 }}
+spec:
+  replicas: 1
+  serviceName: gcs
+  selector:
+    matchLabels:
+{{ tuple "gcs" . | include "ros-core.metadata_labels" | indent 6 }}
+  template:
+    metadata:
+      labels:
+{{ tuple "gcs" . | include "ros-core.metadata_labels" | indent 8 }}
+    spec:
+  {{- if .Values.nodeSelector.enabled }}
+      nodeSelector:
+        {{ .Values.nodeSelector.gcs.label }}: {{ .Values.nodeSelector.gcs.value }}
+  {{- end }}
+      serviceAccountName: gcs
+  {{- if .Values.networks.hostNetwork }}
+      hostNetwork: true
+      dnsPolicy: None
+      dnsConfig:
+        nameservers:
+          - {{ .Values.coredns.service.clusterIP }}
+  {{- end }}
+      initContainers:
+      - name: gcs-dep-check
+        image: {{ .Values.images.tags.depCheck }}
+        imagePullPolicy: {{ .Values.images.pullPolicy }}
+        securityContext:
+          allowPrivilegeEscalation: false
+          readOnlyRootFilesystem: false
+          runAsUser: 0
+        env:
+          - name: POD_NAME
+            valueFrom:
+              fieldRef:
+                apiVersion: v1
+                fieldPath: metadata.name
+          - name: NAMESPACE
+            valueFrom:
+              fieldRef:
+                apiVersion: v1
+                fieldPath: metadata.namespace
+          - name: PATH
+            value: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/
+          - name: COMMAND
+            value: "echo done"
+          - name: DEPENDENCY_POD_JSON
+            value: '[{"labels": {"app": "ros-core"}, "requireSameNode": false}]'
+          # add dependency job for make_certs.sh
+        command:
+          - kubernetes-entrypoint
+        volumeMounts:
+          []
+      containers:
+      - name: gcs
+        image: {{ .Values.images.tags.rosCore }}
+        imagePullPolicy: {{ .Values.images.pullPolicy }}
+        securityContext:
+          privileged: true
+        stdin: true
+        tty: true
+        command:
+        - /root/run_gcs.sh
+        volumeMounts:
+        - name: gcs
+          mountPath: /root/run_gcs.sh
+          subPath: run_gcs.sh
+        - name: gcs-launch
+          mountPath: /root/TrajBridge-PX4/src/bridge_px4/launch
+        - name: gcs-trajectory
+          mountPath: /root/TrajBridge-PX4/src/bridge_px4/trajectories
+      volumes:
+        - name: gcs
+          configMap:
+            name: gcs
+            defaultMode: 493
+        - name: gcs-launch
+          configMap:
+            name: gcs-launch
+            defaultMode: 420
+        - name: gcs-trajectory
+          configMap:
+            name: gcs-trajectory
+            defaultMode: 420
diff --git a/apps/ros-core/templates/statefulset-roscore.yaml b/apps/ros-core/templates/statefulset-roscore.yaml
index 28cb41a..2e88aca 100644
--- a/apps/ros-core/templates/statefulset-roscore.yaml
+++ b/apps/ros-core/templates/statefulset-roscore.yaml
@@ -2,6 +2,7 @@
 #
 # SPDX-License-Identifier: LicenseRef-ONF-Member-Only-1.0
 
+{{ tuple "ros-core" . | include "ros-core.service_account" }}
 ---
 apiVersion: apps/v1
 kind: StatefulSet
@@ -24,6 +25,7 @@
       nodeSelector:
         {{ .Values.nodeSelector.rosCore.label }}: {{ .Values.nodeSelector.rosCore.value }}
   {{- end }}
+      serviceAccountName: ros-core
   {{- if .Values.networks.hostNetwork }}
       hostNetwork: true
       dnsPolicy: None
@@ -45,6 +47,8 @@
         - name: ros-core
           mountPath: /root/run_roscore.sh
           subPath: run_roscore.sh
+        - name: ros-core-launch
+          mountPath: /root/TrajBridge-PX4/src/bridge_px4/launch
       - name: drone-net-listener
         image: {{ .Values.images.tags.droneNetListener }}
         imagePullPolicy: {{ .Values.images.pullPolicy }}
@@ -62,3 +66,7 @@
           configMap:
             name: ros-core
             defaultMode: 493
+        - name: ros-core-launch
+          configMap:
+            name: ros-core-launch
+            defaultMode: 420