#!/bin/bash
#
# Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
# Licensed under the GNU General Public License Version 2 as shown at https://oss.oracle.com/licenses/GPL-2.

# Get all the crio containers running on the system
crioPods=$(crictl ps)
if [ $? -ne 0 ]; then
        echo "$crioPods" 1>&2
        exit 1
fi

# Look for the important control plane pods: etcd and kube-apiserver
echo "$crioPods" | grep etcd | grep -q Running
if [ $? -ne 0 ]; then
        echo "etcd is not running" 1>&2
        exit 1
fi

echo "$crioPods" | grep kube-apiserver | grep -q Running
if [ $? -ne 0 ]; then
        echo "kube-apiserver is not running" 1>&2
        exit 1
fi

# Now that the pods are known to be running, ensure that
# the control port for etcd is open.
ports=$(netstat -ntlp)
if [ $? -ne 0 ]; then
        echo "$ports" 1>&2
        exit 1
fi

echo "$ports" | grep 2379
if [ $? -ne 0 ]; then
        echo "Port 2379 is not open" 1>&2
        exit 1
fi

exit 0
