From 74105f5575380941ed4add33978c57724648f98e Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 23:16:47 -0700 Subject: [PATCH] egress-test: the chart's egress check, three ways (K-C7) An HAProxy on the kind network answers with the caller's address, as server.egress.checkUrl. With the mta pods on one node: its address expected gives a Normal event, another address a Warning, and a checkUrl that doesn't answer a Warning while the install still succeeds. The lab's token policy lets the egress check mount its token, as the upgrade Job does. --- lab/network/egress-test.sh | 81 ++++++++++++++++++++++++++++++++++++++ lab/policy/defaults.yaml | 7 ++-- 2 files changed, 85 insertions(+), 3 deletions(-) create mode 100755 lab/network/egress-test.sh diff --git a/lab/network/egress-test.sh b/lab/network/egress-test.sh new file mode 100755 index 0000000..e5d8257 --- /dev/null +++ b/lab/network/egress-test.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +# SPDX-FileCopyrightText: 2026 Coffey Labs LLC +# SPDX-License-Identifier: Apache-2.0 +# +# K-C7: the chart's egress check. An HAProxy on the kind network stands in +# for server.egress.checkUrl, answering with the caller's address. The mta +# pods are placed on one node (nodeSelector), and kind sends a pod's traffic +# out from its node's address, so that's the address outbound mail leaves +# from. Each install leaves an event on the mta Deployment: +# +# 1. that node's address expected: Normal EgressAddress +# 2. only a documentation address expected: Warning EgressMismatch +# 3. a checkUrl that doesn't answer: Warning EgressUnknown, and the install +# still succeeds +# +# Leaves the chart as it found it and removes HAProxy. +# +# SERVER_TAG=kc6 WEBMAIL_TAG=kw3 lab/network/egress-test.sh +set -euo pipefail + +cd "$(dirname "$0")" +K=(kubectl --context kind-inbuxa-lab -n inbuxa) +CHART="${CHART:-../../../inbuxa-charts/charts/inbuxa}" +SERVER_TAG="${SERVER_TAG:?the server image tag in kind}" +EXTRA=() +[ -n "${WEBMAIL_TAG:-}" ] && EXTRA+=(--set webmail.image.tag="$WEBMAIL_TAG") +HAPROXY_IMAGE="${HAPROXY_IMAGE:-haproxy:3.0-alpine}" +NODE="${NODE:-inbuxa-lab-worker3}" +fails=0 + +install() { # [helm args]: the lab's install, with the webmail and console + helm --kube-context kind-inbuxa-lab -n inbuxa upgrade inbuxa "$CHART" \ + -f ../chart/values-lab.yaml -f ../chart/values-frontends.yaml --set server.image.tag="$SERVER_TAG" \ + "${EXTRA[@]}" "$@" --wait --timeout 10m >/dev/null + for d in front mta; do "${K[@]}" rollout status deploy/inbuxa-$d --timeout 5m >/dev/null; done +} +cfg=$(mktemp) +cleanup() { + rm -f "$cfg" + docker rm -f inbuxa-lab-ipecho >/dev/null 2>&1 || true + install >/dev/null 2>&1 || echo "!! couldn't put the chart back: run frontends-test.sh" >&2 +} +trap cleanup EXIT + +# The last event the check left: " : " +last_event() { + "${K[@]}" get events --field-selector involvedObject.name=inbuxa-mta,source=inbuxa-egress-check \ + --sort-by=.lastTimestamp -o jsonpath='{range .items[*]}{.type} {.reason}: {.message}{"\n"}{end}' | tail -1 +} +check() { # + case "$2" in + "$3"*) echo "ok $1: $2" ;; + *) echo "FAIL $1: got \"$2\", want $3"; fails=$((fails + 1)) ;; + esac +} + +printf 'defaults\n mode http\n timeout connect 5s\n timeout client 30s\n timeout server 30s\nfrontend echo\n bind :8080\n http-request return status 200 content-type text/plain lf-string "%%[src]\\n"\n' >"$cfg" +chmod 644 "$cfg" +docker rm -f inbuxa-lab-ipecho >/dev/null 2>&1 || true +docker run -d --name inbuxa-lab-ipecho --network kind -v "$cfg:/usr/local/etc/haproxy/haproxy.cfg:ro" "$HAPROXY_IMAGE" >/dev/null +ECHO=$(docker inspect inbuxa-lab-ipecho -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}') +NODE_IP=$("${K[@]}" get node "$NODE" -o jsonpath='{.status.addresses[?(@.type=="InternalIP")].address}') +echo " address echo at $ECHO; mta pods on $NODE ($NODE_IP)" +PLACE=(--set-json "server.groups.mta.nodeSelector={\"kubernetes.io/hostname\":\"$NODE\"}" + --set server.egress.checkUrl="http://$ECHO:8080/") + +echo "== 1. the node's address expected" +install "${PLACE[@]}" --set-json "server.egress.expectedAddresses=[\"198.51.100.7\",\"$NODE_IP\"]" +check "as expected" "$(last_event)" "Normal EgressAddress" + +echo "== 2. another address expected" +install "${PLACE[@]}" --set-json 'server.egress.expectedAddresses=["198.51.100.7"]' +check "mismatch" "$(last_event)" "Warning EgressMismatch" + +echo "== 3. a checkUrl that doesn't answer" +install "${PLACE[@]}" --set-json "server.egress.expectedAddresses=[\"$NODE_IP\"]" \ + --set server.egress.checkUrl="http://$ECHO:9/" +check "unknown, install still fine" "$(last_event)" "Warning EgressUnknown" + +echo +[ "$fails" = 0 ] && echo "== all pass" || { echo "== $fails failed"; exit 1; } diff --git a/lab/policy/defaults.yaml b/lab/policy/defaults.yaml index 8de557b..356eade 100644 --- a/lab/policy/defaults.yaml +++ b/lab/policy/defaults.yaml @@ -42,11 +42,12 @@ spec: operations: [CREATE, UPDATE] resources: [pods] validations: - # The upgrade Job scales the server down through a Role only it holds + # The upgrade Job scales the server down through a Role only it holds; + # the egress check records an event through one that may do nothing else - expression: >- (has(object.spec.automountServiceAccountToken) && object.spec.automountServiceAccountToken == false) - || (has(object.metadata.labels) && object.metadata.labels[?"app.kubernetes.io/component"].orValue("") == "upgrade") - message: no pod mounts a service-account token, except the upgrade Job's + || (has(object.metadata.labels) && object.metadata.labels[?"app.kubernetes.io/component"].orValue("") in ["upgrade", "egress-check"]) + message: no pod mounts a service-account token, except the upgrade Job's and the egress check's --- apiVersion: policies.kyverno.io/v1 kind: ValidatingPolicy -- 2.54.0