[VOL-3963] Waiting for ONOS cluster to be ready before pushing the config

Change-Id: Ife82879de966fa35ace0ba1aa650db27dac0b342
diff --git a/voltha-infra/templates/onos-config-loader-script.yaml b/voltha-infra/templates/onos-config-loader-script.yaml
index 8ca2480..6491ab7 100644
--- a/voltha-infra/templates/onos-config-loader-script.yaml
+++ b/voltha-infra/templates/onos-config-loader-script.yaml
@@ -21,6 +21,26 @@
   loader.sh: >
     set -euo pipefail
 
+    # onos-config-loader most likely start before ONOS is deployed, so check for ONOS before waiting for it to be ready
+
+    has_onos=$(kubectl get pods -l app=onos-classic --all-namespaces | wc -l);
+
+    while [[ $has_onos == 0 ]]; do
+      echo -e "Waiting for ONOS to be deployed";
+      sleep 5;
+      has_onos=$(kubectl get pods -l app=onos-classic --all-namespaces | wc -l);
+    done
+
+    # wait all ONOS pods to be ready
+
+    onos_starting=$(kubectl get pods -l app=onos-classic --all-namespaces --field-selector=status.phase!=Running | wc -l);
+
+    while [[ $onos_starting != 0 ]]; do
+      echo -e "$onos_starting ONOS instances are still starting...";
+      sleep 5;
+      onos_starting=$(kubectl get pods --all-namespaces -l app=onos-classic | grep "0/" | wc -l);
+    done
+
     # a POST to a non ready netcfg return 207 in case of failure, while a GET returns 404,
     # check the apps key is ready to accept data before sending them