From d95d9990de876d3f17364928b8cdd531f9e9e46d Mon Sep 17 00:00:00 2001
From: Jeff Augustine <ja224e@att.com>
Date: Fri, 7 Apr 2017 16:37:38 +0200
Subject: [PATCH] Cinder Bootstrap

This commit adds the bootstrap framework to Cinder.

- If volume type already exists do not recreate
- Create volume types as defined in Values.conf.backends
- Create additional volume types defined in Values.bootstrap,
    available backends must be defined in Values.conf.backends

Change-Id: I577df7bf50d66c8ef70e74466a0bf1c4c221ca68
Partially implements: https://blueprints.launchpad.net/openstack-helm/+spec/service-bootstrap
---
 cinder/templates/bin/_bootstrap.sh.tpl | 53 ++++++++++++++++++++
 cinder/templates/configmap-bin.yaml    |  4 ++
 cinder/templates/job_bootstrap.yaml    | 68 ++++++++++++++++++++++++++
 cinder/values.yaml                     | 21 +++++++-
 4 files changed, 145 insertions(+), 1 deletion(-)
 create mode 100644 cinder/templates/bin/_bootstrap.sh.tpl
 create mode 100644 cinder/templates/job_bootstrap.yaml

diff --git a/cinder/templates/bin/_bootstrap.sh.tpl b/cinder/templates/bin/_bootstrap.sh.tpl
new file mode 100644
index 0000000000..42419cd279
--- /dev/null
+++ b/cinder/templates/bin/_bootstrap.sh.tpl
@@ -0,0 +1,53 @@
+#!/bin/bash
+
+# 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.
+
+set -ex
+export HOME=/tmp
+
+{{- if .Values.bootstrap.enabled | default "echo 'Not Enabled'" }}
+
+  {{- /* Create volumes defined in Values.bootstrap */}}
+  {{- range $name, $properties := .Values.bootstrap.volume_types }}
+    {{- if $properties.volume_backend_name }}
+openstack volume type show {{ $name }} || \
+  openstack volume type create \
+    --public \
+      {{- range $key, $value := $properties }}
+    --property {{ $key }}={{ $value }} \
+      {{- end }}
+    {{ $name }}
+    {{- end }}
+  {{- end }}
+
+  {{- /* Create volumes defined in Values.conf.backends */}}
+  {{- if .Values.bootstrap.bootstrap_conf_backends }}
+    {{- range $name, $properties := .Values.conf.backends }}
+      {{- if $properties }}
+openstack volume type show {{ $name }} || \
+  openstack volume type create \
+    --public \
+    --property volume_backend_name={{ $properties.volume_backend_name }} \
+    {{ $name }}
+      {{- end }}
+    {{- end }}
+  {{- end }}
+
+{{- /* Check volume type and properties were added */}}
+cinder extra-specs-list
+
+{{- end }}
+
+exit 0
diff --git a/cinder/templates/configmap-bin.yaml b/cinder/templates/configmap-bin.yaml
index 7f7dcb25e7..961363e16d 100644
--- a/cinder/templates/configmap-bin.yaml
+++ b/cinder/templates/configmap-bin.yaml
@@ -33,3 +33,7 @@ data:
 {{ tuple "bin/_cinder-scheduler.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
   cinder-volume.sh: |
 {{ tuple "bin/_cinder-volume.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
+{{- if .Values.bootstrap.enabled }}
+  bootstrap.sh: |+
+{{ tuple "bin/_bootstrap.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
+{{- end }}
diff --git a/cinder/templates/job_bootstrap.yaml b/cinder/templates/job_bootstrap.yaml
new file mode 100644
index 0000000000..3eeb2eba50
--- /dev/null
+++ b/cinder/templates/job_bootstrap.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.
+
+{{- if .Values.bootstrap.enabled }}
+{{- $envAll := . }}
+{{- $ksAdminSecret := $envAll.Values.keystone.admin_secret | default "cinder-env-keystone-admin" }}
+{{- $dependencies := .Values.dependencies.bootstrap }}
+apiVersion: batch/v1
+kind: Job
+metadata:
+  name: cinder-bootstrap
+spec:
+  template:
+    metadata:
+      annotations:
+        pod.beta.kubernetes.io/init-containers: '[
+{{ tuple $envAll $dependencies "[]" | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 10 }}
+        ]'
+    spec:
+      restartPolicy: OnFailure
+      nodeSelector:
+        {{ .Values.labels.node_selector_key }}: {{ .Values.labels.node_selector_value }}
+      containers:
+        - name: cinder-bootstrap
+          image: {{ .Values.images.bootstrap }}
+          imagePullPolicy: {{ .Values.images.pull_policy }}
+          {{- if .Values.resources.enabled }}
+          resources:
+            requests:
+              memory: {{ .Values.resources.bootstrap.requests.memory | quote }}
+              cpu: {{ .Values.resources.bootstrap.requests.cpu | quote }}
+            limits:
+              memory: {{ .Values.resources.bootstrap.limits.memory | quote }}
+              cpu: {{ .Values.resources.bootstrap.limits.cpu | quote }}
+          {{- end }}
+          command:
+            - bash
+            - /tmp/bootstrap.sh
+          env:
+{{- with $env := dict "ksUserSecret" $ksAdminSecret }}
+{{- include "helm-toolkit.snippets.keystone_openrc_env_vars" $env | indent 12 }}
+{{- end }}
+          volumeMounts:
+            - name: cinder-etc
+              mountPath: /etc/cinder/cinder.conf
+              subPath: cinder.conf
+            - name: cinder-bin
+              mountPath: /tmp/bootstrap.sh
+              subPath: bootstrap.sh
+      volumes:
+        - name: cinder-etc
+          configMap:
+            name: cinder-etc
+        - name: cinder-bin
+          configMap:
+            name: cinder-bin
+{{- end }}
diff --git a/cinder/values.yaml b/cinder/values.yaml
index 9db2144f68..ce2938ae8f 100644
--- a/cinder/values.yaml
+++ b/cinder/values.yaml
@@ -34,6 +34,7 @@ images:
   ks_service: docker.io/kolla/ubuntu-source-kolla-toolbox:3.0.3
   ks_endpoints: docker.io/kolla/ubuntu-source-kolla-toolbox:3.0.3
   api: docker.io/kolla/ubuntu-source-cinder-api:3.0.3
+  bootstrap: docker.io/kolla/ubuntu-source-cinder-api:3.0.3
   scheduler: docker.io/kolla/ubuntu-source-cinder-scheduler:3.0.3
   volume: docker.io/kolla/ubuntu-source-cinder-volume:3.0.3
   backup: docker.io/kolla/ubuntu-source-cinder-backup:3.0.3
@@ -55,6 +56,14 @@ termination_grace_period:
   api:
     timeout: 30
 
+bootstrap:
+  enabled: true
+  bootstrap_conf_backends: true
+  volume_types:
+    name:
+      group:
+      volume_backend_name:
+
 keystone:
   admin_user: "admin"
   admin_user_domain: "default"
@@ -71,7 +80,6 @@ network:
       enabled: false
       port: 30877
 
-
 conf:
   paste:
     override:
@@ -171,6 +179,10 @@ dependencies:
     service:
     - mariadb
     - keystone-api
+  bootstrap:
+    service:
+    - keystone-api
+    - cinder-api
   volume:
     jobs:
     - cinder-db-sync
@@ -285,6 +297,13 @@ resources:
     limits:
       memory: "1024Mi"
       cpu: "2000m"
+  cinder_bootstrap:
+    requests:
+      memory: "124Mi"
+      cpu: "100m"
+    limits:
+      memory: "1024Mi"
+      cpu: "2000m"
   cinder_scheduler:
     requests:
       memory: "124Mi"