swarm
This commit is contained in:
253
helm/mongodb/templates/replicaset/scripts-configmap.yaml
Normal file
253
helm/mongodb/templates/replicaset/scripts-configmap.yaml
Normal file
@ -0,0 +1,253 @@
|
||||
{{- if eq .Values.architecture "replicaset" }}
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: {{ include "mongodb.fullname" . }}-scripts
|
||||
namespace: {{ include "mongodb.namespace" . }}
|
||||
labels: {{- include "common.labels.standard" . | nindent 4 }}
|
||||
app.kubernetes.io/component: mongodb
|
||||
{{- if .Values.commonLabels }}
|
||||
{{- include "common.tplvalues.render" ( dict "value" .Values.commonLabels "context" $ ) | nindent 4 }}
|
||||
{{- end }}
|
||||
{{- if .Values.commonAnnotations }}
|
||||
annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
|
||||
{{- end }}
|
||||
data:
|
||||
{{- $fullname := include "mongodb.fullname" . }}
|
||||
{{- $releaseNamespace := include "mongodb.namespace" . }}
|
||||
{{- if and .Values.externalAccess.autoDiscovery.enabled (eq .Values.externalAccess.service.type "LoadBalancer") }}
|
||||
auto-discovery.sh: |-
|
||||
#!/bin/bash
|
||||
|
||||
SVC_NAME="${MY_POD_NAME}-external"
|
||||
|
||||
# Auxiliary functions
|
||||
retry_while() {
|
||||
local -r cmd="${1:?cmd is missing}"
|
||||
local -r retries="${2:-12}"
|
||||
local -r sleep_time="${3:-5}"
|
||||
local return_value=1
|
||||
|
||||
read -r -a command <<< "$cmd"
|
||||
for ((i = 1 ; i <= retries ; i+=1 )); do
|
||||
"${command[@]}" && return_value=0 && break
|
||||
sleep "$sleep_time"
|
||||
done
|
||||
return $return_value
|
||||
}
|
||||
k8s_svc_lb_ip() {
|
||||
local namespace=${1:?namespace is missing}
|
||||
local service=${2:?service is missing}
|
||||
local service_ip=$(kubectl get svc "$service" -n "$namespace" -o jsonpath="{.status.loadBalancer.ingress[0].ip}")
|
||||
local service_hostname=$(kubectl get svc "$service" -n "$namespace" -o jsonpath="{.status.loadBalancer.ingress[0].hostname}")
|
||||
|
||||
if [[ -n ${service_ip} ]]; then
|
||||
echo "${service_ip}"
|
||||
else
|
||||
echo "${service_hostname}"
|
||||
fi
|
||||
}
|
||||
k8s_svc_lb_ip_ready() {
|
||||
local namespace=${1:?namespace is missing}
|
||||
local service=${2:?service is missing}
|
||||
[[ -n "$(k8s_svc_lb_ip "$namespace" "$service")" ]]
|
||||
}
|
||||
# Wait until LoadBalancer IP is ready
|
||||
retry_while "k8s_svc_lb_ip_ready {{ $releaseNamespace }} $SVC_NAME" || exit 1
|
||||
# Obtain LoadBalancer external IP
|
||||
k8s_svc_lb_ip "{{ $releaseNamespace }}" "$SVC_NAME" | tee "$SHARED_FILE"
|
||||
{{- end }}
|
||||
setup.sh: |-
|
||||
#!/bin/bash
|
||||
|
||||
. /opt/bitnami/scripts/mongodb-env.sh
|
||||
|
||||
{{- if .Values.externalAccess.enabled }}
|
||||
{{- if eq .Values.externalAccess.service.type "LoadBalancer" }}
|
||||
{{- if .Values.externalAccess.autoDiscovery.enabled }}
|
||||
export MONGODB_ADVERTISED_HOSTNAME="$(<${SHARED_FILE})"
|
||||
{{- else }}
|
||||
ID="${MY_POD_NAME#"{{ $fullname }}-"}"
|
||||
export MONGODB_ADVERTISED_HOSTNAME=$(echo '{{ .Values.externalAccess.service.loadBalancerIPs }}' | tr -d '[]' | cut -d ' ' -f "$(($ID + 1))")
|
||||
{{- end }}
|
||||
{{- else if eq .Values.externalAccess.service.type "NodePort" }}
|
||||
{{- if .Values.externalAccess.service.domain }}
|
||||
export MONGODB_ADVERTISED_HOSTNAME={{ .Values.externalAccess.service.domain }}
|
||||
{{- else }}
|
||||
export MONGODB_ADVERTISED_HOSTNAME=$(curl -s https://ipinfo.io/ip)
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- if .Values.replicaSetConfigurationSettings.enabled }}
|
||||
# placed here before root password env is overwritten
|
||||
# makes no assumption about starting state
|
||||
# ensures that any stepDown or non-default starting state is handled
|
||||
/scripts/replicaSetConfigurationSettings.sh &
|
||||
{{- end }}
|
||||
|
||||
echo "Advertised Hostname: $MONGODB_ADVERTISED_HOSTNAME"
|
||||
|
||||
if [[ "$MY_POD_NAME" = "{{ $fullname }}-0" ]]; then
|
||||
echo "Pod name matches initial primary pod name, configuring node as a primary"
|
||||
export MONGODB_REPLICA_SET_MODE="primary"
|
||||
else
|
||||
echo "Pod name doesn't match initial primary pod name, configuring node as a secondary"
|
||||
export MONGODB_REPLICA_SET_MODE="secondary"
|
||||
export MONGODB_INITIAL_PRIMARY_ROOT_USER="$MONGODB_ROOT_USER"
|
||||
export MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD="$MONGODB_ROOT_PASSWORD"
|
||||
export MONGODB_INITIAL_PRIMARY_PORT_NUMBER="$MONGODB_PORT_NUMBER"
|
||||
export MONGODB_ROOT_PASSWORD=""
|
||||
export MONGODB_EXTRA_USERNAMES=""
|
||||
export MONGODB_EXTRA_DATABASES=""
|
||||
export MONGODB_EXTRA_PASSWORDS=""
|
||||
export MONGODB_ROOT_PASSWORD_FILE=""
|
||||
export MONGODB_EXTRA_USERNAMES_FILE=""
|
||||
export MONGODB_EXTRA_DATABASES_FILE=""
|
||||
export MONGODB_EXTRA_PASSWORDS_FILE=""
|
||||
fi
|
||||
|
||||
exec /opt/bitnami/scripts/mongodb/entrypoint.sh /opt/bitnami/scripts/mongodb/run.sh
|
||||
setup-hidden.sh: |-
|
||||
#!/bin/bash
|
||||
|
||||
. /opt/bitnami/scripts/mongodb-env.sh
|
||||
|
||||
{{- if .Values.externalAccess.hidden.enabled }}
|
||||
{{- if eq .Values.externalAccess.hidden.service.type "LoadBalancer" }}
|
||||
{{- if .Values.externalAccess.autoDiscovery.enabled }}
|
||||
export MONGODB_ADVERTISED_HOSTNAME="$(<${SHARED_FILE})"
|
||||
{{- else }}
|
||||
ID="${MY_POD_NAME#"{{ $fullname }}-hidden-"}"
|
||||
export MONGODB_ADVERTISED_HOSTNAME=$(echo '{{ .Values.externalAccess.hidden.service.loadBalancerIPs }}' | tr -d '[]' | cut -d ' ' -f "$(($ID + 1))")
|
||||
{{- end }}
|
||||
{{- else if eq .Values.externalAccess.hidden.service.type "NodePort" }}
|
||||
{{- if .Values.externalAccess.hidden.service.domain }}
|
||||
export MONGODB_ADVERTISED_HOSTNAME={{ .Values.externalAccess.hidden.service.domain }}
|
||||
{{- else }}
|
||||
export MONGODB_ADVERTISED_HOSTNAME=$(curl -s https://ipinfo.io/ip)
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
|
||||
{{- if .Values.replicaSetConfigurationSettings.enabled }}
|
||||
# placed here before root password env is overwritten
|
||||
# makes no assumption about starting state
|
||||
# ensures that any stepDown or non-default starting state is handled
|
||||
/scripts/replicaSetConfigurationSettings.sh &
|
||||
{{- end }}
|
||||
|
||||
echo "Advertised Hostname: $MONGODB_ADVERTISED_HOSTNAME"
|
||||
echo "Configuring node as a hidden node"
|
||||
export MONGODB_REPLICA_SET_MODE="hidden"
|
||||
export MONGODB_INITIAL_PRIMARY_ROOT_USER="$MONGODB_ROOT_USER"
|
||||
export MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD="$MONGODB_ROOT_PASSWORD"
|
||||
export MONGODB_INITIAL_PRIMARY_PORT_NUMBER="$MONGODB_PORT_NUMBER"
|
||||
export MONGODB_ROOT_PASSWORD=""
|
||||
export MONGODB_EXTRA_USERNAMES=""
|
||||
export MONGODB_EXTRA_DATABASES=""
|
||||
export MONGODB_EXTRA_PASSWORDS=""
|
||||
export MONGODB_ROOT_PASSWORD_FILE=""
|
||||
export MONGODB_EXTRA_USERNAMES_FILE=""
|
||||
export MONGODB_EXTRA_DATABASES_FILE=""
|
||||
export MONGODB_EXTRA_PASSWORDS_FILE=""
|
||||
exec /opt/bitnami/scripts/mongodb/entrypoint.sh /opt/bitnami/scripts/mongodb/run.sh
|
||||
{{- if .Values.replicaSetConfigurationSettings.enabled }}
|
||||
replicaSetConfigurationSettings.sh: |-
|
||||
#!/bin/bash
|
||||
# This script to be called when pod starts.
|
||||
# This script sets rs settings which can not be applied via conf file
|
||||
|
||||
function logger ()
|
||||
#$1 is the line to be logged
|
||||
{
|
||||
echo "replicaSetConfigurationSettings.sh -- ${1}" >&1
|
||||
}
|
||||
|
||||
SLEEP_PERIOD=10
|
||||
|
||||
{{- if and .Values.auth.enabled .Values.auth.rootPassword}}
|
||||
usernameAndPassword="-u root -p ${MONGODB_ROOT_PASSWORD}"
|
||||
{{- else }}
|
||||
usernameAndPassword=""
|
||||
{{- end }}
|
||||
|
||||
# load Values.replicaSetConfigurationSettings.configuration into associtive array which makes iterating and string manipulation easy
|
||||
declare -A desiredRsConf
|
||||
{{ range $setting, $value := .Values.replicaSetConfigurationSettings.configuration -}}
|
||||
{{ printf "desiredRsConf[%s]='%v'" $setting $value }}
|
||||
{{ end }}
|
||||
|
||||
rsConfWriteAttempts=0
|
||||
rs_conf_configured_ok=unknown
|
||||
|
||||
while [[ "${rs_conf_configured_ok}" != "true" ]]; do
|
||||
|
||||
# give the rs setup a chance to succeed before attempting to read or configure
|
||||
sleep ${SLEEP_PERIOD}
|
||||
|
||||
counter=0
|
||||
while ! mongo ${usernameAndPassword} --eval 'rs.conf()'; do
|
||||
counter=$((${counter} +1))
|
||||
logger "not yet able to read rs.conf settings from the currently running rs (after ${counter} attempts)"
|
||||
sleep ${SLEEP_PERIOD}
|
||||
done
|
||||
counter=$((${counter} +1))
|
||||
logger "rs.conf settings have been read from the currently running rs (after ${counter} attempts)"
|
||||
|
||||
# read rs.conf again and store it. settings format is '"<key>" : <value>,'
|
||||
currentRsConf=$(mongo ${usernameAndPassword} --eval 'rs.conf()')
|
||||
|
||||
desiredEqualsactual=unknown
|
||||
settingsToConfigure=""
|
||||
for key in ${!desiredRsConf[@]}; do
|
||||
value=${desiredRsConf[$key]}
|
||||
if ! $(echo "\"${currentRsConf}"\" | grep -q -e "\"${key}\" : ${value},"); then
|
||||
logger "rs conf setting: ${key} value will be set to: ${value}"
|
||||
settingsToConfigure="${settingsToConfigure}cfg.settings.${key} = ${value}; "
|
||||
desiredEqualsactual=false
|
||||
else
|
||||
logger "rs conf: ${key} is already at desired value: ${value}"
|
||||
fi
|
||||
done
|
||||
|
||||
if [[ "${desiredEqualsactual}" != "false" ]]; then
|
||||
logger "replicaSetConfigurationSettings match the settings of the currently running rs"
|
||||
desiredEqualsactual=true
|
||||
rs_conf_configured_ok=true
|
||||
logger "Current settings match desired settings (There have been ${rsConfWriteAttempts} attempts to write to mongoDB rs configuration)"
|
||||
exit
|
||||
fi
|
||||
|
||||
# apply the settings only if this member is currently the mongo replicaset PRIMARY
|
||||
# it might take a little time before any pod is PRIMARY
|
||||
isMaster=unknown
|
||||
if ! mongo ${usernameAndPassword} --eval 'rs.isMaster()' | grep -q "ismaster\" : true"; then
|
||||
isMaster=false
|
||||
logger "This node is not yet PRIMARY - replicaSetConfigurationSettings will only be set on the member that is currently PRIMARY"
|
||||
else
|
||||
isMaster=true
|
||||
logger "This node is PRIMARY"
|
||||
fi
|
||||
|
||||
if [[ "${isMaster}" == "true" ]]; then
|
||||
logger "This node is currently PRIMARY - will apply rs.conf settings"
|
||||
|
||||
# avoiding tricky string substitution with single quotes by making the eval string a set of vars
|
||||
rsconf="cfg = rs.conf();"
|
||||
rsreconf="rs.reconfig(cfg);"
|
||||
rsCommand="${rsconf} ${settingsToConfigure} ${rsreconf}"
|
||||
|
||||
mongo ${usernameAndPassword} --eval "${rsCommand}"
|
||||
if [ $? -ne 0 ]; then
|
||||
logger "Failed to apply mongodb cfg.settings configuration"
|
||||
else
|
||||
logger "mongodb replicaset cfg.settings configuration applied"
|
||||
logger "Will check rs conf"
|
||||
# don't exit just yet - the settings will be checked in the next loop
|
||||
fi
|
||||
rsConfWriteAttempts=$((${rsConfWriteAttempts} + 1 ))
|
||||
fi
|
||||
done
|
||||
{{- end }}
|
||||
{{- end }}
|
Reference in New Issue
Block a user