Skip to content

Commit

Permalink
kuberesource: add signal handler to portforwarder
Browse files Browse the repository at this point in the history
Co-authored-by: 3u13r <[email protected]>
  • Loading branch information
burgerdev and 3u13r committed Jan 30, 2025
1 parent fb7fd66 commit 7d52a94
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion internal/kuberesource/parts.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,19 @@ func (p *PortForwarderConfig) WithForwardTarget(host string) *PortForwarderConfi
return p
}

const portForwarderScript = `echo Starting port-forward with socat >&2
handler() {
echo "Received SIGTERM, forwarding to children" >&2
kill -TERM -1
}
trap handler TERM
set -x
for port in ${LISTEN_PORTS}; do
socat -d -d TCP-LISTEN:$port,fork TCP:${FORWARD_HOST}:$port &
done
wait
`

// PortForwarder constructs a port forwarder pod for multiple ports.
func PortForwarder(name, namespace string) *PortForwarderConfig {
name = "port-forwarder-" + name
Expand All @@ -295,7 +308,7 @@ func PortForwarder(name, namespace string) *PortForwarderConfig {
Container().
WithName("port-forwarder").
WithImage("ghcr.io/edgelesssys/contrast/port-forwarder:latest").
WithCommand("/bin/bash", "-c", "echo Starting port-forward with socat; for port in ${LISTEN_PORTS}; do socat -d -d TCP-LISTEN:$port,fork TCP:${FORWARD_HOST}:$port & done; wait").
WithCommand("/bin/bash", "-c", portForwarderScript).
WithResources(ResourceRequirements().
WithMemoryLimitAndRequest(50),
),
Expand Down

0 comments on commit 7d52a94

Please sign in to comment.