Service-init

XGVela Service init module is used as the K8S Service init container.

We can find it in tmaas/charts/topo-engine/templates/deployment.yaml as example:

    apiVersion: apps/v1
    kind: Deployment
    spec: 
        .....
        initContainers:
        - name: svc-discovery        
           image: xgvela-svc-init:v0.3.4                                                                      #use service-init
        .....
           env:
           - name: ETCD_SVC_FQDN                                                                          #etcd cluster's endpoints
        .....
           - name: KAFKA_SVC_FQDN                                                                        #kafka cluster's endpoints
        .....
           command: ["/bin/ash", "-c", "svc-discovery.sh \ "EVENT TMAAS\" "]        #tmass service need to check kafka topic "EVENT TMAAS"



In K8s, A Pod can have multiple containers running apps within it, but it can also have one or more init containers, which are run before the app containers are started.
The main container will not start until all Init Containers is Running. 

XGVela use initcontainer for check etcd cluster's status & kafka cluster's status & zookeeper cluster's status.
When the service is kafka itself, then svc-discovery only check zookeeper cluster's status, for other services, svc-discovery check etcd&kafka cluster's status.



Let's see the code svc-discovery.sh
.....
  get_etcd_cluster_status() {
    etcd_cluster_endpoints=http://etcd-0.$ETCD_SVC_FQDN,http://etcd-1.$ETCD_SVC_FQDN,http://etcd-2.$ETCD_SVC_FQDN
    retval=$(ETCDAPI=3; ./etcdctl -C $etcd_cluster_endpoints cluster-health | awk '/^cluster is healthy|^cluster is degraded/{print}')    #use etcdctl -C <endpoint> cluster-health to check etcd cluster's status
    echo "$retval"
    if [[ "$retval" == "cluster is healthy" ]] || [[ "$retval" == "cluster is degraded" ]]; then
      echo $(date): Healthy etcd Cluster
      return 1
    else
      echo $(date): Un-Healthy etcd Cluster
      return 0
    fi
  }
.....
  get_kafka_cluster_status() {
    tp=$1
    kafka_cluster_endpoints=kafka-0.$KAFKA_SVC_FQDN,kafka-1.$KAFKA_SVC_FQDN,kafka-2.$KAFKA_SVC_FQDN
    retval=$(kafkacat -L -b kafka-0.$KAFKA_SVC_FQDN,kafka-1.$KAFKA_SVC_FQDN,kafka-2.$KAFKA_SVC_FQDN | awk "/topic \"$tp\" with.*partitions:/{print}" | tr -s \ | cut -d ' ' -f 5)
                                                                                                    #use kafkacat -b <kafka_url> 's Topic is mach the input parameter, like tmaas,the topic is “EVENT TMAAS” to check kafka cluster's status

    if [[ -n "$retval" ]] && [[ $retval -ne "" ]] && [[ "$retval" -ne "0" ]] ; then
      echo $(date): $tp kafka topic created with $retval partitions
      return 1
    else
      echo $(date): Un-Healthy kafka Cluster or Topic $tp not ready
      return 0
    fi
  }

.....
  echo "ZK_COUNT: $ZK_COUNT"
  while [[ $zk_cluster_status -eq 0 ]]
  do
    if [[ "$ZK_COUNT" -eq "3" ]]; then
      zk_cluster1=$(echo stat | nc $(echo $KAFKA_ZOOKEEPER_CONNECT|cut -d "," -f 1|cut -d ":" -f1) 2181 |awk '/Mode: follower|Mode: leader/' |wc -l )
      zk_cluster2=$(echo stat | nc $(echo $KAFKA_ZOOKEEPER_CONNECT|cut -d "," -f 2|cut -d ":" -f1) 2181 |awk '/Mode: follower|Mode: leader/' |wc -l )
      zk_cluster3=$(echo stat | nc $(echo $KAFKA_ZOOKEEPER_CONNECT|cut -d "," -f 3|cut -d ":" -f1) 2181 |awk '/Mode: follower|Mode: leader/' |wc -l )
      zk_cluster=$(( $zk_cluster1 + $zk_cluster2 + $zk_cluster3 ))              #use nc to check,and counted to check zk cluster's status
    if [[ "$zk_cluster" -gt "1" ]] ; then
      echo $(date): Healthy zk Cluster
      return 0
    else
      echo $(date): Un-Healthy zk Cluster
      zk_cluster_status=0
      sleep 5
    fi
.....

The grafana-datasource.sh has never used, why pack it to image?