nats.sh 6.24 KB
Newer Older
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#!/bin/bash
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# Copyright 2022-2023 ETSI TeraFlowSDN - TFS OSG (https://tfs.etsi.org/)
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
#
# 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.

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
########################################################################################################################
# Read deployment settings
########################################################################################################################

# If not already set, set the namespace where NATS will be deployed.
export NATS_NAMESPACE=${NATS_NAMESPACE:-"nats"}

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# If not already set, set the external port NATS Client interface will be exposed to.
export NATS_EXT_PORT_CLIENT=${NATS_EXT_PORT_CLIENT:-"4222"}

# If not already set, set the external port NATS HTTP Mgmt GUI interface will be exposed to.
export NATS_EXT_PORT_HTTP=${NATS_EXT_PORT_HTTP:-"8222"}

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
# If not already set, disable flag for re-deploying NATS from scratch.
# WARNING: ACTIVATING THIS FLAG IMPLIES LOOSING THE MESSAGE BROKER INFORMATION!
# If NATS_REDEPLOY is "YES", the message broker will be dropped while checking/deploying NATS.
export NATS_REDEPLOY=${NATS_REDEPLOY:-""}

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
########################################################################################################################
# Automated steps start here
########################################################################################################################

function nats_deploy_single() {
    echo "NATS Namespace"
    echo ">>> Create NATS Namespace (if missing)"
    kubectl create namespace ${NATS_NAMESPACE}
    echo

    echo "Add NATS Helm Chart"
    helm3 repo add nats https://nats-io.github.io/k8s/helm/charts/
    echo

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    echo "Upgrade NATS Helm Chart"
    helm3 repo update nats
    echo

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    echo "Install NATS (single-node)"
    echo ">>> Checking if NATS is deployed..."
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    if kubectl get --namespace ${NATS_NAMESPACE} statefulset/${NATS_NAMESPACE} &> /dev/null; then
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        echo ">>> NATS is present; skipping step."
    else
        echo ">>> Deploy NATS"
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        helm3 install ${NATS_NAMESPACE} nats/nats --namespace ${NATS_NAMESPACE} --set nats.image=nats:2.9-alpine
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed

        echo ">>> Waiting NATS statefulset to be created..."
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        while ! kubectl get --namespace ${NATS_NAMESPACE} statefulset/${NATS_NAMESPACE} &> /dev/null; do
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            printf "%c" "."
            sleep 1
        done

        # Wait for statefulset condition "Available=True" does not work
        # Wait for statefulset condition "jsonpath='{.status.readyReplicas}'=3" throws error:
        #   "error: readyReplicas is not found"
        # Workaround: Check the pods are ready
        #echo ">>> NATS statefulset created. Waiting for readiness condition..."
        #kubectl wait --namespace  ${NATS_NAMESPACE} --for=condition=Available=True --timeout=300s statefulset/nats
        #kubectl wait --namespace ${NATS_NAMESPACE} --for=jsonpath='{.status.readyReplicas}'=3 --timeout=300s \
        #    statefulset/nats
        echo ">>> NATS statefulset created. Waiting NATS pods to be created..."
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        while ! kubectl get --namespace ${NATS_NAMESPACE} pod/${NATS_NAMESPACE}-0 &> /dev/null; do
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
            printf "%c" "."
            sleep 1
        done
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        kubectl wait --namespace ${NATS_NAMESPACE} --for=condition=Ready --timeout=300s pod/${NATS_NAMESPACE}-0
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    fi
    echo

    echo "NATS Port Mapping"
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    echo ">>> Expose NATS Client port (4222->${NATS_EXT_PORT_CLIENT})"
    NATS_PORT_CLIENT=$(kubectl --namespace ${NATS_NAMESPACE} get service ${NATS_NAMESPACE} -o 'jsonpath={.spec.ports[?(@.name=="client")].port}')
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    if [ -z "$NATS_PORT_CLIENT" ]; then
        # NATS charts updated and port name changed from "client" to "nats"; fix to support new name and enable backward compatibility
        NATS_PORT_CLIENT=$(kubectl --namespace ${NATS_NAMESPACE} get service ${NATS_NAMESPACE} -o 'jsonpath={.spec.ports[?(@.name=="nats")].port}')
    fi
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    PATCH='{"data": {"'${NATS_EXT_PORT_CLIENT}'": "'${NATS_NAMESPACE}'/'${NATS_NAMESPACE}':'${NATS_PORT_CLIENT}'"}}'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    kubectl patch configmap nginx-ingress-tcp-microk8s-conf --namespace ingress --patch "${PATCH}"

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    PORT_MAP='{"containerPort": '${NATS_EXT_PORT_CLIENT}', "hostPort": '${NATS_EXT_PORT_CLIENT}'}'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    CONTAINER='{"name": "nginx-ingress-microk8s", "ports": ['${PORT_MAP}']}'
    PATCH='{"spec": {"template": {"spec": {"containers": ['${CONTAINER}']}}}}'
    kubectl patch daemonset nginx-ingress-microk8s-controller --namespace ingress --patch "${PATCH}"
    echo

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    echo ">>> Expose NATS HTTP Mgmt GUI port (8222->${NATS_EXT_PORT_HTTP})"
    NATS_PORT_HTTP=$(kubectl --namespace ${NATS_NAMESPACE} get service ${NATS_NAMESPACE} -o 'jsonpath={.spec.ports[?(@.name=="monitor")].port}')
    PATCH='{"data": {"'${NATS_EXT_PORT_HTTP}'": "'${NATS_NAMESPACE}'/'${NATS_NAMESPACE}':'${NATS_PORT_HTTP}'"}}'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    kubectl patch configmap nginx-ingress-tcp-microk8s-conf --namespace ingress --patch "${PATCH}"

Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    PORT_MAP='{"containerPort": '${NATS_EXT_PORT_HTTP}', "hostPort": '${NATS_EXT_PORT_HTTP}'}'
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    CONTAINER='{"name": "nginx-ingress-microk8s", "ports": ['${PORT_MAP}']}'
    PATCH='{"spec": {"template": {"spec": {"containers": ['${CONTAINER}']}}}}'
    kubectl patch daemonset nginx-ingress-microk8s-controller --namespace ingress --patch "${PATCH}"
    echo
}

function nats_undeploy_single() {
    echo "NATS"
    echo ">>> Checking if NATS is deployed..."
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    if kubectl get --namespace ${NATS_NAMESPACE} statefulset/${NATS_NAMESPACE} &> /dev/null; then
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        echo ">>> Undeploy NATS"
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
        helm3 uninstall --namespace ${NATS_NAMESPACE} ${NATS_NAMESPACE}
Lluis Gifre Renom's avatar
Lluis Gifre Renom committed
    else
        echo ">>> NATS is not present; skipping step."
    fi
    echo

    echo "NATS Namespace"
    echo ">>> Delete NATS Namespace (if exists)"
    kubectl delete namespace ${NATS_NAMESPACE} --ignore-not-found
    echo
}

if [ "$NATS_REDEPLOY" == "YES" ]; then
    nats_undeploy_single
fi

nats_deploy_single