#!/bin/sh
# Generated by StackToIT from execution-plan.json. StackToIT has not run this script and runs none.
# Read-only checks of what the draft wrote, after the apply: a workload is rolled out and has pods (kubectl rollout status, kubectl get pods), a load balancer
# answers (a request with curl). It creates, changes and deletes nothing. The same commands are listed in execution/verify/checks.md.
# Needs: kubectl and the kubeconfig of the cluster in KUBECONFIG, the namespace the manifests were applied into in NAMESPACE, tofu for the load balancers' addresses, curl.
# A manifest the draft holds back (replicas: 0) has no pod until the start step of its service scales it: run this after that step.
# Exit status: 0 when every check held, 1 when one did not.
set -u

here=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd)
tf="$here/../../terraform"
failures=0
run() {
  printf '%s\n' "+ $*"
  "$@" || { failures=$((failures + 1)); printf '%s\n' "FAILED: $*" >&2; }
}
[ -n "${NAMESPACE:-}" ] || { printf '%s\n' 'NAMESPACE is not set: the namespace the manifests were applied into.' >&2; exit 2; }
[ -n "${KUBECONFIG:-}" ] || { printf '%s\n' 'KUBECONFIG is not set: the kubeconfig file of the cluster.' >&2; exit 2; }

# terraform/kubernetes/server/manifests.yaml: Deployment server
run kubectl -n "$NAMESPACE" rollout status 'deployment/server' --timeout=300s
run kubectl -n "$NAMESPACE" get pods -l 'app.kubernetes.io/name=server'

if [ "$failures" -gt 0 ]; then printf '%s\n' "$failures check(s) failed." >&2; exit 1; fi
printf '%s\n' 'Every check held.'
