# If you encounter "SSL certification error (subject name does not match the host name)", you may try to set DOCKER_VERNEMQ_KUBERNETES_INSECURE to "1".
insecure=""
if env | grep-q"DOCKER_VERNEMQ_KUBERNETES_INSECURE";then
insecure="--insecure"
fi
if env | grep-q"DOCKER_VERNEMQ_DISCOVERY_KUBERNETES";then
# Let's set our nodename correctly
VERNEMQ_KUBERNETES_SUBDOMAIN=$(curl -X GET $insecure--cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://kubernetes.default.svc.cluster.local/api/v1/namespaces/$DOCKER_VERNEMQ_KUBERNETES_NAMESPACE/pods?labelSelector=app=$DOCKER_VERNEMQ_KUBERNETES_APP_LABEL-H"Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" | jq '.items[0].spec.subdomain' | sed 's/"//g' | tr '\n''\0')
sed -i.bak -r"s/VerneMQ@.+/VerneMQ@${VERNEMQ_KUBERNETES_HOSTNAME}/" /etc/vernemq/vm.args
# Hack into K8S DNS resolution (temporarily)
kube_pod_names=$(curl -X GET $insecure--cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt https://kubernetes.default.svc.cluster.local/api/v1/namespaces/$DOCKER_VERNEMQ_KUBERNETES_NAMESPACE/pods?labelSelector=app=$DOCKER_VERNEMQ_KUBERNETES_APP_LABEL-H"Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" | jq '.items[].spec.hostname' | sed 's/"//g' | tr '\n'' ')
for kube_pod_name in$kube_pod_names;
# Cluster discovery implementation based on https://github.com/thesandlord/kubernetes-pod-ip-finder
if env | grep-q"KUBE_VERNEMQ_DISCOVERY_URL";then
response=$(curl ${KUBE_VERNEMQ_DISCOVERY_URL})
IFS=','
nodes=($(echo"$response" | tr -d'[]"'))
length=$(echo${#nodes[@]})
for i in"${nodes[@]}"
do
if[$kube_pod_name=="null"]
then
echo"Kubernetes discovery selected, but no pods found. Maybe we're the first?"
echo"Anyway, we won't attempt to join any cluster."
break
fi
if[$kube_pod_name!=$MY_POD_NAME]
then
echo"Will join an existing Kubernetes cluster with discovery node at ${kube_pod_name}.${VERNEMQ_KUBERNETES_SUBDOMAIN}.${DOCKER_VERNEMQ_KUBERNETES_NAMESPACE}.svc.cluster.local"