Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,21 @@ type Config struct {
PowerOnCooldown int `yaml:"powerOnCooldown"` // seconds
ProxyTimeouts ProxyTimeouts `yaml:"proxyTimeouts"`
MachineMetadata map[string]any `yaml:"machineMetadata"`
ProxyTarget *ProxyTarget `yaml:"proxyTarget"`
Machine *machine.GoogleComputeEngine
}

// ProxyTarget optionally overrides where requests are proxied to.
// When set, the machine referenced by MachineMetadata is still powered on and
// pinged, but HTTP traffic is forwarded to ProxyTarget instead. This supports
// topologies where a sidecar (e.g. a Cloud Run frontend container) serves
// requests while still depending on the remote machine being up.
type ProxyTarget struct {
Scheme string `yaml:"scheme"`
Host string `yaml:"host"`
Port int `yaml:"port"`
}

type ProxyTimeouts struct {
DialTimeout int `yaml:"dialTimeout"` // seconds, default: 120
KeepAlive int `yaml:"keepAlive"` // seconds, default: 120
Expand Down
23 changes: 23 additions & 0 deletions pkg/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,26 @@ func TestReverseProxy_SetHost(t *testing.T) {
t.Error("Target is nil after SetHost()")
}
}

func TestReverseProxy_SetHost_ProxyTargetOverride(t *testing.T) {
config := &config.Config{
Scheme: "http",
Port: 8080,
ProxyTarget: &config.ProxyTarget{
Scheme: "http",
Host: "localhost",
Port: 9000,
},
Machine: machine.NewGceMachine(),
}

proxy := New(config)
proxy.SetHost()

if proxy.Target.Host != "localhost:9000" {
t.Errorf("Target.Host = %q, want localhost:9000", proxy.Target.Host)
}
if proxy.Target.Scheme != "http" {
t.Errorf("Target.Scheme = %q, want http", proxy.Target.Scheme)
}
}
15 changes: 14 additions & 1 deletion pkg/proxy/tls.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ func New(c *config.Config) *ReverseProxy {
tlsHandshakeTimeout := time.Duration(c.ProxyTimeouts.TLSHandshakeTimeout) * time.Second
expectContinueTimeout := time.Duration(c.ProxyTimeouts.ExpectContinueTimeout) * time.Second

scheme := c.Scheme
if c.ProxyTarget != nil && c.ProxyTarget.Scheme != "" {
scheme = c.ProxyTarget.Scheme
}
return &ReverseProxy{
Target: &url.URL{
Scheme: c.Scheme,
Scheme: scheme,
},
Config: c,
Transport: &http.Transport{
Expand All @@ -59,6 +63,15 @@ func New(c *config.Config) *ReverseProxy {
}

func (p *ReverseProxy) SetHost() {
if p.Config.ProxyTarget != nil && p.Config.ProxyTarget.Host != "" {
port := p.Config.ProxyTarget.Port
if port == 0 {
port = p.Config.Port
}
p.Target.Host = net.JoinHostPort(p.Config.ProxyTarget.Host, strconv.Itoa(port))
slog.Debug("Set proxy target host", "host", p.Target.Host)
return
}
p.Target.Host = net.JoinHostPort(
p.Config.Machine.Host(),
strconv.Itoa(p.Config.Port),
Expand Down
Loading