Skip to content

Commit 4e45bb9

Browse files
authored
Merge pull request #68 from Yolean/y-k8s-ingress-hosts-check
Add -check flag to y-k8s-ingress-hosts
2 parents 574100b + 47b11e5 commit 4e45bb9

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

bin/y-k8s-ingress-hosts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ YBIN="$(dirname $0)"
66
[ -z "$KUBECONFIG" ] && echo "This script requires a KUBECONFIG" && exit 1
77

88
CTX=""
9+
CHECK=false
910
PASSTHROUGH=()
1011

1112
while [ $# -gt 0 ]; do
@@ -17,11 +18,13 @@ Usage: y-k8s-ingress-hosts [flags]
1718
Flags:
1819
--context=NAME kubeconfig context name (required)
1920
-write rewrite host file
21+
-check|--check check if /etc/hosts includes required entries (no sudo)
2022
-override-ip=IP use this IP for all entries
2123
-h, --help show this help
2224
EOF
2325
exit 0 ;;
2426
--context=*) CTX="${1#*=}"; shift ;;
27+
-check|--check) CHECK=true; shift ;;
2528
*) PASSTHROUGH+=("$1"); shift ;;
2629
esac
2730
done
@@ -34,6 +37,25 @@ kubectl config view --raw --minify --context="$CTX" > "$CONTEXT_KUBECONFIG"
3437

3538
version=$(y-bin-download $YBIN/y-bin.optional.yaml k8s-ingress-hosts)
3639

40+
if $CHECK; then
41+
NEEDED=$($YBIN/y-k8s-ingress-hosts-v${version}-bin -kubeconfig "$CONTEXT_KUBECONFIG" "${PASSTHROUGH[@]}" 2>/dev/null | grep -v '^#')
42+
MISSING=0
43+
while IFS= read -r line; do
44+
[ -z "$line" ] && continue
45+
HOST=$(echo "$line" | awk '{print $2}')
46+
if ! grep -q "$HOST" /etc/hosts; then
47+
echo "Missing: $line"
48+
MISSING=1
49+
fi
50+
done <<< "$NEEDED"
51+
if [ $MISSING -eq 1 ]; then
52+
echo "# /etc/hosts needs updating. Run with -write to fix."
53+
exit 1
54+
fi
55+
echo "# /etc/hosts is up to date"
56+
exit 0
57+
fi
58+
3759
[ $(id -u) -ne 0 ] && exec sudo -E $YBIN/y-k8s-ingress-hosts-v${version}-bin -kubeconfig "$CONTEXT_KUBECONFIG" "${PASSTHROUGH[@]}"
3860

3961
$YBIN/y-k8s-ingress-hosts-v${version}-bin -kubeconfig "$CONTEXT_KUBECONFIG" "${PASSTHROUGH[@]}" || exit $?

0 commit comments

Comments
 (0)