Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | {{/* |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */}} |
| 16 | |
| 17 | set -ex |
| 18 | export HOME=/tmp |
| 19 | |
| 20 | {{- if .Values.bootstrap.enabled | default "echo 'Not Enabled'" }} |
| 21 | |
| 22 | {{- /* Create volume types defined in Values.bootstrap */}} |
| 23 | {{- /* Types can only be created for backends defined in Values.conf */}} |
| 24 | {{- $volumeTypes := .Values.bootstrap.volume_types }} |
| 25 | {{- /* Generating list of backends listed in .Values.conf.backends */}} |
| 26 | {{- $backendsList := list}} |
| 27 | {{- range $backend_name, $backend_properties := .Values.conf.backends }} |
| 28 | {{- if and $backend_properties $backend_properties.volume_backend_name }} |
| 29 | {{- $backendsList = append $backendsList $backend_properties.volume_backend_name }} |
| 30 | {{- end }} |
| 31 | {{- end }} |
| 32 | |
| 33 | {{- range $name, $properties := $volumeTypes }} |
| 34 | {{- if and $properties.volume_backend_name (has $properties.volume_backend_name $backendsList) }} |
| 35 | {{- $access_type := $properties.access_type | default "public"}} |
| 36 | # Create a volume type if it doesn't exist. |
| 37 | # Assumption: the volume type name is unique. |
| 38 | openstack volume type show {{ $name }} || \ |
| 39 | openstack volume type create \ |
| 40 | --{{ $access_type }} \ |
Rico Lin | 7306024 | 2024-08-22 00:27:56 +0800 | [diff] [blame] | 41 | {{- range $key, $value := $properties }} |
| 42 | {{- if or (eq $key "encryption-provider") (eq $key "encryption-cipher") (eq $key "encryption-key-size") (eq $key "encryption-control-location") }} |
| 43 | --{{ $key }} {{ $value}} \ |
| 44 | {{- end }} |
| 45 | {{- end }} |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 46 | {{ $name }} |
| 47 | {{/* |
| 48 | We will try to set or update volume type properties. |
| 49 | To update properties, the volume type MUST NOT BE IN USE, |
| 50 | and projects and domains with access to the volume type |
| 51 | MUST EXIST, as well. |
| 52 | */}} |
| 53 | is_in_use=$(openstack volume list --long --all-projects -c Type -f value | grep -E "^{{ $name }}\s*$" || true) |
| 54 | if [[ -z ${is_in_use} ]]; then |
| 55 | {{- if (eq $access_type "private") }} |
| 56 | volumeTypeID=$(openstack volume type show {{ $name }} -f value -c id) |
| 57 | cinder type-update --is-public false ${volumeTypeID} |
| 58 | {{- end }} |
| 59 | |
| 60 | {{- if and $properties.grant_access (eq $access_type "private") }} |
| 61 | {{- range $domain, $domainProjects := $properties.grant_access }} |
| 62 | {{- range $project := $domainProjects }} |
| 63 | project_id=$(openstack project show --domain {{ $domain }} -c id -f value {{ $project }}) |
| 64 | if [[ -z $(openstack volume type show {{ $name }} -c access_project_ids -f value | grep ${project_id} || true) ]]; then |
| 65 | openstack volume type set --project-domain {{ $domain }} --project {{ $project }} {{ $name }} |
| 66 | fi |
| 67 | {{- end }} |
| 68 | {{- end }} |
| 69 | {{- end }} |
| 70 | |
| 71 | {{- range $key, $value := $properties }} |
Rico Lin | 7306024 | 2024-08-22 00:27:56 +0800 | [diff] [blame] | 72 | {{- if and (ne $key "access_type") (ne $key "grant_access") (ne $key "encryption-provider") (ne $key "encryption-cipher") (ne $key "encryption-key-size") (ne $key "encryption-control-location") $value }} |
Mohammed Naser | f3f59a7 | 2023-01-15 21:02:04 -0500 | [diff] [blame] | 73 | openstack volume type set --property {{ $key }}={{ $value }} {{ $name }} |
| 74 | {{- end }} |
| 75 | {{- end }} |
| 76 | fi |
| 77 | {{- end }} |
| 78 | {{- end }} |
| 79 | |
| 80 | {{- /* Create volumes defined in Values.conf.backends */}} |
| 81 | {{- if .Values.bootstrap.bootstrap_conf_backends }} |
| 82 | {{- range $name, $properties := .Values.conf.backends }} |
| 83 | {{- if $properties }} |
| 84 | openstack volume type show {{ $name }} || \ |
| 85 | openstack volume type create \ |
| 86 | --public \ |
| 87 | --property volume_backend_name={{ $properties.volume_backend_name }} \ |
| 88 | {{ $name }} |
| 89 | {{- end }} |
| 90 | {{- end }} |
| 91 | {{- end }} |
| 92 | |
| 93 | {{- /* Create and associate volume QoS if defined */}} |
| 94 | {{- if .Values.bootstrap.volume_qos}} |
| 95 | {{- range $qos_name, $qos_properties := .Values.bootstrap.volume_qos }} |
| 96 | type_defined=true |
| 97 | {{- /* If the volume type to associate with is not defined, skip the qos */}} |
| 98 | {{- range $qos_properties.associates }} |
| 99 | if ! openstack volume type show {{ . }}; then |
| 100 | type_defined=false |
| 101 | fi |
| 102 | {{- end }} |
| 103 | if [[ ${type_defined} ]]; then |
| 104 | openstack volume qos show {{ $qos_name }} || \ |
| 105 | openstack volume qos create \ |
| 106 | --consumer {{ $qos_properties.consumer }} \ |
| 107 | {{- range $key, $value := $qos_properties.properties }} |
| 108 | --property {{ $key }}={{ $value }} \ |
| 109 | {{- end }} |
| 110 | {{ $qos_name }} |
| 111 | {{- range $qos_properties.associates }} |
| 112 | openstack volume qos associate {{ $qos_name }} {{ . }} |
| 113 | {{- end }} |
| 114 | fi |
| 115 | {{- end }} |
| 116 | {{- end }} |
| 117 | |
| 118 | {{- /* Check volume type and properties were added */}} |
| 119 | openstack volume type list --long |
| 120 | openstack volume qos list |
| 121 | {{- end }} |
| 122 | |
| 123 | exit 0 |