#!/bin/bash
# Readiness of kube-controller-manager = the process LISTENS: its secure port (10257 unless
# --secure-port says otherwise) accepts a TCP connection on the bind address
# (--bind-address; a wildcard is checked on 127.0.0.1).
#
# Not /healthz: this component is active only as the elected leader, its
# health says nothing about this node, and a TLS request would need curl.
# Whether it works is measured by monitoring (leader election, API state).
#
# The flags come from /etc/default/kube-controller-manager (DAEMON_ARGS), the same file
# run-script-daemon sources. Source it before "set -u": it may reference
# variables that are unset here.
DAEMON_ARGS=
[ -r /etc/default/kube-controller-manager ] && . /etc/default/kube-controller-manager 2>/dev/null
set -u
flag() { printf '%s\n' $DAEMON_ARGS | sed -n "s/^$1=//p" | tail -n 1; }
port=$(flag --secure-port); port=${port:-10257}
host=$(flag --bind-address)
case "$host" in ''|0.0.0.0|'::'|'[::]') host=127.0.0.1 ;; esac
if ! { exec 3<>"/dev/tcp/$host/$port"; } 2>/dev/null; then
  echo "port $host:$port does not accept connections"
  exit 1
fi
exec 3<&- 3>&-
exit 0
