From 05bf70f4c7c47b716c0928ebf747bcac5c152c4d Mon Sep 17 00:00:00 2001
From: Pete Birley <pete@port.direct>
Date: Thu, 28 Sep 2017 11:43:10 -0500
Subject: [PATCH] Telemetry: add postgresql chart

This PS adds a PostgreSQL chart.

Change-Id: Id33b110e242d86a78215bc3926bfba4602b9a1c9
Co-Authored-By: Steve Wilkerson <sw5822@att.com>
Co-Authored-By: Larry Rensing <lr699s@att.com>
---
 postgresql/.helmignore                | 21 ++++++++
 postgresql/Chart.yaml                 | 24 +++++++++
 postgresql/requirements.yaml          | 19 +++++++
 postgresql/templates/service.yaml     | 28 ++++++++++
 postgresql/templates/statefulset.yaml | 73 +++++++++++++++++++++++++++
 postgresql/values.yaml                | 68 +++++++++++++++++++++++++
 tools/gate/launch-osh/basic.sh        |  8 +++
 7 files changed, 241 insertions(+)
 create mode 100644 postgresql/.helmignore
 create mode 100644 postgresql/Chart.yaml
 create mode 100644 postgresql/requirements.yaml
 create mode 100644 postgresql/templates/service.yaml
 create mode 100644 postgresql/templates/statefulset.yaml
 create mode 100644 postgresql/values.yaml

diff --git a/postgresql/.helmignore b/postgresql/.helmignore
new file mode 100644
index 0000000000..f0c1319444
--- /dev/null
+++ b/postgresql/.helmignore
@@ -0,0 +1,21 @@
+# Patterns to ignore when building packages.
+# This supports shell glob matching, relative path matching, and
+# negation (prefixed with !). Only one pattern per line.
+.DS_Store
+# Common VCS dirs
+.git/
+.gitignore
+.bzr/
+.bzrignore
+.hg/
+.hgignore
+.svn/
+# Common backup files
+*.swp
+*.bak
+*.tmp
+*~
+# Various IDEs
+.project
+.idea/
+*.tmproj
diff --git a/postgresql/Chart.yaml b/postgresql/Chart.yaml
new file mode 100644
index 0000000000..2af412a4b7
--- /dev/null
+++ b/postgresql/Chart.yaml
@@ -0,0 +1,24 @@
+# Copyright 2017 The Openstack-Helm Authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+apiVersion: v1
+description: OpenStack-Helm PostgreSQL
+name: postgresql
+version: 0.1.0
+home: https://www.postgresql.org
+sources:
+  - https://github.com/postgres/postgres
+  - https://git.openstack.org/cgit/openstack/openstack-helm
+maintainers:
+  - name: OpenStack-Helm Authors
diff --git a/postgresql/requirements.yaml b/postgresql/requirements.yaml
new file mode 100644
index 0000000000..00a045b4e4
--- /dev/null
+++ b/postgresql/requirements.yaml
@@ -0,0 +1,19 @@
+
+# Copyright 2017 The Openstack-Helm Authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+dependencies:
+  - name: helm-toolkit
+    repository: http://localhost:8879/charts
+    version: 0.1.0
diff --git a/postgresql/templates/service.yaml b/postgresql/templates/service.yaml
new file mode 100644
index 0000000000..89f43dc7d2
--- /dev/null
+++ b/postgresql/templates/service.yaml
@@ -0,0 +1,28 @@
+{{/*
+Copyright 2017 The Openstack-Helm Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/}}
+
+{{- $envAll := . }}
+---
+apiVersion: v1
+kind: Service
+metadata:
+  name: {{ tuple "postgresql" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }}
+spec:
+  ports:
+    - name: db
+      port: {{ tuple "postgresql" "internal" "postgresql" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }}
+  selector:
+{{ tuple $envAll "postgresql" "server" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }}
diff --git a/postgresql/templates/statefulset.yaml b/postgresql/templates/statefulset.yaml
new file mode 100644
index 0000000000..23398291de
--- /dev/null
+++ b/postgresql/templates/statefulset.yaml
@@ -0,0 +1,73 @@
+{{/*
+Copyright 2017 The Openstack-Helm Authors.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+   http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+*/}}
+
+{{- $envAll := . }}
+---
+apiVersion: apps/v1beta1
+kind: StatefulSet
+metadata:
+  name: postgresql
+spec:
+  serviceName: {{ tuple "postgresql" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }}
+  replicas: {{ .Values.pod.replicas.server }}
+  template:
+    metadata:
+      labels:
+{{ tuple $envAll "postgresql" "server" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }}
+    spec:
+      affinity:
+{{ tuple $envAll "postgresql" "server" | include "helm-toolkit.snippets.kubernetes_pod_anti_affinity" | indent 8 }}
+      nodeSelector:
+        {{ .Values.labels.node_selector_key }}: {{ .Values.labels.node_selector_value }}
+      containers:
+      - name: postgresql
+        image: {{ .Values.images.postgresql }}
+        imagePullPolicy: {{ .Values.images.pull_policy }}
+        ports:
+        - containerPort: {{ tuple "postgresql" "internal" "postgresql" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }}
+        livenessProbe:
+          exec:
+            command:
+            - pg_isready
+          initialDelaySeconds: 20
+          timeoutSeconds: 5
+        readinessProbe:
+          exec:
+            command:
+            - pg_isready
+          initialDelaySeconds: 20
+          timeoutSeconds: 5
+{{ tuple $envAll $envAll.Values.pod.resources.server | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }}
+        volumeMounts:
+        - name: postgresql-data
+          mountPath: /var/lib/postgresql
+{{- if not .Values.storage.pvc.enabled }}
+      volumes:
+      - name: postgresql-data
+        hostPath:
+          path: {{ .Values.storage.host.host_path }}
+{{- else }}
+  volumeClaimTemplates:
+  - metadata:
+      name: postgresql-data
+      annotations:
+        {{ .Values.storage.pvc.class_path }}: {{ .Values.storage.pvc.class_name }}
+    spec:
+      accessModes: [ "ReadWriteOnce" ]
+      resources:
+        requests:
+          storage: {{ .Values.storage.pvc.size }}
+{{- end }}
diff --git a/postgresql/values.yaml b/postgresql/values.yaml
new file mode 100644
index 0000000000..194cc91b37
--- /dev/null
+++ b/postgresql/values.yaml
@@ -0,0 +1,68 @@
+# Copyright 2017 The Openstack-Helm Authors.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Default values for postgresql.
+# This is a YAML-formatted file.
+# Declare variables to be passed into your templates.
+
+pod:
+  affinity:
+    anti:
+      type:
+        default: preferredDuringSchedulingIgnoredDuringExecution
+      topologyKey:
+        default: kubernetes.io/hostname
+  replicas:
+    #only 1 replica currently supported
+    server: 1
+  resources:
+    enabled: false
+    server:
+      requests:
+        memory: "128Mi"
+        cpu: "100m"
+      limits:
+        memory: "1024Mi"
+        cpu: "2000m"
+
+# using dockerhub postgresql: https://hub.docker.com/r/library/postgres/tags/
+images:
+  postgresql: "docker.io/postgres:9.5"
+  pull_policy: IfNotPresent
+
+storage:
+  pvc:
+    enabled: true
+    size: 5Gi
+    class_name: general
+    class_path: volume.beta.kubernetes.io/storage-class
+  host:
+    host_path: /data/openstack-helm/postgresql
+
+labels:
+  node_selector_key: openstack-control-plane
+  node_selector_value: enabled
+
+endpoints:
+  cluster_domain_suffix: cluster.local
+  postgresql:
+    hosts:
+      default: postgresql
+    host_fqdn_override:
+      default: null
+    path: null
+    scheme: 'http'
+    port:
+      postgresql:
+        default: 5432
diff --git a/tools/gate/launch-osh/basic.sh b/tools/gate/launch-osh/basic.sh
index 1dbe744254..ffc3c43d73 100755
--- a/tools/gate/launch-osh/basic.sh
+++ b/tools/gate/launch-osh/basic.sh
@@ -123,6 +123,14 @@ else
          --set pod.replicas.server=1
 fi
 
+if [ "x$INTEGRATION" == "xmulti" ]; then
+  if [ "x$PVC_BACKEND" == "xceph" ]; then
+    #NOTE(portdirect): Deploy Telemetry componets here to enable ingestion
+    # of data from other services as the come online.
+    helm install --namespace=openstack ${WORK_DIR}/postgresql --name=postgresql
+  fi
+fi
+
 if [[ "x${PVC_BACKEND}" != "xceph"  ]] && [[ "x${GLANCE}" != "xpvc" ]] ; then
     echo "Gate only supports glance with pvc backend when not using ceph"
     exit 1